경기 탭: 더 보기 요청 중 새로고침 · 필터 변경하면 늦게 온 더 보기 응답은 버림 (moreGen 세대)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { useEffect, useMemo, useState } from 'react';
|
import { useEffect, useMemo, useRef, useState } from 'react';
|
||||||
import { Link, useNavigate, useParams } from 'react-router';
|
import { Link, useNavigate, useParams } from 'react-router';
|
||||||
import type { GameType, MatchSummary, User, UserProfile } from '@gvr/shared';
|
import type { GameType, MatchSummary, User, UserProfile } from '@gvr/shared';
|
||||||
import { api, type MatchHas } from '../lib/api';
|
import { api, type MatchHas } from '../lib/api';
|
||||||
@@ -100,7 +100,12 @@ export function MatchesPage() {
|
|||||||
}, [game, data]);
|
}, [game, data]);
|
||||||
// 더 보기: 지난 쪽 경기들 (필터가 바뀌면 처음부터)
|
// 더 보기: 지난 쪽 경기들 (필터가 바뀌면 처음부터)
|
||||||
const [more, setMore] = useState<{ list: MatchSummary[]; done: boolean; busy: boolean; err: unknown }>({ list: [], done: false, busy: false, err: null });
|
const [more, setMore] = useState<{ list: MatchSummary[]; done: boolean; busy: boolean; err: unknown }>({ list: [], done: false, busy: false, err: null });
|
||||||
useEffect(() => setMore({ list: [], done: false, busy: false, err: null }), [data]);
|
// 세대: 첫 쪽(data)이 바뀌면 올리고, 그 전에 보낸 더 보기 응답은 버린다 (새로고침 · 필터 변경 중 늦게 온 응답이 다시 펼치지 않게)
|
||||||
|
const moreGen = useRef(0);
|
||||||
|
useEffect(() => {
|
||||||
|
moreGen.current++;
|
||||||
|
setMore({ list: [], done: false, busy: false, err: null });
|
||||||
|
}, [data]);
|
||||||
const all = useMemo(() => {
|
const all = useMemo(() => {
|
||||||
const seen = new Set<string>();
|
const seen = new Set<string>();
|
||||||
const out: MatchSummary[] = [];
|
const out: MatchSummary[] = [];
|
||||||
@@ -124,11 +129,12 @@ export function MatchesPage() {
|
|||||||
const loadMore = () => {
|
const loadMore = () => {
|
||||||
const last = more.list[more.list.length - 1] ?? data?.[data.length - 1];
|
const last = more.list[more.list.length - 1] ?? data?.[data.length - 1];
|
||||||
if (!last || more.busy) return;
|
if (!last || more.busy) return;
|
||||||
|
const gen = moreGen.current;
|
||||||
setMore((s) => ({ ...s, busy: true, err: null }));
|
setMore((s) => ({ ...s, busy: true, err: null }));
|
||||||
api
|
api
|
||||||
.matches({ ...query, before: last.startedAt })
|
.matches({ ...query, before: last.startedAt })
|
||||||
.then((r) => setMore((s) => ({ list: [...s.list, ...r], done: r.length < PAGE, busy: false, err: null })))
|
.then((r) => gen === moreGen.current && setMore((s) => ({ list: [...s.list, ...r], done: r.length < PAGE, busy: false, err: null })))
|
||||||
.catch((e) => setMore((s) => ({ ...s, busy: false, err: e })));
|
.catch((e) => gen === moreGen.current && setMore((s) => ({ ...s, busy: false, err: e })));
|
||||||
};
|
};
|
||||||
const filtered = !!mode || from !== undefined || to !== undefined;
|
const filtered = !!mode || from !== undefined || to !== undefined;
|
||||||
const groups = useMemo(() => {
|
const groups = useMemo(() => {
|
||||||
|
|||||||
Reference in New Issue
Block a user