(post: Post)
| 23 | ): string | undefined => (username ? `@${username}\u00a0` : undefined); |
| 24 | |
| 25 | export 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 | }; |
no test coverage detected