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

Function useCommentById

packages/shared/src/hooks/comments/useCommentById.ts:27–68  ·  view source on GitHub ↗
({
  id,
  query = COMMENT_BY_ID_QUERY,
  options = {},
}: UseCommentByIdProps)

Source from the content-addressed store, hash-verified

25}
26
27const useCommentById = ({
28 id,
29 query = COMMENT_BY_ID_QUERY,
30 options = {},
31}: UseCommentByIdProps): UseCommentByIdResult => {
32 const { user } = useAuthContext();
33 const { requestMethod } = useRequestProtocol();
34 const client = useQueryClient();
35
36 if (!requestMethod) {
37 throw new Error('Request method is required in useCommentById');
38 }
39
40 const {
41 data: commentById,
42 isError,
43 isLoading,
44 } = useQuery<CommentOnData>({
45 queryKey: generateQueryKey(RequestKey.Comment, user, id),
46 queryFn: () => requestMethod(query, { id: `${id}` }),
47 ...options,
48 enabled: !!id && options.enabled,
49 });
50 const comment = commentById || (options?.initialData as CommentOnData);
51
52 const invalidate = useCallback(() => {
53 client.invalidateQueries({
54 queryKey: generateQueryKey(RequestKey.Comment, user, id),
55 exact: true,
56 });
57 }, [client, user, id]);
58
59 return useMemo(
60 () => ({
61 comment: comment?.comment,
62 onEdit: invalidate,
63 isError,
64 isLoading,
65 }),
66 [comment, isError, isLoading, invalidate],
67 );
68};
69
70export default useCommentById;

Callers 1

useEditCommentPropsFunction · 0.85

Calls 4

useAuthContextFunction · 0.90
useRequestProtocolFunction · 0.90
generateQueryKeyFunction · 0.90
requestMethodFunction · 0.85

Tested by

no test coverage detected