feat : BBS 게시판 목록 무한 스크롤 및 스크롤 위치 복원
SpecialSource Frontend (React) CD / deploy (push) Successful in 12s

- 목록 끝이 보이면 다음 페이지(30건)를 불러와 이어 붙임 (IntersectionObserver)
- 글 읽기에서 돌아오면 불러온 목록과 스크롤 위치를 그대로 유지
- 다음 페이지 중복 요청·실패 시 무한 재시도 방지

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
headporterandClaude Opus 5.5 committed 2026-11-25 15:32:50 +09:00
1 parent 94d7bd3409
commit d94aaa6f0a
5 files changed
+72 -13

No files matched your search

+33 -3
View File
@@ -1,6 +1,6 @@
"use client";
import { useEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
import { useEffect, useLayoutEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
import { AUTH_KEY, PALETTE_KEY, PALETTES } from "../constants";
import { fetchBoards, fetchFeed } from "../api";
import { apiError, execute, initialState, LOGOUT } from "../lib/command";
@@ -52,6 +52,7 @@ export default function BbsTerminal() {
const mounted = useRef(false);
const touch = useRef<{ x: number; y: number; top: boolean } | null>(null); // 손가락을 댄 위치
const [pulling, setPulling] = useState(false); // 아래로 충분히 당겼는지 (안내 문구 표시)
const loadingMore = useRef(false); // 무한 스크롤 다음 페이지를 불러오는 중 (중복 요청 방지)
// 새로고침해도 로그인 유지: 탭이 열려 있는 동안만 sessionStorage에 보관 (탭 닫으면 사라짐)
// 서버 렌더링 결과와 어긋나지 않도록 첫 렌더링 뒤(useEffect)에 복원. 아래 저장 effect보다 먼저 읽어야 함
@@ -79,12 +80,15 @@ export default function BbsTerminal() {
}
}, [state.screen, state.list, state.board, state.postId]);
// 메인(새 글)·목록 화면에 들어오거나, 게시판 탭을 바꾸거나, "L"을 누르면 서버에서 목록을 다시 불러옴
const feedKind: ListKind | null = state.screen === "main" ? "board" : state.screen === "list" ? state.list : null;
// 메인(새 글)·목록 화면에 들어오거나, 게시판 탭을 바꾸거나, "L"을 누르면 서버에서 목록(첫 페이지)을 다시 불러옴
// 글 읽기 화면은 보던 목록을 그대로 유지 → 돌아왔을 때 무한 스크롤로 불러온 글과 위치가 남아 있음
const feedKind: ListKind | null =
state.screen === "main" ? "board" : state.screen === "list" || state.screen === "post" ? state.list : null;
const feedBoard = state.screen === "main" ? null : state.board; // 메인의 "새 글"은 항상 전체
useEffect(() => {
if (!feedKind || !state.token) return;
let stale = false; // 응답 전에 화면이 바뀌면 늦게 온 응답은 버림
loadingMore.current = false; // 첫 페이지부터 다시 → 무한 스크롤 잠금 해제
fetchFeed(state.token, feedKind, feedBoard)
.then((feed) => !stale && dispatch({ feed }))
.catch((e) => !stale && dispatch(apiError(e)));
@@ -93,6 +97,27 @@ export default function BbsTerminal() {
};
}, [feedKind, feedBoard, state.token, state.rev]);
// 무한 스크롤: 목록 맨 아래가 보이면 다음 페이지를 불러와 이어 붙임 (붙일지 말지는 execute가 판단)
const loadMore = () => {
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)
.then((more) => {
loadingMore.current = false;
dispatch({ more });
})
.catch((e) => dispatch(apiError(e))); // 실패하면 잠금을 풀지 않음 → 무한 재시도 방지. "L"(새로고침)하면 풀림
};
// 화면별 스크롤 위치 기억: 글을 읽고 돌아오면 보던 위치로, 글 읽기는 항상 맨 위부터
const screenRef = useRef<HTMLDivElement>(null);
const scrollPos = useRef<Record<string, number>>({});
const scrollKey = state.screen === "list" ? `list:${state.list}:${state.board}` : state.screen;
useLayoutEffect(() => {
if (screenRef.current) screenRef.current.scrollTop = scrollPos.current[scrollKey] ?? 0;
}, [scrollKey, state.postId]);
// 게시판 탭 목록: 로그인하면 한 번 받아옴
useEffect(() => {
if (!state.token) return;
@@ -165,7 +190,11 @@ export default function BbsTerminal() {
{/* 모바일 제스처: 오른쪽으로 밀기 = 뒤로, 왼쪽으로 밀기 = 앞으로, 맨 위에서 아래로 당기기 = 새로고침 */}
<div
ref={screenRef}
className={styles.screen}
onScroll={(e) => {
if (state.screen !== "post") scrollPos.current[scrollKey] = e.currentTarget.scrollTop;
}}
onTouchStart={(e) => {
const t = e.touches[0];
// 화면 가장자리는 브라우저 기본 스와이프(iOS 사파리)에 양보 → 한 번에 두 칸 이동하는 것 방지
@@ -218,6 +247,7 @@ export default function BbsTerminal() {
feed={state.feed}
loggedIn={!!state.user}
onSelect={submitFromClick}
onMore={loadMore}
/>
)}
{state.screen === "post" && state.token && (