업데이트 알림(joke-app과 같게): 웹 빌드 id를 index.html meta(mq-build)에 넣고 서버가 /api/version·welcome으로 알려 줌, 다르면 위쪽에 '업데이트 내용이 있습니다 · 지금 새로고침하기 · ✕' 카드(소켓 다시 연결될 때·5분마다·탭으로 돌아올 때 확인)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-19 22:28:38 +09:00
parent 127513db63
commit 6ed669d5b4
11 changed files with 185 additions and 8 deletions

View File

@@ -2,6 +2,7 @@
import { useEffect, useRef, type ReactNode } from 'react';
import type { PublicUser } from '@mq/shared/lite';
import { useToasts } from '../store/toast';
import { dismissUpdate, useUpdateAvailable } from '../net/update';
/** Per-user fixed colour; skips the 60°~160° green band so names never look like the 정답 green (docs/03). */
export function userHue(id: string): number {
@@ -99,3 +100,20 @@ export function DiscordMark() {
</svg>
);
}
/** The site was updated while this page was open (same as joke-app): refresh, or ✕ to close until the next update. */
export function UpdateBanner() {
const show = useUpdateAvailable();
if (!show) return null;
return (
<div className="update-banner" role="alert">
<span className="update-text">업데이트 내용이 있습니다.</span>
<button type="button" className="btn btn-primary btn-sm" onClick={() => location.reload()}>
지금 새로고침하기
</button>
<button type="button" className="update-close" onClick={dismissUpdate} aria-label="업데이트 알림 닫기">
✕
</button>
</div>
);
}

View File

@@ -3,7 +3,8 @@ import { createRoot } from 'react-dom/client';
import { BrowserRouter, Route, Routes, useLocation, useNavigate } from 'react-router';
import 'pretendard/dist/web/variable/pretendardvariable-dynamic-subset.css';
import './styles/global.css';
import { Toasts } from './components/ui';
import { Toasts, UpdateBanner } from './components/ui';
import { initUpdateCheck } from './net/update';
import { SiteHeader } from './components/SiteHeader';
import { useSession } from './store/session';
import { toast } from './store/toast';
@@ -50,6 +51,7 @@ function Shell() {
<SiteHeader />
<Announcement />
<Toasts />
<UpdateBanner />
<Suspense fallback={<div className="page muted">불러오는 중…</div>}>
<Routes>
<Route path="/" element={<Home />} />
@@ -63,6 +65,8 @@ function Shell() {
);
}
initUpdateCheck();
createRoot(document.getElementById('root')!).render(
<StrictMode>
<BrowserRouter>

View File

@@ -5,6 +5,7 @@ import { create } from 'zustand';
import type { ChatLine, GameView, RoomView, ServerMessage } from '@mq/shared/lite';
import { toast } from '../store/toast';
import { player } from './player';
import { noteServerBuild } from './update';
type Status = 'idle' | 'connecting' | 'open' | 'reconnecting';
@@ -79,6 +80,7 @@ function onMessage(m: ServerMessage): void {
switch (m.t) {
case 'welcome': {
myId = m.userId;
noteServerBuild(m.build);
useRoom.setState({ status: 'open', offset: m.serverNow - Date.now() });
retry = 0;
syncClock();

View File

@@ -0,0 +1,54 @@
/**
* "업데이트 내용이 있습니다" (same as joke-app). The page knows its own build (`<meta name="mq-build">`, written by
* vite.config.ts); the server tells its build in `welcome` and at `/api/version`. When they differ the page offers a
* refresh at the top; ✕ hides it until the next update. Dev builds have no meta and never ask.
*/
import { useSyncExternalStore } from 'react';
const POLL_MS = 5 * 60_000;
const mine = typeof document === 'undefined' ? null : (document.querySelector<HTMLMetaElement>('meta[name="mq-build"]')?.content ?? null);
/** Build the server has that this page doesn't. */
let latest: string | null = null;
let dismissed: string | null = null;
const listeners = new Set<() => void>();
const emit = () => listeners.forEach((l) => l());
/** The server's build (welcome message, `/api/version`). */
export function noteServerBuild(build: string | null | undefined): void {
if (!mine || !build || build === mine || build === latest) return;
latest = build;
emit();
}
export function dismissUpdate(): void {
dismissed = latest;
emit();
}
async function check(): Promise<void> {
try {
const res = await fetch('/api/version', { cache: 'no-store' });
if (res.ok) noteServerBuild(((await res.json()) as { build?: string | null }).build);
} catch {
// Offline or restarting: the next check (or the socket's welcome) will tell.
}
}
/** Call once at startup: checks now and then, and whenever the tab comes back into view. */
export function initUpdateCheck(): void {
if (!mine) return;
setInterval(() => void check(), POLL_MS);
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') void check();
});
}
const subscribe = (l: () => void) => {
listeners.add(l);
return () => void listeners.delete(l);
};
export function useUpdateAvailable(): boolean {
return useSyncExternalStore(subscribe, () => latest !== null && latest !== dismissed, () => false);
}

View File

@@ -170,6 +170,18 @@ textarea.input { padding: 10px 14px; min-height: 90px; resize: vertical; }
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.announcement { background: var(--primary-soft); padding: 8px 16px; text-align: center; font-weight: 700; border-bottom: 1.5px solid var(--line); }
.announcement.maintenance { background: var(--warning-soft); }
/* 업데이트 알림 (same as joke-app): a card at the top, never reloads by itself */
.update-banner {
position: fixed; top: calc(8px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); z-index: 70;
width: min(94vw, 480px); display: flex; align-items: center; gap: 10px;
background: var(--surface); color: var(--text); border: 2px solid var(--primary); border-radius: var(--radius);
box-shadow: 4px 4px 0 var(--ink); padding: 10px 8px 10px 16px; animation: update-in 0.2s ease;
}
@keyframes update-in { from { opacity: 0; transform: translate(-50%, -8px); } }
.update-banner .update-text { flex: 1; min-width: 0; font-weight: 700; }
.update-banner .btn { white-space: nowrap; }
.update-close { flex: none; width: 40px; height: 40px; border: 0; background: transparent; color: var(--text-sub); font-size: 1.2rem; cursor: pointer; border-radius: var(--radius-sm); }
.update-close:hover { background: var(--surface-2); }
/* ---------------------------------------------------------------- pages */
.page { max-width: 1440px; margin: 0 auto; padding: 28px 40px; }