refactoring, linting, formatting
This commit is contained in:
128
src/hooks/usePdfGeneratedOutputs.ts
Normal file
128
src/hooks/usePdfGeneratedOutputs.ts
Normal file
@@ -0,0 +1,128 @@
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import type { SplitResult } from "../pdf/pdfTypes";
|
||||
|
||||
export interface PdfDownload {
|
||||
id: string;
|
||||
filename: string;
|
||||
url: string;
|
||||
}
|
||||
|
||||
export interface SplitPdfDownload extends PdfDownload {
|
||||
pageIndex: number;
|
||||
}
|
||||
|
||||
function revokeDownload(download: PdfDownload | null): void {
|
||||
if (download) {
|
||||
URL.revokeObjectURL(download.url);
|
||||
}
|
||||
}
|
||||
|
||||
function revokeDownloads(downloads: PdfDownload[]): void {
|
||||
downloads.forEach(revokeDownload);
|
||||
}
|
||||
|
||||
function createDownload(id: string, filename: string, blob: Blob): PdfDownload {
|
||||
return {
|
||||
id,
|
||||
filename,
|
||||
url: URL.createObjectURL(blob),
|
||||
};
|
||||
}
|
||||
|
||||
export function usePdfGeneratedOutputs() {
|
||||
const [splitDownloads, setSplitDownloads] = useState<SplitPdfDownload[]>([]);
|
||||
const [subsetDownload, setSubsetDownload] = useState<PdfDownload | null>(
|
||||
null,
|
||||
);
|
||||
const [exportDownload, setExportDownload] = useState<PdfDownload | null>(
|
||||
null,
|
||||
);
|
||||
|
||||
const splitDownloadsRef = useRef<SplitPdfDownload[]>([]);
|
||||
const subsetDownloadRef = useRef<PdfDownload | null>(null);
|
||||
const exportDownloadRef = useRef<PdfDownload | null>(null);
|
||||
|
||||
const replaceSplitResults = useCallback((results: SplitResult[]) => {
|
||||
const nextDownloads: SplitPdfDownload[] = results.map((result) => ({
|
||||
...createDownload(
|
||||
`split-${result.pageIndex}-${result.filename}`,
|
||||
result.filename,
|
||||
result.blob,
|
||||
),
|
||||
pageIndex: result.pageIndex,
|
||||
}));
|
||||
|
||||
revokeDownloads(splitDownloadsRef.current);
|
||||
splitDownloadsRef.current = nextDownloads;
|
||||
setSplitDownloads(nextDownloads);
|
||||
}, []);
|
||||
|
||||
const clearSplitResults = useCallback(() => {
|
||||
revokeDownloads(splitDownloadsRef.current);
|
||||
splitDownloadsRef.current = [];
|
||||
setSplitDownloads([]);
|
||||
}, []);
|
||||
|
||||
const replaceSubsetResult = useCallback((blob: Blob, filename: string) => {
|
||||
const nextDownload = createDownload("subset", filename, blob);
|
||||
|
||||
revokeDownload(subsetDownloadRef.current);
|
||||
subsetDownloadRef.current = nextDownload;
|
||||
setSubsetDownload(nextDownload);
|
||||
}, []);
|
||||
|
||||
const clearSubsetResult = useCallback(() => {
|
||||
revokeDownload(subsetDownloadRef.current);
|
||||
subsetDownloadRef.current = null;
|
||||
setSubsetDownload(null);
|
||||
}, []);
|
||||
|
||||
const replaceExportResult = useCallback((blob: Blob, filename: string) => {
|
||||
const nextDownload = createDownload("export", filename, blob);
|
||||
|
||||
revokeDownload(exportDownloadRef.current);
|
||||
exportDownloadRef.current = nextDownload;
|
||||
setExportDownload(nextDownload);
|
||||
}, []);
|
||||
|
||||
const clearExportResult = useCallback(() => {
|
||||
revokeDownload(exportDownloadRef.current);
|
||||
exportDownloadRef.current = null;
|
||||
setExportDownload(null);
|
||||
}, []);
|
||||
|
||||
const clearAllResults = useCallback(() => {
|
||||
revokeDownloads(splitDownloadsRef.current);
|
||||
revokeDownload(subsetDownloadRef.current);
|
||||
revokeDownload(exportDownloadRef.current);
|
||||
|
||||
splitDownloadsRef.current = [];
|
||||
subsetDownloadRef.current = null;
|
||||
exportDownloadRef.current = null;
|
||||
|
||||
setSplitDownloads([]);
|
||||
setSubsetDownload(null);
|
||||
setExportDownload(null);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
revokeDownloads(splitDownloadsRef.current);
|
||||
revokeDownload(subsetDownloadRef.current);
|
||||
revokeDownload(exportDownloadRef.current);
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
splitDownloads,
|
||||
subsetDownload,
|
||||
exportDownload,
|
||||
replaceSplitResults,
|
||||
clearSplitResults,
|
||||
replaceSubsetResult,
|
||||
clearSubsetResult,
|
||||
replaceExportResult,
|
||||
clearExportResult,
|
||||
clearAllResults,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user