import { useEffect } from 'react'; import type { JSX } from 'react'; import { ArrowLeftIcon, ArrowRightIcon } from '../ui/icons'; import { useAssetNavigator } from '../hooks/useAssetNavigator'; import { preventAll } from './events'; type Identifier = string | number; interface DocumentLike { id?: Identifier; title?: string; [key: string]: unknown; } interface AssetLike { id?: Identifier; cardinality?: number; url?: string | null; metadata?: Record | null; objects?: Array>; [key: string]: unknown; } type EnsureAssetUrl = ( documentId: Identifier, asset: AssetLike, options?: { start?: number; limit?: number; [key: string]: unknown }, ) => Promise; type GetDocumentAsset = (document: DocumentLike | null, assetType: string) => AssetLike | null; interface NavigatorSnapshot { url: string | null; alt?: string; canGoPrev: boolean; canGoNext: boolean; goPrev?: () => void; goNext?: () => void; ordinal: number; width: number | null; height: number | null; } interface DesktopPreviewCardProps { doc: DocumentLike | null; title?: string; ensureAssetUrl?: EnsureAssetUrl | null; getDocumentAsset: GetDocumentAsset; prefetch?: number; onNavigatorSnapshot?: (docId: Identifier, snapshot: NavigatorSnapshot | null) => void; shouldLoad?: boolean; } const DesktopPreviewCard = ({ doc, title, ensureAssetUrl, getDocumentAsset, prefetch = 3, onNavigatorSnapshot, shouldLoad = true, }: DesktopPreviewCardProps): JSX.Element => { const navigator = useAssetNavigator({ document: doc, assetType: 'thumbnail', ensureAssetUrl: shouldLoad ? ensureAssetUrl : null, getAsset: getDocumentAsset, prefetch, }); const { currentUrl, cardinality, canGoPrev, canGoNext, currentMetadata, ordinal } = navigator; const docId = doc?.id ?? null; const metadataWidth = Number((currentMetadata as { width?: number } | null)?.width); const metadataHeight = Number((currentMetadata as { height?: number } | null)?.height); useEffect(() => { if (!onNavigatorSnapshot || !docId) { return undefined; } const snapshot = { url: currentUrl || null, alt: title, canGoPrev, canGoNext, goPrev: navigator.goPrev, goNext: navigator.goNext, ordinal, width: Number.isFinite(metadataWidth) && metadataWidth > 0 ? metadataWidth : null, height: Number.isFinite(metadataHeight) && metadataHeight > 0 ? metadataHeight : null, }; onNavigatorSnapshot(docId, snapshot); return () => onNavigatorSnapshot(docId, null); }, [ docId, currentUrl, title, canGoPrev, canGoNext, ordinal, metadataWidth, metadataHeight, navigator.goPrev, navigator.goNext, onNavigatorSnapshot, ]); const hasPreview = Boolean(currentUrl); const cardClasses = ['desk-item__card']; if (!hasPreview) cardClasses.push('desk-item__card--empty'); const showNav = hasPreview && (cardinality > 1 || canGoPrev || canGoNext); return (
{ if (event instanceof DragEvent) { event.preventDefault(); } }} > {hasPreview ? ( {title} event.preventDefault()} /> ) : (
DOC
{title}
)} {showNav ? (
) : null}
); }; export default DesktopPreviewCard;