24 lines
3.1 KiB
TypeScript
24 lines
3.1 KiB
TypeScript
import { createIcons, ArrowDown, ArrowRight, ArrowUpRight, Check, Globe2, HeartPulse, LogIn, LogOut, MapPin, MoreHorizontal, Play, Plus, RefreshCw, ScrollText, Search, ShieldCheck, Users, X } from 'lucide';
|
|
import type { Character } from './generated';
|
|
import './theme';
|
|
|
|
export type RecordCharacter = { id: string; account: string; worldId: string; name: string; state: string; character: { view: Character; bornAt: string; diedAt?: string; deathReason?: string; dailySeconds: number } };
|
|
export type World = { id: string; kind: string; phase: string; living: number; dead: number; capacity: number; createdAt: string; bossAt: string; closed: boolean; available: boolean; revision: number; catalogVersion: number };
|
|
export type Command = { id: string; worldId: string; actor: string; op: string; target: string; value: string; reason: string; status: string; result: string; createdAt: number; completedAt: number };
|
|
export const $ = <T extends HTMLElement = HTMLElement>(id: string) => document.getElementById(id) as T;
|
|
export const el = <K extends keyof HTMLElementTagNameMap>(tag: K, text = '', className = '') => { const node = document.createElement(tag); node.textContent = text; node.className = className; return node; };
|
|
export function icons() { createIcons({ icons: { ArrowDown, ArrowRight, ArrowUpRight, Check, Globe2, HeartPulse, LogIn, LogOut, MapPin, MoreHorizontal, Play, Plus, RefreshCw, ScrollText, Search, ShieldCheck, Users, X } }); }
|
|
export function icon(name: string) { const node = el('i'); node.dataset.lucide = name; return node; }
|
|
export function badge(state: string) { return el('span', state.replaceAll('-', ' '), `badge state-${state}`); }
|
|
export function date(value: string | number) { return value ? new Date(typeof value === 'number' ? value * 1000 : value).toLocaleString([], { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }) : 'Unknown'; }
|
|
export function gameURL(world: string) { return `/game?world=${encodeURIComponent(world)}`; }
|
|
export class APIError extends Error { constructor(message: string, public status: number) { super(message); } }
|
|
export async function api<T>(path: string, body?: unknown): Promise<T> {
|
|
const response = await fetch(path, { method: body === undefined ? 'GET' : 'POST', credentials: 'same-origin', signal: AbortSignal.timeout(15000), headers: body === undefined ? {} : { 'Content-Type': 'application/json' }, body: body === undefined ? undefined : JSON.stringify(body) });
|
|
const value = await response.json().catch(() => ({ error: `Request failed (${response.status})` }));
|
|
if (!response.ok) throw new APIError(value.error || `Request failed (${response.status})`, response.status);
|
|
return value;
|
|
}
|
|
export function table(headers: string[]) { const node = el('table'); const head = node.createTHead().insertRow(); for (const label of headers) { const th = el('th', label); th.scope = 'col'; head.append(th); } const body = node.createTBody(); return { node, body }; }
|
|
export function cell(row: HTMLTableRowElement, content: string | Node) { const td = row.insertCell(); typeof content === 'string' ? td.textContent = content : td.append(content); return td; }
|