@@ -503,16 +503,36 @@ export function Document({
503503 [ guardDirtyAction , navigateToChunk ]
504504 )
505505
506+ /**
507+ * Confirms before a crumb navigates away from an unsaved chunk. A route change unmounts the
508+ * editor, so the edit is gone with no way back — the same reason the in-page transitions go
509+ * through {@link guardDirtyAction}.
510+ *
511+ * Gated on the editor actually being open, not on `isDirty` alone: `UnsavedChangesModal`
512+ * mounts only alongside the editor, and `isDirty` outlives it when the editor is unmounted
513+ * by a URL change rather than by `closeEditor` (browser Back off an edited chunk). Guarding
514+ * unconditionally would then raise a modal nothing renders, leaving the crumb dead.
515+ */
516+ const guardRouteChange = useCallback (
517+ ( navigate : ( ) => void ) => {
518+ if ( isCreatingNewChunk || selectedChunkId ) guardDirtyAction ( navigate )
519+ else navigate ( )
520+ } ,
521+ [ isCreatingNewChunk , selectedChunkId , guardDirtyAction ]
522+ )
523+
506524 const handleNavToFolder = useCallback (
507525 ( folderId : string | null ) => {
508- router . push ( folderedResourceListHref ( 'knowledge_base' , workspaceId as string , folderId ) )
526+ guardRouteChange ( ( ) =>
527+ router . push ( folderedResourceListHref ( 'knowledge_base' , workspaceId as string , folderId ) )
528+ )
509529 } ,
510- [ router , workspaceId ]
530+ [ guardRouteChange , router , workspaceId ]
511531 )
512532
513533 const handleNavToKBDetail = useCallback ( ( ) => {
514- router . push ( `/workspace/${ workspaceId } /knowledge/${ knowledgeBaseId } ` )
515- } , [ router , workspaceId , knowledgeBaseId ] )
534+ guardRouteChange ( ( ) => router . push ( `/workspace/${ workspaceId } /knowledge/${ knowledgeBaseId } ` ) )
535+ } , [ guardRouteChange , router , workspaceId , knowledgeBaseId ] )
516536
517537 /**
518538 * `Knowledge Base / …the base's folders / <base> / <last>`. Every view on this route is that
0 commit comments