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

Function useCommentById

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

Source from the content-addressed store, hash-verified

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

Callers 2

CommentModalFunction · 0.85
useEditCommentPropsFunction · 0.85

Calls 3

useAuthContextFunction · 0.90
useRequestProtocolFunction · 0.90
generateQueryKeyFunction · 0.90

Tested by

no test coverage detected