From ce3d2d71ae8957d01359cc6239a4019ee707fc9b Mon Sep 17 00:00:00 2001 From: Neil <4138956+nwparker@users.noreply.github.com> Date: Sat, 30 May 2026 11:51:47 -0700 Subject: [PATCH] Reset notebook execution trust during render (#3383) * Reset notebook execution trust during render * Resolve notebook viewer main integration Co-authored-by: Orca --------- Co-authored-by: Jinwoo-H Co-authored-by: Orca --- .../src/components/editor/IpynbViewer.tsx | 56 +++++++++++++++---- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/src/renderer/src/components/editor/IpynbViewer.tsx b/src/renderer/src/components/editor/IpynbViewer.tsx index 7459f9110..bebfaedb1 100644 --- a/src/renderer/src/components/editor/IpynbViewer.tsx +++ b/src/renderer/src/components/editor/IpynbViewer.tsx @@ -103,6 +103,20 @@ function requestIpynbStructuralContentFrame( } } +type NotebookExecutionTrustState = { + filePath: string + trustedForFile: boolean + pendingRunCellIndex: number | null +} + +function createNotebookExecutionTrustState(filePath: string): NotebookExecutionTrustState { + return { + filePath, + trustedForFile: false, + pendingRunCellIndex: null + } +} + function valueToText(value: unknown): string { if (Array.isArray(value)) { return value.map((item) => String(item ?? '')).join('') @@ -515,8 +529,9 @@ export default function IpynbViewer({ const [runningCellIndex, setRunningCellIndex] = useState(null) const [runError, setRunError] = useState(null) const [editingCellKey, setEditingCellKey] = useState(null) - const [executionTrustedForFile, setExecutionTrustedForFile] = useState(false) - const [pendingRunCellIndex, setPendingRunCellIndex] = useState(null) + const [executionTrustState, setExecutionTrustState] = useState(() => + createNotebookExecutionTrustState(filePath) + ) const [sourceDrafts, setSourceDrafts] = useState>({}) const sourceDraftsRef = useRef(sourceDrafts) const contentRef = useRef(content) @@ -541,6 +556,31 @@ export default function IpynbViewer({ onContentChangeRef.current = onContentChange onDirtyStateHintRef.current = onDirtyStateHint + // Why: execution trust belongs to the currently rendered file; resetting + // during render avoids a paint with the previous file's trust prompt state. + if (executionTrustState.filePath !== filePath) { + setExecutionTrustState(createNotebookExecutionTrustState(filePath)) + } + const executionTrustedForFile = + executionTrustState.filePath === filePath ? executionTrustState.trustedForFile : false + const pendingRunCellIndex = + executionTrustState.filePath === filePath ? executionTrustState.pendingRunCellIndex : null + + const setPendingRunCellIndexForFile = (nextPendingRunCellIndex: number | null): void => { + setExecutionTrustState((current) => ({ + filePath, + trustedForFile: current.filePath === filePath ? current.trustedForFile : false, + pendingRunCellIndex: nextPendingRunCellIndex + })) + } + const trustFileForExecution = (): void => { + setExecutionTrustState({ + filePath, + trustedForFile: true, + pendingRunCellIndex: null + }) + } + const materializeSourceDrafts = useCallback((): string => { const notebook = notebookRef.current const drafts = sourceDraftsRef.current @@ -582,11 +622,6 @@ export default function IpynbViewer({ return registerPendingEditorFlush(fileId, flushSourceDrafts) }, [fileId, flushSourceDrafts]) - useEffect(() => { - setExecutionTrustedForFile(false) - setPendingRunCellIndex(null) - }, [filePath]) - useEffect(() => { return () => { void flushSourceDrafts() @@ -751,7 +786,7 @@ export default function IpynbViewer({ return } if (!executionTrustedForFile && !options.skipTrustPrompt) { - setPendingRunCellIndex(index) + setPendingRunCellIndexForFile(index) return } setRunError(null) @@ -775,11 +810,10 @@ export default function IpynbViewer({ setRunningCellIndex(null) } } - const cancelPendingRun = (): void => setPendingRunCellIndex(null) + const cancelPendingRun = (): void => setPendingRunCellIndexForFile(null) const confirmPendingRun = (): void => { const index = pendingRunCellIndex - setPendingRunCellIndex(null) - setExecutionTrustedForFile(true) + trustFileForExecution() if (index !== null) { void runCell(index, { skipTrustPrompt: true }) }