mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-19 14:36:22 -05:00
Fetching search estimate with tags separate from suggestions.
This commit is contained in:
parent
ff62f9e21b
commit
d87bc7b314
9 changed files with 258 additions and 164 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import React, {useContext, useEffect, useState} from "react";
|
||||
import React, {useState} from "react";
|
||||
import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom";
|
||||
import {FilesSearchView} from "./Files";
|
||||
import {useLatestRequest, usePages, useQuery} from "../hooks/customHooks";
|
||||
|
|
@ -7,7 +7,7 @@ import {searchEstimate, searchSuggestions} from "../api";
|
|||
import {fuzzyMatch, normalizeEstimate, TabLinks} from "./Common";
|
||||
import _ from "lodash";
|
||||
import {TagsContext} from "../Tags";
|
||||
import {Header as SHeader} from "semantic-ui-react";
|
||||
import {Header as SHeader, Label} from "semantic-ui-react";
|
||||
|
||||
export const SearchSuggestionsContext = React.createContext({
|
||||
suggestionsResults: {},
|
||||
|
|
@ -18,6 +18,15 @@ export const SearchSuggestionsContext = React.createContext({
|
|||
loading: false,
|
||||
});
|
||||
|
||||
|
||||
export const SearchSuggestionsProvider = (props) => {
|
||||
const value = useSearchSuggestions();
|
||||
|
||||
return <SearchSuggestionsContext.Provider value={value}>
|
||||
{props.children}
|
||||
</SearchSuggestionsContext.Provider>
|
||||
}
|
||||
|
||||
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,142 +38,6 @@ const SUGGESTED_APPS = [
|
|||
{location: '/help', title: 'Help', description: 'Help documents for WROLPi'},
|
||||
];
|
||||
|
||||
|
||||
export function useSearchSuggestions() {
|
||||
const navigate = useNavigate();
|
||||
const {searchParams} = useQuery();
|
||||
const {SingleTag, fuzzyMatchTagsByName} = React.useContext(TagsContext);
|
||||
|
||||
const searchStr = searchParams.get('q');
|
||||
|
||||
const {data, sendRequest, loading} = useLatestRequest(500);
|
||||
const [suggestionsResults, setSuggestionsResults] = useState({});
|
||||
const [suggestions, setSuggestions] = useState({});
|
||||
const [suggestionsSums, setSuggestionsSums] = useState({});
|
||||
|
||||
const normalizeSuggestionsResults = (newSuggestions) => {
|
||||
// Convert the suggestions from the Backend to what the Semantic <Search> expects.
|
||||
const lowerSearchStr = searchStr.toLowerCase();
|
||||
|
||||
let results = {};
|
||||
|
||||
// Suggested results are ordered.
|
||||
if (newSuggestions.fileGroups > 0) {
|
||||
results.fileGroups = {
|
||||
name: 'Files', results: [
|
||||
{
|
||||
title: newSuggestions.fileGroups.toString(),
|
||||
type: 'files',
|
||||
location: `/search?q=${encodeURIComponent(searchStr)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j['estimate'], 0);
|
||||
if (zimSum > 0) {
|
||||
results.zimsSum = {
|
||||
name: 'Zims', results: [
|
||||
{title: zimSum.toString(), type: 'zims', location: `/search/zim?q=${encodeURIComponent(searchStr)}`}
|
||||
],
|
||||
}
|
||||
}
|
||||
if (newSuggestions.channels && newSuggestions.channels.length > 0) {
|
||||
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) {
|
||||
results.domains = {
|
||||
name: 'Domains', results: newSuggestions.domains.map(i => {
|
||||
return {
|
||||
type: 'domain',
|
||||
title: i.domain,
|
||||
id: i.id,
|
||||
domain: i.domain,
|
||||
location: `/archive?domain=${i.domain}`
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const matchingTags = fuzzyMatchTagsByName(searchStr);
|
||||
if (matchingTags && matchingTags.length > 0) {
|
||||
results.tags = {
|
||||
name: 'Tags', results: matchingTags.map(i => {
|
||||
return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.name)}`}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const matchingApps = SUGGESTED_APPS.filter(i =>
|
||||
i.title.toLowerCase().includes(lowerSearchStr)
|
||||
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
||||
if (matchingApps && matchingApps.length > 0) {
|
||||
results.apps = {name: 'Apps', results: matchingApps};
|
||||
}
|
||||
|
||||
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) {
|
||||
setSuggestions(data);
|
||||
normalizeSuggestionsResults(data);
|
||||
}
|
||||
}, [JSON.stringify(data)]);
|
||||
|
||||
React.useEffect(() => {
|
||||
if (!searchStr || searchStr.length === 0) {
|
||||
console.debug('Not getting suggestions because there is no search.');
|
||||
return;
|
||||
}
|
||||
setSuggestions({});
|
||||
setSuggestionsSums({});
|
||||
setSuggestionsResults({});
|
||||
|
||||
// Use the useLatestRequest to handle user typing.
|
||||
sendRequest(async () => await searchSuggestions(searchStr));
|
||||
}, [searchStr, sendRequest]);
|
||||
|
||||
const handleResultSelect = ({result}) => navigate(result.location);
|
||||
|
||||
const resultRenderer = ({type, title, description}) => {
|
||||
if (type === 'tag') {
|
||||
return <SingleTag name={title}/>;
|
||||
}
|
||||
|
||||
// No specific renderer, use the generic.
|
||||
if (description) {
|
||||
return <>
|
||||
<SHeader as='h4'>{title}</SHeader>
|
||||
{description}
|
||||
</>
|
||||
}
|
||||
return <span>{title}</span>
|
||||
};
|
||||
|
||||
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) => {
|
||||
const navigate = useNavigate();
|
||||
|
||||
|
|
@ -224,15 +97,178 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false
|
|||
}
|
||||
}
|
||||
|
||||
export function useSuggestions(searchStr) {
|
||||
const {data, sendRequest, loading} = useLatestRequest(500);
|
||||
|
||||
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}
|
||||
}
|
||||
|
||||
|
||||
export function useSearchSuggestions() {
|
||||
const navigate = useNavigate();
|
||||
const {SingleTag, fuzzyMatchTagsByName} = React.useContext(TagsContext);
|
||||
const {searchStr} = useSearch();
|
||||
const {suggestions, loading} = useSuggestions(searchStr);
|
||||
|
||||
// The results that will be displayed by <Search>.
|
||||
const [suggestionsResults, setSuggestionsResults] = useState({});
|
||||
// The results summarized.
|
||||
const [suggestionsSums, setSuggestionsSums] = useState({});
|
||||
|
||||
const normalizeSuggestionsResults = (newSuggestions) => {
|
||||
// Convert the suggestions from the Backend to what the Semantic <Search> expects.
|
||||
const lowerSearchStr = searchStr.toLowerCase();
|
||||
|
||||
let results = {};
|
||||
|
||||
// Suggested results are ordered.
|
||||
if (newSuggestions.fileGroups > 0) {
|
||||
results.fileGroups = {
|
||||
name: 'Files', results: [
|
||||
{
|
||||
title: newSuggestions.fileGroups.toString(),
|
||||
type: 'files',
|
||||
location: `/search?q=${encodeURIComponent(searchStr)}`
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
|
||||
const zimSum = newSuggestions.zimsEstimates.reduce((i, j) => i + j, 0);
|
||||
if (zimSum > 0) {
|
||||
results.zimsSum = {
|
||||
name: 'Zims', results: [
|
||||
{title: zimSum.toString(), type: 'zims', location: `/search/zim?q=${encodeURIComponent(searchStr)}`}
|
||||
],
|
||||
}
|
||||
}
|
||||
if (newSuggestions.channels && newSuggestions.channels.length > 0) {
|
||||
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) {
|
||||
results.domains = {
|
||||
name: 'Domains', results: newSuggestions.domains.map(i => {
|
||||
return {
|
||||
type: 'domain',
|
||||
title: i.domain,
|
||||
id: i.id,
|
||||
domain: i.domain,
|
||||
location: `/archive?domain=${i.domain}`
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const matchingTags = fuzzyMatchTagsByName(searchStr);
|
||||
if (matchingTags && matchingTags.length > 0) {
|
||||
results.tags = {
|
||||
name: 'Tags', results: matchingTags.map(i => {
|
||||
return {type: 'tag', title: i.name, location: `/search?tag=${encodeURIComponent(i.name)}`}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
const matchingApps = SUGGESTED_APPS.filter(i =>
|
||||
i.title.toLowerCase().includes(lowerSearchStr)
|
||||
|| fuzzyMatch(i.title.toLowerCase(), lowerSearchStr));
|
||||
if (matchingApps && matchingApps.length > 0) {
|
||||
results.apps = {name: 'Apps', results: matchingApps};
|
||||
}
|
||||
|
||||
setSuggestionsResults(results);
|
||||
setSuggestionsSums({
|
||||
fileGroups: newSuggestions.fileGroups,
|
||||
zims: zimSum,
|
||||
channels: newSuggestions.channels.length,
|
||||
domains: newSuggestions.domains.length,
|
||||
tags: matchingTags.length,
|
||||
apps: matchingApps.length,
|
||||
});
|
||||
}
|
||||
|
||||
React.useEffect(() => {
|
||||
setSuggestionsSums({});
|
||||
setSuggestionsResults({});
|
||||
|
||||
if (!_.isEmpty(suggestions)) {
|
||||
normalizeSuggestionsResults(suggestions);
|
||||
}
|
||||
}, [JSON.stringify(suggestions)]);
|
||||
|
||||
// User clicked on a result in the dropdown.
|
||||
const handleResultSelect = ({result}) => navigate(result.location);
|
||||
|
||||
const resultRenderer = ({type, title, description}) => {
|
||||
if (type === 'tag') {
|
||||
return <SingleTag name={title}/>;
|
||||
}
|
||||
|
||||
if (description) {
|
||||
return <>
|
||||
<SHeader as='h4'>{title}</SHeader>
|
||||
{description}
|
||||
</>
|
||||
}
|
||||
// No specific renderer, use the generic.
|
||||
return <span>{title}</span>
|
||||
};
|
||||
|
||||
return {suggestions, suggestionsResults, suggestionsSums, searchStr, handleResultSelect, resultRenderer, loading}
|
||||
}
|
||||
|
||||
|
||||
export function useSearchEstimate(searchStr, tagNames) {
|
||||
const {data, sendRequest, loading} = useLatestRequest(250);
|
||||
|
||||
// The estimates from the response.
|
||||
const [estimates, setEstimates] = React.useState({});
|
||||
// The estimates summarized.
|
||||
const [estimatesSums, setEstimatesSums] = React.useState({});
|
||||
|
||||
React.useEffect(() => {
|
||||
// Handle results from `sendRequest`.
|
||||
if (_.isEmpty(data)) {
|
||||
return;
|
||||
}
|
||||
|
||||
setEstimates(data);
|
||||
setEstimatesSums({
|
||||
fileGroups: data.fileGroups,
|
||||
zims: data.zimsEstimates.reduce((i, j) => i + j['estimate'], 0),
|
||||
});
|
||||
}, [JSON.stringify(data)]);
|
||||
|
||||
React.useEffect(() => {
|
||||
// Use the `useLatestRequest` to handle user typing.
|
||||
sendRequest(async () => await searchEstimate(searchStr, tagNames));
|
||||
}, [searchStr, JSON.stringify(tagNames)], sendRequest);
|
||||
|
||||
return {estimates, estimatesSums, loading}
|
||||
}
|
||||
|
||||
|
||||
export function SearchView() {
|
||||
const {suggestionsSums, suggestions} = useContext(SearchSuggestionsContext);
|
||||
const {searchStr, activeTags} = useSearch();
|
||||
const {estimates, estimatesSums} = useSearchEstimate(searchStr, activeTags);
|
||||
|
||||
let filesTabName = 'Files';
|
||||
let zimsTabName = 'Zims';
|
||||
if (!_.isEmpty(suggestionsSums)) {
|
||||
filesTabName = `Files (${normalizeEstimate(suggestionsSums.fileGroups)})`;
|
||||
zimsTabName = `Zims (${normalizeEstimate(suggestionsSums.zims)})`;
|
||||
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>;
|
||||
}
|
||||
|
||||
const links = [
|
||||
|
|
@ -244,7 +280,7 @@ export function SearchView() {
|
|||
<TabLinks links={links}/>
|
||||
<Routes>
|
||||
<Route path='/*' element={<FilesSearchView/>}/>
|
||||
<Route path='/zim' exact element={<ZimSearchView estimates={suggestions}/>}/>
|
||||
<Route path='/zim' exact element={<ZimSearchView estimates={estimates}/>}/>
|
||||
</Routes>
|
||||
</React.Fragment>
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue