1- import { useEffect , useRef , useState } from "react" ;
1+ import { useEffect , useState } from "react" ;
22import { EditorInstance } from "novel" ;
33import { useDebouncedCallback } from "use-debounce" ;
44import * as Y from "yjs" ;
@@ -15,19 +15,18 @@ export default function EditorView() {
1515 const { currentPage } = usePageStore ( ) ;
1616 const { page, isLoading } = usePage ( currentPage ) ;
1717 const [ saveStatus , setSaveStatus ] = useState < "saved" | "unsaved" > ( "saved" ) ;
18-
19- const ydoc = useRef < Y . Doc > ( ) ;
20- const provider = useRef < SocketIOProvider > ( ) ;
18+ const [ ydoc , setYDoc ] = useState < Y . Doc | null > ( null ) ;
19+ const [ provider , setProvider ] = useState < SocketIOProvider | null > ( null ) ;
2120
2221 useEffect ( ( ) => {
2322 if ( ! currentPage ) return ;
2423
25- if ( provider . current ) {
26- provider . current . disconnect ( ) ;
24+ if ( provider ) {
25+ provider . disconnect ( ) ;
2726 }
2827
2928 const doc = new Y . Doc ( ) ;
30- ydoc . current = doc ;
29+ setYDoc ( doc ) ;
3130
3231 const wsProvider = new SocketIOProvider (
3332 import . meta. env . VITE_WS_URL ,
@@ -44,7 +43,7 @@ export default function EditorView() {
4443 } ,
4544 ) ;
4645
47- provider . current = wsProvider ;
46+ setProvider ( wsProvider ) ;
4847
4948 return ( ) => {
5049 wsProvider . disconnect ( ) ;
@@ -79,7 +78,7 @@ export default function EditorView() {
7978 return null ;
8079 }
8180
82- if ( ! ydoc . current || ! provider . current ) return null ;
81+ if ( ! ydoc || ! provider ) return null ;
8382
8483 return (
8584 < EditorLayout >
@@ -90,11 +89,11 @@ export default function EditorView() {
9089 pageContent = { pageContent }
9190 />
9291 < Editor
93- key = { provider . current . doc . guid + currentPage }
92+ key = { ydoc . guid }
9493 initialContent = { pageContent }
9594 pageId = { currentPage }
96- ydoc = { ydoc . current }
97- provider = { provider . current }
95+ ydoc = { ydoc }
96+ provider = { provider }
9897 onEditorUpdate = { handleEditorUpdate }
9998 />
10099 </ EditorLayout >
0 commit comments