Implement ChessUp server, vanilla TS UI, Stockfish analytics, and Docker/Cloudflared setup
This commit is contained in:
commit
e2e085b341
52 changed files with 15730 additions and 0 deletions
235
src/client/components/navbar.ts
Normal file
235
src/client/components/navbar.ts
Normal file
|
|
@ -0,0 +1,235 @@
|
|||
import type { UserSummary } from '../../shared/types';
|
||||
import {
|
||||
type BoardTheme,
|
||||
type UiTheme,
|
||||
applyBoardTheme,
|
||||
applyUiTheme,
|
||||
clearNode,
|
||||
el,
|
||||
getSavedBoardTheme,
|
||||
getSavedUiTheme
|
||||
} from '../dom';
|
||||
import { sound } from '../sound';
|
||||
|
||||
export interface NavbarState {
|
||||
user: UserSummary | null;
|
||||
currentPath: string;
|
||||
friendBadgeCount: number;
|
||||
activeGameId: string | null;
|
||||
}
|
||||
|
||||
export interface NavbarActions {
|
||||
onNavigate: (path: string) => void;
|
||||
onLogout: () => void;
|
||||
}
|
||||
|
||||
export class Navbar {
|
||||
private settingsOpen = false;
|
||||
private uiTheme: UiTheme = getSavedUiTheme();
|
||||
private boardTheme: BoardTheme = getSavedBoardTheme();
|
||||
|
||||
constructor(
|
||||
private readonly container: HTMLElement,
|
||||
private readonly actions: NavbarActions
|
||||
) {
|
||||
applyUiTheme(this.uiTheme);
|
||||
applyBoardTheme(this.boardTheme);
|
||||
document.addEventListener('click', (e) => {
|
||||
if (this.settingsOpen && !this.container.contains(e.target as Node)) {
|
||||
this.settingsOpen = false;
|
||||
this.render(this.lastState);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private lastState: NavbarState = {
|
||||
user: null,
|
||||
currentPath: '/',
|
||||
friendBadgeCount: 0,
|
||||
activeGameId: null
|
||||
};
|
||||
|
||||
render(state: NavbarState): void {
|
||||
this.lastState = state;
|
||||
clearNode(this.container);
|
||||
|
||||
const inner = el('div', { className: 'nav-inner' });
|
||||
|
||||
// Brand
|
||||
const brand = el('a', { href: '/', className: 'nav-brand' }, 'ChessUp');
|
||||
brand.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
this.actions.onNavigate('/');
|
||||
});
|
||||
inner.appendChild(brand);
|
||||
|
||||
// Primary nav links
|
||||
const links = el('nav', { className: 'nav-links' });
|
||||
const navItems: Array<{ href: string; label: string; badge?: number; match: (p: string) => boolean }> = [
|
||||
{ href: '/', label: 'Lobby', match: (p) => p === '/' },
|
||||
{ href: '/bots', label: 'Play Bot', match: (p) => p.indexOf('/bots') === 0 },
|
||||
{ href: '/analysis', label: 'Analysis', match: (p) => p.indexOf('/analysis') === 0 },
|
||||
{
|
||||
href: '/friends',
|
||||
label: 'Friends',
|
||||
badge: state.friendBadgeCount > 0 ? state.friendBadgeCount : undefined,
|
||||
match: (p) => p.indexOf('/friends') === 0
|
||||
},
|
||||
{ href: '/leaderboard', label: 'Leaderboard', match: (p) => p.indexOf('/leaderboard') === 0 }
|
||||
];
|
||||
|
||||
for (const item of navItems) {
|
||||
const a = el('a', {
|
||||
href: item.href,
|
||||
className: item.match(state.currentPath) ? 'nav-link active' : 'nav-link'
|
||||
});
|
||||
a.appendChild(document.createTextNode(item.label));
|
||||
if (item.badge !== undefined) {
|
||||
a.appendChild(el('span', { className: 'nav-badge' }, item.badge));
|
||||
}
|
||||
a.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
this.actions.onNavigate(item.href);
|
||||
});
|
||||
links.appendChild(a);
|
||||
}
|
||||
|
||||
if (state.activeGameId && state.currentPath !== `/game/${state.activeGameId}`) {
|
||||
const resume = el('a', { href: `/game/${state.activeGameId}`, className: 'nav-resume' }, 'Resume Game');
|
||||
resume.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
this.actions.onNavigate(`/game/${state.activeGameId}`);
|
||||
});
|
||||
links.appendChild(resume);
|
||||
}
|
||||
|
||||
inner.appendChild(links);
|
||||
|
||||
// Right side: settings + account
|
||||
const right = el('div', { className: 'nav-right' });
|
||||
|
||||
const settingsWrap = el('div', { className: 'nav-settings-wrap' });
|
||||
const settingsBtn = el(
|
||||
'button',
|
||||
{ type: 'button', className: 'btn btn-sm btn-ghost', title: 'Appearance & Sound' },
|
||||
'Prefs'
|
||||
);
|
||||
settingsBtn.addEventListener('click', (e) => {
|
||||
e.stopPropagation();
|
||||
this.settingsOpen = !this.settingsOpen;
|
||||
this.render(this.lastState);
|
||||
});
|
||||
settingsWrap.appendChild(settingsBtn);
|
||||
|
||||
if (this.settingsOpen) {
|
||||
settingsWrap.appendChild(this.buildSettingsPopover());
|
||||
}
|
||||
right.appendChild(settingsWrap);
|
||||
|
||||
if (state.user) {
|
||||
const profileLink = el(
|
||||
'a',
|
||||
{ href: `/u/${encodeURIComponent(state.user.username)}`, className: 'nav-user-link' },
|
||||
state.user.username
|
||||
);
|
||||
profileLink.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
this.actions.onNavigate(`/u/${encodeURIComponent(state.user!.username)}`);
|
||||
});
|
||||
right.appendChild(profileLink);
|
||||
|
||||
const logoutBtn = el('button', { type: 'button', className: 'btn btn-sm btn-ghost' }, 'Sign out');
|
||||
logoutBtn.addEventListener('click', () => this.actions.onLogout());
|
||||
right.appendChild(logoutBtn);
|
||||
} else {
|
||||
const loginLink = el('a', { href: '/login', className: 'btn btn-sm btn-primary' }, 'Sign in');
|
||||
loginLink.addEventListener('click', (e) => {
|
||||
e.preventDefault();
|
||||
this.actions.onNavigate('/login');
|
||||
});
|
||||
right.appendChild(loginLink);
|
||||
}
|
||||
|
||||
inner.appendChild(right);
|
||||
this.container.appendChild(inner);
|
||||
}
|
||||
|
||||
private buildSettingsPopover(): HTMLElement {
|
||||
const pop = el('div', { className: 'settings-popover' });
|
||||
pop.addEventListener('click', (e) => e.stopPropagation());
|
||||
|
||||
// UI Theme
|
||||
const themeRow = el('div', { className: 'settings-row' });
|
||||
themeRow.appendChild(el('span', { className: 'settings-label' }, 'UI Theme'));
|
||||
const themeBtns = el('div', { className: 'btn-group' });
|
||||
for (const t of ['dark', 'light'] as UiTheme[]) {
|
||||
const b = el(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: this.uiTheme === t ? 'btn btn-xs btn-primary' : 'btn btn-xs'
|
||||
},
|
||||
t === 'dark' ? 'Dark' : 'Light'
|
||||
);
|
||||
b.addEventListener('click', () => {
|
||||
this.uiTheme = t;
|
||||
applyUiTheme(t);
|
||||
this.render(this.lastState);
|
||||
});
|
||||
themeBtns.appendChild(b);
|
||||
}
|
||||
themeRow.appendChild(themeBtns);
|
||||
pop.appendChild(themeRow);
|
||||
|
||||
// Board Theme
|
||||
const boardRow = el('div', { className: 'settings-row' });
|
||||
boardRow.appendChild(el('span', { className: 'settings-label' }, 'Board'));
|
||||
const boardBtns = el('div', { className: 'btn-group' });
|
||||
const boards: Array<{ id: BoardTheme; label: string }> = [
|
||||
{ id: 'brown', label: 'Brown' },
|
||||
{ id: 'green', label: 'Green' },
|
||||
{ id: 'blue', label: 'Blue' },
|
||||
{ id: 'mono', label: 'Mono' }
|
||||
];
|
||||
for (const bt of boards) {
|
||||
const b = el(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: this.boardTheme === bt.id ? 'btn btn-xs btn-primary' : 'btn btn-xs'
|
||||
},
|
||||
bt.label
|
||||
);
|
||||
b.addEventListener('click', () => {
|
||||
this.boardTheme = bt.id;
|
||||
applyBoardTheme(bt.id);
|
||||
this.render(this.lastState);
|
||||
});
|
||||
boardBtns.appendChild(b);
|
||||
}
|
||||
boardRow.appendChild(boardBtns);
|
||||
pop.appendChild(boardRow);
|
||||
|
||||
// Sound toggle
|
||||
const soundRow = el('div', { className: 'settings-row' });
|
||||
soundRow.appendChild(el('span', { className: 'settings-label' }, 'Sounds'));
|
||||
const muted = sound.isMuted();
|
||||
const soundBtn = el(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
className: !muted ? 'btn btn-xs btn-primary' : 'btn btn-xs'
|
||||
},
|
||||
muted ? 'Muted' : 'On'
|
||||
);
|
||||
soundBtn.addEventListener('click', () => {
|
||||
sound.setMuted(!muted);
|
||||
if (muted) sound.play('move');
|
||||
this.render(this.lastState);
|
||||
});
|
||||
soundRow.appendChild(soundBtn);
|
||||
pop.appendChild(soundRow);
|
||||
|
||||
return pop;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue