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

Function useComments

packages/shared/src/hooks/post/useComments.ts:25–78  ·  view source on GitHub ↗
(post: Post)

Source from the content-addressed store, hash-verified

23): string | undefined => (username ? `@${username}\u00a0` : undefined);
24
25export const useComments = (post: Post): UseComments => {
26 const { logEvent } = useLogContext();
27 const { logOpts } = useActiveFeedContext();
28 const { user, showLogin } = useAuthContext();
29 const [replyTo, setReplyTo] = useState<ReplyTo | null>(null);
30
31 const inputProps = useMemo(() => {
32 if (!replyTo) {
33 return null;
34 }
35
36 const { username, parentCommentId } = replyTo ?? {};
37
38 return {
39 ...(parentCommentId ? { parentCommentId } : {}),
40 ...(username ? { replyTo: username } : {}),
41 initialContent: getReplyToInitialContent(username ?? undefined),
42 };
43 }, [replyTo]);
44
45 const onReplyTo = useCallback(
46 (params: ReplyTo | null) => {
47 if (!user) {
48 return showLogin({ trigger: AuthTriggers.Comment });
49 }
50
51 if (!isNullOrUndefined(params)) {
52 logEvent(
53 postLogEvent(LogEvent.OpenComment, post, {
54 extra: { origin: Origin.PostCommentButton },
55 ...(logOpts && logOpts),
56 }),
57 );
58 }
59
60 if (!params) {
61 return setReplyTo(null);
62 }
63
64 if (params.username === user.username) {
65 return setReplyTo({ ...params, username: null });
66 }
67
68 return setReplyTo(params);
69 },
70 [user, showLogin, logEvent, post, logOpts],
71 );
72
73 return {
74 onReplyTo,
75 inputProps,
76 commentId: replyTo?.commentId ?? null,
77 };
78};

Callers 2

SubCommentFunction · 0.90
MainCommentFunction · 0.90

Calls 7

useLogContextFunction · 0.90
useActiveFeedContextFunction · 0.90
useAuthContextFunction · 0.90
isNullOrUndefinedFunction · 0.90
postLogEventFunction · 0.90
getReplyToInitialContentFunction · 0.85
logEventFunction · 0.85

Tested by

no test coverage detected