add remote json (storj) retrieval mechanism
This commit is contained in:
parent
aee545293e
commit
50990f83d2
|
@ -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
|
||||
```
|
||||
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
# .env.production
|
||||
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'
|
|
@ -1,2 +1,3 @@
|
|||
# .env.production
|
||||
VITE_BACKEND_BASE_API = ''
|
||||
VITE_STORJ_JSON_URL = ''
|
|
@ -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, string>): string {
|
||||
return Object.keys(parts)
|
||||
|
@ -48,6 +49,8 @@ const Search: React.FC<SearchProps> = ({ setBooks }) => {
|
|||
const [complexQuery, setComplexQuery] = useState<string>('');
|
||||
const [showLanguageDropdown, setShowLanguageDropdown] = useState<boolean>(true)
|
||||
|
||||
const [booksFromJsonArchive, setBooksFromJsonArchive] = useState<Book[]>([])
|
||||
|
||||
useEffect(() => {
|
||||
const params = new Proxy(new URLSearchParams(decodeURIComponent(window.location.search)), {
|
||||
//@ts-ignore
|
||||
|
@ -66,9 +69,15 @@ const Search: React.FC<SearchProps> = ({ 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<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(
|
||||
() => {
|
||||
const query = complexQuery
|
||||
|
@ -111,7 +160,7 @@ const Search: React.FC<SearchProps> = ({ 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],
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
export const JSON_ARCHIVE_WINDOW_KEY = 'JSON_ARCHIVE'
|
|
@ -1,11 +1,46 @@
|
|||
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[];
|
||||
|
|
Loading…
Reference in New Issue