mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 08:13:56 +10:00
45 lines
1.2 KiB
TypeScript
45 lines
1.2 KiB
TypeScript
import { useCallback, useEffect, useState } from 'react';
|
|
|
|
import { msg } from '@lingui/core/macro';
|
|
import { useLingui } from '@lingui/react';
|
|
import { useSearchParams } from 'react-router';
|
|
|
|
import { useDebouncedValue } from '@documenso/lib/client-only/hooks/use-debounced-value';
|
|
import { Input } from '@documenso/ui/primitives/input';
|
|
|
|
export const DocumentSearch = ({ initialValue = '' }: { initialValue?: string }) => {
|
|
const { _ } = useLingui();
|
|
|
|
const [searchParams, setSearchParams] = useSearchParams();
|
|
|
|
const [searchTerm, setSearchTerm] = useState(initialValue);
|
|
const debouncedSearchTerm = useDebouncedValue(searchTerm, 500);
|
|
|
|
const handleSearch = useCallback(
|
|
(term: string) => {
|
|
const params = new URLSearchParams(searchParams?.toString() ?? '');
|
|
if (term) {
|
|
params.set('query', term);
|
|
} else {
|
|
params.delete('query');
|
|
}
|
|
|
|
setSearchParams(params);
|
|
},
|
|
[searchParams],
|
|
);
|
|
|
|
useEffect(() => {
|
|
handleSearch(searchTerm);
|
|
}, [debouncedSearchTerm]);
|
|
|
|
return (
|
|
<Input
|
|
type="search"
|
|
placeholder={_(msg`Search documents...`)}
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
/>
|
|
);
|
|
};
|