mirror of
https://github.com/headporter81/ss-front-react.git
synced 2026-12-06 07:41:11 +09:00
This commit is contained in:
1 parent
cfda95c1ba
commit
a49df04adf
28 files changed
+734
-304
No files matched your search
@@ -0,0 +1,116 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
||||
import { PALETTE_KEY, PALETTES } from "../constants";
|
||||
import { execute, initialState } from "../lib/command";
|
||||
import type { Nav, Palette } from "../types";
|
||||
import MainMenu from "./MainMenu";
|
||||
import PaletteSwitch from "./PaletteSwitch";
|
||||
import PostDetail from "./PostDetail";
|
||||
import PostList from "./PostList";
|
||||
import styles from "./bbs.module.css";
|
||||
|
||||
const DEFAULT_PALETTE: Palette = "blue";
|
||||
|
||||
// localStorage를 외부 저장소로 보고 React가 구독 (다른 탭에서 바꿔도 동기화됨)
|
||||
const subscribe = (onChange: () => void) => {
|
||||
const handler = () => {
|
||||
document.documentElement.dataset.palette = readPalette(); // 색은 CSS가 <html> 속성으로 적용
|
||||
onChange(); // 버튼 활성 표시는 React가 갱신
|
||||
};
|
||||
window.addEventListener("storage", handler);
|
||||
return () => window.removeEventListener("storage", handler);
|
||||
};
|
||||
|
||||
const readPalette = (): Palette => {
|
||||
try {
|
||||
const saved = localStorage.getItem(PALETTE_KEY);
|
||||
return PALETTES.find((p) => p.key === saved)?.key ?? DEFAULT_PALETTE;
|
||||
} catch {
|
||||
return DEFAULT_PALETTE;
|
||||
}
|
||||
};
|
||||
|
||||
export default function BbsTerminal() {
|
||||
const [state, dispatch] = useReducer(execute, initialState);
|
||||
const palette = useSyncExternalStore(subscribe, readPalette, () => DEFAULT_PALETTE);
|
||||
const [input, setInput] = useState("");
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
const mounted = useRef(false);
|
||||
|
||||
// 화면 이동을 브라우저 history에 기록 → 스와이프/뒤로가기 버튼/Alt+←가 그대로 앞뒤 이동이 됨
|
||||
useEffect(() => {
|
||||
const nav: Nav = { screen: state.screen, list: state.list, postId: state.postId };
|
||||
if (!mounted.current) {
|
||||
mounted.current = true;
|
||||
history.replaceState({ bbs: nav }, ""); // 새로고침 시 이전 기록 대신 현재 화면으로 시작
|
||||
} else if (JSON.stringify(history.state?.bbs) !== JSON.stringify(nav)) {
|
||||
history.pushState({ bbs: nav }, "");
|
||||
}
|
||||
}, [state.screen, state.list, state.postId]);
|
||||
|
||||
useEffect(() => {
|
||||
const onPop = (e: PopStateEvent) => {
|
||||
if (e.state?.bbs) dispatch(e.state.bbs);
|
||||
};
|
||||
addEventListener("popstate", onPop);
|
||||
return () => removeEventListener("popstate", onPop);
|
||||
}, []);
|
||||
|
||||
const changePalette = (p: Palette) => {
|
||||
try {
|
||||
localStorage.setItem(PALETTE_KEY, p);
|
||||
} catch {}
|
||||
window.dispatchEvent(new StorageEvent("storage")); // 같은 탭은 storage 이벤트가 안 오므로 직접 알림
|
||||
};
|
||||
|
||||
// 클릭 = 명령어 대리 입력: 입력창에 채운 뒤 엔터와 같은 경로로 실행
|
||||
const submitFromClick = (cmd: string) => {
|
||||
setInput(cmd);
|
||||
// 터치 기기에서 포커스하면 가상 키보드가 튀어나와 화면을 가림 → 마우스 환경에서만
|
||||
if (matchMedia("(pointer: fine)").matches) inputRef.current?.focus();
|
||||
setTimeout(() => {
|
||||
dispatch(cmd);
|
||||
setInput("");
|
||||
}, 160);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className={styles.shell}>
|
||||
<PaletteSwitch value={palette} onChange={changePalette} />
|
||||
|
||||
<div className={styles.frame}>
|
||||
<div className={styles.titlebar}>
|
||||
<span>SPECIALSOURCE-BBS v3.2</span>
|
||||
<span>{state.loggedIn ? "ONLINE: 7 · admin 접속중" : "ONLINE: 6 · 비로그인"}</span>
|
||||
</div>
|
||||
|
||||
<div className={styles.screen}>
|
||||
{state.screen === "main" && <MainMenu readIds={state.readIds} onSelect={submitFromClick} />}
|
||||
{state.screen === "list" && <PostList kind={state.list} readIds={state.readIds} onSelect={submitFromClick} />}
|
||||
{state.screen === "post" && state.postId !== null && <PostDetail postId={state.postId} />}
|
||||
{state.error && <div className={`${styles.sub} ${styles.error}`}>{state.error}</div>}
|
||||
</div>
|
||||
|
||||
<div className={styles.cmdline}>
|
||||
<span className={styles.prompt}>></span>
|
||||
<input
|
||||
ref={inputRef}
|
||||
type="text"
|
||||
value={input}
|
||||
onChange={(e) => setInput(e.target.value)}
|
||||
onKeyDown={(e) => {
|
||||
if (e.key !== "Enter" || e.nativeEvent.isComposing) return;
|
||||
dispatch(input);
|
||||
setInput("");
|
||||
}}
|
||||
placeholder="번호 또는 메뉴명을 입력하세요"
|
||||
aria-label="BBS 명령어 입력"
|
||||
autoComplete="off"
|
||||
autoFocus
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user