add remote json (storj) retrieval mechanism

This commit is contained in:
lamacchinadesiderante 2023-01-16 21:15:27 +01:00
parent aee545293e
commit 50990f83d2
6 changed files with 93 additions and 6 deletions

View File

@ -18,7 +18,7 @@ Occorre clonare il repo e il file contenente gli indici dei libri:
``` ```
git clone https://git.lamacchinadesiderante.org/lamacchinadesiderante/millelibri && cd millelibri git clone https://git.lamacchinadesiderante.org/lamacchinadesiderante/millelibri && cd millelibri
wget https://github.com/zlib-searcher/index/releases/download/0.8.0/index_0.8.0.zip && unzip index_0.8.0.zip wget https://link.storjshare.io/juobea2obv6nadmij22tidrenlfa/millelibri%2Findex%2Frust%2Findex_0.6.zip?download=1 && unzip index_0.6.zip
docker-compose up -d docker-compose up -d
``` ```

View File

@ -1,2 +1,3 @@
# .env.production # .env.production
VITE_BACKEND_BASE_API = 'http://127.0.0.1:7070/' VITE_BACKEND_BASE_API = 'http://127.0.0.1:7070/'
VITE_STORJ_JSON_URL = 'https://link.storjshare.io/juowot6xaa2rz4vjqeal2uo44jka/millelibri%2Findex%2Fjson%2Fmillelibri.json?download=1'

View File

@ -1,2 +1,3 @@
# .env.production # .env.production
VITE_BACKEND_BASE_API = '' VITE_BACKEND_BASE_API = ''
VITE_STORJ_JSON_URL = ''

View File

@ -19,6 +19,7 @@ import { useTranslation } from 'react-i18next';
import SearchLanguage from './SearchLanguage'; import SearchLanguage from './SearchLanguage';
import { MEDIA_QUERY_DESKTOP_STARTS, MEDIA_QUERY_MOBILE_ENDS } from '../constants/mediaquery'; import { MEDIA_QUERY_DESKTOP_STARTS, MEDIA_QUERY_MOBILE_ENDS } from '../constants/mediaquery';
import CopyToClipboardButton from './CopyToClipboardButton'; import CopyToClipboardButton from './CopyToClipboardButton';
import { getJsonArchive } from '../scripts/searcher-browser';
function constructQuery(parts: Record<string, string>): string { function constructQuery(parts: Record<string, string>): string {
return Object.keys(parts) return Object.keys(parts)
@ -48,6 +49,8 @@ const Search: React.FC<SearchProps> = ({ setBooks }) => {
const [complexQuery, setComplexQuery] = useState<string>(''); const [complexQuery, setComplexQuery] = useState<string>('');
const [showLanguageDropdown, setShowLanguageDropdown] = useState<boolean>(true) const [showLanguageDropdown, setShowLanguageDropdown] = useState<boolean>(true)
const [booksFromJsonArchive, setBooksFromJsonArchive] = useState<Book[]>([])
useEffect(() => { useEffect(() => {
const params = new Proxy(new URLSearchParams(decodeURIComponent(window.location.search)), { const params = new Proxy(new URLSearchParams(decodeURIComponent(window.location.search)), {
//@ts-ignore //@ts-ignore
@ -66,9 +69,15 @@ const Search: React.FC<SearchProps> = ({ setBooks }) => {
setLanguage(String(params.language)) setLanguage(String(params.language))
setShowLanguageDropdown(false) setShowLanguageDropdown(false)
} }
}, []) }, [])
useEffect(() => {
(async () => {
const jsonArchive = await getJsonArchive() as Book[]
setBooksFromJsonArchive(jsonArchive)
})();
}, []);
const handleLanguageChange = (language: string) => { const handleLanguageChange = (language: string) => {
if (language == 'input') { if (language == 'input') {
setShowLanguageDropdown(false) setShowLanguageDropdown(false)
@ -104,6 +113,46 @@ const Search: React.FC<SearchProps> = ({ setBooks }) => {
}); });
} }
const filterBooks = (books: Book[]) => {
if (!hasAnySearchBeenMade()) {
return []
}
return books.filter((x) => {
let matchTitle = true;
let matchAuthor = true;
let matchPublisher = true;
let matchExtension = true;
let matchLanguage = true;
if (title !== '') {
matchTitle = x.title.includes(title)
}
if (author !== '') {
matchAuthor = x.author.includes(author)
}
if (publisher !== '' && x.publisher) {
matchPublisher = x.publisher.includes(publisher)
}
if (extension !== '') {
matchExtension = x.extension.includes(extension)
}
if (language !== '') {
matchLanguage = x.language.includes(language)
}
return matchTitle && matchAuthor && matchPublisher && matchExtension && matchLanguage
})
}
useDebounceEffect( useDebounceEffect(
() => { () => {
const query = complexQuery const query = complexQuery
@ -111,7 +160,7 @@ const Search: React.FC<SearchProps> = ({ setBooks }) => {
: constructQuery({ title, author, publisher, extension, language, isbn }); : constructQuery({ title, author, publisher, extension, language, isbn });
search(query, 100).then((books) => { search(query, 100).then((books) => {
setBooks(books); setBooks(complexQuery ? books : books.concat(filterBooks(booksFromJsonArchive)));
}); });
}, },
[title, author, publisher, extension, language, isbn, complexQuery], [title, author, publisher, extension, language, isbn, complexQuery],

View File

@ -0,0 +1 @@
export const JSON_ARCHIVE_WINDOW_KEY = 'JSON_ARCHIVE'

View File

@ -1,12 +1,47 @@
import type { Book } from './searcher'; import type { Book } from './searcher';
import axios from 'axios'; import axios from 'axios';
import { JSON_ARCHIVE_WINDOW_KEY } from '../constants/system';
const http = axios.create({ const http = axios.create({
baseURL: import.meta.env.VITE_BACKEND_BASE_API, baseURL: import.meta.env.VITE_BACKEND_BASE_API,
timeout: 5000 timeout: 5000
}); });
const storj = axios.create({
baseURL: import.meta.env.VITE_STORJ_JSON_URL,
timeout: 5000
});
export const getJsonArchive = async () => {
//@ts-ignore
if (!window[JSON_ARCHIVE_WINDOW_KEY]) {
const response = await storj.get(``);
if (response.status == 200) {
//@ts-ignore
window[JSON_ARCHIVE_WINDOW_KEY] = response.data as Book[];
} else {
console.log(response);
//@ts-ignore
window[JSON_ARCHIVE_WINDOW_KEY] = [];
}
//@ts-ignore
return window[JSON_ARCHIVE_WINDOW_KEY] as Book[];
} else {
//@ts-ignore
return window[JSON_ARCHIVE_WINDOW_KEY] as Book[];
}
}
export default async function search(query: string, limit: number) { export default async function search(query: string, limit: number) {
const response = await http.get(`search?limit=${limit}&query=${query}`); const response = await http.get(`search?limit=${limit}&query=${query}`);
return response.data.books as Book[]; return response.data.books as Book[];
} }