import { WebApplication } from '@/Application/Application' import { DialogContent, DialogOverlay } from '@reach/dialog' import { FunctionComponent, KeyboardEventHandler, useCallback, useMemo, useRef, useState } from 'react' import { getFileIconComponent } from '@/Components/AttachedFilesPopover/getFileIconComponent' import Icon from '@/Components/Icon/Icon' import FilePreviewInfoPanel from './FilePreviewInfoPanel' import { FOCUSABLE_BUT_NOT_TABBABLE } from '@/Constants/Constants' import { KeyboardKey } from '@/Services/IOService' import { ViewControllerManager } from '@/Services/ViewControllerManager' import { observer } from 'mobx-react-lite' import FilePreview from './FilePreview' type Props = { application: WebApplication viewControllerManager: ViewControllerManager } const FilePreviewModal: FunctionComponent = observer(({ application, viewControllerManager }) => { const { currentFile, setCurrentFile, otherFiles, dismiss } = viewControllerManager.filePreviewModalController if (!currentFile) { return null } const [showFileInfoPanel, setShowFileInfoPanel] = useState(false) const closeButtonRef = useRef(null) const keyDownHandler: KeyboardEventHandler = useCallback( (event) => { const hasNotPressedLeftOrRightKeys = event.key !== KeyboardKey.Left && event.key !== KeyboardKey.Right if (hasNotPressedLeftOrRightKeys) { return } event.preventDefault() const currentFileIndex = otherFiles.findIndex((fileFromArray) => fileFromArray.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 } } }, [currentFile.uuid, otherFiles, setCurrentFile], ) const IconComponent = useMemo( () => getFileIconComponent( application.iconsController.getIconForFileType(currentFile.mimeType), 'w-6 h-6 flex-shrink-0', ), [application.iconsController, currentFile.mimeType], ) return (
{IconComponent}
{currentFile.name}
{showFileInfoPanel && }
) }) FilePreviewModal.displayName = 'FilePreviewModal' const FilePreviewModalWrapper: FunctionComponent = ({ application, viewControllerManager }) => { return viewControllerManager.filePreviewModalController.isOpen ? ( ) : null } export default observer(FilePreviewModalWrapper)