mirror of
https://github.com/headporter81/ss-front-react.git
synced 2026-12-06 07:41:11 +09:00
fix : 무한 스크롤 중복·누락 수정 (안 읽은 글은 id 커서 방식으로 조회)
SpecialSource Frontend (React) CD / deploy (push) Successful in 13s
SpecialSource Frontend (React) CD / deploy (push) Successful in 13s
- 스크롤 중 새 글이 스크랩되거나 글을 읽어 순서가 밀리면 다음 페이지에 중복·누락이 생기던 문제 - 안 읽은 글: before(마지막 글 id) 커서로 다음 페이지 조회 (백엔드 before 파라미터 필요) - 읽은 글: 페이지 번호 방식 유지, 이미 받은 글은 걸러냄 - 요청 사이 목록이 바뀌었으면 늦게 온 응답은 버림 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d94aaa6f0a
commit
cbc41ef444
3 files changed
+26
-12
No files matched your search
+13
-5
@@ -16,12 +16,20 @@ async function call(path: string, token: string, init?: RequestInit) {
|
||||
|
||||
const PAGE_SIZE = 30;
|
||||
|
||||
/** 목록 한 페이지 (page는 0부터). 무한 스크롤은 page+1을 불러와 이어 붙임 */
|
||||
export async function fetchFeed(token: string, kind: ListKind, board: string | null, page = 0): Promise<Feed> {
|
||||
/**
|
||||
* 목록 첫 페이지, 또는 prev 다음 페이지 (무한 스크롤).
|
||||
* - 안 읽은 글: 커서 방식 (prev의 마지막 글 id보다 오래된 글) → 새 글 추가·읽음 처리로 순서가 밀려도 중복·누락 없음
|
||||
* - 읽은 글: 페이지 번호 방식 (최근 읽은 순이라 id 커서를 못 씀. 밀려서 생기는 중복은 execute에서 걸러냄)
|
||||
*/
|
||||
export async function fetchFeed(token: string, kind: ListKind, board: string | null, prev?: Feed): Promise<Feed> {
|
||||
const page = prev ? prev.page + 1 : 0;
|
||||
const before = prev?.posts.at(-1)?.id;
|
||||
const query =
|
||||
kind === "viewed" ? `/viewed?page=${page}` : `?page=0${before === undefined ? "" : `&before=${before}`}`;
|
||||
const filter = board ? `&board=${encodeURIComponent(board)}` : "";
|
||||
const path = `/api/scraped-posts${kind === "viewed" ? "/viewed" : ""}?size=${PAGE_SIZE}&page=${page}${filter}`;
|
||||
const data = await (await call(path, token)).json(); // Spring Page 응답: { content: [...], totalElements, ... }
|
||||
const last = (page + 1) * PAGE_SIZE >= data.totalElements;
|
||||
const data = await (await call(`/api/scraped-posts${query}&size=${PAGE_SIZE}${filter}`, token)).json();
|
||||
// Spring Page 응답 { content, totalElements, ... }. 커서 조회의 totalElements = 커서 이후 남은 글 수
|
||||
const last = kind === "viewed" ? (page + 1) * PAGE_SIZE >= data.totalElements : data.totalElements <= PAGE_SIZE;
|
||||
return { kind, board, posts: data.content, total: data.totalElements, page, last };
|
||||
}
|
||||
|
||||
|
||||
@@ -102,10 +102,10 @@ export default function BbsTerminal() {
|
||||
const f = state.feed;
|
||||
if (!state.token || !f || f.last || loadingMore.current) return;
|
||||
loadingMore.current = true;
|
||||
fetchFeed(state.token, f.kind, f.board, f.page + 1)
|
||||
fetchFeed(state.token, f.kind, f.board, f)
|
||||
.then((more) => {
|
||||
loadingMore.current = false;
|
||||
dispatch({ more });
|
||||
dispatch({ more, after: f });
|
||||
})
|
||||
.catch((e) => dispatch(apiError(e))); // 실패하면 잠금을 풀지 않음 → 무한 재시도 방지. "L"(새로고침)하면 풀림
|
||||
};
|
||||
|
||||
@@ -31,13 +31,19 @@ function findMenu(raw: string): MenuItem | undefined {
|
||||
}
|
||||
|
||||
/** 클릭/키보드 입력이 모두 거치는 단일 명령 처리기 (순수 함수 → useReducer reducer로 사용) */
|
||||
export function execute(prev: BbsState, input: string | Nav | Partial<BbsState> | { more: Feed }): BbsState {
|
||||
export function execute(
|
||||
prev: BbsState,
|
||||
input: string | Nav | Partial<BbsState> | { more: Feed; after: Feed },
|
||||
): BbsState {
|
||||
if (typeof input !== "string" && "more" in input) {
|
||||
// 무한 스크롤 다음 페이지: 지금 목록의 바로 다음 페이지일 때만 이어 붙임 (그 사이 탭을 바꿨거나 새로고침했으면 버림)
|
||||
const { more } = input;
|
||||
// 무한 스크롤 다음 페이지: 요청할 때의 목록이 지금도 그대로일 때만 이어 붙임 (그 사이 탭을 바꿨거나 새로고침했으면 버림)
|
||||
const { more, after } = input;
|
||||
const f = prev.feed;
|
||||
if (!f || f.kind !== more.kind || f.board !== more.board || more.page !== f.page + 1) return prev;
|
||||
return { ...prev, feed: { ...more, posts: [...f.posts, ...more.posts] } };
|
||||
if (f !== after) return prev;
|
||||
// 읽은 글 목록은 페이지 번호 방식이라, 그 사이 순서가 밀리면 받은 글이 또 나올 수 있음 → 이미 받은 글은 버림
|
||||
const seen = new Set(f.posts.map((p) => p.id));
|
||||
const posts = [...f.posts, ...more.posts.filter((p) => !seen.has(p.id))];
|
||||
return { ...prev, feed: { ...more, total: f.total, posts } }; // 건수는 첫 페이지 기준 유지
|
||||
}
|
||||
if (typeof input !== "string") return { ...prev, error: null, ...input }; // 화면 복원, 로그인, 목록 도착, 오류
|
||||
const raw = input.trim();
|
||||
|
||||
Reference in new issue
Block a user