Portal/rsconcept/frontend/src/pages/LibraryPage/SearchPanel.tsx

203 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import clsx from 'clsx';
import { useCallback } from 'react';
import { LocationIcon, SubscribeIcon, VisibilityIcon } from '@/components/DomainIcons';
2024-06-18 15:06:52 +03:00
import { IconEditor, IconFilterReset, IconFolder, IconOwner } from '@/components/Icons';
2024-06-07 20:17:03 +03:00
import BadgeHelp from '@/components/info/BadgeHelp';
import Dropdown from '@/components/ui/Dropdown';
import DropdownButton from '@/components/ui/DropdownButton';
import MiniButton from '@/components/ui/MiniButton';
2024-06-18 15:06:52 +03:00
import Overlay from '@/components/ui/Overlay';
2024-06-07 20:17:03 +03:00
import SearchBar from '@/components/ui/SearchBar';
import SelectorButton from '@/components/ui/SelectorButton';
2024-06-14 21:43:05 +03:00
import { useAuth } from '@/context/AuthContext';
2024-06-07 20:17:03 +03:00
import useDropdown from '@/hooks/useDropdown';
import { LocationHead } from '@/models/library';
import { HelpTopic } from '@/models/miscellaneous';
2024-06-09 20:40:41 +03:00
import { PARAMETER, prefixes } from '@/utils/constants';
2024-06-07 20:17:03 +03:00
import { describeLocationHead, labelLocationHead } from '@/utils/labels';
import { tripleToggleColor } from '@/utils/utils';
interface SearchPanelProps {
total: number;
filtered: number;
2024-06-18 15:06:52 +03:00
hasCustomFilter: boolean;
2024-06-07 20:17:03 +03:00
query: string;
setQuery: React.Dispatch<React.SetStateAction<string>>;
path: string;
setPath: React.Dispatch<React.SetStateAction<string>>;
head: LocationHead | undefined;
setHead: React.Dispatch<React.SetStateAction<LocationHead | undefined>>;
isVisible: boolean | undefined;
toggleVisible: () => void;
isOwned: boolean | undefined;
toggleOwned: () => void;
isSubscribed: boolean | undefined;
toggleSubscribed: () => void;
isEditor: boolean | undefined;
toggleEditor: () => void;
2024-06-18 15:06:52 +03:00
resetFilter: () => void;
2024-06-07 20:17:03 +03:00
}
function SearchPanel({
total,
filtered,
2024-06-18 15:06:52 +03:00
hasCustomFilter,
2024-06-07 20:17:03 +03:00
query,
setQuery,
path,
setPath,
head,
setHead,
isVisible,
toggleVisible,
isOwned,
toggleOwned,
isSubscribed,
toggleSubscribed,
isEditor,
2024-06-18 15:06:52 +03:00
toggleEditor,
resetFilter
2024-06-07 20:17:03 +03:00
}: SearchPanelProps) {
2024-06-14 21:43:05 +03:00
const { user } = useAuth();
2024-06-07 20:17:03 +03:00
const headMenu = useDropdown();
const handleChange = useCallback(
(newValue: LocationHead | undefined) => {
headMenu.hide();
setHead(newValue);
},
[headMenu, setHead]
);
return (
<div
className={clsx(
'sticky top-0', // prettier: split lines
'w-full h-[2.2rem]',
'flex items-center',
'border-b',
'text-sm',
'clr-input'
)}
>
<div className={clsx('px-3 pt-1 self-center', 'min-w-[5.5rem]', 'select-none', 'whitespace-nowrap')}>
{filtered} из {total}
</div>
2024-06-14 21:43:05 +03:00
{user ? (
<div className='cc-icons'>
<MiniButton
title='Видимость'
icon={<VisibilityIcon value={true} className={tripleToggleColor(isVisible)} />}
onClick={toggleVisible}
/>
<MiniButton
title='Я - Подписчик'
icon={<SubscribeIcon value={true} className={tripleToggleColor(isSubscribed)} />}
onClick={toggleSubscribed}
/>
2024-06-07 20:17:03 +03:00
2024-06-14 21:43:05 +03:00
<MiniButton
title='Я - Владелец'
icon={<IconOwner size='1.25rem' className={tripleToggleColor(isOwned)} />}
onClick={toggleOwned}
/>
<MiniButton
title='Я - Редактор'
icon={<IconEditor size='1.25rem' className={tripleToggleColor(isEditor)} />}
onClick={toggleEditor}
/>
2024-06-18 15:06:52 +03:00
<MiniButton
title='Сбросить фильтры'
icon={<IconFilterReset size='1.25rem' className='icon-primary' />}
onClick={resetFilter}
disabled={!hasCustomFilter}
/>
2024-06-14 21:43:05 +03:00
</div>
) : null}
2024-06-07 20:17:03 +03:00
<div className='flex items-center h-full mx-auto'>
<SearchBar
id='library_search'
placeholder='Поиск'
noBorder
className='min-w-[7rem] sm:min-w-[10rem]'
value={query}
onChange={setQuery}
/>
<div ref={headMenu.ref} className='flex items-center h-full py-1 select-none'>
<SelectorButton
transparent
className='h-full rounded-lg'
title={head ? describeLocationHead(head) : 'Выберите каталог'}
hideTitle={headMenu.isOpen}
icon={
head ? (
<LocationIcon value={head} size='1.25rem' />
) : (
<IconFolder size='1.25rem' className='clr-text-controls' />
)
}
onClick={headMenu.toggle}
text={head ?? '//'}
/>
<Dropdown isOpen={headMenu.isOpen} stretchLeft className='z-modalTooltip'>
<DropdownButton className='w-[10rem]' onClick={() => handleChange(undefined)}>
<div className='inline-flex items-center gap-3'>
<IconFolder size='1rem' className='clr-text-controls' />
<span>отображать все</span>
</div>
</DropdownButton>
{Object.values(LocationHead).map((head, index) => {
return (
<DropdownButton
className='w-[10rem]'
key={`${prefixes.location_head_list}${index}`}
onClick={() => handleChange(head)}
title={describeLocationHead(head)}
>
<div className='inline-flex items-center gap-3'>
<LocationIcon value={head} size='1rem' />
{labelLocationHead(head)}
</div>
</DropdownButton>
);
})}
</Dropdown>
</div>
<SearchBar
id='path_search'
placeholder='Путь'
noIcon
noBorder
2024-06-18 15:06:52 +03:00
className='min-w-[4.5rem] sm:min-w-[5rem]'
2024-06-07 20:17:03 +03:00
value={path}
onChange={setPath}
/>
</div>
2024-06-18 15:06:52 +03:00
<Overlay position='top-[-0.75rem] right-0'>
<BadgeHelp
topic={HelpTopic.UI_LIBRARY}
className={clsx(PARAMETER.TOOLTIP_WIDTH, 'text-sm')}
offset={5}
place='right-start'
/>
</Overlay>
2024-06-07 20:17:03 +03:00
</div>
);
}
export default SearchPanel;