Reset notebook execution trust during render (#3383)
* Reset notebook execution trust during render * Resolve notebook viewer main integration Co-authored-by: Orca <help@stably.ai> --------- Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com> Co-authored-by: Orca <help@stably.ai>
This commit is contained in:
@@ -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<number | null>(null)
|
||||
const [runError, setRunError] = useState<string | null>(null)
|
||||
const [editingCellKey, setEditingCellKey] = useState<string | null>(null)
|
||||
const [executionTrustedForFile, setExecutionTrustedForFile] = useState(false)
|
||||
const [pendingRunCellIndex, setPendingRunCellIndex] = useState<number | null>(null)
|
||||
const [executionTrustState, setExecutionTrustState] = useState(() =>
|
||||
createNotebookExecutionTrustState(filePath)
|
||||
)
|
||||
const [sourceDrafts, setSourceDrafts] = useState<Record<string, string>>({})
|
||||
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 })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user