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 ? '์ฌ๋ฆฌ๋ ์คโฆ' : '๊ฒ์'}
>
);
}