mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-03 13:19:58 -05:00
Converting Search Suggetions to a Context. Search estimates is removed and suggestions is used instead.
This commit is contained in:
parent
57262442bd
commit
bbfe76890b
13 changed files with 101 additions and 208 deletions
|
|
@ -492,10 +492,10 @@ function ArchivesPage() {
|
|||
!!searchStr,
|
||||
);
|
||||
|
||||
const searchInput = <SearchInput clearable
|
||||
searchStr={searchStr}
|
||||
onSubmit={setSearchStr}
|
||||
placeholder='Search Archives...'
|
||||
const searchInput = <SearchInput
|
||||
searchStr={searchStr}
|
||||
onSubmit={setSearchStr}
|
||||
placeholder='Search Archives...'
|
||||
/>;
|
||||
|
||||
return <>
|
||||
|
|
|
|||
|
|
@ -510,15 +510,14 @@ export function ChannelsPage() {
|
|||
<Grid stackable columns={2}>
|
||||
<Grid.Row>
|
||||
<Grid.Column>
|
||||
<SearchInput clearable={true}
|
||||
placeholder='Name filter...'
|
||||
size="large"
|
||||
actionIcon='search'
|
||||
searchStr={searchStr}
|
||||
disabled={!Array.isArray(channels) || channels.length === 0}
|
||||
onClear={() => setSearchStr('')}
|
||||
onChange={setSearchStr}
|
||||
onSubmit={null}
|
||||
<SearchInput
|
||||
placeholder='Name filter...'
|
||||
size='large'
|
||||
searchStr={searchStr}
|
||||
disabled={!Array.isArray(channels) || channels.length === 0}
|
||||
onClear={() => setSearchStr('')}
|
||||
onChange={setSearchStr}
|
||||
onSubmit={null}
|
||||
/>
|
||||
</Grid.Column>
|
||||
<Grid.Column textAlign='right'>
|
||||
|
|
|
|||
|
|
@ -538,7 +538,7 @@ export function SearchResultsInput({
|
|||
onResultSelect={localHandleResultSelect}
|
||||
value={value}
|
||||
size={size}
|
||||
results={!loading && results ? results : loadingResults}
|
||||
results={!loading && !_.isEmpty(results) ? results : loadingResults}
|
||||
resultRenderer={resultRenderer}
|
||||
className='search-input'
|
||||
loading={loading}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import React, {useEffect, useState} from "react";
|
||||
import React, {useContext, useEffect, useState} from "react";
|
||||
import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom";
|
||||
import {FilesSearchView} from "./Files";
|
||||
import {useLatestRequest, usePages, useQuery} from "../hooks/customHooks";
|
||||
|
|
@ -9,6 +9,15 @@ import _ from "lodash";
|
|||
import {TagsContext} from "../Tags";
|
||||
import {Header as SHeader} from "semantic-ui-react";
|
||||
|
||||
export const SearchSuggestionsContext = React.createContext({
|
||||
suggestionsResults: {},
|
||||
suggestions: {},
|
||||
suggestionsSums: {},
|
||||
handleResultSelect: null,
|
||||
resultRenderer: null,
|
||||
loading: false,
|
||||
});
|
||||
|
||||
const SUGGESTED_APPS = [
|
||||
{location: '/more/otp', title: 'One Time Pad', description: 'Encrypt and Decrypt messages'},
|
||||
{location: '/inventory', title: 'Inventory', description: 'Track and organize your food storage'},
|
||||
|
|
@ -29,17 +38,19 @@ export function useSearchSuggestions() {
|
|||
const searchStr = searchParams.get('q');
|
||||
|
||||
const {data, sendRequest, loading} = useLatestRequest(500);
|
||||
const [suggestions, setSuggestions] = useState(null);
|
||||
const [suggestionsResults, setSuggestionsResults] = useState({});
|
||||
const [suggestions, setSuggestions] = useState({});
|
||||
const [suggestionsSums, setSuggestionsSums] = useState({});
|
||||
|
||||
const normalizeSuggestions = (newSuggestions) => {
|
||||
const normalizeSuggestionsResults = (newSuggestions) => {
|
||||
// Convert the suggestions from the Backend to what the Semantic <Search> expects.
|
||||
const lowerSearchStr = searchStr.toLowerCase();
|
||||
|
||||
let matchingSuggestions = {};
|
||||
let results = {};
|
||||
|
||||
// Suggested results are ordered.
|
||||
if (newSuggestions.fileGroups > 0) {
|
||||
matchingSuggestions.fileGroups = {
|
||||
results.fileGroups = {
|
||||
name: 'Files', results: [
|
||||
{
|
||||
title: newSuggestions.fileGroups.toString(),
|
||||
|
|
@ -50,23 +61,23 @@ export function useSearchSuggestions() {
|
|||
};
|
||||
}
|
||||
|
||||
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0).toString();
|
||||
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0);
|
||||
if (zimSum > 0) {
|
||||
matchingSuggestions.zimsSum = {
|
||||
results.zimsSum = {
|
||||
name: 'Zims', results: [
|
||||
{title: zimSum, type: 'zims', location: `/search/zim?q=${encodeURIComponent(searchStr)}`}
|
||||
{title: zimSum.toString(), type: 'zims', location: `/search/zim?q=${encodeURIComponent(searchStr)}`}
|
||||
],
|
||||
}
|
||||
}
|
||||
if (newSuggestions.channels && newSuggestions.channels.length > 0) {
|
||||
matchingSuggestions.channels = {
|
||||
results.channels = {
|
||||
name: 'Channels', results: newSuggestions.channels.map(i => {
|
||||
return {type: 'channel', title: i['name'], id: i['id'], location: `/videos/channel/${i.id}/video`}
|
||||
})
|
||||
}
|
||||
}
|
||||
if (newSuggestions.domains && newSuggestions.domains.length > 0) {
|
||||
matchingSuggestions.domains = {
|
||||
results.domains = {
|
||||
name: 'Domains', results: newSuggestions.domains.map(i => {
|
||||
return {
|
||||
type: 'domain',
|
||||
|
|
@ -81,9 +92,9 @@ export function useSearchSuggestions() {
|
|||
|
||||
const matchingTags = fuzzyMatchTagsByName(searchStr);
|
||||
if (matchingTags && matchingTags.length > 0) {
|
||||
matchingSuggestions.tags = {
|
||||
results.tags = {
|
||||
name: 'Tags', results: matchingTags.map(i => {
|
||||
return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.title)}`}
|
||||
return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.name)}`}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -92,16 +103,24 @@ export function useSearchSuggestions() {
|
|||
i.title.toLowerCase().includes(lowerSearchStr)
|
||||
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
||||
if (matchingApps && matchingApps.length > 0) {
|
||||
matchingSuggestions.apps = {name: 'Apps', results: matchingApps};
|
||||
results.apps = {name: 'Apps', results: matchingApps};
|
||||
}
|
||||
|
||||
console.debug('matchingSuggestions', matchingSuggestions);
|
||||
setSuggestions(matchingSuggestions);
|
||||
setSuggestionsResults(results);
|
||||
setSuggestionsSums({
|
||||
fileGroups: newSuggestions.fileGroups,
|
||||
zims: zimSum,
|
||||
channels: newSuggestions.channels.length,
|
||||
domains: newSuggestions.domains.length,
|
||||
tags: matchingTags.length,
|
||||
apps: matchingApps.length,
|
||||
})
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
if (data) {
|
||||
normalizeSuggestions(data);
|
||||
setSuggestions(data);
|
||||
normalizeSuggestionsResults(data);
|
||||
}
|
||||
}, [JSON.stringify(data)]);
|
||||
|
||||
|
|
@ -110,7 +129,9 @@ export function useSearchSuggestions() {
|
|||
console.debug('Not getting suggestions because there is no search.');
|
||||
return;
|
||||
}
|
||||
setSuggestions(undefined);
|
||||
setSuggestions({});
|
||||
setSuggestionsSums({});
|
||||
setSuggestionsResults({});
|
||||
|
||||
// Use the useLatestRequest to handle user typing.
|
||||
sendRequest(async () => await searchSuggestions(searchStr));
|
||||
|
|
@ -133,7 +154,15 @@ export function useSearchSuggestions() {
|
|||
return <span>{title}</span>
|
||||
};
|
||||
|
||||
return {suggestions, searchStr, handleResultSelect, resultRenderer, loading}
|
||||
return {suggestions, suggestionsResults, suggestionsSums, searchStr, handleResultSelect, resultRenderer, loading}
|
||||
}
|
||||
|
||||
export const SearchSuggestionsProvider = (props) => {
|
||||
const value = useSearchSuggestions();
|
||||
|
||||
return <SearchSuggestionsContext.Provider value={value}>
|
||||
{props.children}
|
||||
</SearchSuggestionsContext.Provider>
|
||||
}
|
||||
|
||||
export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => {
|
||||
|
|
@ -196,51 +225,14 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false
|
|||
}
|
||||
|
||||
|
||||
export const useSearchEstimate = (search_str, activeTags) => {
|
||||
const [fileGroups, setFileGroups] = useState('?');
|
||||
const [zims, setZims] = useState(null);
|
||||
const [zimsSum, setZimsSum] = useState('?');
|
||||
|
||||
const localFetchSearchEstimate = async () => {
|
||||
if (!search_str && (!activeTags || activeTags.length === 0)) {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
const debouncedEstimate = _.debounce(async () => await localFetchSearchEstimate(), 800);
|
||||
|
||||
useEffect(() => {
|
||||
debouncedEstimate();
|
||||
|
||||
return () => debouncedEstimate.cancel();
|
||||
}, [search_str, JSON.stringify(activeTags)]);
|
||||
|
||||
return {fileGroups, zimsSum, zims}
|
||||
}
|
||||
|
||||
export function SearchView() {
|
||||
const {searchStr, activeTags} = useSearch();
|
||||
const estimates = useSearchEstimate(searchStr, activeTags);
|
||||
const {suggestionsSums, suggestions} = useContext(SearchSuggestionsContext);
|
||||
|
||||
let filesTabName = 'Files';
|
||||
let zimsTabName = 'Zims';
|
||||
if (estimates) {
|
||||
const {fileGroups, zimsSum} = estimates;
|
||||
filesTabName = `Files (${normalizeEstimate(fileGroups)})`;
|
||||
zimsTabName = `Zims (${normalizeEstimate(zimsSum)})`;
|
||||
if (!_.isEmpty(suggestionsSums)) {
|
||||
filesTabName = `Files (${normalizeEstimate(suggestionsSums.fileGroups)})`;
|
||||
zimsTabName = `Zims (${normalizeEstimate(suggestionsSums.zims)})`;
|
||||
}
|
||||
|
||||
const links = [
|
||||
|
|
@ -252,7 +244,7 @@ export function SearchView() {
|
|||
<TabLinks links={links}/>
|
||||
<Routes>
|
||||
<Route path='/*' element={<FilesSearchView/>}/>
|
||||
<Route path='/zim' exact element={<ZimSearchView estimates={estimates}/>}/>
|
||||
<Route path='/zim' exact element={<ZimSearchView estimates={suggestions}/>}/>
|
||||
</Routes>
|
||||
</React.Fragment>
|
||||
}
|
||||
|
|
|
|||
|
|
@ -181,7 +181,7 @@ function VideosPage() {
|
|||
!!searchStr,
|
||||
);
|
||||
|
||||
const searchInput = <SearchInput clearable
|
||||
const searchInput = <SearchInput
|
||||
searchStr={searchStr}
|
||||
onSubmit={setSearchStr}
|
||||
placeholder='Search Videos...'
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ import {ThemeContext} from "../contexts/contexts";
|
|||
import {Link, Route, Routes} from "react-router-dom";
|
||||
import {SortableTable} from "./SortableTable";
|
||||
import {toast} from "react-semantic-toasts-2";
|
||||
import _ from "lodash";
|
||||
|
||||
const VIEWER_URL = `http://${window.location.hostname}:8085/`;
|
||||
|
||||
|
|
@ -288,17 +289,15 @@ const ZimsRefreshWarning = () => {
|
|||
export const ZimSearchView = ({estimates}) => {
|
||||
const [activeIndex, setActiveIndex] = React.useState(null);
|
||||
const {searchStr, activeTags, setTags} = useSearch();
|
||||
const {zims} = estimates;
|
||||
const {zimsEstimates} = estimates;
|
||||
|
||||
const handleClick = (index, activeIndex_) => {
|
||||
setActiveIndex(index === activeIndex_ ? -1 : index);
|
||||
}
|
||||
|
||||
let body = <TextPlaceholder/>;
|
||||
if (zims && zims.length === 0) {
|
||||
body = <ZimsRefreshWarning/>;
|
||||
} else if (zims) {
|
||||
body = zims.map((i, index) => <ZimAccordion
|
||||
let body = <ZimsRefreshWarning/>;
|
||||
if (!_.isEmpty(zimsEstimates)) {
|
||||
body = zimsEstimates.map((i, index) => <ZimAccordion
|
||||
key={i['path']}
|
||||
index={index}
|
||||
activeIndex={activeIndex}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue