2023-05-29 18:04:03 -06:00
|
|
|
import React, {useEffect, useState} from "react";
|
2023-11-02 15:24:07 -06:00
|
|
|
import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom";
|
2023-05-29 18:04:03 -06:00
|
|
|
import {FilesSearchView} from "./Files";
|
|
|
|
|
import {usePages, useQuery} from "../hooks/customHooks";
|
|
|
|
|
import {ZimSearchView} from "./Zim";
|
2023-11-20 11:01:46 -07:00
|
|
|
import {searchEstimate, searchSuggestions} from "../api";
|
2023-11-21 13:01:52 -07:00
|
|
|
import {fuzzyMatch, normalizeEstimate, TabLinks} from "./Common";
|
2023-11-20 11:07:19 -07:00
|
|
|
import _ from "lodash";
|
2023-11-21 13:01:52 -07:00
|
|
|
import {TagsContext} from "../Tags";
|
|
|
|
|
import {Header as SHeader} from "semantic-ui-react";
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2023-11-20 11:29:14 -07:00
|
|
|
const suggestedApps = [
|
|
|
|
|
{location: '/more/otp', title: 'One Time Pad', description: 'Encrypt and Decrypt messages'},
|
|
|
|
|
{location: '/inventory', title: 'Inventory', description: 'Track and organize your food storage'},
|
|
|
|
|
{location: '/more/vin', title: 'Vin Decoder', description: 'Decode and analyze vehicle VIN numbers'},
|
|
|
|
|
{location: '/admin', title: 'Downloads', description: 'View your downloads'},
|
|
|
|
|
{location: '/admin/settings', title: 'Settings', description: 'View and modify your settings'},
|
2023-11-21 14:46:55 -07:00
|
|
|
{location: '/admin/status', title: 'Status', description: 'View the status of this WROLPi'},
|
2023-11-20 11:29:14 -07:00
|
|
|
{location: '/admin/wrol', title: 'WROL Mode', description: 'Enable or disable WROL Mode'},
|
|
|
|
|
];
|
|
|
|
|
|
2023-11-20 11:01:46 -07:00
|
|
|
export function useSearchSuggestions() {
|
2023-11-20 11:29:14 -07:00
|
|
|
const navigate = useNavigate();
|
2023-11-20 11:01:46 -07:00
|
|
|
const {searchParams} = useQuery();
|
2023-11-21 13:01:52 -07:00
|
|
|
const {SingleTag} = React.useContext(TagsContext);
|
2023-11-20 11:29:14 -07:00
|
|
|
|
2023-11-20 11:01:46 -07:00
|
|
|
const searchStr = searchParams.get('q');
|
|
|
|
|
|
|
|
|
|
const [suggestions, setSuggestions] = useState(null);
|
|
|
|
|
|
|
|
|
|
const fetchSuggestions = async () => {
|
|
|
|
|
if (searchStr && searchStr.length > 0) {
|
2023-11-21 13:01:52 -07:00
|
|
|
const lowerSearchStr = searchStr.toLowerCase();
|
2023-11-20 11:01:46 -07:00
|
|
|
try {
|
2023-11-21 14:46:55 -07:00
|
|
|
const newSuggestions = await searchSuggestions(searchStr);
|
2023-11-21 13:01:52 -07:00
|
|
|
const matchingApps = suggestedApps.filter(i =>
|
|
|
|
|
i.title.toLowerCase().includes(lowerSearchStr)
|
|
|
|
|
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
2023-11-21 14:46:55 -07:00
|
|
|
|
|
|
|
|
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0);
|
|
|
|
|
|
|
|
|
|
// Suggested results are ordered.
|
|
|
|
|
let matchingSuggestions = {};
|
|
|
|
|
if (newSuggestions.fileGroups > 0) {
|
|
|
|
|
matchingSuggestions.fileGroups = {
|
|
|
|
|
name: 'Files', results: [
|
|
|
|
|
{description: newSuggestions.fileGroups, type: 'files'}
|
|
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (zimSum > 0) {
|
|
|
|
|
matchingSuggestions.zimsSum = {
|
|
|
|
|
name: 'Zims', results: [
|
|
|
|
|
{description: zimSum, type: 'zims'}
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newSuggestions.channels && newSuggestions.channels.length > 0) {
|
|
|
|
|
matchingSuggestions.channels = {
|
|
|
|
|
name: 'Channels', results: newSuggestions.channels.map(i => {
|
|
|
|
|
return {type: 'channel', description: i['name'], id: i['id']}
|
2023-11-20 11:01:46 -07:00
|
|
|
})
|
2023-11-21 14:46:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newSuggestions.domains && newSuggestions.domains.length > 0) {
|
|
|
|
|
matchingSuggestions.domains = {
|
|
|
|
|
name: 'Domains', results: newSuggestions.domains.map(i => {
|
|
|
|
|
return {type: 'domain', description: i.domain, id: i['id'], domain: i.domain}
|
2023-11-20 11:01:46 -07:00
|
|
|
})
|
2023-11-21 14:46:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newSuggestions.tags && newSuggestions.tags.length > 0) {
|
|
|
|
|
matchingSuggestions.tags = {
|
|
|
|
|
name: 'Tags', results: newSuggestions.tags.map(i => {
|
|
|
|
|
return {type: 'tag', description: i['name']}
|
2023-11-20 11:29:14 -07:00
|
|
|
})
|
2023-11-21 14:46:55 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (matchingApps && matchingApps.length > 0) {
|
|
|
|
|
matchingSuggestions.apps = {name: 'Apps', results: matchingApps};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.debug('matchingSuggestions', matchingSuggestions);
|
2023-11-21 13:01:52 -07:00
|
|
|
setSuggestions(matchingSuggestions);
|
2023-11-20 11:01:46 -07:00
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
console.error('Failed to get search suggestions');
|
|
|
|
|
}
|
2023-11-20 11:07:19 -07:00
|
|
|
} else {
|
|
|
|
|
setSuggestions(null);
|
2023-11-20 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
fetchSuggestions();
|
|
|
|
|
}, [searchStr]);
|
|
|
|
|
|
2023-11-20 11:29:14 -07:00
|
|
|
|
|
|
|
|
const handleResultSelect = ({result}) => {
|
|
|
|
|
if (!result) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.location) {
|
2023-11-21 14:46:55 -07:00
|
|
|
return navigate(result.location);
|
2023-11-20 11:29:14 -07:00
|
|
|
} else if (result['type'] === 'channel') {
|
2023-11-21 14:46:55 -07:00
|
|
|
return navigate(`/videos/channel/${result['id']}/video`);
|
2023-11-20 11:29:14 -07:00
|
|
|
} else if (result['type'] === 'domain') {
|
2023-11-21 14:46:55 -07:00
|
|
|
return navigate(`/archive?domain=${result['domain']}`);
|
2023-11-20 11:29:14 -07:00
|
|
|
} else if (result['type'] === 'tag') {
|
2023-11-21 14:46:55 -07:00
|
|
|
return navigate(`/search?tag=${encodeURIComponent(result['title'])}`);
|
|
|
|
|
} else if (result['type'] === 'files') {
|
|
|
|
|
return navigate(`/search?q=${encodeURIComponent(searchStr)}`);
|
|
|
|
|
} else if (result['type'] === 'zims') {
|
|
|
|
|
return navigate(`/search/zim?q=${encodeURIComponent(searchStr)}`);
|
2023-11-20 11:29:14 -07:00
|
|
|
}
|
2023-11-21 14:46:55 -07:00
|
|
|
console.error('No handleResultSelect defined:', result);
|
2023-11-21 13:01:52 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const resultRenderer = ({type, title, description}) => {
|
|
|
|
|
description = description !== null ? <SHeader as='h5'>{description}</SHeader> : null;
|
|
|
|
|
if (type === 'tag') {
|
|
|
|
|
return <SingleTag name={title}/>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No specific renderer, use the generic.
|
2023-11-21 14:46:55 -07:00
|
|
|
if (title) {
|
|
|
|
|
return <>
|
|
|
|
|
<SHeader as='h4'>{title}</SHeader>
|
|
|
|
|
{description}
|
|
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
return <span>{description}</span>
|
2023-11-21 13:01:52 -07:00
|
|
|
};
|
2023-11-20 11:29:14 -07:00
|
|
|
|
2023-11-21 13:01:52 -07:00
|
|
|
return {suggestions, searchStr, handleResultSelect, resultRenderer}
|
2023-11-20 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
2023-05-29 18:04:03 -06:00
|
|
|
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
|
|
const {searchParams, updateQuery} = useQuery();
|
2023-11-02 15:24:07 -06:00
|
|
|
// `searchStr` means actually fetch the files/zims.
|
2023-05-29 18:04:03 -06:00
|
|
|
const searchStr = searchParams.get('q');
|
2023-11-02 15:24:07 -06:00
|
|
|
// User can search only by Tags, `searchStr` not required.
|
2023-05-29 18:04:03 -06:00
|
|
|
const activeTags = searchParams.getAll('tag');
|
|
|
|
|
const pages = usePages(defaultLimit, totalPages);
|
2023-11-02 15:24:07 -06:00
|
|
|
// text/html, video*, image*, etc.
|
2023-05-29 18:04:03 -06:00
|
|
|
const filter = searchParams.get('filter');
|
2023-11-02 15:24:07 -06:00
|
|
|
// archive/video/ebook/etc.
|
2023-05-29 18:04:03 -06:00
|
|
|
const model_ = searchParams.get('model') || model;
|
|
|
|
|
|
|
|
|
|
const setSearchStr = (value) => {
|
2023-11-02 15:24:07 -06:00
|
|
|
const searchQuery = {q: value, o: 0};
|
|
|
|
|
if (filter) {
|
|
|
|
|
searchQuery['filter'] = filter;
|
|
|
|
|
}
|
2023-05-29 18:04:03 -06:00
|
|
|
navigate({
|
|
|
|
|
pathname: '/search',
|
|
|
|
|
// Start new search at offset 0.
|
2023-11-02 15:24:07 -06:00
|
|
|
search: createSearchParams(searchQuery).toString(),
|
2023-05-29 18:04:03 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-02 15:24:07 -06:00
|
|
|
const clearSearch = () => {
|
|
|
|
|
navigate({pathname: window.location.pathname, search: ''});
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-29 18:04:03 -06:00
|
|
|
const setTags = (tags) => {
|
|
|
|
|
updateQuery({tag: tags});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const addTag = (name) => {
|
|
|
|
|
const newTags = [...activeTags, name];
|
|
|
|
|
setTags(newTags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const removeTag = (name) => {
|
|
|
|
|
const newTags = activeTags.filter(i => i !== name);
|
|
|
|
|
setTags(newTags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
activeTags,
|
|
|
|
|
addTag,
|
|
|
|
|
filter,
|
|
|
|
|
model: model_,
|
|
|
|
|
pages,
|
|
|
|
|
removeTag,
|
|
|
|
|
searchParams,
|
|
|
|
|
searchStr,
|
|
|
|
|
setSearchStr,
|
2023-11-02 15:24:07 -06:00
|
|
|
clearSearch,
|
2023-05-29 18:04:03 -06:00
|
|
|
setTags,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const useSearchEstimate = (search_str, activeTags) => {
|
|
|
|
|
const [fileGroups, setFileGroups] = useState('?');
|
|
|
|
|
const [zims, setZims] = useState(null);
|
|
|
|
|
const [zimsSum, setZimsSum] = useState('?');
|
|
|
|
|
|
|
|
|
|
const localFetchSearchEstimate = async () => {
|
2023-11-02 15:24:07 -06:00
|
|
|
if (!search_str && (!activeTags || activeTags.length === 0)) {
|
2023-05-29 18:04:03 -06:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
const estimates = await searchEstimate(search_str, activeTags);
|
|
|
|
|
console.debug('estimates', estimates);
|
|
|
|
|
setFileGroups(estimates['file_groups']);
|
|
|
|
|
setZimsSum(estimates['zimSum']);
|
|
|
|
|
setZims(estimates['zims']);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Failed to fetch estimates');
|
|
|
|
|
console.error(e);
|
|
|
|
|
setFileGroups(null);
|
|
|
|
|
setZimsSum(null);
|
|
|
|
|
setZims(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-20 11:07:19 -07:00
|
|
|
const debouncedEstimate = _.debounce(async () => await localFetchSearchEstimate(), 800);
|
|
|
|
|
|
2023-05-29 18:04:03 -06:00
|
|
|
useEffect(() => {
|
2023-11-20 11:07:19 -07:00
|
|
|
debouncedEstimate();
|
|
|
|
|
|
|
|
|
|
return () => debouncedEstimate.cancel();
|
2023-05-29 18:04:03 -06:00
|
|
|
}, [search_str, JSON.stringify(activeTags)]);
|
|
|
|
|
|
|
|
|
|
return {fileGroups, zimsSum, zims}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function SearchView() {
|
|
|
|
|
const {searchStr, activeTags} = useSearch();
|
|
|
|
|
const estimates = useSearchEstimate(searchStr, activeTags);
|
|
|
|
|
|
|
|
|
|
let filesTabName = 'Files';
|
|
|
|
|
let zimsTabName = 'Zims';
|
|
|
|
|
if (estimates) {
|
|
|
|
|
const {fileGroups, zimsSum} = estimates;
|
|
|
|
|
filesTabName = `Files (${normalizeEstimate(fileGroups)})`;
|
|
|
|
|
zimsTabName = `Zims (${normalizeEstimate(zimsSum)})`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const links = [
|
|
|
|
|
{text: filesTabName, to: '/search', key: 'filesSearch', end: true},
|
|
|
|
|
{text: zimsTabName, to: '/search/zim', key: 'zimsSearch'},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
return <React.Fragment>
|
|
|
|
|
<TabLinks links={links}/>
|
|
|
|
|
<Routes>
|
|
|
|
|
<Route path='/*' element={<FilesSearchView/>}/>
|
|
|
|
|
<Route path='/zim' exact element={<ZimSearchView estimates={estimates}/>}/>
|
|
|
|
|
</Routes>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
}
|