From f4583c6df4b38827111c0dce44f8325f0d0e4689 Mon Sep 17 00:00:00 2001 From: Herman Wikner Date: Wed, 25 Oct 2023 16:22:18 +0200 Subject: [PATCH] refactor: setup when creating the first comment --- .../src/context/comments/CommentsProvider.tsx | 31 ++++--------------- .../src/hooks/useCommentOperations.ts | 22 ++++++++++--- 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/packages/sanity/src/desk/comments/src/context/comments/CommentsProvider.tsx b/packages/sanity/src/desk/comments/src/context/comments/CommentsProvider.tsx index 0638ef1ec27b..ee7b6fb6eca9 100644 --- a/packages/sanity/src/desk/comments/src/context/comments/CommentsProvider.tsx +++ b/packages/sanity/src/desk/comments/src/context/comments/CommentsProvider.tsx @@ -207,32 +207,8 @@ const CommentsProviderInner = memo(function CommentsProviderInner( _state: hasError ? {type: 'createRetrying'} : undefined, }, }) - - // If we don't have a client, that means that the dataset doesn't have an addon dataset. - // Therefore, when the first comment is created, we need to create the addon dataset and create - // a client for it and then post the comment. We do this here, since we know that we have a - // comment to create. - if (!client) { - try { - await runSetup(payload) - } catch (err) { - // If we fail to create the addon dataset, we update the comment state to `createError`. - // This will make the comment appear in the UI as a comment that failed to be created. - // The user can then retry the comment creation. - dispatch({ - type: 'COMMENT_UPDATED', - payload: { - _id: payload._id, - _state: { - error: err, - type: 'createError', - }, - }, - }) - } - } }, - [client, data, dispatch, runSetup], + [data, dispatch], ) const handleOnUpdate = useCallback( @@ -293,6 +269,10 @@ const CommentsProviderInner = memo(function CommentsProviderInner( schemaType, workspace: workspaceName, getThreadLength, + // This function runs when the first comment creation is executed. + // It is used to create the addon dataset and configure a client for + // the addon dataset. + runSetup, // The following callbacks runs when the comment operations are executed. // They are used to update the local state of the comments immediately after // a comment operation has been executed. This is done to avoid waiting for @@ -314,6 +294,7 @@ const CommentsProviderInner = memo(function CommentsProviderInner( schemaType, workspaceName, getThreadLength, + runSetup, handleOnCreate, handleOnCreateError, handleOnEdit, diff --git a/packages/sanity/src/desk/comments/src/hooks/useCommentOperations.ts b/packages/sanity/src/desk/comments/src/hooks/useCommentOperations.ts index e916135c1701..2241d1a6f027 100644 --- a/packages/sanity/src/desk/comments/src/hooks/useCommentOperations.ts +++ b/packages/sanity/src/desk/comments/src/hooks/useCommentOperations.ts @@ -35,6 +35,8 @@ export interface CommentOperationsHookOptions { onEdit?: (id: string, comment: CommentEditPayload) => void onRemove?: (id: string) => void onUpdate?: (id: string, comment: Partial) => void + + runSetup: (comment: CommentPostPayload) => Promise } export function useCommentOperations( @@ -53,6 +55,7 @@ export function useCommentOperations( onRemove, onUpdate, projectId, + runSetup, workspace, } = opts @@ -127,18 +130,26 @@ export function useCommentOperations( }, } satisfies CommentPostPayload - // We still want to run the `onCreate` callback even if the client is not defined. - // This is because, if this is the first comment being created, we'll want to - // handle creation of the client in the `CommentsProvider` and then post the comment. onCreate?.(nextComment) - if (!client) return + // If we don't have a client, that means that the dataset doesn't have an addon dataset. + // Therefore, when the first comment is created, we need to create the addon dataset and create + // a client for it and then post the comment. We do this here, since we know that we have a + // comment to create. + if (!client) { + try { + await runSetup(nextComment) + } catch (err) { + onCreateError?.(nextComment._id, err) + throw err + } + return + } try { await client.create(nextComment) } catch (err) { onCreateError?.(nextComment._id, err) - throw err } }, @@ -154,6 +165,7 @@ export function useCommentOperations( onCreate, onCreateError, projectId, + runSetup, workspace, ], )