Merge branch 'develop' of https://gitea.o2o.kr/castad/o2o-triple-pick into develop
This commit is contained in:
commit
220eb946c1
8 files changed
+162
-199
No files matched your search
@@ -4,10 +4,9 @@ import { relativeTime } from "@/lib/format";
|
||||
import {
|
||||
listComments,
|
||||
postComment,
|
||||
deleteComment,
|
||||
getSessionNickname,
|
||||
rerollSessionNickname,
|
||||
getDeviceId,
|
||||
getRecentEmails,
|
||||
rememberEmail,
|
||||
ApiError,
|
||||
} from "@/lib/api";
|
||||
import type { Comment } from "@/lib/types";
|
||||
@@ -15,7 +14,10 @@ import type { Comment } from "@/lib/types";
|
||||
const FIRST = 3; // 처음 보여줄 최신 댓글 수
|
||||
const PAGE = 10; // "더보기" 한 번에 더 불러올 수
|
||||
|
||||
// 경기별 한마디(댓글). 최신 3개 + "더보기" 인라인 펼침.
|
||||
// 한 댓글을 식별하는 합성 키 (id 비노출이라 시각+내용으로 dedup/렌더)
|
||||
const keyOf = (c: Comment) => `${c.createdAt}|${c.nickname}|${c.body}`;
|
||||
|
||||
// 경기별 한마디(댓글) — 완전 익명. 최신 3개 + 더보기/접기.
|
||||
export default function Comments({
|
||||
matchId,
|
||||
lang,
|
||||
@@ -28,12 +30,10 @@ export default function Comments({
|
||||
const [total, setTotal] = useState(0);
|
||||
const [cursor, setCursor] = useState(0); // 서버에서 페이징으로 받은 개수
|
||||
const [loadingMore, setLoadingMore] = useState(false);
|
||||
const [expanded, setExpanded] = useState(false); // true=전체 보기 / false=최신 FIRST개만
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
|
||||
const [body, setBody] = useState("");
|
||||
// 투표 폼과 동일: 칸은 비우고 입력/포커스 시 datalist 드롭다운으로 최근 이메일 자동완성
|
||||
const [email, setEmail] = useState("");
|
||||
const [recentEmails, setRecentEmails] = useState<string[]>(getRecentEmails);
|
||||
const [nickname, setNickname] = useState(""); // 세션 닉(캐싱)
|
||||
const [posting, setPosting] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
@@ -47,17 +47,18 @@ export default function Comments({
|
||||
setCursor(res.items.length);
|
||||
})
|
||||
.catch(() => {});
|
||||
getSessionNickname(matchId)
|
||||
.then((n) => alive && setNickname(n))
|
||||
.catch(() => {});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [matchId]);
|
||||
|
||||
// 접힘 상태면 최신 FIRST개만 노출
|
||||
const visible = expanded ? items : items.slice(0, FIRST);
|
||||
const unloaded = Math.max(0, total - items.length); // 아직 서버에서 안 받은 수
|
||||
const hiddenWhenCollapsed = Math.max(0, total - FIRST); // 접힘 시 가려진 전체 수
|
||||
const unloaded = Math.max(0, total - items.length);
|
||||
const hiddenWhenCollapsed = Math.max(0, total - FIRST);
|
||||
|
||||
// "더보기": 펼치고, 받아온 게 FIRST개뿐이면 다음 페이지도 가져온다
|
||||
async function showMore() {
|
||||
setExpanded(true);
|
||||
if (items.length <= FIRST && unloaded > 0) await loadMore();
|
||||
@@ -69,8 +70,8 @@ export default function Comments({
|
||||
try {
|
||||
const res = await listComments(matchId, PAGE, cursor);
|
||||
setItems((prev) => {
|
||||
const seen = new Set(prev.map((c) => c.id));
|
||||
return [...prev, ...res.items.filter((c) => !seen.has(c.id))];
|
||||
const seen = new Set(prev.map(keyOf));
|
||||
return [...prev, ...res.items.filter((c) => !seen.has(keyOf(c)))];
|
||||
});
|
||||
setTotal(res.total);
|
||||
setCursor((c) => c + res.items.length);
|
||||
@@ -81,46 +82,31 @@ export default function Comments({
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id: number) {
|
||||
if (!window.confirm(t.commentDeleteConfirm)) return;
|
||||
async function reroll() {
|
||||
try {
|
||||
await deleteComment(matchId, id);
|
||||
setItems((prev) => prev.filter((c) => c.id !== id));
|
||||
setTotal((n) => Math.max(0, n - 1));
|
||||
const n = await rerollSessionNickname(matchId);
|
||||
setNickname(n);
|
||||
} catch {
|
||||
setError(t.commentError);
|
||||
/* 무시 */
|
||||
}
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
const text = body.trim();
|
||||
const mail = email.trim();
|
||||
if (posting) return;
|
||||
if (posting || !text) return;
|
||||
setError("");
|
||||
if (!mail) {
|
||||
setError(t.commentNeedEmail);
|
||||
return;
|
||||
}
|
||||
if (!text) return;
|
||||
setPosting(true);
|
||||
try {
|
||||
const created = await postComment(matchId, {
|
||||
deviceId: getDeviceId(),
|
||||
email: mail,
|
||||
nickname,
|
||||
body: text,
|
||||
});
|
||||
rememberEmail(mail);
|
||||
setRecentEmails(getRecentEmails()); // 방금 쓴 이메일을 자동완성 목록에 반영
|
||||
setItems((prev) => [created, ...prev]); // 낙관적: 맨 위에 즉시 표시
|
||||
setTotal((n) => n + 1);
|
||||
setBody("");
|
||||
} catch (e) {
|
||||
if (e instanceof ApiError && e.status === 403)
|
||||
setError(t.commentNeedVote);
|
||||
else if (e instanceof ApiError && e.status === 429)
|
||||
setError(t.commentCooldown);
|
||||
else if (e instanceof ApiError && e.status === 422)
|
||||
setError(t.commentNeedEmail);
|
||||
if (e instanceof ApiError && e.status === 429) setError(t.commentCooldown);
|
||||
else setError(t.commentError);
|
||||
} finally {
|
||||
setPosting(false);
|
||||
@@ -141,22 +127,20 @@ export default function Comments({
|
||||
|
||||
{/* 입력 */}
|
||||
<div className="mt-3 space-y-2">
|
||||
<input
|
||||
type="email"
|
||||
inputMode="email"
|
||||
autoComplete="email"
|
||||
list="tp-comment-email-suggestions"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder={t.commentEmailPh}
|
||||
className="w-full rounded-lg border border-[var(--line-d)] bg-[#0f1217] px-3 py-2.5 text-[14px] text-white outline-none focus:border-[var(--green)]"
|
||||
/>
|
||||
{recentEmails.length > 0 && (
|
||||
<datalist id="tp-comment-email-suggestions">
|
||||
{recentEmails.map((e) => (
|
||||
<option key={e} value={e} />
|
||||
))}
|
||||
</datalist>
|
||||
{nickname && (
|
||||
<p className="flex items-center gap-1.5 text-[12px] font-bold text-white/45">
|
||||
⚽ {t.commentAs}{" "}
|
||||
<span className="text-[var(--green)]">{nickname}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={reroll}
|
||||
title={t.commentReroll}
|
||||
aria-label={t.commentReroll}
|
||||
className="grid h-5 w-5 place-items-center rounded-full text-[13px] leading-none text-white/45 transition hover:text-white active:rotate-180"
|
||||
>
|
||||
↻
|
||||
</button>
|
||||
</p>
|
||||
)}
|
||||
<div className="flex items-end gap-2">
|
||||
<textarea
|
||||
@@ -199,8 +183,8 @@ export default function Comments({
|
||||
expanded ? "max-h-[360px] overflow-y-auto pr-1" : ""
|
||||
}`}
|
||||
>
|
||||
{visible.map((c) => (
|
||||
<li key={c.id} className="py-3 first:pt-0">
|
||||
{visible.map((c, i) => (
|
||||
<li key={`${keyOf(c)}-${i}`} className="py-3 first:pt-0">
|
||||
<div className="flex items-baseline gap-2">
|
||||
<span className="text-[13px] font-extrabold text-[var(--green)]">
|
||||
{c.nickname}
|
||||
@@ -208,15 +192,6 @@ export default function Comments({
|
||||
<span className="text-[11px] text-white/35">
|
||||
{relativeTime(c.createdAt, lang)}
|
||||
</span>
|
||||
{c.mine && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => remove(c.id)}
|
||||
className="ml-auto text-[11px] font-bold text-white/35 transition hover:text-red-400"
|
||||
>
|
||||
{t.commentDelete}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
<p className="mt-1 whitespace-pre-wrap break-words text-[14px] leading-snug text-white/90">
|
||||
{c.body}
|
||||
|
||||
Reference in new issue
Block a user