Skip to content

Commit 0fc434a

Browse files
committed
move snappy to takeScreenshots param
1 parent b19dc42 commit 0fc434a

File tree

3 files changed

+67
-119
lines changed

3 files changed

+67
-119
lines changed

frontend/src/components/editor/actions/useNotebookActions.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function useNotebookActions() {
129129
const { selectedLayout } = useLayoutState();
130130
const { setLayoutView } = useLayoutActions();
131131
const togglePresenting = useTogglePresenting();
132-
const takeScreenshots = useEnrichCellOutputs({ snappy: false });
132+
const takeScreenshots = useEnrichCellOutputs();
133133

134134
// Fallback: if sharing is undefined, both are enabled by default
135135
const sharingHtmlEnabled = resolvedConfig.sharing?.html ?? true;
@@ -241,8 +241,8 @@ export function useNotebookActions() {
241241

242242
const downloadPDF = async (progress: ProgressState) => {
243243
await updateCellOutputsWithScreenshots({
244-
progress,
245-
takeScreenshots,
244+
takeScreenshots: () =>
245+
takeScreenshots({ progress, snappy: false }),
246246
updateCellOutputs,
247247
});
248248
await downloadAsPDF({

frontend/src/core/export/__tests__/hooks.test.ts

Lines changed: 46 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,10 @@ describe("useEnrichCellOutputs", () => {
100100
}),
101101
);
102102

103-
const { result } = renderHook(
104-
() => useEnrichCellOutputs({ snappy: false }),
105-
{
106-
wrapper,
107-
},
108-
);
103+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
109104

110-
const enrichCellOutputs = result.current;
111-
const output = await enrichCellOutputs(progress);
105+
const takeScreenshots = result.current;
106+
const output = await takeScreenshots({ progress, snappy: false });
112107

113108
expect(output).toEqual({});
114109
expect(document.getElementById).not.toHaveBeenCalled();
@@ -137,15 +132,10 @@ describe("useEnrichCellOutputs", () => {
137132
}),
138133
);
139134

140-
const { result } = renderHook(
141-
() => useEnrichCellOutputs({ snappy: false }),
142-
{
143-
wrapper,
144-
},
145-
);
135+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
146136

147-
const enrichCellOutputs = result.current;
148-
const output = await enrichCellOutputs(progress);
137+
const takeScreenshots = result.current;
138+
const output = await takeScreenshots({ progress, snappy: false });
149139

150140
expect(document.getElementById).toHaveBeenCalledWith(
151141
CellOutputId.create(cellId),
@@ -184,15 +174,10 @@ describe("useEnrichCellOutputs", () => {
184174
}),
185175
);
186176

187-
const { result } = renderHook(
188-
() => useEnrichCellOutputs({ snappy: true }),
189-
{
190-
wrapper,
191-
},
192-
);
177+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
193178

194-
const enrichCellOutputs = result.current;
195-
const output = await enrichCellOutputs(progress);
179+
const takeScreenshots = result.current;
180+
const output = await takeScreenshots({ progress, snappy: true });
196181

197182
expect(document.getElementById).toHaveBeenCalledWith(
198183
CellOutputId.create(cellId),
@@ -233,25 +218,22 @@ describe("useEnrichCellOutputs", () => {
233218
}),
234219
);
235220

236-
const { result, rerender } = renderHook(
237-
() => useEnrichCellOutputs({ snappy: false }),
238-
{
239-
wrapper,
240-
},
241-
);
221+
const { result, rerender } = renderHook(() => useEnrichCellOutputs(), {
222+
wrapper,
223+
});
242224

243225
// First call - should capture
244-
let enrichCellOutputs = result.current;
245-
let output = await enrichCellOutputs(progress);
226+
let takeScreenshots = result.current;
227+
let output = await takeScreenshots({ progress, snappy: false });
246228
expect(output).toEqual({ [cellId]: ["image/png", mockDataUrl] });
247229
expect(toPng).toHaveBeenCalledTimes(1);
248230

249231
// Rerender to get updated atom state
250232
rerender();
251233

252234
// Second call with same output - should not capture again
253-
enrichCellOutputs = result.current;
254-
output = await enrichCellOutputs(progress);
235+
takeScreenshots = result.current;
236+
output = await takeScreenshots({ progress, snappy: false });
255237
expect(output).toEqual({}); // Empty because output hasn't changed
256238
expect(toPng).toHaveBeenCalledTimes(1); // Still only 1 call
257239
});
@@ -277,15 +259,10 @@ describe("useEnrichCellOutputs", () => {
277259
}),
278260
);
279261

280-
const { result } = renderHook(
281-
() => useEnrichCellOutputs({ snappy: false }),
282-
{
283-
wrapper,
284-
},
285-
);
262+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
286263

287-
const enrichCellOutputs = result.current;
288-
const output = await enrichCellOutputs(progress);
264+
const takeScreenshots = result.current;
265+
const output = await takeScreenshots({ progress, snappy: false });
289266

