feat : 자료실(4번 메뉴) 추가 — 링크 등록 시 영상/이미지 다운로드 후 게시
SpecialSource Frontend (React) CD / deploy (push) Successful in 14s

- 목록: 링크·제목 등록 칸, 받는 중인 글이 있으면 5초마다 자동 새로고침
- 상세: 파일별 <video>/<img> (백엔드가 준 1시간짜리 토큰 주소)
- API 오류 시 서버가 준 이유({"error"})를 팝업에 표시

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
headporterandClaude Opus 5.5 committed 2026-11-27 17:04:44 +09:00
1 parent ca4df3e9a3
commit eac57c04e7
8 files changed
+273 -10

No files matched your search

+39 -3
View File
@@ -1,12 +1,14 @@
"use client";
import { useEffect, useLayoutEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
import { useCallback, useEffect, useLayoutEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
import { AUTH_KEY, PALETTE_KEY, PALETTES } from "../constants";
import { fetchBoards, fetchFeed } from "../api";
import { fetchBoards, fetchFeed, fetchMediaPosts } from "../api";
import { apiError, execute, initialState, LOGOUT } from "../lib/command";
import type { ListKind, Nav, Palette } from "../types";
import LoginForm from "./LoginForm";
import MainMenu from "./MainMenu";
import MediaDetail from "./MediaDetail";
import MediaList from "./MediaList";
import PostDetail from "./PostDetail";
import PostList from "./PostList";
import styles from "./bbs.module.css";
@@ -97,6 +99,28 @@ export default function BbsTerminal() {
};
}, [feedKind, feedBoard, state.token, state.rev]);
// 자료실 목록: 들어오거나 "L"을 누르면 다시 불러옴. 받는 중인 글이 있으면 5초마다 다시 불러와 상태 갱신
const onApiError = useCallback((e: unknown) => dispatch(apiError(e)), []);
useEffect(() => {
if (state.screen !== "media" || !state.token) return;
const token = state.token;
let stale = false;
let timer: ReturnType<typeof setTimeout>;
const load = () =>
fetchMediaPosts(token)
.then((media) => {
if (stale) return;
dispatch({ media });
if (media.some((p) => p.status === "DOWNLOADING")) timer = setTimeout(load, 5000);
})
.catch((e) => !stale && onApiError(e));
load();
return () => {
stale = true;
clearTimeout(timer);
};
}, [state.screen, state.token, state.rev, onApiError]);
// 오류·안내 메시지가 생기면 팝업으로 띄우고, 다른 경로로 지워지면 닫음
const dialogRef = useRef<HTMLDialogElement>(null);
useEffect(() => {
@@ -201,7 +225,7 @@ export default function BbsTerminal() {
ref={screenRef}
className={styles.screen}
onScroll={(e) => {
if (state.screen !== "post") scrollPos.current[scrollKey] = e.currentTarget.scrollTop;
if (state.screen !== "post" && state.screen !== "mediaPost") scrollPos.current[scrollKey] = e.currentTarget.scrollTop;
}}
onTouchStart={(e) => {
const t = e.touches[0];
@@ -252,6 +276,18 @@ export default function BbsTerminal() {
{state.screen === "post" && state.token && (
<PostDetail post={state.feed?.posts.find((p) => p.id === state.postId)} token={state.token} />
)}
{state.screen === "media" && (
<MediaList
posts={state.media}
token={state.token}
onSelect={submitFromClick}
onCreated={() => dispatch({ rev: state.rev + 1 })}
onError={onApiError}
/>
)}
{state.screen === "mediaPost" && state.token && state.postId !== null && (
<MediaDetail id={state.postId} token={state.token} onError={onApiError} />
)}
</div>
</div>
@@ -0,0 +1,77 @@
import { useEffect, useState } from "react";
import { API_URL, fetchMediaPost } from "../api";
import type { MediaPost } from "../types";
import { STATUS } from "./MediaList";
import styles from "./bbs.module.css";
/** 자료실 글 보기: 파일마다 <video>/<img>. 파일 주소의 토큰은 1시간 유효 → 오래 열어 두었으면 "L" 후 다시 열기 */
export default function MediaDetail({ id, token, onError }: { id: number; token: string; onError: (e: unknown) => void }) {
const [post, setPost] = useState<MediaPost | null>(null);
// 받는 중이면 5초마다 다시 조회 → 끝나면 파일이 나타남
useEffect(() => {
let stale = false;
let timer: ReturnType<typeof setTimeout>;
const load = () =>
fetchMediaPost(token, id)
.then((p) => {
if (stale) return;
setPost(p);
if (p.status === "DOWNLOADING") timer = setTimeout(load, 5000);
})
.catch((e) => !stale && onError(e));
load();
return () => {
stale = true;
clearTimeout(timer);
};
}, [id, token, onError]);
if (!post) {
return (
<section className={`${styles.win} ${styles.full}`}>
<div className={styles.dim}>불러오는 중...</div>
</section>
);
}
return (
<section className={`${styles.win} ${styles.full}`}>
<span className={styles.cap}>═ [4] 자료실 ═</span>
<div className={styles.postHead}>
<span>제목 :</span> {post.title}
</div>
<div className={styles.postHead}>
<span>등록:</span> {post.author} <span>날짜:</span> {post.createdAt.slice(0, 16).replace("T", " ")}{" "}
<span>상태:</span> {STATUS[post.status]}
</div>
<hr className={styles.rule} />
{post.status === "DOWNLOADING" ? (
<div className={styles.dim}>다운로드 중입니다... 끝나면 자동으로 표시됩니다.</div>
) : post.files.length === 0 ? (
<div className={styles.error}>※ 다운로드에 실패했습니다. (쿠키 만료·삭제된 게시물·지원하지 않는 링크)</div>
) : (
<div className={styles.html}>
{post.files.map((f) => {
const src = API_URL + f.url;
if (f.type === "video") return <video key={f.url} src={src} controls preload="metadata" playsInline />;
// eslint-disable-next-line @next/next/no-img-element -- 정적 export라 next/image 최적화 불가
if (f.type === "image") return <img key={f.url} src={src} alt={f.name} loading="lazy" />;
return (
<p key={f.url}>
<a href={src}>{f.name}</a>
</p>
);
})}
</div>
)}
<hr className={styles.rule} />
<div className={styles.footer}>
<a className={styles.accent} href={post.sourceUrl} target="_blank" rel="noreferrer">
원본 보기 ↗
</a>{" "}
· <b>L</b>=목록 · <b>M</b>=메인
</div>
</section>
);
}
+93
View File
@@ -0,0 +1,93 @@
import { useState, type FormEvent } from "react";
import { createMediaPost } from "../api";
import type { MediaPost } from "../types";
import styles from "./bbs.module.css";
interface Props {
posts: MediaPost[] | null;
token: string | null;
onSelect: (cmd: string) => void;
onCreated: () => void; // 등록 성공 → 목록 다시 불러오기
onError: (e: unknown) => void;
}
export const STATUS: Record<MediaPost["status"], string> = { DOWNLOADING: "받는중", DONE: "완료", FAILED: "실패" };
/** [4] 자료실: 위에는 링크 등록 칸, 아래는 목록. 받는 중인 글이 있으면 BbsTerminal이 5초마다 새로고침 */
export default function MediaList({ posts, token, onSelect, onCreated, onError }: Props) {
const [busy, setBusy] = useState(false);
const submit = async (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
if (!token) return;
const form = e.currentTarget;
const data = new FormData(form);
setBusy(true);
try {
await createMediaPost(token, String(data.get("url")).trim(), String(data.get("title")).trim());
form.reset();
onCreated();
} catch (err) {
onError(err);
} finally {
setBusy(false);
}
};
return (
<section className={`${styles.win} ${styles.full}`}>
<span className={styles.cap}>
═ [4] 자료실{posts && ` (${posts.length}건)`} ═
</span>
{!token ? (
<div className={styles.dim}>로그인이 필요합니다. (F5 또는 메인에서 7)</div>
) : (
<>
<form className={styles.loginForm} onSubmit={submit}>
<label>
<span className={styles.num}>링크</span>
<input name="url" type="url" className={styles.wide} placeholder="유튜브·인스타 주소" required />
</label>
<label>
<span className={styles.num}>제목</span>
<input name="title" className={styles.wide} placeholder="비우면 영상 제목으로" maxLength={500} />
</label>
<button type="submit" disabled={busy}>
{busy ? "요청중..." : "다운로드 등록"}
</button>
</form>
<hr className={styles.rule} />
{!posts ? (
<div className={styles.dim}>불러오는 중...</div>
) : posts.length === 0 ? (
<div className={styles.dim}>등록된 자료가 없습니다.</div>
) : (
<table className={styles.tbl}>
<thead>
<tr>
<th className={styles.idx}>번호</th>
<th>상태</th>
<th>제목</th>
<th>날짜</th>
</tr>
</thead>
<tbody>
{posts.map((p, i) => (
<tr key={p.id} onClick={() => onSelect(String(i + 1))}>
<td className={styles.idx}>{i + 1}</td>
<td className={p.status === "FAILED" ? styles.error : styles.accent}>{STATUS[p.status]}</td>
<td className={styles.title}>{p.title}</td>
<td>{p.createdAt.slice(0, 10).replaceAll("-", "")}</td>
</tr>
))}
</tbody>
</table>
)}
</>
)}
<div className={styles.footer}>
<b>번호</b>=보기 · <b>M</b>=메인 · <b>L</b>=새로고침
</div>
</section>
);
}
@@ -111,6 +111,7 @@
color: var(--bbs-hi); font: inherit; outline: none; caret-color: var(--bbs-highlight);
}
.loginForm input:focus { border-bottom-color: var(--bbs-accent); }
.loginForm input.wide { max-width: none; } /* 자료실 링크·제목 칸 */
.loginForm button {
align-self: flex-start; background: none; border: 1px solid var(--bbs-accent); color: var(--bbs-accent);
font: inherit; padding: 0 1.5ch; cursor: pointer;