({
post,
editCommentId,
parentCommentId,
onCommented,
}: UseMutateCommentProps)
| 56 | } |
| 57 | |
| 58 | export const useMutateComment = ({ |
| 59 | post, |
| 60 | editCommentId, |
| 61 | parentCommentId, |
| 62 | onCommented, |
| 63 | }: UseMutateCommentProps): UseMutateCommentResult => { |
| 64 | const sourceId = post?.source?.id; |
| 65 | const postId = post?.id; |
| 66 | const parentOrPostId = parentCommentId ?? postId; |
| 67 | |
| 68 | const { user } = useAuthContext(); |
| 69 | const client = useQueryClient(); |
| 70 | const { requestMethod, isCompanion } = useRequestProtocol(); |
| 71 | const { logEvent } = useLogContext(); |
| 72 | const { logOpts } = useContext(ActiveFeedContext); |
| 73 | const { displayToast } = useToastNotification(); |
| 74 | |
| 75 | const onError = useCallback( |
| 76 | (error: unknown) => { |
| 77 | const message = (error as ApiErrorResult)?.response?.errors?.[0]?.message; |
| 78 | displayToast(message ?? DEFAULT_ERROR); |
| 79 | }, |
| 80 | [displayToast], |
| 81 | ); |
| 82 | |
| 83 | const key = useMemo( |
| 84 | () => |
| 85 | generateQueryKey( |
| 86 | RequestKey.PostCommentsMutations, |
| 87 | user, |
| 88 | ...[postId, sourceId, editCommentId, parentCommentId].filter( |
| 89 | (value) => !!value, |
| 90 | ), |
| 91 | ), |
| 92 | [user, postId, sourceId, editCommentId, parentCommentId], |
| 93 | ); |
| 94 | |
| 95 | const onSuccess = (comment: Comment) => { |
| 96 | if (!comment) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | const updateQueryData = (data: PostCommentsData) => { |
| 101 | if (!data) { |
| 102 | return data; |
| 103 | } |
| 104 | const copy = structuredClone(data); |
| 105 | |
| 106 | if (!editCommentId) { |
| 107 | const edge = generateCommentEdge(comment); |
| 108 | |
| 109 | if (!parentCommentId) { |
| 110 | copy.postComments.edges.unshift(edge); |
| 111 | return copy; |
| 112 | } |
| 113 | |
| 114 | const index = copy.postComments.edges.findIndex( |
| 115 | ({ node }) => node.id === parentCommentId, |
no test coverage detected