diff --git a/frontend/src/contexts/FileContext.tsx b/frontend/src/contexts/FileContext.tsx index 921c88333..ba14041c6 100644 --- a/frontend/src/contexts/FileContext.tsx +++ b/frontend/src/contexts/FileContext.tsx @@ -124,6 +124,11 @@ function FileContextInner({ baseActions.unpinFile(file.fileId); }, [baseActions]); + // Refresh file context - triggers re-render of file-dependent components + const refreshFileContext = useCallback(() => { + dispatch({ type: 'REFRESH_CONTEXT' }); + }, []); + // Complete actions object const actions = useMemo(() => ({ ...baseActions, @@ -170,6 +175,7 @@ function FileContextInner({ } } }, + refreshFileContext, // Pinned files functionality with File object wrappers pinFile: pinFileWrapper, unpinFile: unpinFileWrapper, @@ -191,7 +197,8 @@ function FileContextInner({ pinFileWrapper, unpinFileWrapper, indexedDB, - enablePersistence + enablePersistence, + refreshFileContext ]); // Split context values to minimize re-renders diff --git a/frontend/src/contexts/file/FileReducer.ts b/frontend/src/contexts/file/FileReducer.ts index a646b90fe..0818df704 100644 --- a/frontend/src/contexts/file/FileReducer.ts +++ b/frontend/src/contexts/file/FileReducer.ts @@ -282,6 +282,17 @@ export function fileContextReducer(state: FileContextState, action: FileContextA return { ...initialFileContextState }; } + case 'REFRESH_CONTEXT': { + // Trigger re-render by returning new state object while preserving data + // This forces components to reload without losing file data + return { + ...state, + ui: { + ...state.ui + } + }; + } + default: return state; } diff --git a/frontend/src/tools/Sign.tsx b/frontend/src/tools/Sign.tsx index 807dd732d..9929bfe29 100644 --- a/frontend/src/tools/Sign.tsx +++ b/frontend/src/tools/Sign.tsx @@ -16,7 +16,7 @@ const Sign = (props: BaseToolProps) => { const { t } = useTranslation(); const { setWorkbench } = useNavigation(); const { setSignatureConfig, activateDrawMode, activateSignaturePlacementMode, deactivateDrawMode, updateDrawSettings, undo, redo, signatureApiRef, getImageData, setSignaturesApplied } = useSignature(); - const { consumeFiles, selectors } = useFileContext(); + const { consumeFiles, selectors, actions } = useFileContext(); const { exportActions, getScrollState } = useViewer(); // Track which signature mode was active for reactivation after save @@ -97,20 +97,16 @@ const Sign = (props: BaseToolProps) => { // Mark signatures as applied setSignaturesApplied(true); - // Force refresh the viewer to show the flattened PDF + // Refresh the file context to reload the flattened PDF in viewer setTimeout(() => { - // Navigate away from viewer and back to force reload - setWorkbench('fileEditor'); - setTimeout(() => { - setWorkbench('viewer'); + actions.refreshFileContext(); - // Reactivate the signature mode that was active before save - if (activeModeRef.current === 'draw') { - activateDrawMode(); - } else if (activeModeRef.current === 'placement') { - handleSignaturePlacement(); - } - }, 100); + // Reactivate the signature mode that was active before save + if (activeModeRef.current === 'draw') { + activateDrawMode(); + } else if (activeModeRef.current === 'placement') { + handleSignaturePlacement(); + } }, 200); } else { console.error('Signature flattening failed'); @@ -118,7 +114,7 @@ const Sign = (props: BaseToolProps) => { } catch (error) { console.error('Error saving signed document:', error); } - }, [exportActions, base.selectedFiles, selectors, consumeFiles, signatureApiRef, getImageData, setWorkbench, activateDrawMode]); + }, [exportActions, base.selectedFiles, selectors, consumeFiles, signatureApiRef, getImageData, actions, activateDrawMode, handleSignaturePlacement, setSignaturesApplied]); const getSteps = () => { const steps = []; diff --git a/frontend/src/types/fileContext.ts b/frontend/src/types/fileContext.ts index 7f62f945a..e4e61838f 100644 --- a/frontend/src/types/fileContext.ts +++ b/frontend/src/types/fileContext.ts @@ -250,7 +250,8 @@ export type FileContextAction = | { type: 'SET_UNSAVED_CHANGES'; payload: { hasChanges: boolean } } // Context management - | { type: 'RESET_CONTEXT' }; + | { type: 'RESET_CONTEXT' } + | { type: 'REFRESH_CONTEXT' }; export interface FileContextActions { // File management - lightweight actions only @@ -285,6 +286,7 @@ export interface FileContextActions { // Context management resetContext: () => void; + refreshFileContext: () => void; // Resource management trackBlobUrl: (url: string) => void;