@@ -6,10 +6,15 @@ import { useEffect, useState } from 'react';
6
6
7
7
import { Box , Text } from '@/components' ;
8
8
import { TextErrors } from '@/components/TextErrors' ;
9
- import { useCollaborationUrl } from '@/core' ;
10
9
import { useAuthStore } from '@/core/auth' ;
11
10
import { DocEditor } from '@/features/docs/doc-editor' ;
12
- import { KEY_DOC , useDoc , useDocStore } from '@/features/docs/doc-management' ;
11
+ import {
12
+ Doc ,
13
+ KEY_DOC ,
14
+ useCollaboration ,
15
+ useDoc ,
16
+ useDocStore ,
17
+ } from '@/features/docs/doc-management/' ;
13
18
import { MainLayout } from '@/layouts' ;
14
19
import { useBroadcastStore } from '@/stores' ;
15
20
import { NextPageWithLayout } from '@/types/next' ;
@@ -41,14 +46,25 @@ interface DocProps {
41
46
42
47
const DocPage = ( { id } : DocProps ) => {
43
48
const { login } = useAuthStore ( ) ;
44
- const { data : docQuery , isError, error } = useDoc ( { id } ) ;
45
- const [ doc , setDoc ] = useState ( docQuery ) ;
46
- const { setCurrentDoc, createProvider, providers } = useDocStore ( ) ;
47
- const { setBroadcastProvider, addTask } = useBroadcastStore ( ) ;
49
+ const {
50
+ data : docQuery ,
51
+ isError,
52
+ isFetching,
53
+ error,
54
+ } = useDoc (
55
+ { id } ,
56
+ {
57
+ staleTime : 0 ,
58
+ queryKey : [ KEY_DOC , { id } ] ,
59
+ } ,
60
+ ) ;
61
+
62
+ const [ doc , setDoc ] = useState < Doc > ( ) ;
63
+ const { setCurrentDoc } = useDocStore ( ) ;
64
+ const { addTask } = useBroadcastStore ( ) ;
48
65
const queryClient = useQueryClient ( ) ;
49
66
const { replace } = useRouter ( ) ;
50
- const provider = providers ?. [ id ] ;
51
- const collaborationUrl = useCollaborationUrl ( doc ?. id ) ;
67
+ useCollaboration ( doc ?. id , doc ?. content ) ;
52
68
53
69
useEffect ( ( ) => {
54
70
if ( doc ?. title ) {
@@ -59,26 +75,13 @@ const DocPage = ({ id }: DocProps) => {
59
75
} , [ doc ?. title ] ) ;
60
76
61
77
useEffect ( ( ) => {
62
- if ( ! docQuery ) {
78
+ if ( ! docQuery || isFetching ) {
63
79
return ;
64
80
}
65
81
66
82
setDoc ( docQuery ) ;
67
83
setCurrentDoc ( docQuery ) ;
68
- } , [ docQuery , setCurrentDoc ] ) ;
69
-
70
- useEffect ( ( ) => {
71
- if ( ! doc ?. id || ! collaborationUrl ) {
72
- return ;
73
- }
74
-
75
- let newProvider = provider ;
76
- if ( ! provider || provider . document . guid !== doc . id ) {
77
- newProvider = createProvider ( collaborationUrl , doc . id , doc . content ) ;
78
- }
79
-
80
- setBroadcastProvider ( newProvider ) ;
81
- } , [ createProvider , doc , provider , setBroadcastProvider , collaborationUrl ] ) ;
84
+ } , [ docQuery , setCurrentDoc , isFetching ] ) ;
82
85
83
86
/**
84
87
* We add a broadcast task to reset the query cache
0 commit comments