import { KeyboardKey } from '@/services/ioService'; import { formatSizeToReadableString } from '@standardnotes/filepicker'; import { IconType, SNFile } from '@standardnotes/snjs'; import { FunctionComponent } from 'preact'; import { useEffect, useRef, useState } from 'preact/hooks'; import { ICONS } from '../Icon'; import { PopoverFileItemAction, PopoverFileItemActionType, } from './PopoverFileItemAction'; import { PopoverFileSubmenu } from './PopoverFileSubmenu'; export const getFileIconComponent = (iconType: string, className: string) => { const IconComponent = ICONS[iconType as keyof typeof ICONS]; return ; }; export type PopoverFileItemProps = { file: SNFile; isAttachedToNote: boolean; handleFileAction: (action: PopoverFileItemAction) => Promise; getIconType(type: string): IconType; closeOnBlur: (event: { relatedTarget: EventTarget | null }) => void; }; export const PopoverFileItem: FunctionComponent = ({ file, isAttachedToNote, handleFileAction, getIconType, closeOnBlur, }) => { const [fileName, setFileName] = useState(file.name); const [isRenamingFile, setIsRenamingFile] = useState(false); const fileNameInputRef = useRef(null); useEffect(() => { if (isRenamingFile) { fileNameInputRef.current?.focus(); } }, [isRenamingFile]); const renameFile = async (file: SNFile, name: string) => { await handleFileAction({ type: PopoverFileItemActionType.RenameFile, payload: { file, name, }, }); setIsRenamingFile(false); }; const handleFileNameInput = (event: Event) => { setFileName((event.target as HTMLInputElement).value); }; const handleFileNameInputKeyDown = (event: KeyboardEvent) => { if (event.key === KeyboardKey.Enter) { fileNameInputRef.current?.blur(); } }; const handleFileNameInputBlur = () => { renameFile(file, fileName); }; return (
{getFileIconComponent( getIconType(file.mimeType), 'w-8 h-8 flex-shrink-0' )}
{isRenamingFile ? ( ) : (
{file.name}
)}
{file.created_at.toLocaleString()} ยท{' '} {formatSizeToReadableString(file.size)}
); };