2023-12-13 14:32:57 +03:00
|
|
|
|
'use client';
|
2023-08-26 19:39:49 +03:00
|
|
|
|
|
2023-12-15 17:34:50 +03:00
|
|
|
|
import clsx from 'clsx';
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import { useCallback } from 'react';
|
2023-08-27 15:39:49 +03:00
|
|
|
|
|
2024-01-04 19:38:12 +03:00
|
|
|
|
import SearchBar from '@/components/ui/SearchBar';
|
2023-12-26 14:23:51 +03:00
|
|
|
|
import { useConceptNavigation } from '@/context/NavigationContext';
|
|
|
|
|
import { ILibraryFilter } from '@/models/miscellaneous';
|
|
|
|
|
import { LibraryFilterStrategy } from '@/models/miscellaneous';
|
2023-08-27 15:39:49 +03:00
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
import PickerStrategy from './PickerStrategy';
|
2023-08-26 19:39:49 +03:00
|
|
|
|
|
|
|
|
|
interface SearchPanelProps {
|
2023-12-28 14:04:44 +03:00
|
|
|
|
total: number;
|
|
|
|
|
filtered: number;
|
|
|
|
|
setFilter: React.Dispatch<React.SetStateAction<ILibraryFilter>>;
|
|
|
|
|
query: string;
|
|
|
|
|
setQuery: React.Dispatch<React.SetStateAction<string>>;
|
|
|
|
|
strategy: LibraryFilterStrategy;
|
2023-08-26 19:39:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-13 14:32:57 +03:00
|
|
|
|
function SearchPanel({ total, filtered, query, setQuery, strategy, setFilter }: SearchPanelProps) {
|
|
|
|
|
const router = useConceptNavigation();
|
2023-08-26 19:39:49 +03:00
|
|
|
|
|
2023-12-08 19:24:08 +03:00
|
|
|
|
function handleChangeQuery(newQuery: string) {
|
2023-08-27 00:19:19 +03:00
|
|
|
|
setQuery(newQuery);
|
2023-09-09 20:36:55 +03:00
|
|
|
|
setFilter(prev => ({
|
|
|
|
|
query: newQuery,
|
|
|
|
|
is_owned: prev.is_owned,
|
|
|
|
|
is_common: prev.is_common,
|
|
|
|
|
is_canonical: prev.is_canonical,
|
|
|
|
|
is_subscribed: prev.is_subscribed,
|
|
|
|
|
is_personal: prev.is_personal
|
|
|
|
|
}));
|
2023-08-27 00:19:19 +03:00
|
|
|
|
}
|
2023-08-26 19:39:49 +03:00
|
|
|
|
|
2023-08-27 15:39:49 +03:00
|
|
|
|
const handleChangeStrategy = useCallback(
|
2023-12-28 14:04:44 +03:00
|
|
|
|
(value: LibraryFilterStrategy) => {
|
|
|
|
|
if (value !== strategy) {
|
|
|
|
|
router.push(`/library?filter=${value}`);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[strategy, router]
|
|
|
|
|
);
|
2023-08-27 15:39:49 +03:00
|
|
|
|
|
2023-08-26 19:39:49 +03:00
|
|
|
|
return (
|
2023-12-30 19:43:24 +03:00
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
2024-01-04 14:35:46 +03:00
|
|
|
|
'sticky top-0', // prettier: split lines
|
2024-02-22 11:35:27 +03:00
|
|
|
|
'w-full h-[2.2rem]',
|
|
|
|
|
'sm:pr-[12rem] flex',
|
2023-12-30 19:43:24 +03:00
|
|
|
|
'border-b',
|
2024-01-16 13:47:29 +03:00
|
|
|
|
'text-sm',
|
2023-12-30 19:43:24 +03:00
|
|
|
|
'clr-input'
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className={clsx(
|
2024-01-04 14:35:46 +03:00
|
|
|
|
'min-w-[10rem]', // prettier: split lines
|
2023-12-30 19:43:24 +03:00
|
|
|
|
'px-2 self-center',
|
|
|
|
|
'select-none',
|
|
|
|
|
'whitespace-nowrap'
|
|
|
|
|
)}
|
|
|
|
|
>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
Фильтр
|
|
|
|
|
<span className='ml-2'>
|
|
|
|
|
{filtered} из {total}
|
|
|
|
|
</span>
|
|
|
|
|
</div>
|
2024-02-22 11:35:27 +03:00
|
|
|
|
<PickerStrategy value={strategy} onChange={handleChangeStrategy} />
|
2024-03-18 16:21:39 +03:00
|
|
|
|
<SearchBar
|
|
|
|
|
id='library_search'
|
|
|
|
|
noBorder
|
|
|
|
|
className='mx-auto min-w-[10rem]'
|
|
|
|
|
value={query}
|
|
|
|
|
onChange={handleChangeQuery}
|
|
|
|
|
/>
|
2023-11-27 11:33:34 +03:00
|
|
|
|
</div>
|
2023-12-28 14:04:44 +03:00
|
|
|
|
);
|
2023-08-26 19:39:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-28 14:04:44 +03:00
|
|
|
|
export default SearchPanel;
|