{"record":{"id":"0eaef7a5fa5b510d","repo":"Stirling-Tools/Stirling-PDF","slug":"current-file-id-not-found","errorCode":null,"errorMessage":"Current file ID not found","messagePattern":"Current file ID not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"frontend/editor/src/core/components/viewer/EmbedPdfViewer.tsx","lineNumber":640,"sourceCode":"        // Give a small delay for the commit to process\n        await new Promise((resolve) => setTimeout(resolve, 100));\n      }\n\n      // Step 1: Export PDF with annotations using EmbedPDF\n      const arrayBuffer = await exportActions.saveAsCopy();\n      if (!arrayBuffer) {\n        throw new Error(\"Failed to export PDF\");\n      }\n\n      // Step 2: Convert ArrayBuffer to File\n      const blob = new Blob([arrayBuffer], { type: \"application/pdf\" });\n      const filename = currentFile.name || \"document.pdf\";\n      const file = new File([blob], filename, { type: \"application/pdf\" });\n\n      // Step 3: Create StirlingFiles and stubs for version history\n      // Only consume the current file, not all active files\n      const currentFileId = currentFileStableId;\n      if (!currentFileId) throw new Error(\"Current file ID not found\");\n\n      const parentStub = selectors.getStirlingFileStub(currentFileId);\n      if (!parentStub) throw new Error(\"Parent stub not found\");\n\n      const { stirlingFiles, stubs } = await createStirlingFilesAndStubs(\n        [file],\n        parentStub,\n        selectedTool ?? \"multiTool\",\n      );\n\n      // Store the page to restore after file replacement triggers re-render\n      pendingScrollRestoreRef.current = pageToRestore;\n      scrollRestoreAttemptsRef.current = 0;\n\n      // Store the rotation to restore after file replacement\n      pendingRotationRestoreRef.current = currentRotation;\n      rotationRestoreAttemptsRef.current = 0;\n      // Track the new file ID so the viewer follows it after the list reorders","sourceCodeStart":622,"sourceCodeEnd":658,"githubUrl":"https://github.com/Stirling-Tools/Stirling-PDF/blob/9ef20dcab80b85041912f045e17a6aea1d08f969/frontend/editor/src/core/components/viewer/EmbedPdfViewer.tsx#L622-L658","documentation":"Thrown in EmbedPdfViewer's export path (handleSaveAsCopy) when currentFileStableId is null/undefined. This stableId is the FileContext identifier used to look up the file's stub for version history. Without it, the code cannot create child stubs linking the exported PDF to its parent.","triggerScenarios":"The active file in FileContext doesn't have a stableId assigned, or the file was navigated away from (switched/closed) during the async export operation, leaving currentFileStableId stale or null.","commonSituations":"File loaded from IndexedDB before stableId assignment completed. User switched to another file while the export was mid-flight. The file was consumed/removed by another tool operation before this export finished.","solutions":["Capture currentFileStableId at the start of the export function into a local const, not read it mid-async.","Disable file switching (tool navigation, file removal) while an export is in progress.","Guard the export button with a check that currentFileStableId is truthy before allowing the click.","Use an AbortController to cancel the export if the active file changes."],"exampleFix":"// before\nconst currentFileId = currentFileStableId;\nif (!currentFileId) throw new Error(\"Current file ID not found\");\n\n// after\nconst currentFileId = currentFileStableId;\nif (!currentFileId) {\n  setErrorMessage(\"Cannot export: no active file. Please select a file and try again.\");\n  return;\n}","handlingStrategy":"validation","validationCode":"// Capture the file ID synchronously at function entry, before any awaits\nconst fileId = currentFileStableId;\nif (!fileId) {\n  setErrorMessage('No active file to export. Please select a file first.');\n  return;\n}\n// Use fileId throughout — never re-read currentFileStableId after awaits","typeGuard":"function hasStableId(value: string | null | undefined): value is string {\n  return typeof value === 'string' && value.length > 0;\n}","tryCatchPattern":null,"preventionTips":["Capture currentFileStableId into a local const at the top of the function — never read it after an await.","Disable the export button when currentFileStableId is null.","Prevent file switching (tool navigation, file removal) during active export operations.","Use an AbortController to cancel exports if the active file changes."],"tags":["file-context","export","state-management","viewer","race-condition"],"backgroundTag":null,"analyzedSha":"9ef20dcab80b85041912f045e17a6aea1d08f969","analyzedAt":"2026-08-13T22:11:39.827Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}