Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: comments create error handling #4978

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions dev/test-studio/schema/debug/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ export const commentsDebug = defineType({
},
],
},
{
name: 'image',
type: 'image',
title: 'Image title',
hidden: ({document}) => Boolean(document?.hideFields),
description: DESCRIPTION,
},
{
type: 'array',
name: 'arrayOfObjects',
Expand Down Expand Up @@ -111,5 +104,12 @@ export const commentsDebug = defineType({
},
],
},
{
name: 'image',
type: 'image',
title: 'Image title',
hidden: ({document}) => Boolean(document?.hideFields),
description: DESCRIPTION,
},
],
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import React from 'react'
import {useAction, useBoolean} from '@sanity/ui-workshop'
import {CommentDeleteDialog} from '../components'

export default function CommentDeleteDialogStory() {
const isParent = useBoolean('Is parent', false, 'Props') || false
const error = useBoolean('Error', false, 'Props') || false
const loading = useBoolean('Loading', false, 'Props') || false

return (
<CommentDeleteDialog
commentId="123"
error={error ? new Error('Something went wrong') : null}
isParent={isParent}
loading={loading}
onClose={useAction('onClose')}
onConfirm={useAction('onConfirm')}
/>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ export default function CommentsListStory() {
onEdit={handleEdit}
onReply={handleReplySubmit}
status={status}
onCreateRetry={() => {
// ...
}}
onNewThreadCreate={() => {
// ...
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ function Inner() {
onNewThreadCreate={create.execute}
onReply={create.execute}
status="open"
onCreateRetry={() => {
// ...
}}
/>
)
}
5 changes: 5 additions & 0 deletions packages/sanity/src/core/comments/__workshop__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,10 @@ export default defineScope({
title: 'MentionsMenu',
component: lazy(() => import('./MentionsMenuStory')),
},
{
name: 'comment-delete-dialog',
title: 'CommentDeleteDialog',
component: lazy(() => import('./CommentDeleteDialogStory')),
},
],
})
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Dialog, Grid, Button, Stack, Text} from '@sanity/ui'
import React, {useCallback} from 'react'
import {TextWithTone} from '../../components'

const DIALOG_COPY: Record<
'thread' | 'comment',
Expand Down Expand Up @@ -60,10 +61,14 @@ export function CommentDeleteDialog(props: CommentDeleteDialogProps) {
</Grid>
}
>
{error && <span>Something went wrong</span>}

<Stack padding={4}>
<Stack padding={4} space={4}>
<Text>{body}</Text>

{error && (
<TextWithTone tone="critical">
An error occurred while deleting the comment. Please try again.
</TextWithTone>
)}
</Stack>
</Dialog>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import React from 'react'

export const AVATAR_HEIGHT = 25

const INLINE_STYLE: React.CSSProperties = {
minWidth: 25,
minWidth: AVATAR_HEIGHT,
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function CommentThreadLayout(props: CommentThreadLayoutProps) {
} = props
const createNewThreadInputRef = useRef<CommentInputHandle>(null)
const [displayNewThreadInput, setDisplayNewThreadInput] = useState<boolean>(false)
const [newThreadButtonElement, setNewThreadButtonElement] = useState<HTMLButtonElement | null>(
null,
)

const onCreateNewThreadClick = useCallback(() => {
setDisplayNewThreadInput(true)
Expand Down Expand Up @@ -75,8 +78,11 @@ export function CommentThreadLayout(props: CommentThreadLayoutProps) {

onNewThreadCreate?.(nextComment)
setDisplayNewThreadInput(false)

// When the new thread is created, we focus the button again
newThreadButtonElement?.focus()
},
[onNewThreadCreate, path],
[newThreadButtonElement, onNewThreadCreate, path],
)

return (
Expand Down Expand Up @@ -114,6 +120,7 @@ export function CommentThreadLayout(props: CommentThreadLayoutProps) {
mode="bleed"
onClick={onCreateNewThreadClick}
padding={2}
ref={setNewThreadButtonElement}
/>
</TextTooltip>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface CommentsListProps {
error: Error | null
loading: boolean
mentionOptions: MentionOptionsHookValue
onCreateRetry: (id: string) => void
onDelete: (id: string) => void
onEdit: (id: string, payload: CommentEditPayload) => void
onNewThreadCreate: (payload: CommentCreatePayload) => void
Expand Down Expand Up @@ -78,6 +79,7 @@ export const CommentsList = forwardRef<CommentsListHandle, CommentsListProps>(fu
error,
loading,
mentionOptions,
onCreateRetry,
onDelete,
onEdit,
onNewThreadCreate,
Expand Down Expand Up @@ -202,12 +204,18 @@ export const CommentsList = forwardRef<CommentsListHandle, CommentsListProps>(fu
// We use slice() to avoid mutating the original array.
const replies = item.replies.slice().reverse()

const canReply =
status === 'open' &&
item.parentComment._state?.type !== 'createError' &&
item.parentComment._state?.type !== 'createRetrying'

return (
<CommentsListItem
canReply={status === 'open'}
canReply={canReply}
currentUser={currentUser}
key={item.parentComment._id}
mentionOptions={mentionOptions}
onCreateRetry={onCreateRetry}
onDelete={onDelete}
onEdit={onEdit}
onPathFocus={onPathFocus}
Expand Down
120 changes: 66 additions & 54 deletions packages/sanity/src/core/comments/components/list/CommentsListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ interface CommentsListItemProps {
canReply?: boolean
currentUser: CurrentUser
mentionOptions: MentionOptionsHookValue
onCreateRetry: (id: string) => void
onDelete: (id: string) => void
onEdit: (id: string, payload: CommentEditPayload) => void
onPathFocus?: (path: Path) => void
Expand All @@ -60,6 +61,7 @@ export function CommentsListItem(props: CommentsListItemProps) {
canReply,
currentUser,
mentionOptions,
onCreateRetry,
onDelete,
onEdit,
onPathFocus,
Expand Down Expand Up @@ -137,67 +139,77 @@ export function CommentsListItem(props: CommentsListItemProps) {
return (
<Stack space={2}>
<StyledThreadCard>
<Stack paddingBottom={canReply ? undefined : 1}>
<Stack as="ul" space={4}>
<Stack as="li">
<Stack
as="ul"
// We add some extra padding to the bottom of the thread root if the thread is resolved
// since the reply input is hidden in that case.
paddingBottom={parentComment.status === 'resolved' ? 1 : undefined}
space={4}
>
<Stack as="li">
<CommentsListItemLayout
canDelete={parentComment.authorId === currentUser.id}
canEdit={parentComment.authorId === currentUser.id}
comment={parentComment}
currentUser={currentUser}
hasError={parentComment._state?.type === 'createError'}
isParent
isRetrying={parentComment._state?.type === 'createRetrying'}
mentionOptions={mentionOptions}
onCreateRetry={onCreateRetry}
onDelete={onDelete}
onEdit={onEdit}
onPathFocus={onPathFocus}
onStatusChange={onStatusChange}
/>
</Stack>

{showCollapseButton && !didExpand.current && (
<Flex gap={1} paddingY={1} sizing="border">
<SpacerAvatar />

<ExpandButton
fontSize={1}
iconRight={ChevronDownIcon}
mode="bleed"
onClick={handleExpand}
padding={2}
space={2}
text={expandButtonText}
/>
</Flex>
)}

{splicedReplies.map((reply) => (
<Stack as="li" key={reply._id}>
<CommentsListItemLayout
canDelete={parentComment.authorId === currentUser.id}
canEdit={parentComment.authorId === currentUser.id}
comment={parentComment}
canDelete={reply.authorId === currentUser.id}
canEdit={reply.authorId === currentUser.id}
comment={reply}
currentUser={currentUser}
isParent
hasError={reply._state?.type === 'createError'}
isRetrying={reply._state?.type === 'createRetrying'}
mentionOptions={mentionOptions}
onCreateRetry={onCreateRetry}
onDelete={onDelete}
onEdit={onEdit}
onPathFocus={onPathFocus}
onStatusChange={onStatusChange}
/>
</Stack>

{showCollapseButton && !didExpand.current && (
<Flex gap={1} paddingY={1} sizing="border">
<SpacerAvatar />

<ExpandButton
fontSize={1}
iconRight={ChevronDownIcon}
mode="bleed"
onClick={handleExpand}
padding={2}
space={2}
text={expandButtonText}
/>
</Flex>
)}

{splicedReplies.map((reply) => (
<Stack as="li" key={reply._id}>
<CommentsListItemLayout
canDelete={reply.authorId === currentUser.id}
canEdit={reply.authorId === currentUser.id}
comment={reply}
currentUser={currentUser}
mentionOptions={mentionOptions}
onDelete={onDelete}
onEdit={onEdit}
/>
</Stack>
))}

{canReply && (
<CommentInput
currentUser={currentUser}
expandOnFocus
mentionOptions={mentionOptions}
onChange={setValue}
onEditDiscard={cancelEdit}
onSubmit={handleReplySubmit}
placeholder="Reply"
ref={replyInputRef}
value={value}
/>
)}
</Stack>
))}

{canReply && (
<CommentInput
currentUser={currentUser}
expandOnFocus
mentionOptions={mentionOptions}
onChange={setValue}
onEditDiscard={cancelEdit}
onSubmit={handleReplySubmit}
placeholder="Reply"
ref={replyInputRef}
value={value}
/>
)}
</Stack>
</StyledThreadCard>
</Stack>
Expand Down
Loading
Loading