Skip to content

Commit 03f109c

Browse files
committed
fix grotesque hook kludge
1 parent a1ffbd6 commit 03f109c

1 file changed

Lines changed: 44 additions & 44 deletions

File tree

src/components/Main.js

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,52 +45,52 @@ const Main = () => {
4545
});
4646
}, [setSharedState]);
4747

48+
// Helper to update preview width based on window size
49+
const handleResize = (setSharedState) => {
50+
setSharedState(currentState => {
51+
const newPreviewWidth = window.innerWidth > 599 ? window.innerWidth * 0.4 : window.innerWidth * 0.8;
52+
return { ...currentState, previewWidth: newPreviewWidth };
53+
});
54+
};
55+
56+
// Handles loading, imposing, and preparing the PDF for preview
57+
const processFile = async ({ file, impose, sharedState, setSharedState }) => {
58+
handleResize(setSharedState);
59+
let completedPDF = false;
60+
try {
61+
setSpinner(true);
62+
console.log("loading");
63+
await impose.loadPDF(await file.arrayBuffer());
64+
console.log("imposing");
65+
const completedPdf = await impose.createBooklet({ rtl: sharedState.rtl });
66+
console.log("converting to binary blob");
67+
if (completedPdf) {
68+
const pageIndex = sharedState.rtl ? completedPdf.getPages().length : 1;
69+
const blob = new Blob([completedPdf], { type: "application/pdf" });
70+
console.log("setting state for preview rendering");
71+
setTimeout(() => {
72+
setSharedState({ ...sharedState, pageNumberFolded: pageIndex, foldedPDF: blob, loaded: true });
73+
}, 1000);
74+
console.log(sharedState);
75+
} else {
76+
throw new Error(`completedPDF was ${completedPDF}`);
77+
}
78+
} catch (error) {
79+
console.error("Error processing file:", error);
80+
}
81+
setTimeout(() => setSpinner(false), 1250);
82+
};
83+
4884
useEffect(() => {
49-
50-
const handleResize = () => {
51-
setSharedState(currentState => {
52-
const newPreviewWidth = window.innerWidth > 599 ? window.innerWidth * 0.4 : window.innerWidth * 0.8;
53-
return {...currentState, previewWidth: newPreviewWidth};
85+
if (sharedState.origPDF) {
86+
processFile({
87+
file: sharedState.origPDF,
88+
impose,
89+
sharedState,
90+
setSharedState
5491
});
55-
};
56-
57-
// pdf file passed to imposify via drag and drop or by open
58-
// menu. currently this function loads a pdf, imposes it
59-
// via a simple two-page spread method, converts it back to
60-
// a pdf blob, and prepares it for rendering.
61-
const processFile = async (file) => {
62-
handleResize();
63-
let completedPDF = false;
64-
try {
65-
setSpinner(true);
66-
console.log("loading");
67-
// load pdf here
68-
await impose.loadPDF(await file.arrayBuffer());
69-
console.log("imposing");
70-
// createBooklet() does a lot all at once.
71-
const completedPdf = await impose.createBooklet({rtl: sharedState.rtl});
72-
console.log("converting to binary blob");
73-
// generate blob from pdf
74-
if (completedPdf) {
75-
const pageIndex = sharedState.rtl ? completedPdf.getPages().length : 1;
76-
const blob = new Blob([completedPdf], { type: "application/pdf" });
77-
console.log("setting state for preview rendering")
78-
setTimeout(() => {
79-
setSharedState({...sharedState, pageNumberFolded: pageIndex, foldedPDF: blob, loaded: true});
80-
}, 1000);
81-
console.log(sharedState);
82-
}
83-
else {
84-
throw new Error(`completedPDF was ${completedPDF}`);
85-
}
86-
} catch (error) {
87-
console.error("Error processing file:", error);
88-
}
89-
// We're loaded but we should delay unshowing the Spinner just a while
90-
setTimeout(() => setSpinner(false), 1250);
91-
};
92-
processFile(sharedState.origPDF);
93-
// eslint-disable-next-line react-hooks/exhaustive-deps
92+
}
93+
// eslint-disable-next-line react-hooks/exhaustive-deps
9494
}, [impose, sharedState.rtl, setSharedState, sharedState.origPDF]);
9595

9696
useEffect(() => {

0 commit comments

Comments
 (0)