MCPcopy Create free account
hub / github.com/dailydotdev/apps / useMutateComment

Function useMutateComment

packages/shared/src/hooks/post/useMutateComment.ts:58–231  ·  view source on GitHub ↗
({
  post,
  editCommentId,
  parentCommentId,
  onCommented,
}: UseMutateCommentProps)

Source from the content-addressed store, hash-verified

56}
57
58export 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,

Callers 2

CommentInputFunction · 0.90

Calls 8

useAuthContextFunction · 0.90
useRequestProtocolFunction · 0.90
useLogContextFunction · 0.90
useToastNotificationFunction · 0.90
generateQueryKeyFunction · 0.90
useBackgroundRequestFunction · 0.90
displayToastFunction · 0.85
onSuccessFunction · 0.70

Tested by

no test coverage detected