import { memo, useCallback, type MouseEvent as ReactMouseEvent } from 'react' import type { FolderNode, SidebarSelection } from '../../types' import { NoteDropTarget } from '../note-retargeting/NoteDropTarget' import { useNoteRetargetingContext } from '../note-retargeting/noteRetargetingContext' import { FolderNameInput } from './FolderNameInput' import { FolderItemRow } from './FolderItemRow' import { translate, type AppLocale } from '../../lib/i18n' interface FolderTreeRowProps { depth: number expanded: Record node: FolderNode onDeleteFolder?: (folderPath: string) => void onOpenMenu: (node: FolderNode, event: ReactMouseEvent) => void onRenameFolder?: (folderPath: string, nextName: string) => Promise | boolean onSelect: (selection: SidebarSelection) => void onStartRenameFolder?: (folderPath: string) => void onToggle: (path: string) => void onCancelRenameFolder?: () => void locale?: AppLocale renamingFolderPath?: string | null selection: SidebarSelection } function FolderRenameRow({ contentInset, depthIndent, node, locale, onCancelRenameFolder, onRenameFolder, }: { contentInset: number depthIndent: number node: FolderNode locale: AppLocale onCancelRenameFolder: () => void onRenameFolder: (folderPath: string, nextName: string) => Promise | boolean }) { return (
onRenameFolder(node.path, nextName)} />
) } function FolderChildren({ depth, expanded, node, onDeleteFolder, onOpenMenu, onRenameFolder, onSelect, onStartRenameFolder, onToggle, onCancelRenameFolder, locale, renamingFolderPath, selection, }: FolderTreeRowProps) { const isExpanded = expanded[node.path] ?? false const hasChildren = node.children.length > 0 if (!isExpanded || !hasChildren) return null return (
{node.children.map((child) => ( ))}
) } export const FolderTreeRow = memo(function FolderTreeRow({ depth, expanded, node, onDeleteFolder, onOpenMenu, onRenameFolder, onSelect, onStartRenameFolder, onToggle, onCancelRenameFolder, locale = 'en', renamingFolderPath, selection, }: FolderTreeRowProps) { const isExpanded = expanded[node.path] ?? false const isRenaming = renamingFolderPath === node.path const isSelected = selection.kind === 'folder' && selection.path === node.path const depthIndent = depth * 16 const contentInset = 16 const noteRetargeting = useNoteRetargetingContext() const selectFolder = useCallback(() => { onSelect({ kind: 'folder', path: node.path }) }, [node.path, onSelect]) const row = ( ) return ( <> {isRenaming && onRenameFolder && onCancelRenameFolder ? ( ) : ( noteRetargeting ? ( noteRetargeting.canDropNoteOnFolder(notePath, node.path)} onDropNote={(notePath) => noteRetargeting.dropNoteOnFolder(notePath, node.path)} > {row} ) : row )} ) })