From 50990f83d2dc8c18c0adadd836b2e1ff68437d63 Mon Sep 17 00:00:00 2001 From: lamacchinadesiderante Date: Mon, 16 Jan 2023 21:15:27 +0100 Subject: [PATCH] add remote json (storj) retrieval mechanism --- README.md | 2 +- frontend/.env.development | 3 +- frontend/.env.production | 3 +- frontend/src/components/Search.tsx | 53 +++++++++++++++++++++++- frontend/src/constants/system.ts | 1 + frontend/src/scripts/searcher-browser.ts | 37 ++++++++++++++++- 6 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 frontend/src/constants/system.ts diff --git a/README.md b/README.md index a6e3478..44c7116 100644 --- a/README.md +++ b/README.md @@ -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 -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 ``` diff --git a/frontend/.env.development b/frontend/.env.development index 2137c05..5cd0d30 100644 --- a/frontend/.env.development +++ b/frontend/.env.development @@ -1,2 +1,3 @@ # .env.production -VITE_BACKEND_BASE_API = 'http://127.0.0.1:7070/' \ No newline at end of file +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' \ No newline at end of file diff --git a/frontend/.env.production b/frontend/.env.production index 7947c31..375f178 100644 --- a/frontend/.env.production +++ b/frontend/.env.production @@ -1,2 +1,3 @@ # .env.production -VITE_BACKEND_BASE_API = '' \ No newline at end of file +VITE_BACKEND_BASE_API = '' +VITE_STORJ_JSON_URL = '' \ No newline at end of file diff --git a/frontend/src/components/Search.tsx b/frontend/src/components/Search.tsx index c64843c..12f9b71 100644 --- a/frontend/src/components/Search.tsx +++ b/frontend/src/components/Search.tsx @@ -19,6 +19,7 @@ import { useTranslation } from 'react-i18next'; import SearchLanguage from './SearchLanguage'; import { MEDIA_QUERY_DESKTOP_STARTS, MEDIA_QUERY_MOBILE_ENDS } from '../constants/mediaquery'; import CopyToClipboardButton from './CopyToClipboardButton'; +import { getJsonArchive } from '../scripts/searcher-browser'; function constructQuery(parts: Record): string { return Object.keys(parts) @@ -48,6 +49,8 @@ const Search: React.FC = ({ setBooks }) => { const [complexQuery, setComplexQuery] = useState(''); const [showLanguageDropdown, setShowLanguageDropdown] = useState(true) + const [booksFromJsonArchive, setBooksFromJsonArchive] = useState([]) + useEffect(() => { const params = new Proxy(new URLSearchParams(decodeURIComponent(window.location.search)), { //@ts-ignore @@ -66,9 +69,15 @@ const Search: React.FC = ({ setBooks }) => { setLanguage(String(params.language)) setShowLanguageDropdown(false) } - }, []) + useEffect(() => { + (async () => { + const jsonArchive = await getJsonArchive() as Book[] + setBooksFromJsonArchive(jsonArchive) + })(); + }, []); + const handleLanguageChange = (language: string) => { if (language == 'input') { setShowLanguageDropdown(false) @@ -104,6 +113,46 @@ const Search: React.FC = ({ 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( () => { const query = complexQuery @@ -111,7 +160,7 @@ const Search: React.FC = ({ setBooks }) => { : constructQuery({ title, author, publisher, extension, language, isbn }); search(query, 100).then((books) => { - setBooks(books); + setBooks(complexQuery ? books : books.concat(filterBooks(booksFromJsonArchive))); }); }, [title, author, publisher, extension, language, isbn, complexQuery], diff --git a/frontend/src/constants/system.ts b/frontend/src/constants/system.ts new file mode 100644 index 0000000..9b62d0b --- /dev/null +++ b/frontend/src/constants/system.ts @@ -0,0 +1 @@ +export const JSON_ARCHIVE_WINDOW_KEY = 'JSON_ARCHIVE' \ No newline at end of file diff --git a/frontend/src/scripts/searcher-browser.ts b/frontend/src/scripts/searcher-browser.ts index c40a1c1..9333fa9 100644 --- a/frontend/src/scripts/searcher-browser.ts +++ b/frontend/src/scripts/searcher-browser.ts @@ -1,12 +1,47 @@ import type { Book } from './searcher'; import axios from 'axios'; +import { JSON_ARCHIVE_WINDOW_KEY } from '../constants/system'; + const http = axios.create({ baseURL: import.meta.env.VITE_BACKEND_BASE_API, 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) { const response = await http.get(`search?limit=${limit}&query=${query}`); return response.data.books as Book[]; -} +} \ No newline at end of file