mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-12-04 21:59:58 -05:00
Auto-submitting global search input, but debouncing with 1 second.
This commit is contained in:
parent
5b7aec59cc
commit
472846038b
6 changed files with 81 additions and 33 deletions
|
|
@ -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 = <Segment>
|
||||
const getter = <Segment>
|
||||
<Grid columns={2} textAlign='center'>
|
||||
<Divider vertical>Or</Divider>
|
||||
<GridRow verticalAlign='middle'>
|
||||
|
|
@ -120,6 +117,7 @@ export function DashboardPage() {
|
|||
</GridRow>
|
||||
</Grid>
|
||||
</Segment>;
|
||||
|
||||
let getterModal;
|
||||
if (selectedGetter === 'downloads') {
|
||||
getterModal = <Modal closeIcon
|
||||
|
|
@ -145,25 +143,44 @@ export function DashboardPage() {
|
|||
</Modal>;
|
||||
}
|
||||
|
||||
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 = <React.Fragment>
|
||||
<Getters/>
|
||||
<TagsDashboard/>
|
||||
<DashboardStatus/>
|
||||
</React.Fragment>;
|
||||
if (searchStr || (activeTags && activeTags.length > 0)) {
|
||||
// User has submitted and wants full search.
|
||||
body = <SearchView/>;
|
||||
} else {
|
||||
body = <>
|
||||
{!wrolModeEnabled && getter}
|
||||
{getterModal}
|
||||
<TagsDashboard/>
|
||||
<DashboardStatus/>
|
||||
</>;
|
||||
}
|
||||
|
||||
return <PageContainer>
|
||||
<Grid>
|
||||
<Grid.Row>
|
||||
<Grid.Column mobile={16} computer={8}>
|
||||
<Grid.Column mobile={13} computer={8}>
|
||||
<SearchInput clearable
|
||||
searchStr={searchStr}
|
||||
onChange={setSearchStr}
|
||||
onSubmit={setSearchStr}
|
||||
size='large'
|
||||
placeholder='Search everywhere...'
|
||||
|
|
@ -172,6 +189,9 @@ export function DashboardPage() {
|
|||
style={{marginBottom: '2em'}}
|
||||
/>
|
||||
</Grid.Column>
|
||||
<Grid.Column mobile={3} computer={2}>
|
||||
<FileSearchFilterButton size='large'/>
|
||||
</Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
{!searchStr && <FlagsMessages flags={status['flags']}/>}
|
||||
|
|
|
|||
|
|
@ -514,7 +514,9 @@ export function ChannelsPage() {
|
|||
searchStr={searchStr}
|
||||
disabled={!Array.isArray(channels) || channels.length === 0}
|
||||
onClear={() => setSearchStr('')}
|
||||
onChange={setSearchStr}/>
|
||||
onChange={setSearchStr}
|
||||
onSubmit={null}
|
||||
/>
|
||||
</Grid.Column>
|
||||
<Grid.Column textAlign='right'>
|
||||
<Link to='/videos/channel/new'>
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 <SearchFilter filters={filterOptions} size={size}/>
|
||||
}
|
||||
|
||||
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({
|
|||
<Grid.Column width={2}>{viewButton}</Grid.Column>}
|
||||
<Grid.Column width={4}>{limitDropdown}</Grid.Column>
|
||||
<Grid.Column width={2}>{tagQuerySelector}</Grid.Column>
|
||||
<Grid.Column width={2}><SearchFilter filters={filterOptions}/></Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
</Media>
|
||||
|
|
@ -499,7 +505,6 @@ export function FilesSearchView({
|
|||
<Grid.Column width={1}>{viewButton}</Grid.Column>}
|
||||
<Grid.Column width={2}>{limitDropdown}</Grid.Column>
|
||||
<Grid.Column width={1}>{tagQuerySelector}</Grid.Column>
|
||||
<Grid.Column width={1}><SearchFilter filters={filterOptions}/></Grid.Column>
|
||||
</Grid.Row>
|
||||
</Grid>
|
||||
</Media>
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue