Skip to content

Commit 204ae21

Browse files
ButteryCrumpetRokt33r
authored andcommitted
move comment thread control up and trigger comment sidebar on location change
1 parent 87359a7 commit 204ae21

File tree

6 files changed

+42
-56
lines changed

6 files changed

+42
-56
lines changed

src/cloud/components/molecules/Editor/index.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ import { getDocLinkHref } from '../../atoms/Link/DocLink'
9191
import throttle from 'lodash.throttle'
9292
import { useI18n } from '../../../lib/hooks/useI18n'
9393
import { lngKeys } from '../../../lib/i18n/types'
94+
import { parse } from 'querystring'
9495

9596
type LayoutMode = 'split' | 'preview' | 'editor'
9697

@@ -101,7 +102,6 @@ interface EditorProps {
101102
revisionHistory: SerializedRevision[]
102103
contributors: SerializedUser[]
103104
backLinks: SerializedDoc[]
104-
thread?: string
105105
}
106106

107107
interface EditorPosition {
@@ -124,7 +124,6 @@ const Editor = ({
124124
contributors,
125125
backLinks,
126126
revisionHistory,
127-
thread,
128127
}: EditorProps) => {
129128
const { translate } = useI18n()
130129
const { currentUserPermissions, permissions } = usePage()
@@ -141,7 +140,8 @@ const Editor = ({
141140
)
142141
const [editorContent, setEditorContent] = useState('')
143142
const docRef = useRef<string>('')
144-
const { state, push } = useRouter()
143+
const router = useRouter()
144+
const { state, push } = router
145145
const [shortcodeConvertMenu, setShortcodeConvertMenu] = useState<{
146146
pos: PositionRange
147147
cb: Callback
@@ -195,7 +195,16 @@ const Editor = ({
195195
userInfo,
196196
})
197197

198-
const [commentState, commentActions] = useCommentManagerState(doc.id, thread)
198+
const [commentState, commentActions] = useCommentManagerState(doc.id)
199+
200+
useEffect(() => {
201+
const { thread } = parse(router.search.slice(1))
202+
const threadId = Array.isArray(thread) ? thread[0] : thread
203+
if (threadId != null) {
204+
commentActions.setMode({ mode: 'thread', thread: { id: threadId } })
205+
setPreferences({ docContextMode: 'comment' })
206+
}
207+
}, [router, commentActions, setPreferences])
199208

200209
const normalizedCommentState = useMemo(() => {
201210
if (commentState.mode === 'list_loading' || permissions == null) {

src/cloud/components/organisms/CommentManager.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { useI18n } from '../../lib/hooks/useI18n'
2020
import { lngKeys } from '../../lib/i18n/types'
2121

2222
export type State =
23-
| { mode: 'list_loading' }
23+
| { mode: 'list_loading'; thread?: { id: string } }
2424
| { mode: 'list'; threads: Thread[]; filter?: (thread: Thread) => boolean }
2525
| { mode: 'thread_loading'; thread: Thread; threads: Thread[] }
2626
| { mode: 'thread'; thread: Thread; comments: Comment[]; threads: Thread[] }
@@ -31,7 +31,7 @@ export type State =
3131
}
3232

3333
export type ModeTransition =
34-
| { mode: 'thread'; thread: Thread }
34+
| { mode: 'thread'; thread: { id: string } }
3535
| { mode: 'list'; filter?: (thread: Thread) => boolean }
3636
| {
3737
mode: 'new_thread'

src/cloud/components/organisms/DocPage/Edit.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ interface EditPageProps {
1717
contributors: SerializedUser[]
1818
revisionHistory: SerializedRevision[]
1919
backLinks: SerializedDoc[]
20-
thread?: string
2120
}
2221

2322
const EditPage = ({
@@ -27,7 +26,6 @@ const EditPage = ({
2726
contributors,
2827
revisionHistory,
2928
backLinks,
30-
thread,
3129
}: EditPageProps) => {
3230
return (
3331
<StyledDocEditPage>
@@ -38,7 +36,6 @@ const EditPage = ({
3836
contributors={contributors}
3937
backLinks={backLinks}
4038
revisionHistory={revisionHistory}
41-
thread={thread}
4239
/>
4340
</StyledDocEditPage>
4441
)

src/cloud/components/organisms/DocPage/View.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import { TopbarControlProps } from '../../../../shared/components/organisms/Topb
4646
import { getDocLinkHref } from '../../atoms/Link/DocLink'
4747
import { useI18n } from '../../../lib/hooks/useI18n'
4848
import { lngKeys } from '../../../lib/i18n/types'
49+
import { parse } from 'querystring'
4950

5051
interface ViewPageProps {
5152
team: SerializedTeam
@@ -68,7 +69,8 @@ const ViewPage = ({
6869
}: ViewPageProps) => {
6970
const { preferences, setPreferences } = usePreferences()
7071
const { foldersMap, workspacesMap, loadDoc } = useNav()
71-
const { push } = useRouter()
72+
const router = useRouter()
73+
const { push } = router
7274
const { currentUserIsCoreMember, permissions } = usePage()
7375
const { sendingMap, toggleDocBookmark } = useCloudApi()
7476
const {
@@ -119,6 +121,15 @@ const ViewPage = ({
119121

120122
const [commentState, commentActions] = useCommentManagerState(doc.id)
121123

124+
useEffect(() => {
125+
const { thread } = parse(router.search.slice(1))
126+
const threadId = Array.isArray(thread) ? thread[0] : thread
127+
if (threadId != null) {
128+
commentActions.setMode({ mode: 'thread', thread: { id: threadId } })
129+
setPreferences({ docContextMode: 'comment' })
130+
}
131+
}, [router, commentActions, setPreferences])
132+
122133
const normalizedCommentState = useMemo(() => {
123134
if (commentState.mode === 'list_loading' || permissions == null) {
124135
return commentState

src/cloud/components/organisms/DocPage/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import Application from '../../Application'
2222
import { SerializedUser } from '../../../interfaces/db/user'
2323
import { SerializedRevision } from '../../../interfaces/db/revision'
2424
import { useRouter } from '../../../lib/router'
25+
import { parse } from 'querystring'
2526

2627
interface DocPageProps {
2728
doc: SerializedDocWithBookmark
@@ -36,7 +37,6 @@ const DocPage = ({
3637
contributors,
3738
backLinks,
3839
revisionHistory,
39-
thread,
4040
}: DocPageProps) => {
4141
const {
4242
team,
@@ -164,7 +164,6 @@ const DocPage = ({
164164
contributors={contributors}
165165
backLinks={currentBacklinks}
166166
revisionHistory={revisionHistory}
167-
thread={thread}
168167
/>
169168
) : (
170169
<ViewPage

src/cloud/lib/hooks/useCommentManagerState.ts

Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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(
12181
function 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[]) {
166133
function 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

Comments
 (0)