Skip to content

Commit

Permalink
refactor: setup when creating the first comment
Browse files Browse the repository at this point in the history
  • Loading branch information
hermanwikner committed Oct 25, 2023
1 parent d6e5b99 commit f4583c6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -314,6 +294,7 @@ const CommentsProviderInner = memo(function CommentsProviderInner(
schemaType,
workspaceName,
getThreadLength,
runSetup,
handleOnCreate,
handleOnCreateError,
handleOnEdit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export interface CommentOperationsHookOptions {
onEdit?: (id: string, comment: CommentEditPayload) => void
onRemove?: (id: string) => void
onUpdate?: (id: string, comment: Partial<CommentCreatePayload>) => void

runSetup: (comment: CommentPostPayload) => Promise<void>
}

export function useCommentOperations(
Expand All @@ -53,6 +55,7 @@ export function useCommentOperations(
onRemove,
onUpdate,
projectId,
runSetup,
workspace,
} = opts

Expand Down Expand Up @@ -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
}
},
Expand All @@ -154,6 +165,7 @@ export function useCommentOperations(
onCreate,
onCreateError,
projectId,
runSetup,
workspace,
],
)
Expand Down

0 comments on commit f4583c6

Please sign in to comment.