@@ -18,11 +18,12 @@ export default function PageEdit() {
1818 const [ saving , setSaving ] = useState ( false )
1919 const [ error , setError ] = useState ( '' )
2020 const [ conflict , setConflict ] = useState ( null ) // { currentVersion } on 409
21- const [ dirty , setDirty ] = useState ( false )
22- const originalRef = useRef ( { title : '' , content : '' } )
21+ const [ original , setOriginal ] = useState ( { title : '' , content : '' } )
2322 const baseVersionRef = useRef ( null )
2423 const editorRef = useRef ( null )
2524
25+ const dirty = ! ! page && ( title !== original . title || content !== original . content )
26+
2627 // Preview state
2728 const [ showPreview , setShowPreview ] = useState ( false )
2829
@@ -38,19 +39,13 @@ export default function PageEdit() {
3839 setPage ( p )
3940 setTitle ( p . title )
4041 setContent ( p . content_md )
41- originalRef . current = { title : p . title , content : p . content_md }
42+ setOriginal ( { title : p . title , content : p . content_md } )
4243 baseVersionRef . current = p . version
4344 } ) . catch ( ( ) => {
4445 navigate ( '/' )
4546 } )
4647 } , [ slug ] )
4748
48- useEffect ( ( ) => {
49- if ( ! page ) return
50- const { title : origTitle , content : origContent } = originalRef . current
51- setDirty ( title !== origTitle || content !== origContent )
52- } , [ title , content , page ] )
53-
5449 // Extract diagram IDs from content and fetch their data
5550 const diagramIds = useMemo ( ( ) => {
5651 const ids = [ ]
@@ -142,7 +137,7 @@ export default function PageEdit() {
142137 } )
143138 baseVersionRef . current = updated . version
144139 await fetchTree ( )
145- setDirty ( false )
140+ setOriginal ( { title , content } )
146141 setSaving ( false )
147142 navigate ( `/page/${ slug } ` )
148143 } catch ( err ) {
@@ -171,10 +166,9 @@ export default function PageEdit() {
171166 if ( confirm ( 'Discard your changes and load the latest version?' ) ) {
172167 setTitle ( latest . title )
173168 setContent ( latest . content_md )
174- originalRef . current = { title : latest . title , content : latest . content_md }
169+ setOriginal ( { title : latest . title , content : latest . content_md } )
175170 baseVersionRef . current = latest . version
176171 setConflict ( null )
177- setDirty ( false )
178172 }
179173 } catch ( e ) {
180174 console . error ( 'Failed to reload:' , e )
0 commit comments