-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(comments): lost comment message while document is reconnecting
- Loading branch information
1 parent
3afe5a2
commit 542257c
Showing
10 changed files
with
151 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
.../sanity/src/structure/comments/src/context/authoring-path/CommentsAuthoringPathContext.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {createContext} from 'react' | ||
|
||
import {type CommentsAuthoringPathContextValue} from './types' | ||
|
||
/** | ||
* @beta | ||
* @hidden | ||
*/ | ||
export const CommentsAuthoringPathContext = createContext<CommentsAuthoringPathContextValue | null>( | ||
null, | ||
) |
41 changes: 41 additions & 0 deletions
41
...anity/src/structure/comments/src/context/authoring-path/CommentsAuthoringPathProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {type ReactNode, useCallback, useMemo, useState} from 'react' | ||
|
||
import {CommentsAuthoringPathContext} from './CommentsAuthoringPathContext' | ||
import {type CommentsAuthoringPathContextValue} from './types' | ||
|
||
interface CommentsAuthoringPathProviderProps { | ||
children: ReactNode | ||
} | ||
|
||
/** | ||
* @beta | ||
* @hidden | ||
* This provider keeps track of the path that the user is currently authoring a comment for. | ||
* This is needed to make sure that we consistently keep the editor open when the user is | ||
* authoring a comment. The state is kept in a context to make sure that it is preserved | ||
* across re-renders. If this state was kept in a component, it would be reset every time | ||
* the component re-renders, for example, when the form is temporarily set to `readOnly` | ||
* while reconnecting. | ||
*/ | ||
export function CommentsAuthoringPathProvider(props: CommentsAuthoringPathProviderProps) { | ||
const {children} = props | ||
const [authoringPath, setAuthoringPath] = useState<string | null>(null) | ||
|
||
const handleSetAuthoringPath = useCallback((nextAuthoringPath: string | null) => { | ||
setAuthoringPath(nextAuthoringPath) | ||
}, []) | ||
|
||
const value = useMemo( | ||
(): CommentsAuthoringPathContextValue => ({ | ||
authoringPath, | ||
setAuthoringPath: handleSetAuthoringPath, | ||
}), | ||
[authoringPath, handleSetAuthoringPath], | ||
) | ||
|
||
return ( | ||
<CommentsAuthoringPathContext.Provider value={value}> | ||
{children} | ||
</CommentsAuthoringPathContext.Provider> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/sanity/src/structure/comments/src/context/authoring-path/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './CommentsAuthoringPathContext' | ||
export * from './CommentsAuthoringPathProvider' | ||
export * from './types' |
8 changes: 8 additions & 0 deletions
8
packages/sanity/src/structure/comments/src/context/authoring-path/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* @beta | ||
* @hidden | ||
*/ | ||
export interface CommentsAuthoringPathContextValue { | ||
setAuthoringPath: (nextAuthoringPath: string | null) => void | ||
authoringPath: string | null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './authoring-path' | ||
export * from './comments' | ||
export * from './enabled' | ||
export * from './intent' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
packages/sanity/src/structure/comments/src/hooks/useCommentsAuthoringPath.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {useContext} from 'react' | ||
|
||
import {CommentsAuthoringPathContext, type CommentsAuthoringPathContextValue} from '../context' | ||
|
||
/** | ||
* @beta | ||
* @hidden | ||
*/ | ||
export function useCommentsAuthoringPath(): CommentsAuthoringPathContextValue { | ||
const value = useContext(CommentsAuthoringPathContext) | ||
|
||
if (!value) { | ||
throw new Error('useCommentsAuthoringPath: missing context value') | ||
} | ||
|
||
return value | ||
} |