cleanup
This commit is contained in:
@@ -35,7 +35,7 @@ interface DocumentsRouteAppShell {
|
||||
openTagsModal?: () => void;
|
||||
openCorrespondentsModal?: () => void;
|
||||
previewWorkspaceDocument?: unknown;
|
||||
previewWorkspaceEntry?: unknown;
|
||||
documentLink?: unknown;
|
||||
previewDocumentId?: Identifier | null;
|
||||
closeDocumentPreview?: () => void;
|
||||
ensurePreviewData?: EnsurePreviewData;
|
||||
@@ -54,7 +54,7 @@ const DocumentsRouteContent: React.FC = () => {
|
||||
openTagsModal,
|
||||
openCorrespondentsModal,
|
||||
previewWorkspaceDocument,
|
||||
previewWorkspaceEntry,
|
||||
documentLink,
|
||||
previewDocumentId,
|
||||
closeDocumentPreview,
|
||||
ensurePreviewData,
|
||||
@@ -107,7 +107,7 @@ const DocumentsRouteContent: React.FC = () => {
|
||||
detailPanelProps,
|
||||
detailPanelOpen,
|
||||
previewWorkspaceDocument,
|
||||
previewWorkspaceEntry,
|
||||
documentLink,
|
||||
previewDocumentId,
|
||||
ensureAssetUrl,
|
||||
ensurePreviewData,
|
||||
|
||||
@@ -16,7 +16,7 @@ type DocumentLike = {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
type PreviewEntry = {
|
||||
type DocumentLink = {
|
||||
url?: string;
|
||||
contentType?: string | null;
|
||||
filename?: string | null;
|
||||
@@ -54,14 +54,14 @@ interface UseDocumentPreviewArgs {
|
||||
}
|
||||
|
||||
interface UseDocumentPreviewResult {
|
||||
previewEntries: Map<DocumentId, PreviewEntry>;
|
||||
documentLinks: Map<DocumentId, DocumentLink>;
|
||||
previewDocuments: Map<DocumentId, DocumentLike>;
|
||||
ensureDownloadUrl: (documentId: DocumentId, options?: { force?: boolean }) => Promise<PreviewEntry | null>;
|
||||
ensureDownloadUrl: (documentId: DocumentId, options?: { force?: boolean }) => Promise<DocumentLink | null>;
|
||||
ensurePreviewData: (documentId: DocumentId) => Promise<DocumentLike | null>;
|
||||
openDocumentPreview: (documentId: DocumentId, options?: { replace?: boolean }) => void;
|
||||
closeDocumentPreview: (folderId?: FolderId) => void;
|
||||
resetPreviewState: () => void;
|
||||
removePreviewEntries: (ids: DocumentId[]) => void;
|
||||
removeDocumentLinks: (ids: DocumentId[]) => void;
|
||||
}
|
||||
|
||||
const useDocumentPreview = ({
|
||||
@@ -79,22 +79,22 @@ const useDocumentPreview = ({
|
||||
detailPanelControlRef,
|
||||
setActivePreviewId,
|
||||
}: UseDocumentPreviewArgs): UseDocumentPreviewResult => {
|
||||
const [previewEntries, setPreviewEntries] = useState<Map<DocumentId, PreviewEntry>>(() => new Map());
|
||||
const [documentLinks, setDocumentLinks] = useState<Map<DocumentId, DocumentLink>>(() => new Map());
|
||||
const [previewDocuments, setPreviewDocuments] = useState<Map<DocumentId, DocumentLike>>(() => new Map());
|
||||
const previewInflightRef = useRef<Map<DocumentId, Promise<PreviewEntry | null>>>(new Map());
|
||||
const previewInflightRef = useRef<Map<DocumentId, Promise<DocumentLink | null>>>(new Map());
|
||||
const previewReturnPathRef = useRef<string | null>(null);
|
||||
|
||||
const resetPreviewState = useCallback(() => {
|
||||
setPreviewEntries(() => new Map());
|
||||
setDocumentLinks(() => new Map());
|
||||
previewInflightRef.current = new Map();
|
||||
previewReturnPathRef.current = null;
|
||||
}, []);
|
||||
|
||||
const removePreviewEntries = useCallback((ids: DocumentId[]) => {
|
||||
const removeDocumentLinks = useCallback((ids: DocumentId[]) => {
|
||||
if (!Array.isArray(ids) || ids.length === 0) {
|
||||
return;
|
||||
}
|
||||
setPreviewEntries((prev) => {
|
||||
setDocumentLinks((prev) => {
|
||||
if (!prev.size) {
|
||||
return prev;
|
||||
}
|
||||
@@ -140,10 +140,10 @@ const useDocumentPreview = ({
|
||||
}, []);
|
||||
|
||||
const ensureDownloadUrl = useCallback(
|
||||
async (documentId: DocumentId, { force = false }: { force?: boolean } = {}): Promise<PreviewEntry | null> => {
|
||||
async (documentId: DocumentId, { force = false }: { force?: boolean } = {}): Promise<DocumentLink | null> => {
|
||||
if (!documentId) return null;
|
||||
|
||||
const existing = previewEntries.get(documentId) || null;
|
||||
const existing = documentLinks.get(documentId) || null;
|
||||
const now = Date.now();
|
||||
const expiresAt = Number.isFinite(existing?.expiresAt) ? Number(existing?.expiresAt) : null;
|
||||
if (!force && existing && (!expiresAt || expiresAt > now)) {
|
||||
@@ -154,7 +154,7 @@ const useDocumentPreview = ({
|
||||
return previewInflightRef.current.get(documentId) || null;
|
||||
}
|
||||
|
||||
const request: Promise<PreviewEntry | null> = (async () => {
|
||||
const request: Promise<DocumentLink | null> = (async () => {
|
||||
try {
|
||||
const docResponse = await api.get<{ document?: Record<string, any> }>(`/documents/${documentId}`);
|
||||
const downloadPath = docResponse.data?.document?.current_version?.download_path;
|
||||
@@ -163,13 +163,13 @@ const useDocumentPreview = ({
|
||||
}
|
||||
|
||||
const href = resolveApiPath(downloadPath);
|
||||
const entry: PreviewEntry = {
|
||||
const entry: DocumentLink = {
|
||||
url: href,
|
||||
contentType: docResponse.data?.document?.current_version?.version?.content_type || null,
|
||||
filename: docResponse.data?.document?.filename,
|
||||
expiresAt: Date.now() + 5 * 60 * 1000,
|
||||
};
|
||||
setPreviewEntries((prev) => {
|
||||
setDocumentLinks((prev) => {
|
||||
const next = new Map(prev);
|
||||
next.set(documentId, entry);
|
||||
return next;
|
||||
@@ -186,7 +186,7 @@ const useDocumentPreview = ({
|
||||
previewInflightRef.current.set(documentId, request);
|
||||
return request;
|
||||
},
|
||||
[previewEntries, api, resolveApiPath, notifyApiError],
|
||||
[documentLinks, api, resolveApiPath, notifyApiError],
|
||||
);
|
||||
|
||||
const ensurePreviewData = useCallback(
|
||||
@@ -330,14 +330,14 @@ const useDocumentPreview = ({
|
||||
}, [documents, searchResults]);
|
||||
|
||||
return {
|
||||
previewEntries,
|
||||
documentLinks,
|
||||
previewDocuments,
|
||||
ensureDownloadUrl,
|
||||
ensurePreviewData,
|
||||
openDocumentPreview,
|
||||
closeDocumentPreview,
|
||||
resetPreviewState,
|
||||
removePreviewEntries,
|
||||
removeDocumentLinks,
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -24,7 +24,7 @@ interface UseWorkspaceSurfaceArgs {
|
||||
detailPanelProps?: (Record<string, any> & { onClose?: () => void }) | null;
|
||||
detailPanelOpen?: boolean;
|
||||
previewWorkspaceDocument?: unknown;
|
||||
previewWorkspaceEntry?: unknown;
|
||||
documentLink?: unknown;
|
||||
previewDocumentId?: Identifier | null;
|
||||
ensureAssetUrl?: EnsureAssetUrl;
|
||||
ensurePreviewData?: EnsurePreviewData;
|
||||
@@ -45,7 +45,7 @@ export const useWorkspaceSurface = ({
|
||||
detailPanelProps,
|
||||
detailPanelOpen = false,
|
||||
previewWorkspaceDocument,
|
||||
previewWorkspaceEntry,
|
||||
documentLink,
|
||||
previewDocumentId,
|
||||
ensureAssetUrl,
|
||||
ensurePreviewData,
|
||||
@@ -122,7 +122,7 @@ export const useWorkspaceSurface = ({
|
||||
} = detailExtras;
|
||||
return createDocumentViewerSurface({
|
||||
document: previewWorkspaceDocument,
|
||||
previewEntry: previewWorkspaceEntry,
|
||||
documentLink,
|
||||
ensureAssetUrl,
|
||||
ensurePreviewData,
|
||||
getDocumentAsset,
|
||||
@@ -144,7 +144,7 @@ export const useWorkspaceSurface = ({
|
||||
}, [
|
||||
showPreviewWorkspace,
|
||||
previewWorkspaceDocument,
|
||||
previewWorkspaceEntry,
|
||||
documentLink,
|
||||
ensureAssetUrl,
|
||||
ensurePreviewData,
|
||||
getDocumentAsset,
|
||||
|
||||
Reference in New Issue
Block a user