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

Function onSuccess

packages/shared/src/hooks/post/useMutateComment.ts:95–175  ·  view source on GitHub ↗
(comment: Comment)

Source from the content-addressed store, hash-verified

93 );
94
95 const onSuccess = (comment: Comment) => {
96 if (!comment) {
97 return;
98 }
99
100 const updateQueryData = (
101 data: PostCommentsData,
102 ): PostCommentsData | undefined => {
103 const copy = structuredClone(data);
104 const { edges } = copy.postComments;
105
106 if (!editCommentId) {
107 const edge = generateCommentEdge(comment);
108
109 if (!parentCommentId) {
110 edges.unshift(edge);
111 return copy;
112 }
113
114 const parent = edges.find(({ node }) => node.id === parentCommentId);
115
116 if (!parent) {
117 return undefined;
118 }
119
120 parent.node.children.edges.push(edge);
121 return copy;
122 }
123
124 const replies = edges.flatMap(({ node }) => node.children?.edges ?? []);
125 const edited = [...edges, ...replies].find(
126 ({ node }) => node.id === editCommentId,
127 );
128
129 if (!edited) {
130 return undefined;
131 }
132
133 edited.node = { ...comment, children: edited.node.children };
134 return copy;
135 };
136
137 const forInvalidation: QueryKey[] = [];
138
139 getAllCommentsQuery(postId).forEach((queryKey) => {
140 client.setQueryData<PostCommentsData>(queryKey, (data) => {
141 if (!data) {
142 forInvalidation.push(queryKey);
143 return null;
144 }
145
146 const updated = updateQueryData(data);
147
148 if (!updated) {
149 forInvalidation.push(queryKey);
150 return data;
151 }
152

Callers 4

useMutateCommentFunction · 0.70
useProfileFormFunction · 0.50
useSquadCreateFunction · 0.50
useSourcePostModerationFunction · 0.50

Calls 7

getAllCommentsQueryFunction · 0.90
updatePostCacheFunction · 0.90
postLogEventFunction · 0.90
logEventFunction · 0.85
pushMethod · 0.80
updateQueryDataFunction · 0.70
onCommentedFunction · 0.50

Tested by

no test coverage detected