diff --git a/app/src/DashboardPage.js b/app/src/DashboardPage.js index 6bc89ace..caade35c 100644 --- a/app/src/DashboardPage.js +++ b/app/src/DashboardPage.js @@ -25,6 +25,7 @@ import {Upload} from "./components/Upload"; import {SearchView, useSearch} from "./components/Search"; import {KiwixRestartMessage, OutdatedZimsMessage} from "./components/Zim"; import {useSettings, useWROLMode} from "./hooks/customHooks"; +import {FileSearchFilterButton} from "./components/Files"; function FlagsMessages({flags}) { const {settings, fetchSettings} = useSettings(); @@ -82,18 +83,14 @@ function FlagsMessages({flags}) { } -export function DashboardPage() { - useTitle('Dashboard'); - - const navigate = useNavigate(); - - const {searchStr, setSearchStr, activeTags} = useSearch(); +export function Getters() { const {status} = useContext(StatusContext); const wrolModeEnabled = useWROLMode(); // Getters are Downloads or Uploads. const [selectedGetter, setSelectedGetter] = useState(null); const gettersDisabled = status?.flags?.indexOf('refresh_complete') === -1; + const handleSetGetter = (e, value) => { if (e) { e.preventDefault(); @@ -101,7 +98,7 @@ export function DashboardPage() { setSelectedGetter(value); } - let getter = + const getter = Or @@ -120,6 +117,7 @@ export function DashboardPage() { ; + let getterModal; if (selectedGetter === 'downloads') { getterModal = ; } + if (wrolModeEnabled) { + return <> + } + + return <> + {getter} + {getterModal} + +} + +export function DashboardPage() { + + const navigate = useNavigate(); + + const {searchStr, setSearchStr, activeTags} = useSearch(); + const {status} = useContext(StatusContext); + + const title = searchStr ? `Search: ${searchStr} - Dashboard` : 'Dashboard'; + useTitle(title); + // Only show dashboard parts if not searching. - let body; + let body = + + + + ; if (searchStr || (activeTags && activeTags.length > 0)) { + // User has submitted and wants full search. body = ; - } else { - body = <> - {!wrolModeEnabled && getter} - {getterModal} - - - ; } return - + + + + {!searchStr && } diff --git a/app/src/components/Channels.js b/app/src/components/Channels.js index ce11c386..85d9ca51 100644 --- a/app/src/components/Channels.js +++ b/app/src/components/Channels.js @@ -514,7 +514,9 @@ export function ChannelsPage() { searchStr={searchStr} disabled={!Array.isArray(channels) || channels.length === 0} onClear={() => setSearchStr('')} - onChange={setSearchStr}/> + onChange={setSearchStr} + onSubmit={null} + /> diff --git a/app/src/components/Common.js b/app/src/components/Common.js index a158764a..cb43d835 100644 --- a/app/src/components/Common.js +++ b/app/src/components/Common.js @@ -409,7 +409,7 @@ export function SearchInput({ onChange = null, ...props }) { - let [value, setValue] = useState(searchStr || ''); + const [value, setValue] = useState(searchStr || ''); const handleChange = (e) => { if (e) { diff --git a/app/src/components/Files.js b/app/src/components/Files.js index 4d60cfa8..11bd12f0 100644 --- a/app/src/components/Files.js +++ b/app/src/components/Files.js @@ -369,7 +369,7 @@ export function FilesView( } } -export function SearchFilter({filters = [], modalHeader}) { +export function SearchFilter({filters = [], modalHeader, size = 'medium'}) { const {filter, setFilter} = useSearchFilter(); const [open, setOpen] = useState(false); @@ -418,9 +418,11 @@ export function SearchFilter({filters = [], modalHeader}) { icon='filter' onClick={handleOpen} primary={!!filter} + size={size} /> } + return <> } @@ -445,12 +447,7 @@ export function TagsQuerySelector({onChange}) { /> } -export function FilesSearchView({ - showView = true, - showSelect = false, - emptySearch = false, - model, - }) { +export function FileSearchFilterButton({size = 'medium'}) { const filterOptions = [ {key: 'video', text: 'Video', value: 'video'}, {key: 'archive', text: 'Archive', value: 'archive'}, @@ -463,6 +460,16 @@ export function FilesSearchView({ {key: 'software', text: 'Software', value: 'software'}, ]; + return +} + +export function FilesSearchView({ + showView = true, + showSelect = false, + emptySearch = false, + model, + }) { + const {searchFiles, pages} = useSearchFiles(24, emptySearch, model); const {body, paginator, selectButton, viewButton, limitDropdown, tagQuerySelector} = FilesView( @@ -486,7 +493,6 @@ export function FilesSearchView({ {viewButton}} {limitDropdown} {tagQuerySelector} - @@ -499,7 +505,6 @@ export function FilesSearchView({ {viewButton}} {limitDropdown} {tagQuerySelector} - diff --git a/app/src/components/Search.js b/app/src/components/Search.js index 58e22779..7df36836 100644 --- a/app/src/components/Search.js +++ b/app/src/components/Search.js @@ -1,5 +1,5 @@ import React, {useEffect, useState} from "react"; -import {createSearchParams, Route, Routes, useHref, useNavigate} from "react-router-dom"; +import {createSearchParams, Route, Routes, useNavigate} from "react-router-dom"; import {FilesSearchView} from "./Files"; import {usePages, useQuery} from "../hooks/customHooks"; import {ZimSearchView} from "./Zim"; @@ -8,23 +8,34 @@ import {normalizeEstimate, TabLinks} from "./Common"; export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false, model) => { const navigate = useNavigate(); - const href = useHref(); const {searchParams, updateQuery} = useQuery(); + // `searchStr` means actually fetch the files/zims. const searchStr = searchParams.get('q'); + // 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({q: value, o: 0}).toString(), + search: createSearchParams(searchQuery).toString(), }); } + const clearSearch = () => { + navigate({pathname: window.location.pathname, search: ''}); + } + const setTags = (tags) => { updateQuery({tag: tags}); } @@ -43,13 +54,13 @@ export const useSearch = (defaultLimit = 48, totalPages = 0, emptySearch = false activeTags, addTag, filter, - href, model: model_, pages, removeTag, searchParams, searchStr, setSearchStr, + clearSearch, setTags, } } @@ -61,7 +72,7 @@ export const useSearchEstimate = (search_str, activeTags) => { const [zimsSum, setZimsSum] = useState('?'); const localFetchSearchEstimate = async () => { - if (!search_str && !activeTags) { + if (!search_str && (!activeTags || activeTags.length === 0)) { return; } try { diff --git a/app/src/hooks/customHooks.js b/app/src/hooks/customHooks.js index cdb9e40a..1e99c757 100644 --- a/app/src/hooks/customHooks.js +++ b/app/src/hooks/customHooks.js @@ -31,6 +31,7 @@ import {enumerate, filterToMimetypes, humanFileSize, secondsToFullDuration} from import {StatusContext} from "../contexts/contexts"; import {toast} from "react-semantic-toasts-2"; import {useSearch} from "../components/Search"; +import _ from "lodash"; const calculatePage = (offset, limit) => { return offset && limit ? Math.round((offset / limit) + 1) : 1; @@ -448,8 +449,17 @@ export const useSearchFiles = (defaultLimit = 48, emptySearch = false, model) => } } + // Only search after the user has stopped typing. Estimates will always happen. + const debouncedLocalSearchFiles = _.debounce(async () => { + await localSearchFiles(); + }, 1000); + useEffect(() => { - localSearchFiles(); + if (searchStr || (activeTags && activeTags.length > 0)) { + debouncedLocalSearchFiles(); + } + // Handle when this is unmounted. + return () => debouncedLocalSearchFiles.cancel(); }, [searchStr, pages.effect, filter, model, model_, JSON.stringify(activeTags), headline]); return {