import { IconCaretDown, IconCaretRight, IconCheck, IconPencil, IconTrash, IconX, } from '@tabler/icons-react'; import { KeyboardEvent, ReactElement, useContext, useEffect, useState, } from 'react'; import { FolderInterface } from '@/types/folder'; import HomeContext from '@/pages/api/home/home.context'; import SidebarActionButton from '@/components/Buttons/SidebarActionButton'; interface Props { currentFolder: FolderInterface; searchTerm: string; handleDrop: (e: any, folder: FolderInterface) => void; folderComponent: (ReactElement | undefined)[]; } const Folder = ({ currentFolder, searchTerm, handleDrop, folderComponent, }: Props) => { const { handleDeleteFolder, handleUpdateFolder } = useContext(HomeContext); const [isDeleting, setIsDeleting] = useState(false); const [isRenaming, setIsRenaming] = useState(false); const [renameValue, setRenameValue] = useState(''); const [isOpen, setIsOpen] = useState(false); const handleEnterDown = (e: KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); handleRename(); } }; const handleRename = () => { handleUpdateFolder(currentFolder.id, renameValue); setRenameValue(''); setIsRenaming(false); }; const dropHandler = (e: any) => { if (e.dataTransfer) { setIsOpen(true); handleDrop(e, currentFolder); e.target.style.background = 'none'; } }; const allowDrop = (e: any) => { e.preventDefault(); }; const highlightDrop = (e: any) => { e.target.style.background = '#343541'; }; const removeHighlight = (e: any) => { e.target.style.background = 'none'; }; useEffect(() => { if (isRenaming) { setIsDeleting(false); } else if (isDeleting) { setIsRenaming(false); } }, [isRenaming, isDeleting]); useEffect(() => { if (searchTerm) { setIsOpen(true); } else { setIsOpen(false); } }, [searchTerm]); return ( <>
{isRenaming ? (
{isOpen ? ( ) : ( )} setRenameValue(e.target.value)} onKeyDown={handleEnterDown} autoFocus />
) : ( )} {(isDeleting || isRenaming) && (
{ e.stopPropagation(); if (isDeleting) { handleDeleteFolder(currentFolder.id); } else if (isRenaming) { handleRename(); } setIsDeleting(false); setIsRenaming(false); }} > { e.stopPropagation(); setIsDeleting(false); setIsRenaming(false); }} >
)} {!isDeleting && !isRenaming && (
{ e.stopPropagation(); setIsRenaming(true); setRenameValue(currentFolder.name); }} > { e.stopPropagation(); setIsDeleting(true); }} >
)}
{isOpen ? folderComponent : null} ); }; export default Folder;