290267
expect(output).toEqual({}); // Failed screenshot should be filtered out
291268
expect(Logger.error).toHaveBeenCalledWith(
@@ -312,15 +289,10 @@ describe("useEnrichCellOutputs", () => {
312289
}),
313290
);
314291

315-
const { result } = renderHook(
316-
() => useEnrichCellOutputs({ snappy: false }),
317-
{
318-
wrapper,
319-
},
320-
);
292+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
321293

322-
const enrichCellOutputs = result.current;
323-
const output = await enrichCellOutputs(progress);
294+
const takeScreenshots = result.current;
295+
const output = await takeScreenshots({ progress, snappy: false });
324296

325297
expect(output).toEqual({});
326298
expect(Logger.error).toHaveBeenCalledWith(
@@ -366,15 +338,10 @@ describe("useEnrichCellOutputs", () => {
366338
}),
367339
);
368340

369-
const { result } = renderHook(
370-
() => useEnrichCellOutputs({ snappy: false }),
371-
{
372-
wrapper,
373-
},
374-
);
341+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
375342

376-
const enrichCellOutputs = result.current;
377-
const output = await enrichCellOutputs(progress);
343+
const takeScreenshots = result.current;
344+
const output = await takeScreenshots({ progress, snappy: false });
378345

379346
expect(output).toEqual({
380347
[cell1]: ["image/png", mockDataUrl1],
@@ -417,15 +384,10 @@ describe("useEnrichCellOutputs", () => {
417384
}),
418385
);
419386

420-
const { result } = renderHook(
421-
() => useEnrichCellOutputs({ snappy: false }),
422-
{
423-
wrapper,
424-
},
425-
);
387+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
426388

427-
const enrichCellOutputs = result.current;
428-
const output = await enrichCellOutputs(progress);
389+
const takeScreenshots = result.current;
390+
const output = await takeScreenshots({ progress, snappy: false });
429391

430392
// Only the successful screenshot should be in the result
431393
expect(output).toEqual({
@@ -461,22 +423,19 @@ describe("useEnrichCellOutputs", () => {
461423
}),
462424
);
463425

464-
const { result, rerender } = renderHook(
465-
() => useEnrichCellOutputs({ snappy: false }),
466-
{
467-
wrapper,
468-
},
469-
);
426+
const { result, rerender } = renderHook(() => useEnrichCellOutputs(), {
427+
wrapper,
428+
});
470429

471430
// First screenshot
472-
let enrichCellOutputs = result.current;
473-
let output = await enrichCellOutputs(progress);
431+
let takeScreenshots = result.current;
432+
let output = await takeScreenshots({ progress, snappy: false });
474433
expect(output).toEqual({ [cellId]: ["image/png", mockDataUrl1] });
475434

476435
// Second call - same output, should not be captured
477436
rerender();
478-
enrichCellOutputs = result.current;
479-
output = await enrichCellOutputs(progress);
437+
takeScreenshots = result.current;
438+
output = await takeScreenshots({ progress, snappy: false });
480439
expect(output).toEqual({});
481440

482441
// Third call - output changed, should be captured
@@ -494,8 +453,8 @@ describe("useEnrichCellOutputs", () => {
494453
);
495454

496455
rerender();
497-
enrichCellOutputs = result.current;
498-
output = await enrichCellOutputs(progress);
456+
takeScreenshots = result.current;
457+
output = await takeScreenshots({ progress, snappy: false });
499458
expect(output).toEqual({ [cellId]: ["image/png", mockDataUrl2] });
500459
expect(toPng).toHaveBeenCalledTimes(2);
501460
});
@@ -532,15 +491,10 @@ describe("useEnrichCellOutputs", () => {
532491
}),
533492
);
534493

535-
const { result } = renderHook(
536-
() => useEnrichCellOutputs({ snappy: false }),
537-
{
538-
wrapper,
539-
},
540-
);
494+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
541495

542-
const enrichCellOutputs = result.current;
543-
const output = await enrichCellOutputs(progress);
496+
const takeScreenshots = result.current;
497+
const output = await takeScreenshots({ progress, snappy: false });
544498

545499
// None of these should trigger screenshots
546500
expect(output).toEqual({});
@@ -562,15 +516,10 @@ describe("useEnrichCellOutputs", () => {
562516
}),
563517
);
564518

565-
const { result } = renderHook(
566-
() => useEnrichCellOutputs({ snappy: false }),
567-
{
568-
wrapper,
569-
},
570-
);
519+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
571520

572-
const enrichCellOutputs = result.current;
573-
const output = await enrichCellOutputs(progress);
521+
const takeScreenshots = result.current;
522+
const output = await takeScreenshots({ progress, snappy: false });
574523

575524
expect(output).toEqual({});
576525
expect(document.getElementById).not.toHaveBeenCalled();
@@ -599,15 +548,10 @@ describe("useEnrichCellOutputs", () => {
599548
}),
600549
);
601550

602-
const { result } = renderHook(
603-
() => useEnrichCellOutputs({ snappy: false }),
604-
{
605-
wrapper,
606-
},
607-
);
551+
const { result } = renderHook(() => useEnrichCellOutputs(), { wrapper });
608552

