import { WebApplication } from '@/Application/WebApplication' import { FunctionComponent, KeyboardEventHandler, useCallback, useMemo, useRef, useState } from 'react' import { getFileIconComponent } from './getFileIconComponent' import Icon from '@/Components/Icon/Icon' import FilePreviewInfoPanel from './FilePreviewInfoPanel' import { FOCUSABLE_BUT_NOT_TABBABLE } from '@/Constants/Constants' import { KeyboardKey } from '@standardnotes/ui-services' import { ViewControllerManager } from '@/Controllers/ViewControllerManager' import { observer } from 'mobx-react-lite' import FilePreview from './FilePreview' import { getIconForFileType } from '@/Utils/Items/Icons/getIconForFileType' import FileMenuOptions from '../FileContextMenu/FileMenuOptions' import Menu from '../Menu/Menu' import Popover from '../Popover/Popover' import LinkedItemBubblesContainer from '../LinkedItems/LinkedItemBubblesContainer' import StyledTooltip from '../StyledTooltip/StyledTooltip' import DecoratedInput from '../Input/DecoratedInput' import { mergeRefs } from '@/Hooks/mergeRefs' import { classNames } from '@standardnotes/snjs' import ModalOverlay from '../Modal/ModalOverlay' import Modal from '../Modal/Modal' import { MutuallyExclusiveMediaQueryBreakpoints, useMediaQuery } from '@/Hooks/useMediaQuery' type Props = { application: WebApplication viewControllerManager: ViewControllerManager } const FilePreviewModal = observer(({ application, viewControllerManager }: Props) => { const { currentFile, setCurrentFile, otherFiles, dismiss } = viewControllerManager.filePreviewModalController const [isRenaming, setIsRenaming] = useState(false) const renameInputRef = useRef(null) const [showLinkedBubblesContainer, setShowLinkedBubblesContainer] = useState(false) const [showOptionsMenu, setShowOptionsMenu] = useState(false) const [showFileInfoPanel, setShowFileInfoPanel] = useState(false) const menuButtonRef = useRef(null) const closeButtonRef = useRef(null) const keyDownHandler: KeyboardEventHandler = useCallback( (event) => { if (!currentFile) { return null } const KeysToHandle: string[] = [KeyboardKey.Left, KeyboardKey.Right, KeyboardKey.Escape] if (!KeysToHandle.includes(event.key) || event.target === renameInputRef.current) { return } event.preventDefault() const currentFileIndex = otherFiles.findIndex((file) => file.uuid === currentFile.uuid) switch (event.key) { case KeyboardKey.Left: { const previousFileIndex = currentFileIndex - 1 >= 0 ? currentFileIndex - 1 : otherFiles.length - 1 const previousFile = otherFiles[previousFileIndex] if (previousFile) { setCurrentFile(previousFile) } break } case KeyboardKey.Right: { const nextFileIndex = currentFileIndex + 1 < otherFiles.length ? currentFileIndex + 1 : 0 const nextFile = otherFiles[nextFileIndex] if (nextFile) { setCurrentFile(nextFile) } break } case KeyboardKey.Escape: dismiss() break } }, [currentFile, dismiss, otherFiles, setCurrentFile], ) const IconComponent = useMemo(() => { return currentFile ? getFileIconComponent(getIconForFileType(currentFile.mimeType), 'w-6 h-6 flex-shrink-0') : null }, [currentFile]) const focusElementOnMount = useCallback((element: HTMLElement | null) => { if (element) { element.focus() } }, []) const handleRename = useCallback(async () => { if (!currentFile) { return null } if (renameInputRef.current) { const newName = renameInputRef.current.value if (newName && newName !== currentFile.name) { await application.mutator.renameFile(currentFile, newName) setIsRenaming(false) setCurrentFile(application.items.findSureItem(currentFile.uuid)) return } setIsRenaming(false) } }, [application.items, application.mutator, currentFile, setCurrentFile]) const isMobileScreen = useMediaQuery(MutuallyExclusiveMediaQueryBreakpoints.sm) const toggleOptionsMenu = () => setShowOptionsMenu((show) => !show) const closeOptionsMenu = () => setShowOptionsMenu(false) const toggleInfoPanel = () => setShowFileInfoPanel((show) => !show) const toggleLinkedBubblesContainer = () => setShowLinkedBubblesContainer((show) => !show) if (!currentFile) { return null } return ( } disableCustomHeader={isMobileScreen} >
{IconComponent}
{isRenaming ? ( { if (event.key === KeyboardKey.Enter) { void handleRename() } }} right={[ , ]} ref={mergeRefs([renameInputRef, focusElementOnMount])} /> ) : ( {currentFile.name} )}
{showLinkedBubblesContainer && (
)}
{showFileInfoPanel && }
) }) FilePreviewModal.displayName = 'FilePreviewModal' const FilePreviewModalWrapper: FunctionComponent = ({ application, viewControllerManager }) => { return ( ) } export default observer(FilePreviewModalWrapper)