Portal/rsconcept/frontend/src/features/library/pages/library-page/view-side-location.tsx

104 lines
3.9 KiB
TypeScript
Raw Normal View History

2024-06-20 21:10:59 +03:00
import { toast } from 'react-toastify';
2025-03-13 14:40:56 +03:00
import clsx from 'clsx';
2025-02-12 21:36:03 +03:00
import { useAuthSuspense } from '@/features/auth';
2025-02-26 00:16:22 +03:00
import { HelpTopic } from '@/features/help';
2025-07-02 12:37:47 +03:00
import { BadgeHelp } from '@/features/help/components/badge-help';
2024-06-19 22:09:31 +03:00
2025-03-12 12:04:23 +03:00
import { MiniButton } from '@/components/control';
import { IconFolderEdit, IconFolderTree } from '@/components/icons';
2025-03-12 11:54:32 +03:00
import { useFitHeight } from '@/stores/app-layout';
2025-03-13 14:40:56 +03:00
import { prefixes } from '@/utils/constants';
import { infoMsg } from '@/utils/labels';
2024-06-19 22:09:31 +03:00
2025-03-12 11:54:32 +03:00
import { useLibrary } from '../../backend/use-library';
import { IconShowSubfolders } from '../../components/icon-show-subfolders';
import { SelectLocation } from '../../components/select-location';
import { type FolderNode } from '../../models/folder-tree';
import { useLibrarySearchStore } from '../../stores/library-search';
2024-06-27 11:34:52 +03:00
interface ViewSideLocationProps {
2024-12-11 23:37:23 +03:00
isVisible: boolean;
onRenameLocation: () => void;
2024-06-19 22:09:31 +03:00
}
2025-02-19 23:29:45 +03:00
export function ViewSideLocation({ isVisible, onRenameLocation }: ViewSideLocationProps) {
const { user, isAnonymous } = useAuthSuspense();
const { items } = useLibrary();
2024-09-12 13:27:06 +03:00
const location = useLibrarySearchStore(state => state.location);
const setLocation = useLibrarySearchStore(state => state.setLocation);
const toggleFolderMode = useLibrarySearchStore(state => state.toggleFolderMode);
const subfolders = useLibrarySearchStore(state => state.subfolders);
const toggleSubfolders = useLibrarySearchStore(state => state.toggleSubfolders);
const canRename = (() => {
if (location.length <= 3 || isAnonymous) {
2024-09-12 13:27:06 +03:00
return false;
}
if (user.is_staff) {
return true;
}
const owned = items.filter(item => item.owner == user.id);
const located = owned.filter(item => item.location == location || item.location.startsWith(`${location}/`));
2024-09-12 13:27:06 +03:00
return located.length !== 0;
})();
2024-09-12 13:27:06 +03:00
const maxHeight = useFitHeight('4.5rem');
2024-09-12 13:27:06 +03:00
2025-02-22 14:03:13 +03:00
function handleClickFolder(event: React.MouseEvent<Element>, target: FolderNode) {
event.preventDefault();
event.stopPropagation();
if (event.ctrlKey || event.metaKey) {
navigator.clipboard
.writeText(target.getPath())
.then(() => toast.success(infoMsg.pathReady))
.catch(console.error);
} else {
setLocation(target.getPath());
}
}
2024-06-19 22:09:31 +03:00
return (
2024-12-11 23:37:23 +03:00
<div
2025-03-13 14:40:56 +03:00
className={clsx(
'max-w-40 sm:max-w-60 flex flex-col text:xs sm:text-sm select-none cc-side-location',
isVisible && 'open min-w-[10rem] sm:min-w-[15rem]'
)}
2024-06-19 22:09:31 +03:00
>
2025-03-09 21:57:21 +03:00
<div className='h-8 flex justify-between items-center pr-1 pl-0.5'>
2025-03-07 22:04:56 +03:00
<BadgeHelp topic={HelpTopic.UI_LIBRARY} contentClass='text-sm' offset={5} place='right-start' />
<div className='cc-icons'>
2024-09-27 12:03:13 +03:00
{canRename ? (
<MiniButton
titleHtml='<b>Редактирование пути</b><br/>Перемещаются только Ваши схемы<br/>в указанной папке (и подпапках)'
2025-03-20 11:33:19 +03:00
aria-label='Редактирование расположения'
icon={<IconFolderEdit size='1.25rem' className='icon-primary' />}
2024-09-27 12:03:13 +03:00
onClick={onRenameLocation}
/>
) : null}
{!!location ? (
2024-12-13 13:22:13 +03:00
<MiniButton
2025-03-10 16:01:40 +03:00
title={subfolders ? 'Вложенные папки: Вкл' : 'Вложенные папки: Выкл'}
2025-03-20 11:33:19 +03:00
aria-label='Переключатель отображения вложенных папок'
2025-02-26 00:16:22 +03:00
icon={<IconShowSubfolders value={subfolders} />}
2024-12-13 13:22:13 +03:00
onClick={toggleSubfolders}
/>
) : null}
<MiniButton
2025-06-18 16:14:18 +03:00
title='Переключение в режим Таблица'
2025-06-19 12:44:40 +03:00
icon={<IconFolderTree size='1.25rem' className='text-primary' />}
onClick={toggleFolderMode}
/>
</div>
2024-06-19 22:09:31 +03:00
</div>
2024-06-26 18:59:49 +03:00
<SelectLocation
value={location}
2024-06-26 18:59:49 +03:00
prefix={prefixes.folders_list}
onClick={handleClickFolder}
2024-09-19 20:52:17 +03:00
style={{ maxHeight: maxHeight }}
2024-06-26 18:59:49 +03:00
/>
2024-12-11 23:37:23 +03:00
</div>
2024-06-19 22:09:31 +03:00
);
}