609-
const enrichCellOutputs = result.current;
610-
const output = await enrichCellOutputs(progress);
553+
const takeScreenshots = result.current;
554+
const output = await takeScreenshots({ progress, snappy: false });
611555

612556
// Verify the exact return type structure
613557
expect(output).toHaveProperty(cellId);
@@ -639,7 +583,6 @@ describe("updateCellOutputsWithScreenshots", () => {
639583
const updateCellOutputs = vi.fn().mockResolvedValue(null);
640584

641585
await updateCellOutputsWithScreenshots({
642-
progress,
643586
takeScreenshots,
644587
updateCellOutputs,
645588
});
@@ -656,7 +599,6 @@ describe("updateCellOutputsWithScreenshots", () => {
656599
const updateCellOutputs = vi.fn().mockResolvedValue(null);
657600

658601
await updateCellOutputsWithScreenshots({
659-
progress,
660602
takeScreenshots,
661603
updateCellOutputs,
662604
});
@@ -683,7 +625,6 @@ describe("updateCellOutputsWithScreenshots", () => {
683625
const updateCellOutputs = vi.fn().mockResolvedValue(null);
684626

685627
await updateCellOutputsWithScreenshots({
686-
progress,
687628
takeScreenshots,
688629
updateCellOutputs,
689630
});
@@ -700,7 +641,6 @@ describe("updateCellOutputsWithScreenshots", () => {
700641

701642
// Should not throw - errors are caught and shown via toast
702643
await updateCellOutputsWithScreenshots({
703-
progress,
704644
takeScreenshots,
705645
updateCellOutputs,
706646
});
@@ -733,7 +673,6 @@ describe("updateCellOutputsWithScreenshots", () => {
733673

734674
// Should not throw - errors are caught and shown via toast
735675
await updateCellOutputsWithScreenshots({
736-
progress,
737676
takeScreenshots,
738677
updateCellOutputs,
739678
});

frontend/src/core/export/hooks.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function useAutoExport() {
3636
autoExportAsMarkdown,
3737
updateCellOutputs,
3838
} = useRequestClient();
39-
const takeScreenshots = useEnrichCellOutputs({ snappy: true });
39+
const takeScreenshots = useEnrichCellOutputs();
4040

4141
useInterval(
4242
async () => {
@@ -64,9 +64,13 @@ export function useAutoExport() {
6464

6565
useInterval(
6666
async () => {
67+
const screenshotFn = () =>
68+
takeScreenshots({
69+
progress: ProgressState.indeterminate(),
70+
snappy: true,
71+
});
6772
await updateCellOutputsWithScreenshots({
68-
progress: ProgressState.indeterminate(),
69-
takeScreenshots,
73+
takeScreenshots: screenshotFn,
7074
updateCellOutputs,
7175
});
7276
await autoExportAsIPYNB({
@@ -103,11 +107,17 @@ type ScreenshotResults = Record<CellId, ["image/png", string]>;
103107
* Take screenshots of cells with HTML outputs. These images will be sent to the backend to be exported to IPYNB.
104108
* @returns A map of cell IDs to their screenshots data.
105109
*/
106-
export function useEnrichCellOutputs({ snappy }: { snappy: boolean }) {
110+
export function useEnrichCellOutputs() {
107111
const [richCellsOutput, setRichCellsOutput] = useAtom(richCellsToOutputAtom);
108112
const cellRuntimes = useAtomValue(cellsRuntimeAtom);
109113

110-
return async (progress: ProgressState): Promise<ScreenshotResults> => {
114+
return async ({
115+
progress,
116+
snappy,
117+
}: {
118+
progress: ProgressState;
119+
snappy: boolean;
120+
}): Promise<ScreenshotResults> => {
111121
const trackedCellsOutput: Record<CellId, unknown> = {};
112122

113123
const cellsToCaptureScreenshot: [CellId, unknown][] = [];
@@ -159,13 +169,12 @@ export function useEnrichCellOutputs({ snappy }: { snappy: boolean }) {
159169
* Utility function to take screenshots of cells with HTML outputs and update the cell outputs.
160170
*/
161171
export async function updateCellOutputsWithScreenshots(opts: {
162-
progress: ProgressState;
163-
takeScreenshots: (progress: ProgressState) => Promise<ScreenshotResults>;
172+
takeScreenshots: () => Promise<ScreenshotResults>;
164173
updateCellOutputs: (request: UpdateCellOutputsRequest) => Promise<null>;
165174
}) {
166-
const { progress, takeScreenshots, updateCellOutputs } = opts;
175+
const { takeScreenshots, updateCellOutputs } = opts;
167176
try {
168-
const cellIdsToOutput = await takeScreenshots(progress);
177+
const cellIdsToOutput = await takeScreenshots();
169178
if (Objects.size(cellIdsToOutput) > 0) {
170179
await updateCellOutputs({ cellIdsToOutput });
171180
}

0 commit comments

Comments
 (0)