import { getIcon, iconNames } from '@sd/assets/util'; import clsx from 'clsx'; import { SyntheticEvent, useEffect, useMemo, useRef, useState, type VideoHTMLAttributes } from 'react'; import { getItemFilePath, useLibraryContext } from '@sd/client'; import i18n from '~/app/I18n'; import { PDFViewer, TextViewer } from '~/components'; import { useLocale } from '~/hooks'; import { pdfViewerEnabled } from '~/util/pdfViewer'; import { usePlatform } from '~/util/Platform'; import { useExplorerContext } from '../Context'; import { explorerStore } from '../store'; import { ExplorerItemData } from '../useExplorerItemData'; import { Image } from './Image'; import { useBlackBars, useSize } from './utils'; interface OriginalRendererProps { src: string; className: string; frameClassName: string; itemData: ExplorerItemData; isDark: boolean; childClassName?: string; size?: number; magnification?: number; mediaControls?: boolean; frame?: boolean; isSidebarPreview?: boolean; pauseVideo?: boolean; blackBars?: boolean; blackBarsSize?: number; onLoad?(): void; onError?(e: ErrorEvent | SyntheticEvent): void; } export function Original({ itemData, filePath, ...props }: Omit & { filePath: ReturnType; }) { const [error, setError] = useState(false); if (error) throw new Error('onError'); const Renderer = useMemo(() => { const kind = originalRendererKind(itemData); return ORIGINAL_RENDERERS[kind]; }, [itemData]); if (!Renderer) throw new Error('no renderer!'); const platform = usePlatform(); const { library } = useLibraryContext(); const { parent } = useExplorerContext(); const src = useMemo(() => { const locationId = itemData.locationId ?? (parent?.type === 'Location' ? parent.location.id : null); if (filePath && (itemData.extension !== 'pdf' || pdfViewerEnabled())) { if ('id' in filePath && locationId) return platform.getFileUrl(library.uuid, locationId, filePath.id); else if ('path' in filePath) return platform.getFileUrlByPath(filePath.path); } }, [itemData, filePath, library.uuid, parent, platform]); if (src === undefined) throw new Error('no src!'); return setError(true)} {...props} />; } const TEXT_RENDERER: OriginalRenderer = (props) => ( ); type OriginalRenderer = (props: OriginalRendererProps) => JSX.Element; function originalRendererKind(itemData: ExplorerItemData) { return itemData.extension === 'pdf' ? 'PDF' : itemData.kind; } type OriginalRendererKind = ReturnType; const ORIGINAL_RENDERERS: { [K in OriginalRendererKind]?: OriginalRenderer; } = { PDF: (props) => ( ), Text: TEXT_RENDERER, Code: TEXT_RENDERER, Config: TEXT_RENDERER, Video: (props) => (