From f37e598c53c74fd1ddc0231914c59fd72734a219 Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 20 Nov 2023 11:01:46 -0700 Subject: [PATCH] Global search will suggests Channels and Domains for user navigation. --- app/src/DashboardPage.js | 5 ++++- app/src/api.js | 14 +++++++++++++ app/src/components/Common.js | 34 ++++++++++++++++++++++++------- app/src/components/Search.js | 39 +++++++++++++++++++++++++++++++++++- wrolpi/root_api.py | 16 +++++++++++++++ wrolpi/schema.py | 5 +++++ wrolpi/test/test_root_api.py | 26 ++++++++++++++++++++++++ 7 files changed, 130 insertions(+), 9 deletions(-) diff --git a/app/src/DashboardPage.js b/app/src/DashboardPage.js index 62166c38..a62c8995 100644 --- a/app/src/DashboardPage.js +++ b/app/src/DashboardPage.js @@ -22,7 +22,7 @@ import {refreshFiles} from "./api"; import _ from "lodash"; import {TagsDashboard} from "./Tags"; import {Upload} from "./components/Upload"; -import {SearchView, useSearch} from "./components/Search"; +import {SearchView, useSearch, useSearchSuggestions} from "./components/Search"; import {KiwixRestartMessage, OutdatedZimsMessage} from "./components/Zim"; import {useSettings, useWROLMode} from "./hooks/customHooks"; import {FileSearchFilterButton} from "./components/Files"; @@ -158,6 +158,7 @@ export function DashboardPage() { const navigate = useNavigate(); const {searchStr, setSearchStr, activeTags} = useSearch(); + const {suggestions} = useSearchSuggestions(); const {status} = useContext(StatusContext); let title = 'Dashboard'; @@ -196,6 +197,7 @@ export function DashboardPage() { actionIcon='search' onClear={() => navigate('/')} style={{marginBottom: '2em'}} + results={suggestions} /> @@ -217,6 +219,7 @@ export function DashboardPage() { actionIcon='search' onClear={() => navigate('/')} style={{marginBottom: '2em'}} + results={suggestions} /> diff --git a/app/src/api.js b/app/src/api.js index 219ef240..89a72827 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1019,6 +1019,20 @@ export async function searchEstimate(search_str, tagNames) { } } +export async function searchSuggestions(search_str) { + const body = {search_str: search_str}; + const response = await apiPost(`${API_URI}/search_suggestions`, body); + if (response.ok) { + const content = await response.json(); + const channels = content['channels']; + const domains = content['domains']; + return { + channels, + domains, + } + } +} + export async function getOutdatedZims() { const response = await apiGet(`${API_URI}/zim/outdated`); if (response.status === 200) { diff --git a/app/src/components/Common.js b/app/src/components/Common.js index 9bbb5b8e..7008c1a2 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -412,9 +412,11 @@ export function SearchInput({ autoFocus = false, onClear = null, onChange = null, + results = null, ...props }) { const [value, setValue] = useState(searchStr || ''); + const navigate = useNavigate(); const handleChange = (e) => { if (e) { @@ -453,6 +455,22 @@ export function SearchInput({ } } + const localHandleResultSelect = (e, {result}) => { + if (e) { + e.preventDefault(); + } + + if (!result) { + return; + } + + if (result['type'] === 'channel') { + navigate(`/videos/channel/${result['id']}/video`); + } else if (result['type'] === 'domain') { + navigate(`/archive?domain=${result['domain']}`); + } + } + if ((action || actionIcon) && searchStr && searchStr === value && clearable) { action =