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

Function removePostComments

packages/shared/src/hooks/usePostById.ts:52–92  ·  view source on GitHub ↗
(
  client: QueryClient,
  post: Post,
  commentId: string,
  parentId: string,
)

Source from the content-addressed store, hash-verified

50};
51
52export const removePostComments = (
53 client: QueryClient,
54 post: Post,
55 commentId: string,
56 parentId: string,
57): void => {
58 const keys = getAllCommentsQuery(post.id);
59 const removeCachedComment = (data: PostCommentsData) => {
60 if (!data) {
61 return data;
62 }
63 const copy = { ...data };
64
65 if (commentId !== parentId) {
66 const parent = copy.postComments.edges.find(
67 ({ node }) => node.id === parentId,
68 );
69 parent.node.children.edges = parent.node.children.edges.filter(
70 ({ node }) => node.id !== commentId,
71 );
72 updatePostCache(client, post.id, { numComments: post.numComments - 1 });
73 return copy;
74 }
75
76 const parent = copy.postComments.edges.find(
77 ({ node }) => node.id === commentId,
78 );
79 const count = parent.node.children.edges.length + 1;
80 const numComments = post.numComments - count;
81 updatePostCache(client, post.id, { numComments });
82 copy.postComments.edges = data.postComments.edges.filter(
83 ({ node }) => node.id !== commentId,
84 );
85
86 return data;
87 };
88
89 keys.forEach((key) => {
90 client.setQueryData(key, removeCachedComment);
91 });
92};
93
94export const usePostById = ({
95 id,

Callers 1

useDeleteCommentFunction · 0.90

Calls 1

getAllCommentsQueryFunction · 0.90

Tested by

no test coverage detected