@hadoku/game-ui 0.1.106 → 0.1.108

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,25 @@
1
+ import { type ReactNode } from 'react';
2
+ import { type AppHeaderProps } from '@wolffm/task-ui-components';
3
+ import { type Who } from '../room/whoami.js';
4
+ /** What the frame resolved, for the game's own layout. */
5
+ export interface GameShell {
6
+ /** The viewer, or null while asked and when the fleet could not say. */
7
+ who: Who | null;
8
+ narrow: boolean;
9
+ touchFirst: boolean;
10
+ isDarkTheme: boolean;
11
+ }
12
+ export interface GameAppShellProps {
13
+ /** The `<main>`'s class: the game's own container (`bomberman-container`). */
14
+ className: string;
15
+ children: (shell: GameShell) => ReactNode;
16
+ }
17
+ export declare function GameAppShell(props: GameAppShellProps): import("react").JSX.Element;
18
+ /**
19
+ * The fleet's `<AppHeader>`, handed the identity the page already resolved, so the settings popout
20
+ * does not fetch whoami a second time. An identity HINT, never a way to present a tier the edge did
21
+ * not assign: every gate that matters is enforced server-side off the edge-stamped tier.
22
+ */
23
+ export declare function GameHeader({ children, appTabs, ...props }: Omit<AppHeaderProps, 'settingsProps'>): import("react").JSX.Element;
24
+ /** The `appTabs` id `GameHeader` adds the chat sound switch to. */
25
+ export declare const SOUND_TAB_ID = "sound";
@@ -0,0 +1,66 @@
1
+ import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
2
+ /*
3
+ * A game's standalone page frame: everything a game's `App.tsx` would otherwise copy.
4
+ *
5
+ * Three things resolve ONCE, here, and are handed down:
6
+ *
7
+ * the theme `<HadokuThemeRoot>` owns the theme, its persistence and the picker. A game adds no
8
+ * `useTheme` of its own: the fleet's apps each carried a copy once, and they drifted
9
+ * into real bugs.
10
+ * the identity one `/session/whoami` (`../room/whoami.ts`), handed to every `useWhoami` below
11
+ * through `WhoProvider`: the header's settings popout (`GameHeader`) and the room
12
+ * chrome (`../room/RoomShell.tsx`) read the same answer.
13
+ * the platform `usePlatform`'s `narrow` and `touchFirst`, rendered as `data-narrow` /
14
+ * `data-touch-first` on the `<main>` from the first paint, so the game's CSS keys
15
+ * off the same source as its JS. (`usePlatform` stamps them itself, but in an effect
16
+ * that runs on a change, and on the first render the `<main>` does not exist yet:
17
+ * the skeleton holds the theme's first resolve. On a phone the stamp found no element
18
+ * and never came back.)
19
+ *
20
+ * The skeleton holds the page through the theme's first resolve, so it never flashes the wrong
21
+ * palette. Inside, the game lays out its own column: `children` is called with what was resolved.
22
+ *
23
+ * This is the standalone page only. Embedded in a host's page the room and the theme are the host's,
24
+ * and a game draws none of this (`@hadoku/ui-shell/embed`'s `mountEmbed`).
25
+ */
26
+ import { lazy, Suspense, useRef } from 'react';
27
+ import { AppHeader, LoadingSkeleton } from '@wolffm/task-ui-components';
28
+ import { HadokuThemeRoot, useHadokuTheme, usePlatform } from '@wolffm/themes';
29
+ import { useResolvedWhoami, useWhoami, WhoProvider } from "../room/whoami.js";
30
+ export function GameAppShell(props) {
31
+ const containerRef = useRef(null);
32
+ return (_jsx(HadokuThemeRoot, { containerRef: containerRef, children: _jsx(ShellInner, { containerRef: containerRef, ...props }) }));
33
+ }
34
+ function ShellInner({ containerRef, className, children }) {
35
+ const { isDarkTheme, isThemeReady, isInitialThemeLoad } = useHadokuTheme();
36
+ const { narrow, touchFirst } = usePlatform({ containerRef });
37
+ const who = useResolvedWhoami();
38
+ if (isInitialThemeLoad && !isThemeReady)
39
+ return _jsx(LoadingSkeleton, { isDarkTheme: isDarkTheme });
40
+ return (_jsx(WhoProvider, { who: who, children: _jsx("main", { ref: containerRef, className: className, "data-dark-theme": isDarkTheme ? 'true' : 'false', "data-narrow": narrow ? '' : undefined, "data-touch-first": touchFirst ? '' : undefined, children: children({ who: who === 'pending' ? null : who, narrow, touchFirst, isDarkTheme }) }) }));
41
+ }
42
+ /**
43
+ * The fleet's `<AppHeader>`, handed the identity the page already resolved, so the settings popout
44
+ * does not fetch whoami a second time. An identity HINT, never a way to present a tier the edge did
45
+ * not assign: every gate that matters is enforced server-side off the edge-stamped tier.
46
+ */
47
+ export function GameHeader({ children, appTabs, ...props }) {
48
+ const who = useWhoami();
49
+ const viewer = who === 'pending' ? null : who;
50
+ // The room chat's sound switch goes beside the game's own sound settings when it has a Sound tab,
51
+ // else in the App tab: never a tab of its own for one row.
52
+ const soundTab = appTabs?.some((t) => t.id === SOUND_TAB_ID) ?? false;
53
+ return (_jsx(AppHeader, { ...props, settingsProps: viewer === null ? undefined : { userType: viewer.edgeTier, name: viewer.name }, appTabs: soundTab ? appTabs.map((t) => (t.id === SOUND_TAB_ID ? { ...t, render: () => _jsxs(_Fragment, { children: [t.render(), chatSound] }) } : t)) : appTabs,
54
+ // Passed only when it holds something: any `children` at all draws an App tab.
55
+ children: soundTab ? children : _jsxs(_Fragment, { children: [children, chatSound] }) }));
56
+ }
57
+ /** The `appTabs` id `GameHeader` adds the chat sound switch to. */
58
+ export const SOUND_TAB_ID = 'sound';
59
+ /**
60
+ * The room chat's "Chat sound" on/off (watchparty's chime after a quiet minute). It is watchparty's,
61
+ * from the same room-chrome module `RoomShell` loads, so it writes the store that chrome's chime
62
+ * reads. Lazy: the popout renders a tab only while it is open, so the chunk is fetched then (and
63
+ * usually already is, for the room).
64
+ */
65
+ const ChatSoundSetting = lazy(() => import('@wolffm/watchparty-ui/room-chrome').then((m) => ({ default: m.ChatSoundSetting })));
66
+ const chatSound = (_jsx(Suspense, { fallback: null, children: _jsx(ChatSoundSetting, {}) }));
@@ -0,0 +1,44 @@
1
+ import type { HadokuThemeValue } from '@wolffm/themes';
2
+ import { type Fetch } from '../http.js';
3
+ export type BindOutcome = {
4
+ ok: true;
5
+ roomId: string;
6
+ } | {
7
+ ok: false;
8
+ message: string;
9
+ };
10
+ /** Which of this game's rooms a host room plays in. Never throws: a failure is an outcome the embed renders. */
11
+ export type BindRoom = (hostRoom: string) => Promise<BindOutcome>;
12
+ /** The game server's own answer (room-core's binding, through game-ui's room client), over `fetchImpl`. */
13
+ export declare function bindWatchpartyRoom(fetchImpl?: Fetch): BindRoom;
14
+ export type Binding = {
15
+ state: 'binding';
16
+ } | {
17
+ state: 'bound';
18
+ roomId: string;
19
+ } | {
20
+ state: 'failed';
21
+ message: string;
22
+ };
23
+ /**
24
+ * The room `hostRoom` plays in, asked of `bind` whenever `hostRoom` changes. With `skip` the host's
25
+ * room key IS the room: `bind` is never called, and the answer is derived rather than stored, so it
26
+ * cannot drift from `skip` or `hostRoom` changing.
27
+ */
28
+ export declare function useBoundRoom(hostRoom: string, bind: BindRoom, skip?: boolean): Binding;
29
+ /**
30
+ * A route that is not the bound room goes to it: `go` is the game's own navigation. An effect,
31
+ * never a write during render. `go` should be stable (a module-level function).
32
+ */
33
+ export declare function ToRoom({ roomId, go }: {
34
+ roomId: string;
35
+ go: (roomId: string) => void;
36
+ }): null;
37
+ /** Whether a fleet theme name is one of the dark ones. */
38
+ export declare function isDarkThemeName(theme: string): boolean;
39
+ /**
40
+ * The theme context for a tree whose theme is the HOST's: anything below reading `useHadokuTheme()`
41
+ * reads the host's theme rather than throwing. Changing it is the host's, so `setTheme` is inert and
42
+ * there is no picker.
43
+ */
44
+ export declare function hostThemeValue(theme: string): HadokuThemeValue;
@@ -0,0 +1,79 @@
1
+ /*
2
+ * The room a game's embed plays in, inside a host's room (watchparty's game slot).
3
+ *
4
+ * The game's own server answers which of its rooms a host room plays in (`POST /api/rooms/watchparty`,
5
+ * `@hadoku/game-server`'s room-core route), deriving it from the host's room id, so every member's
6
+ * bundle lands at the same table without any of them choosing it. The host's room id is opaque here:
7
+ * the server hashes it, so its shape need not fit a game's room-id rules.
8
+ *
9
+ * A host that runs the match itself (a `MatchChannel` in the bundle context) needs no binding: its
10
+ * room key IS the room (`skip`).
11
+ */
12
+ import { useEffect, useState } from 'react';
13
+ import { THEME_FAMILIES } from '@wolffm/themes';
14
+ import { ROOM_ID_RE } from '@wolffm/platform-wire/room-feed';
15
+ import { isRecord, pageFetch } from "../http.js";
16
+ import { roomCoreClient } from "../rooms/room-core-client.js";
17
+ /** A bound room's id, and only one this game's server could have made (`ROOM_ID_RE`): anything else is not a room. */
18
+ const boundRoomId = (body) => (isRecord(body) && typeof body.id === 'string' && ROOM_ID_RE.test(body.id) ? body.id : null);
19
+ /** The game server's own answer (room-core's binding, through game-ui's room client), over `fetchImpl`. */
20
+ export function bindWatchpartyRoom(fetchImpl = pageFetch) {
21
+ const rooms = roomCoreClient({ parse: boundRoomId, fetch: fetchImpl });
22
+ return async (hostRoom) => {
23
+ const outcome = await rooms.bindWatchparty(hostRoom);
24
+ return outcome.ok ? { ok: true, roomId: outcome.room } : outcome;
25
+ };
26
+ }
27
+ /**
28
+ * The room `hostRoom` plays in, asked of `bind` whenever `hostRoom` changes. With `skip` the host's
29
+ * room key IS the room: `bind` is never called, and the answer is derived rather than stored, so it
30
+ * cannot drift from `skip` or `hostRoom` changing.
31
+ */
32
+ export function useBoundRoom(hostRoom, bind, skip = false) {
33
+ const [binding, setBinding] = useState({ state: 'binding' });
34
+ useEffect(() => {
35
+ if (skip)
36
+ return;
37
+ let live = true;
38
+ setBinding({ state: 'binding' });
39
+ void bind(hostRoom).then((outcome) => {
40
+ if (!live)
41
+ return;
42
+ setBinding(outcome.ok ? { state: 'bound', roomId: outcome.roomId } : { state: 'failed', message: outcome.message });
43
+ });
44
+ return () => {
45
+ live = false;
46
+ };
47
+ }, [hostRoom, bind, skip]);
48
+ return skip ? { state: 'bound', roomId: hostRoom } : binding;
49
+ }
50
+ /**
51
+ * A route that is not the bound room goes to it: `go` is the game's own navigation. An effect,
52
+ * never a write during render. `go` should be stable (a module-level function).
53
+ */
54
+ export function ToRoom({ roomId, go }) {
55
+ useEffect(() => {
56
+ go(roomId);
57
+ }, [roomId, go]);
58
+ return null;
59
+ }
60
+ /** Whether a fleet theme name is one of the dark ones. */
61
+ export function isDarkThemeName(theme) {
62
+ return theme === 'dark' || theme.endsWith('-dark');
63
+ }
64
+ /**
65
+ * The theme context for a tree whose theme is the HOST's: anything below reading `useHadokuTheme()`
66
+ * reads the host's theme rather than throwing. Changing it is the host's, so `setTheme` is inert and
67
+ * there is no picker.
68
+ */
69
+ export function hostThemeValue(theme) {
70
+ return {
71
+ theme,
72
+ setTheme: () => undefined,
73
+ themeFamilies: [...THEME_FAMILIES],
74
+ getThemeIcon: () => null,
75
+ isDarkTheme: isDarkThemeName(theme),
76
+ isThemeReady: true,
77
+ isInitialThemeLoad: false,
78
+ };
79
+ }