"use client"; import { IconDownload, IconPencil, IconTrash, IconUpload } from "@tabler/icons-react"; import { ContextMenuItem, ContextMenuSeparator } from "@kandev/ui/context-menu"; import { useTranslation } from "react-i18next"; import type { FileTreeNode } from "@/lib/types/backend"; export type FileContextMenuItemsProps = { node: FileTreeNode; isBulk: boolean; selectedCount: number; onDeleteFile?: (path: string) => Promise; onRenameFile?: (oldPath: string, newPath: string) => Promise; onDownloadFile?: (path: string) => Promise; onUploadFilesHere?: (path: string) => void; onStartRename: () => void; onDelete: (event: Event) => void; }; /** * Which node-scoped actions apply, resolved once so the render stays flat. * * Download and upload are exact inverses: a file can be downloaded, a folder can * receive an upload, and a multi-selection supports neither because it has no * single subject or destination. */ function resolveActions({ node, isBulk, onRenameFile, onDownloadFile, onUploadFilesHere, }: FileContextMenuItemsProps) { return { showRename: !!onRenameFile && !isBulk, download: !node.is_dir && !isBulk ? onDownloadFile : undefined, upload: node.is_dir && !isBulk ? onUploadFilesHere : undefined, }; } export function FileContextMenuItems(props: FileContextMenuItemsProps) { const { node, isBulk, selectedCount, onDeleteFile, onStartRename, onDelete } = props; const { t } = useTranslation(); const { showRename, download, upload } = resolveActions(props); const deleteLabel = isBulk ? t("task:deleteItemsLabel", { count: selectedCount }) : t("task:delete"); const hasLeadingItem = showRename || !!onDeleteFile; return ( <> {onDeleteFile && ( {deleteLabel} )} {showRename && onDeleteFile && } {showRename && ( {t("task:rename")} )} {download && hasLeadingItem && } {download && ( void download(node.path)}> {t("task:download")} )} {upload && hasLeadingItem && } {upload && ( upload(node.path)}> {t("task:uploadFilesHere")} )} ); }