2024-01-01 11:31:17 -07:00
|
|
|
import React, {useContext, useState} from "react";
|
2023-11-02 15:24:07 -06:00
|
|
|
import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom";
|
2024-01-01 11:31:17 -07:00
|
|
|
import {FileSearchFilterButton, FilesSearchView} from "./Files";
|
2023-11-21 21:15:24 -07:00
|
|
|
import {useLatestRequest, usePages, useQuery} from "../hooks/customHooks";
|
2023-05-29 18:04:03 -06:00
|
|
|
import {ZimSearchView} from "./Zim";
|
2024-01-01 11:31:17 -07:00
|
|
|
import {searchEstimateFiles, searchEstimateZims, searchSuggestions} from "../api";
|
|
|
|
|
import {fuzzyMatch, normalizeEstimate, SearchResultsInput, 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";
|
2023-11-26 20:49:36 -07:00
|
|
|
import {Header as SHeader, Label} from "semantic-ui-react";
|
2024-01-01 11:31:17 -07:00
|
|
|
import {Button, Modal, ModalContent} from "./Theme";
|
|
|
|
|
import Grid from "semantic-ui-react/dist/commonjs/collections/Grid";
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2023-11-22 12:19:49 -07:00
|
|
|
export const SearchSuggestionsContext = React.createContext({
|
|
|
|
|
suggestionsResults: {},
|
|
|
|
|
suggestions: {},
|
|
|
|
|
suggestionsSums: {},
|
|
|
|
|
handleResultSelect: null,
|
|
|
|
|
resultRenderer: null,
|
|
|
|
|
loading: false,
|
2024-01-01 11:31:17 -07:00
|
|
|
searchStr: '',
|
|
|
|
|
setSearchStr: null,
|
2023-11-22 12:19:49 -07:00
|
|
|
});
|
|
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
|
|
|
|
|
export const SearchSuggestionsProvider = (props) => {
|
|
|
|
|
const value = useSearchSuggestions();
|
|
|
|
|
|
|
|
|
|
return <SearchSuggestionsContext.Provider value={value}>
|
|
|
|
|
{props.children}
|
|
|
|
|
</SearchSuggestionsContext.Provider>
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 21:20:09 -07:00
|
|
|
const SUGGESTED_APPS = [
|
2023-11-20 11:29:14 -07:00
|
|
|
{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 21:20:09 -07:00
|
|
|
{location: '/admin/status', title: 'Status', description: 'View the status of this WROLPi server'},
|
2023-11-20 11:29:14 -07:00
|
|
|
{location: '/admin/wrol', title: 'WROL Mode', description: 'Enable or disable WROL Mode'},
|
2023-11-21 21:20:09 -07:00
|
|
|
{location: '/help', title: 'Help', description: 'Help documents for WROLPi'},
|
2023-11-20 11:29:14 -07:00
|
|
|
];
|
|
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
|
2023-11-20 11:29:14 -07:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
const {searchParams, updateQuery} = useQuery();
|
|
|
|
|
// `searchStr` means actually fetch the files/zims.
|
2023-11-20 11:01:46 -07:00
|
|
|
const searchStr = searchParams.get('q');
|
2023-11-26 20:49:36 -07:00
|
|
|
// User can search only by Tags, `searchStr` not required.
|
|
|
|
|
const activeTags = searchParams.getAll('tag');
|
|
|
|
|
const pages = usePages(defaultLimit, totalPages);
|
|
|
|
|
// text/html, video*, image*, etc.
|
|
|
|
|
const filter = searchParams.get('filter');
|
|
|
|
|
// archive/video/ebook/etc.
|
|
|
|
|
const model_ = searchParams.get('model') || model;
|
|
|
|
|
|
|
|
|
|
const setSearchStr = (value) => {
|
|
|
|
|
const searchQuery = {q: value, o: 0};
|
|
|
|
|
if (filter) {
|
|
|
|
|
searchQuery['filter'] = filter;
|
|
|
|
|
}
|
|
|
|
|
navigate({
|
|
|
|
|
pathname: '/search',
|
|
|
|
|
// Start new search at offset 0.
|
|
|
|
|
search: createSearchParams(searchQuery).toString(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const clearSearch = () => {
|
|
|
|
|
navigate({pathname: window.location.pathname, search: ''});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
clearSearch,
|
|
|
|
|
setTags,
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-11-20 11:01:46 -07:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
export function useSuggestions(searchStr) {
|
2023-11-21 21:15:24 -07:00
|
|
|
const {data, sendRequest, loading} = useLatestRequest(500);
|
2023-11-26 20:49:36 -07:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (!searchStr || searchStr.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the `useLatestRequest` to handle user typing.
|
|
|
|
|
sendRequest(async () => await searchSuggestions(searchStr));
|
|
|
|
|
}, [searchStr, sendRequest]);
|
|
|
|
|
|
|
|
|
|
return {suggestions: data, loading}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-01-01 11:31:17 -07:00
|
|
|
export function useSearchSuggestions(defaultSearchStr) {
|
2023-11-26 20:49:36 -07:00
|
|
|
const navigate = useNavigate();
|
2024-01-01 11:31:17 -07:00
|
|
|
const [searchStr, setSearchStr] = React.useState(defaultSearchStr || '');
|
2023-11-26 20:49:36 -07:00
|
|
|
const {SingleTag, fuzzyMatchTagsByName} = React.useContext(TagsContext);
|
|
|
|
|
const {suggestions, loading} = useSuggestions(searchStr);
|
|
|
|
|
|
|
|
|
|
// The results that will be displayed by <Search>.
|
2023-11-22 12:19:49 -07:00
|
|
|
const [suggestionsResults, setSuggestionsResults] = useState({});
|
2023-11-26 20:49:36 -07:00
|
|
|
// The results summarized.
|
2023-11-22 12:19:49 -07:00
|
|
|
const [suggestionsSums, setSuggestionsSums] = useState({});
|
2023-11-20 11:01:46 -07:00
|
|
|
|
2023-11-22 12:19:49 -07:00
|
|
|
const normalizeSuggestionsResults = (newSuggestions) => {
|
2023-11-21 21:15:24 -07:00
|
|
|
// Convert the suggestions from the Backend to what the Semantic <Search> expects.
|
|
|
|
|
const lowerSearchStr = searchStr.toLowerCase();
|
|
|
|
|
|
2023-11-22 12:19:49 -07:00
|
|
|
let results = {};
|
2023-11-21 21:15:24 -07:00
|
|
|
|
|
|
|
|
// Suggested results are ordered.
|
|
|
|
|
if (newSuggestions.fileGroups > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.fileGroups = {
|
2023-11-21 21:15:24 -07:00
|
|
|
name: 'Files', results: [
|
2023-11-21 21:20:09 -07:00
|
|
|
{
|
|
|
|
|
title: newSuggestions.fileGroups.toString(),
|
|
|
|
|
type: 'files',
|
|
|
|
|
location: `/search?q=${encodeURIComponent(searchStr)}`
|
|
|
|
|
}
|
2023-11-21 21:15:24 -07:00
|
|
|
]
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-11-22 10:35:01 -07:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j, 0);
|
2023-11-21 21:15:24 -07:00
|
|
|
if (zimSum > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.zimsSum = {
|
2023-11-21 21:15:24 -07:00
|
|
|
name: 'Zims', results: [
|
2023-11-22 12:19:49 -07:00
|
|
|
{title: zimSum.toString(), type: 'zims', location: `/search/zim?q=${encodeURIComponent(searchStr)}`}
|
2023-11-21 21:15:24 -07:00
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (newSuggestions.channels && newSuggestions.channels.length > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.channels = {
|
2023-11-21 21:15:24 -07:00
|
|
|
name: 'Channels', results: newSuggestions.channels.map(i => {
|
2023-11-21 21:20:09 -07:00
|
|
|
return {type: 'channel', title: i['name'], id: i['id'], location: `/videos/channel/${i.id}/video`}
|
2023-11-21 21:15:24 -07:00
|
|
|
})
|
2023-11-20 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
}
|
2023-11-21 21:15:24 -07:00
|
|
|
if (newSuggestions.domains && newSuggestions.domains.length > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.domains = {
|
2023-11-21 21:15:24 -07:00
|
|
|
name: 'Domains', results: newSuggestions.domains.map(i => {
|
2023-11-21 21:20:09 -07:00
|
|
|
return {
|
|
|
|
|
type: 'domain',
|
|
|
|
|
title: i.domain,
|
|
|
|
|
id: i.id,
|
|
|
|
|
domain: i.domain,
|
|
|
|
|
location: `/archive?domain=${i.domain}`
|
|
|
|
|
}
|
2023-11-21 21:15:24 -07:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-22 10:35:01 -07:00
|
|
|
const matchingTags = fuzzyMatchTagsByName(searchStr);
|
|
|
|
|
if (matchingTags && matchingTags.length > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.tags = {
|
2023-11-22 10:35:01 -07:00
|
|
|
name: 'Tags', results: matchingTags.map(i => {
|
2023-11-22 12:19:49 -07:00
|
|
|
return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.name)}`}
|
2023-11-22 10:35:01 -07:00
|
|
|
})
|
|
|
|
|
}
|
2023-11-21 21:15:24 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-22 10:35:01 -07:00
|
|
|
const matchingApps = SUGGESTED_APPS.filter(i =>
|
|
|
|
|
i.title.toLowerCase().includes(lowerSearchStr)
|
|
|
|
|
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
2023-11-21 21:15:24 -07:00
|
|
|
if (matchingApps && matchingApps.length > 0) {
|
2023-11-22 12:19:49 -07:00
|
|
|
results.apps = {name: 'Apps', results: matchingApps};
|
2023-11-21 21:15:24 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-22 12:19:49 -07:00
|
|
|
setSuggestionsResults(results);
|
|
|
|
|
setSuggestionsSums({
|
|
|
|
|
fileGroups: newSuggestions.fileGroups,
|
|
|
|
|
zims: zimSum,
|
|
|
|
|
channels: newSuggestions.channels.length,
|
|
|
|
|
domains: newSuggestions.domains.length,
|
|
|
|
|
tags: matchingTags.length,
|
|
|
|
|
apps: matchingApps.length,
|
2023-11-26 20:49:36 -07:00
|
|
|
});
|
2023-11-20 11:01:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
2023-11-22 12:19:49 -07:00
|
|
|
setSuggestionsSums({});
|
|
|
|
|
setSuggestionsResults({});
|
2023-11-20 11:01:46 -07:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
if (!_.isEmpty(suggestions)) {
|
|
|
|
|
normalizeSuggestionsResults(suggestions);
|
|
|
|
|
}
|
|
|
|
|
}, [JSON.stringify(suggestions)]);
|
2023-11-20 11:29:14 -07:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
// User clicked on a result in the dropdown.
|
2023-11-21 21:20:09 -07:00
|
|
|
const handleResultSelect = ({result}) => navigate(result.location);
|
2023-11-21 13:01:52 -07:00
|
|
|
|
|
|
|
|
const resultRenderer = ({type, title, description}) => {
|
|
|
|
|
if (type === 'tag') {
|
2023-11-21 21:15:24 -07:00
|
|
|
return <SingleTag name={title}/>;
|
2023-11-21 13:01:52 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-21 21:15:24 -07:00
|
|
|
if (description) {
|
2023-11-21 14:46:55 -07:00
|
|
|
return <>
|
|
|
|
|
<SHeader as='h4'>{title}</SHeader>
|
|
|
|
|
{description}
|
|
|
|
|
</>
|
|
|
|
|
}
|
2023-11-26 20:49:36 -07:00
|
|
|
// No specific renderer, use the generic.
|
2023-11-21 21:15:24 -07:00
|
|
|
return <span>{title}</span>
|
2023-11-21 13:01:52 -07:00
|
|
|
};
|
2023-11-20 11:29:14 -07:00
|
|
|
|
2024-01-01 11:31:17 -07:00
|
|
|
return {
|
|
|
|
|
suggestions,
|
|
|
|
|
suggestionsResults,
|
|
|
|
|
suggestionsSums,
|
|
|
|
|
searchStr,
|
|
|
|
|
setSearchStr,
|
|
|
|
|
handleResultSelect,
|
|
|
|
|
resultRenderer,
|
|
|
|
|
loading
|
|
|
|
|
}
|
2023-11-22 12:19:49 -07:00
|
|
|
}
|
|
|
|
|
|
2023-11-20 11:01:46 -07:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
export function useSearchEstimate(searchStr, tagNames) {
|
2024-01-01 11:31:17 -07:00
|
|
|
const {data: filesData, sendRequest: sendFilesRequest, loading: loadingFiles} = useLatestRequest(250);
|
|
|
|
|
const {data: zimsData, sendRequest: sendZimsRequest, loading: loadingZims} = useLatestRequest(250);
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
// The estimates from the response.
|
2024-01-01 11:31:17 -07:00
|
|
|
const [estimates, setEstimates] = React.useState({
|
|
|
|
|
fileGroups: [],
|
|
|
|
|
zims: [],
|
|
|
|
|
});
|
2023-11-26 20:49:36 -07:00
|
|
|
// The estimates summarized.
|
2024-01-01 11:31:17 -07:00
|
|
|
const [estimatesSums, setEstimatesSums] = React.useState({
|
|
|
|
|
fileGroups: 0,
|
|
|
|
|
zims: 0,
|
|
|
|
|
});
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
// Handle results from `sendRequest`.
|
2024-01-01 11:31:17 -07:00
|
|
|
if (_.isEmpty(filesData)) {
|
2023-11-26 20:49:36 -07:00
|
|
|
return;
|
2023-11-02 15:24:07 -06:00
|
|
|
}
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2024-01-01 11:31:17 -07:00
|
|
|
setEstimates({...estimates, fileGroups: filesData.fileGroups});
|
|
|
|
|
setEstimatesSums({...estimatesSums, fileGroups: filesData.fileGroups});
|
|
|
|
|
}, [JSON.stringify(filesData)]);
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2023-11-26 20:49:36 -07:00
|
|
|
React.useEffect(() => {
|
2024-01-01 11:31:17 -07:00
|
|
|
sendFilesRequest(async () => await searchEstimateFiles(searchStr, tagNames));
|
|
|
|
|
}, [searchStr, JSON.stringify(tagNames)], sendFilesRequest);
|
2023-05-29 18:04:03 -06:00
|
|
|
|
2024-01-01 11:31:17 -07:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
// Handle results from `sendRequest`.
|
|
|
|
|
if (_.isEmpty(zimsData)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setEstimates({...estimates, zimsEstimates: zimsData.zimsEstimates});
|
|
|
|
|
setEstimatesSums({...estimatesSums, zims: zimsData.zimsEstimates.reduce((i, j) => i + j['estimate'], 0)});
|
|
|
|
|
}, [JSON.stringify(zimsData)]);
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
sendZimsRequest(async () => await searchEstimateZims(searchStr, tagNames));
|
|
|
|
|
}, [searchStr, JSON.stringify(tagNames)], sendZimsRequest);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
estimates,
|
|
|
|
|
estimatesSums,
|
|
|
|
|
loading: loadingFiles || loadingZims,
|
|
|
|
|
}
|
2023-05-29 18:04:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function SearchView() {
|
2023-11-26 20:49:36 -07:00
|
|
|
const {searchStr, activeTags} = useSearch();
|
2024-01-01 11:31:17 -07:00
|
|
|
const {estimates, estimatesSums, loading} = useSearchEstimate(searchStr, activeTags);
|
2023-11-26 20:49:36 -07:00
|
|
|
|
|
|
|
|
let filesTabName = <span>Files <Label>?</Label></span>;
|
|
|
|
|
let zimsTabName = <span>Zims<Label>?</Label></span>;
|
|
|
|
|
if (!_.isEmpty(estimatesSums)) {
|
|
|
|
|
filesTabName = <span>Files <Label>{normalizeEstimate(estimatesSums.fileGroups)}</Label></span>;
|
|
|
|
|
zimsTabName = <span>Zims <Label>{normalizeEstimate(estimatesSums.zims)}</Label></span>;
|
2023-05-29 18:04:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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/>}/>
|
2024-01-01 11:31:17 -07:00
|
|
|
<Route path='/zim' exact element={<ZimSearchView estimates={estimates} loading={loading}/>}/>
|
2023-05-29 18:04:03 -06:00
|
|
|
</Routes>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
}
|
2024-01-01 11:31:17 -07:00
|
|
|
|
|
|
|
|
export function SearchIconButton() {
|
|
|
|
|
// A single button which displays a modal for search suggestions.
|
|
|
|
|
const {
|
|
|
|
|
suggestionsResults,
|
|
|
|
|
handleResultSelect,
|
|
|
|
|
resultRenderer,
|
|
|
|
|
loading,
|
|
|
|
|
searchStr,
|
|
|
|
|
setSearchStr,
|
|
|
|
|
} = useContext(SearchSuggestionsContext);
|
|
|
|
|
const [open, setOpen] = React.useState(false);
|
|
|
|
|
|
|
|
|
|
const localHandleResultSelect = (i) => {
|
|
|
|
|
// Close modal when user selects a result.
|
|
|
|
|
setOpen(false);
|
|
|
|
|
handleResultSelect(i);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <React.Fragment>
|
|
|
|
|
<Button icon='search' onClick={() => setOpen(!open)}/>
|
|
|
|
|
<Modal open={open} onClose={() => setOpen(false)} centered={false}>
|
|
|
|
|
<ModalContent>
|
|
|
|
|
<Grid columns={2}>
|
|
|
|
|
<Grid.Row>
|
|
|
|
|
<Grid.Column mobile={13} computer={14}>
|
|
|
|
|
<SearchResultsInput clearable
|
|
|
|
|
searchStr={searchStr}
|
|
|
|
|
onChange={setSearchStr}
|
|
|
|
|
onSubmit={setSearchStr}
|
|
|
|
|
size='large'
|
|
|
|
|
placeholder='Search everywhere...'
|
|
|
|
|
results={suggestionsResults}
|
|
|
|
|
handleResultSelect={localHandleResultSelect}
|
|
|
|
|
resultRenderer={resultRenderer}
|
|
|
|
|
loading={loading}
|
|
|
|
|
/>
|
|
|
|
|
</Grid.Column>
|
|
|
|
|
<Grid.Column mobile={3} computer={2}>
|
|
|
|
|
<FileSearchFilterButton/>
|
|
|
|
|
</Grid.Column>
|
|
|
|
|
</Grid.Row>
|
|
|
|
|
</Grid>
|
|
|
|
|
</ModalContent>
|
|
|
|
|
</Modal>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
}
|