import { addCommentMutation, agreeTermsMutation, blockAuthorMutation, deleteCommentMutation, getFeedQueryKey, likeCommentMutation, listBlockedQueryKey, listCommentsInfiniteOptions, listCommentsInfiniteQueryKey, 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 { useAuth } from '@/lib/auth'; import { useRegion } from '@/lib/region'; import { useSheet } 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[] }) => (q.queryKey[0] as { _id?: string } | undefined)?._id === 'listComments'; function Chip({ label, onPress, disabled, danger }: { label: string; onPress: () => void; disabled?: boolean; danger?: boolean }) { const s = useSoft(); return ( {label} ); } /** λ‚¨μ˜ λŒ“κΈ€: β‹― β†’ μ‹ κ³ ν•˜κΈ°(이유 κ³ λ₯΄κΈ°) / 이 μ‚¬λžŒ μ°¨λ‹¨ν•˜κΈ°(ν•œ 번 더 묻기). λλ‚˜λ©΄ λͺ©λ‘μ„ λ‹€μ‹œ λ°›μ•„ κ·Έ λŒ“κΈ€μ΄ λΉ μ§„λ‹€ */ function CommentMenu({ c, postId, onNotice }: { c: CommentData; postId: number; 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) => ({ onSuccess: (res: { result?: { success?: boolean } }) => { if (!res.result?.success) return setFailed(true); 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', postId } }); 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('μ‹ κ³ ν–ˆμ–΄μš”. ν™•μΈν•˜κ³  μ‘°μΉ˜ν• κ²Œμš”.'))} /> ))} {step === 'block' && ( <> {c.nickname}λ‹˜ λŒ“κΈ€μ„ λ”λŠ” μ•ˆ λ³ΌκΉŒμš”? block.mutate({ path: { comment_id: c.comment_id } }, finish('μ°¨λ‹¨ν–ˆμ–΄μš”. λ‚΄ μ •λ³΄μ—μ„œ ν’€ 수 μžˆμ–΄μš”.'))} /> go('closed')} /> )} {failed && ( μž μ‹œ 뒀에 λ‹€μ‹œ ν•΄ μ£Όμ„Έμš”. )} )} ); } function CommentsSkel() { return ( {N3.map((i) => ( ))} ); } function CommentRow({ c, postId, onNotice }: { c: CommentData; postId: number; onNotice: (text: string) => void }) { const { region } = useRegion(); const { loggedIn } = useAuth(); const { open } = useSheet(); 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 (!loggedIn) return open({ type: 'login' }); 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); return; } revert(); // κ·Έ 사이 μ§€μ›Œμ§„ λŒ“κΈ€μ΄λ©΄ μ•Œλ¦Ό 없이 λͺ©λ‘μ„ λ‹€μ‹œ λ°›μ•„ μ‚¬λΌμ§€κ²Œ ν•œλ‹€ if (res.result?.code === NOT_FOUND) queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) }); 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 = listCommentsInfiniteQueryKey({ path: { post_id: postId } }); 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 }); 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({ postId, draft }: { postId: number; draft?: string }) { const { region } = useRegion(); const { open } = useSheet(); const { loggedIn, me } = useAuth(); const queryClient = useQueryClient(); const list = useInfiniteQuery({ ...listCommentsInfiniteOptions({ path: { post_id: postId } }), initialPageParam: { path: { post_id: postId } }, getNextPageParam: (last) => last.next_cursor ?? undefined, }); const add = useMutation(addCommentMutation()); 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); add.mutate( { path: { post_id: postId }, body: { body } }, { onSuccess: (res) => { if (res.result?.desc === 'TERMS_REQUIRED') return setAskTerms(true); setAddFailed(!res.result?.success); if (!res.result?.success) return; setText(''); queryClient.invalidateQueries({ queryKey: listCommentsInfiniteQueryKey({ path: { post_id: postId } }) }); queryClient.invalidateQueries({ queryKey: getFeedQueryKey({ query: { region: region.code } }) }); }, onError: () => setAddFailed(true), }, ); }; // 약관에 λ™μ˜ν•΄μ•Ό λŒ“κΈ€μ„ μ“Έ 수 μžˆλ‹€. λ™μ˜ν•˜λ©΄ μ“°λ˜ λŒ“κΈ€μ„ λ°”λ‘œ μ˜¬λ¦°λ‹€ const submit = () => { if (!loggedIn) return open({ type: 'login', then: { type: 'comments', postId, 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); 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 ? 'μ˜¬λ¦¬λŠ” 쀑…' : 'κ²Œμ‹œ'} ); }