import { addCommentMutation, addTalkCommentMutation, agreeTermsMutation, blockAuthorMutation, deleteCommentMutation, getFeedQueryKey, likeCommentMutation, listBlockedQueryKey, listCommentsInfiniteOptions, listCommentsInfiniteQueryKey, listTalkCommentsInfiniteOptions, listTalkCommentsInfiniteQueryKey, meQueryKey, reportCommentMutation, unlikeCommentMutation, } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'; import { ReportReason, type CommentData, type ResCommentList } from '@nalssigak/shared/api/client/types.gen'; import { ago, loadOf } from '@nalssigak/shared/lib'; import { useInfiniteQuery, useMutation, useQueryClient, type InfiniteData } from '@tanstack/react-query'; import { useEffect, useRef, useState } from 'react'; import { Linking, Pressable, TextInput, View } from 'react-native'; import { Text } from '@/components/text'; import { useCSSVariable } from 'uniwind'; import { Avatar, Skel, StateBox } from '@/components/ui'; import { track } from '@/lib/analytics'; import { useAuth } from '@/lib/auth'; import { useRegion } from '@/lib/region'; import { useSheet, type CommentTarget } from '@/lib/sheet'; import { SITE_URL } from '@/lib/site'; import { Body, ErrorLine, Field, HeartIcon, useSoft } from './parts'; const NOT_FOUND = 3001; // ์„œ๋ฒ„ ErrorType.NOT_FOUND const EMOJIS = ['โ˜€๏ธ', '๐ŸŒ‚', '๐Ÿป', '๐ŸŒ‡', '๐Ÿ˜', '๐Ÿ‘']; const N3 = [0, 1, 2]; const REPORT_REASONS = [ [ReportReason.ABUSE, '์š•์„คยท๋น„ํ•˜'], [ReportReason.AD, '๊ด‘๊ณ '], [ReportReason.PRIVACY, '๊ฐœ์ธ์ •๋ณด ๋…ธ์ถœ'], [ReportReason.OTHER, '๊ธฐํƒ€'], ] as const; export const isCommentList = (q: { queryKey: readonly unknown[] }) => ['listComments', 'listTalkComments'].includes((q.queryKey[0] as { _id?: string } | undefined)?._id ?? ''); const isTalkList = (q: { queryKey: readonly unknown[] }) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'listTalks'; /** ๋Œ“๊ธ€ ๋ชฉ๋ก ์บ์‹œ ํ‚ค: ์ง€์šฐ๊ธฐยท์‹ ๊ณ  ๋’ค ๋‹ค์‹œ ๋ฐ›์„ ๋•Œ ์“ด๋‹ค */ function commentListKey(to: CommentTarget) { return 'talkId' in to ? listTalkCommentsInfiniteQueryKey({ path: { talk_id: to.talkId } }) : listCommentsInfiniteQueryKey({ path: { post_id: to.postId } }); } function Chip({ label, onPress, disabled, danger }: { label: string; onPress: () => void; disabled?: boolean; danger?: boolean }) { const s = useSoft(); return ( {label} ); } /** ๋‚จ์˜ ๋Œ“๊ธ€: โ‹ฏ โ†’ ์‹ ๊ณ ํ•˜๊ธฐ(์ด์œ  ๊ณ ๋ฅด๊ธฐ) / ์ด ์‚ฌ๋žŒ ์ฐจ๋‹จํ•˜๊ธฐ(ํ•œ ๋ฒˆ ๋” ๋ฌป๊ธฐ). ๋๋‚˜๋ฉด ๋ชฉ๋ก์„ ๋‹ค์‹œ ๋ฐ›์•„ ๊ทธ ๋Œ“๊ธ€์ด ๋น ์ง„๋‹ค */ function CommentMenu({ c, to, onNotice }: { c: CommentData; to: CommentTarget; onNotice: (text: string) => void }) { const { loggedIn } = useAuth(); const { open } = useSheet(); const queryClient = useQueryClient(); const [step, setStep] = useState<'closed' | 'menu' | 'report' | 'block'>('closed'); const [failed, setFailed] = useState(false); const report = useMutation(reportCommentMutation()); const block = useMutation(blockAuthorMutation()); const pending = report.isPending || block.isPending; const finish = (notice: string, done: () => void) => ({ onSuccess: (res: { result?: { success?: boolean } }) => { if (!res.result?.success) return setFailed(true); done(); setStep('closed'); onNotice(notice); queryClient.invalidateQueries({ predicate: isCommentList }); queryClient.invalidateQueries({ queryKey: listBlockedQueryKey() }); }, onError: () => setFailed(true), }); const go = (next: typeof step) => { setFailed(false); if (next !== 'closed' && !loggedIn) return open({ type: 'login', then: { type: 'comments', ...to } }); setStep(next); }; return ( <> go(step === 'closed' ? 'menu' : 'closed')} hitSlop={10} accessibilityRole="button" accessibilityState={{ expanded: step !== 'closed' }} accessibilityLabel="๋Œ“๊ธ€ ๋ฉ”๋‰ด" className="px-1"> โ‹ฏ {step !== 'closed' && ( {step === 'menu' && ( <> go('report')} /> go('block')} /> )} {step === 'report' && REPORT_REASONS.map(([reason, label]) => ( report.mutate({ path: { comment_id: c.comment_id }, body: { reason } }, finish('์‹ ๊ณ ํ–ˆ์–ด์š”. ํ™•์ธํ•˜๊ณ  ์กฐ์น˜ํ• ๊ฒŒ์š”.', () => track('report', { content_type: 'comment', reason })))} /> ))} {step === 'block' && ( <> {c.nickname}๋‹˜ ๋Œ“๊ธ€์„ ๋”๋Š” ์•ˆ ๋ณผ๊นŒ์š”? block.mutate({ path: { comment_id: c.comment_id } }, finish('์ฐจ๋‹จํ–ˆ์–ด์š”. ๋‚ด ์ •๋ณด์—์„œ ํ’€ ์ˆ˜ ์žˆ์–ด์š”.', () => track('block', { content_type: 'comment' })))} /> go('closed')} /> )} {failed && ( ์ž ์‹œ ๋’ค์— ๋‹ค์‹œ ํ•ด ์ฃผ์„ธ์š”. )} )} ); } function CommentsSkel() { return ( {N3.map((i) => ( ))} ); } function CommentRow({ c, to, onNotice }: { c: CommentData; to: CommentTarget; onNotice: (text: string) => void }) { const { region } = useRegion(); const queryClient = useQueryClient(); const [heart, ink3] = useCSSVariable(['--color-heart', '--color-ink3']).map(String); const [liked, setLiked] = useState(c.liked); const [count, setCount] = useState(c.like_count); const like = useMutation(likeCommentMutation()); const unlike = useMutation(unlikeCommentMutation()); const remove = useMutation(deleteCommentMutation()); const [removeFailed, setRemoveFailed] = useState(false); const [likeFailed, setLikeFailed] = useState(false); // ์ข‹์•„์š”๋Š” ๋กœ๊ทธ์ธ ์—†์ด ๋œ๋‹ค (์›น๊ณผ ๊ฐ™์ด) const toggle = () => { if (like.isPending || unlike.isPending) return; const next = !liked; setLikeFailed(false); setLiked(next); setCount((n) => n + (next ? 1 : -1)); const revert = () => { setLiked(!next); setCount(c.like_count); }; (next ? like : unlike).mutate( { path: { comment_id: c.comment_id } }, { onSuccess: (res) => { if (res.result?.success) { setLiked(!!res.liked); if (res.like_count != null) setCount(res.like_count); if (next) track('like', { content_type: 'comment', item_id: c.comment_id }); return; } revert(); // ๊ทธ ์‚ฌ์ด ์ง€์›Œ์ง„ ๋Œ“๊ธ€์ด๋ฉด ์•Œ๋ฆผ ์—†์ด ๋ชฉ๋ก์„ ๋‹ค์‹œ ๋ฐ›์•„ ์‚ฌ๋ผ์ง€๊ฒŒ ํ•œ๋‹ค if (res.result?.code === NOT_FOUND) queryClient.invalidateQueries({ queryKey: commentListKey(to) }); else setLikeFailed(true); }, onError: () => { revert(); setLikeFailed(true); }, }, ); }; // ์ง€์šฐ๋Š” ๋™์•ˆ์€ ํ๋ฆฌ๊ฒŒ ๋‘๊ณ , ์„ฑ๊ณตํ•˜๋ฉด ๋‹ค์‹œ ๋ฐ›๊ธฐ ์ „์— ๋ชฉ๋ก ์บ์‹œ์—์„œ ๋ฐ”๋กœ ๋บ€๋‹ค const del = () => { setRemoveFailed(false); remove.mutate( { path: { comment_id: c.comment_id } }, { onSuccess: async (res) => { if (!res.result?.success) return setRemoveFailed(true); const key = commentListKey(to); await queryClient.cancelQueries({ queryKey: key }); queryClient.setQueryData>(key, (data) => data ? { ...data, pages: data.pages.map((p) => ({ ...p, comments: p.comments?.filter((x) => x.comment_id !== c.comment_id) })) } : data, ); queryClient.invalidateQueries({ queryKey: key }); if ('talkId' in to) queryClient.invalidateQueries({ predicate: isTalkList }); else queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) }); }, onError: () => setRemoveFailed(true), }, ); }; return ( {c.nickname} {c.official && ( <> {' ๋‚ ์”จ๊ฐ '}{' '} )} {c.body} {ago(c.created_at)} ยท {likeFailed ? ์ข‹์•„์š”๋ฅผ ๋ชป ํ–ˆ์–ด์š”. ์ž ์‹œ ๋’ค์— ๋‹ค์‹œ ๋ˆŒ๋Ÿฌ ์ฃผ์„ธ์š” : `์ข‹์•„์š” ${count}๊ฐœ`} {c.mine && ( <> {' ยท '} {remove.isPending ? '์ง€์šฐ๋Š” ์ค‘โ€ฆ' : removeFailed ? '๋ชป ์ง€์› ์–ด์š” ยท ๋‹ค์‹œ' : '์ง€์šฐ๊ธฐ'} )} {/* ๋‚ ์”จ๊ฐ ์บ๋ฆญํ„ฐ ๋Œ“๊ธ€์€ ์šด์˜ ๋ฌธ๊ตฌ๋ผ ์‹ ๊ณ ยท์ฐจ๋‹จ ๋ฉ”๋‰ด๊ฐ€ ์—†๋‹ค */} {!c.mine && !c.official && } ); } /** ํฌ์ŠคํŠธ ๋Œ“๊ธ€, ๋˜๋Š” ์‚ฌ๋žŒ ๊ธ€ ๋Œ“๊ธ€(๋กœ๊ทธ์ธํ•œ ์‚ฌ๋žŒ๋งŒ ์“ด๋‹ค) */ export function CommentsSheet({ to, draft }: { to: CommentTarget; draft?: string }) { const { region } = useRegion(); const { me } = useAuth(); const { open } = useSheet(); const queryClient = useQueryClient(); const talkId = 'talkId' in to ? to.talkId : undefined; const postId = 'postId' in to ? to.postId : undefined; // ๋‘˜ ์ค‘ ํ•˜๋‚˜๋งŒ ๋ฐ›๋Š”๋‹ค const postList = useInfiniteQuery({ ...listCommentsInfiniteOptions({ path: { post_id: postId ?? 0 } }), initialPageParam: { path: { post_id: postId ?? 0 } }, getNextPageParam: (last) => last.next_cursor ?? undefined, enabled: postId != null, }); const talkList = useInfiniteQuery({ ...listTalkCommentsInfiniteOptions({ path: { talk_id: talkId ?? 0 } }), initialPageParam: { path: { talk_id: talkId ?? 0 } }, getNextPageParam: (last) => last.next_cursor ?? undefined, enabled: talkId != null, }); const list = talkId != null ? talkList : postList; const addPost = useMutation(addCommentMutation()); const addTalk = useMutation(addTalkCommentMutation()); const add = { isPending: addPost.isPending || addTalk.isPending }; const agree = useMutation(agreeTermsMutation()); const s = useSoft(); const [addFailed, setAddFailed] = useState(false); const [askTerms, setAskTerms] = useState(false); const [notice, setNotice] = useState(''); const [text, setText] = useState(draft ?? ''); const inputRef = useRef(null); const comments = list.data?.pages.flatMap((p) => p.comments ?? []) ?? []; useEffect(() => { if (!notice) return; const t = setTimeout(() => setNotice(''), 3000); return () => clearTimeout(t); }, [notice]); const send = () => { const body = text.trim(); if (!body || add.isPending) return; setAddFailed(false); const handlers = { onSuccess: (res: { result?: { success?: boolean; desc?: string } }) => { if (res.result?.desc === 'TERMS_REQUIRED') return setAskTerms(true); setAddFailed(!res.result?.success); if (!res.result?.success) return; setText(''); queryClient.invalidateQueries({ queryKey: commentListKey(to) }); if (talkId != null) { track('talk_comment', { talk_id: talkId }); queryClient.invalidateQueries({ predicate: isTalkList }); } else if (postId != null) { track('comment', { post_id: postId }); queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) }); } }, onError: () => setAddFailed(true), }; if (talkId != null) addTalk.mutate({ path: { talk_id: talkId }, body: { body } }, handlers); else if (postId != null) addPost.mutate({ path: { post_id: postId }, body: { body, region: region.code } }, handlers); }; // ๋Œ“๊ธ€์€ ๋กœ๊ทธ์ธ ์—†์ด 'โ—‹โ—‹๊ตฌ ์ต๋ช…'์œผ๋กœ ์“ด๋‹ค. ๋กœ๊ทธ์ธํ–ˆ์œผ๋ฉด ์•ฝ๊ด€์— ๋™์˜ํ•ด์•ผ ์“ธ ์ˆ˜ ์žˆ๊ณ , ๋™์˜ํ•˜๋ฉด ์“ฐ๋˜ ๋Œ“๊ธ€์„ ๋ฐ”๋กœ ์˜ฌ๋ฆฐ๋‹ค const submit = () => { if (talkId != null && !me) return open({ type: 'login', then: { type: 'comments', ...to, draft: text } }); if (me && !me.terms_agreed) return setAskTerms(true); send(); }; const agreeAndSend = () => { setAddFailed(false); agree.mutate( {}, { onSuccess: (res) => { if (!res.result?.success) return setAddFailed(true); queryClient.setQueryData(meQueryKey(), res); track('terms_agree', {}); setAskTerms(false); send(); }, onError: () => setAddFailed(true), }, ); }; return ( <> {comments.length ? ( <> {/* ๋‹ค์‹œ ๋ฐ›์€ ์ข‹์•„์š” ์ƒํƒœ(๋กœ๊ทธ์ธ ๋ฐ”๋€œ ๋“ฑ)๊ฐ€ ํ–‰์— ๋ฐ˜์˜๋˜๋„๋ก key ์— ๋„ฃ๋Š”๋‹ค */} {comments.map((c) => ( ))} {list.hasNextPage && ( list.fetchNextPage()} disabled={list.isFetchingNextPage} className="my-2 disabled:opacity-50"> {list.isFetchingNextPage ? '๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘โ€ฆ' : '๋Œ“๊ธ€ ๋” ๋ณด๊ธฐ'} )} ) : loadOf(list) === 'loading' ? ( ) : ( list.refetch()} empty="์•„์ง ๋Œ“๊ธ€์ด ์—†์–ด์š”. ์ฒซ ๋Œ“๊ธ€์„ ๋‚จ๊ฒจ ๋ณด์„ธ์š”." hold={} boxClassName="inset-0" /> )} {askTerms ? ( ๋Œ“๊ธ€์„ ์“ฐ๋ ค๋ฉด ์ด์šฉ์•ฝ๊ด€์— ๋™์˜ํ•ด ์ฃผ์„ธ์š”. ์š•์„คยท๊ด‘๊ณ ยท๊ฐœ์ธ์ •๋ณด ๋…ธ์ถœ์€ ์ˆจ๊ฒจ์ง€๊ณ , ๊ณ„์†๋˜๋ฉด ์ด์šฉ์ด ์ œํ•œ๋ผ์š”. void Linking.openURL(`${SITE_URL}/terms.html`)} accessibilityRole="link" className="flex-1 rounded-[12px] bg-card p-2.5"> ์ด์šฉ์•ฝ๊ด€ ๋ณด๊ธฐ ๋™์˜ํ•˜๊ณ  ์“ฐ๊ธฐ ) : ( {EMOJIS.map((e) => ( { setText((t) => (t + e).slice(0, 60)); inputRef.current?.focus(); }} > {e} ))} )} {addFailed ? : {notice}} {add.isPending ? '์˜ฌ๋ฆฌ๋Š” ์ค‘โ€ฆ' : '๊ฒŒ์‹œ'} ); }