import { FOCUSABLE_BUT_NOT_TABBABLE } from '@/Constants' import { KeyboardKey } from '@/Services/IOService' import { formatSizeToReadableString } from '@standardnotes/filepicker' import { IconType, FileItem } from '@standardnotes/snjs' import { FunctionComponent } from 'preact' import { useEffect, useRef, useState } from 'preact/hooks' import { Icon, ICONS } from '@/Components/Icon/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: FileItem 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 itemRef = useRef(null) const fileNameInputRef = useRef(null) useEffect(() => { if (isRenamingFile) { fileNameInputRef.current?.focus() } }, [isRenamingFile]) const renameFile = async (file: FileItem, 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) { itemRef.current?.focus() } } const handleFileNameInputBlur = () => { renameFile(file, fileName).catch(console.error) } const clickPreviewHandler = () => { handleFileAction({ type: PopoverFileItemActionType.PreviewFile, payload: file, }).catch(console.error) } return ( {getFileIconComponent(getIconType(file.mimeType), 'w-8 h-8 flex-shrink-0')} {isRenamingFile ? ( ) : ( {file.name} {file.protected && ( )} )} {file.created_at.toLocaleString()} ยท {formatSizeToReadableString(file.decryptedSize)} ) }