@@ -6,60 +6,20 @@ import {
66 Actions ,
77 ModeTransition ,
88} from '../../../cloud/components/organisms/CommentManager'
9- import { useRouter } from '../../../cloud/lib/router'
10- import { parse as parseQuery } from 'querystring'
119
12- function useCommentManagerState (
13- docId : string ,
14- initialThread ?: string
15- ) : [ State , Actions ] {
16- const location = useRouter ( )
10+ function useCommentManagerState ( docId : string ) : [ State , Actions ] {
1711 const {
1812 observeDocThreads,
1913 observeComments,
2014 threadActions,
2115 commentActions,
2216 } = useComments ( )
2317 const [ state , setState ] = useState < State > ( { mode : 'list_loading' } )
24- const initialThreadRef = useRef ( initialThread )
25-
26- useEffect ( ( ) => {
27- initialThreadRef . current = initialThread
28- } , [ initialThread ] )
29-
30- useEffect ( ( ) => {
31- setState ( ( prev ) => {
32- if ( prev . mode === 'list_loading' ) {
33- return prev
34- }
35-
36- const { thread : threadId } = parseQuery ( location . search . slice ( 1 ) )
37- if ( threadId == null ) {
38- return prev
39- }
40-
41- const thread = prev . threads . find ( ( thread ) => thread . id === threadId )
42- if ( thread == null ) {
43- return prev
44- }
45-
46- return transitionState ( { mode : 'thread' , thread } ) ( prev )
47- } )
48- } , [ location ] )
4918
5019 useEffect ( ( ) => {
5120 setState ( { mode : 'list_loading' } )
5221 return observeDocThreads ( docId , ( threads ) => {
5322 setState ( updateThreads ( threads ) )
54- if ( initialThreadRef . current !== '' ) {
55- setState (
56- transitionState ( {
57- mode : 'thread' ,
58- thread : { id : initialThreadRef . current } as Thread ,
59- } )
60- )
61- initialThreadRef . current = ''
62- }
6323 } )
6424 } , [ docId , observeDocThreads ] )
6525
@@ -121,12 +81,19 @@ function useCommentManagerState(
12181function updateThreads ( threads : Thread [ ] ) {
12282 return ( oldState : State ) : State => {
12383 switch ( oldState . mode ) {
124- case 'list_loading' : {
125- return { mode : 'list' , threads }
126- }
12784 case 'list' : {
12885 return { mode : 'list' , threads }
12986 }
87+ case 'list_loading' : {
88+ if ( oldState . thread == null ) {
89+ return { mode : 'list' , threads }
90+ }
91+ const threadId = oldState . thread . id
92+ const updated = threads . find ( ( thread ) => thread . id === threadId )
93+ return updated != null
94+ ? { mode : 'thread_loading' , thread : updated , threads }
95+ : { mode : 'list' , threads }
96+ }
13097 case 'thread_loading' : {
13198 const updated = threads . find (
13299 ( thread ) => thread . id === oldState . thread . id
@@ -166,6 +133,9 @@ function updateComments(comments: Comment[]) {
166133function transitionState ( transition : ModeTransition ) {
167134 return ( state : State ) : State => {
168135 if ( state . mode === 'list_loading' ) {
136+ if ( transition . mode === 'thread' ) {
137+ return { ...state , thread : transition . thread }
138+ }
169139 return state
170140 }
171141
0 commit comments