@roster-lock/match-agent 0.2.0 → 0.4.0
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.
- package/client/dist/assets/index-D2395vmC.css +1 -0
- package/client/dist/assets/index-DcKsa72V.js +125 -0
- package/client/dist/index.html +2 -2
- package/client/e2e/lib/env.ts +50 -0
- package/client/e2e/lib/fileDialog.ts +31 -0
- package/client/e2e/lib/mockGamepad.ts +71 -0
- package/client/e2e/lib/onscreenKeyboard.ts +32 -0
- package/client/e2e/playwright.config.ts +48 -0
- package/client/e2e/tests/gamepad-only.spec.ts +274 -0
- package/client/e2e/tests/keyboard-only.spec.ts +67 -0
- package/client/e2e/tsconfig.json +15 -0
- package/client/node_modules/.bin/playwright +17 -0
- package/client/node_modules/.bin/roster-lock-plugin-manager +17 -0
- package/client/node_modules/.vite/deps/@rjsf_core.js +95 -95
- package/client/node_modules/.vite/deps/@rjsf_validator-ajv8.js +4 -3
- package/client/node_modules/.vite/deps/@roster-lock_ts-client.js +25 -25
- package/client/node_modules/.vite/deps/@roster-lock_types.js +2 -1
- package/client/node_modules/.vite/deps/@roster-lock_utils.js +2 -1
- package/client/node_modules/.vite/deps/_metadata.json +27 -27
- package/client/node_modules/.vite/deps/{chunk-NE7PXY4V.js → chunk-3HBL6DQ6.js} +99 -98
- package/client/node_modules/.vite/deps/{chunk-NE7PXY4V.js.map → chunk-3HBL6DQ6.js.map} +3 -3
- package/client/node_modules/.vite/deps/{chunk-4D2L7OBZ.js → chunk-3IRPLLFB.js} +498 -501
- package/client/node_modules/.vite/deps/{chunk-4D2L7OBZ.js.map → chunk-3IRPLLFB.js.map} +1 -1
- package/client/node_modules/.vite/deps/{chunk-I55GDNGV.js → chunk-7DRNB6P7.js} +5 -5
- package/client/node_modules/.vite/deps/{chunk-77TXW5ZT.js → chunk-FFVS6IUX.js} +6 -6
- package/client/node_modules/.vite/deps/{chunk-5KLROPUR.js → chunk-G44QLPFQ.js} +6 -6
- package/client/node_modules/.vite/deps/{chunk-IIDO6PKU.js → chunk-QTQN6EZX.js} +98 -98
- package/client/node_modules/.vite/deps/chunk-QTQN6EZX.js.map +7 -0
- package/client/node_modules/.vite/deps/chunk-WMTNK64S.js +1 -0
- package/client/node_modules/.vite/deps/react-dom.js +3 -2
- package/client/node_modules/.vite/deps/react-dom_client.js +12 -12
- package/client/node_modules/.vite/deps/react-dom_client.js.map +1 -1
- package/client/node_modules/.vite/deps/react-router-dom.js +42 -9
- package/client/node_modules/.vite/deps/react.js +2 -1
- package/client/node_modules/.vite/deps/react_jsx-dev-runtime.js +5 -5
- package/client/node_modules/.vite/deps/react_jsx-runtime.js +3 -2
- package/client/package.json +4 -1
- package/client/src/App.tsx +23 -14
- package/client/src/NavBar.tsx +15 -5
- package/client/src/api/matchAgent.ts +208 -5
- package/client/src/bridge/hostBridge.ts +39 -10
- package/client/src/components/FullScreenMenu.tsx +32 -0
- package/client/src/components/GameLauncherSettingsForm.tsx +80 -5
- package/client/src/components/InputHints.tsx +37 -0
- package/client/src/components/InstallGameLauncherLightbox.tsx +27 -14
- package/client/src/components/OnScreenFileDialog.tsx +83 -0
- package/client/src/components/OnScreenKeyboard.tsx +75 -0
- package/client/src/components/Selection/PieceCard.tsx +29 -4
- package/client/src/components/Selection/PiecePreviewPanel.tsx +48 -0
- package/client/src/components/Selection/PieceTypeSection.tsx +124 -46
- package/client/src/components/Selection/PlayerSelectionPanel.tsx +1 -5
- package/client/src/components/Selection/index.tsx +72 -47
- package/client/src/context/GlobalNavContext.tsx +340 -0
- package/client/src/context/SelectionSourceContext.tsx +26 -0
- package/client/src/hooks/useCursorInput.ts +66 -24
- package/client/src/hooks/useDownloadedPieceVersions.ts +39 -0
- package/client/src/hooks/useGridColumns.ts +29 -0
- package/client/src/hooks/useInView.ts +32 -0
- package/client/src/hooks/usePiecePreview.ts +124 -0
- package/client/src/pages/Controls/index.tsx +126 -0
- package/client/src/pages/Game/index.tsx +11 -15
- package/client/src/pages/MatchMaking/index.tsx +14 -7
- package/client/src/pages/PreviewRoster/index.tsx +118 -0
- package/client/src/styles/global.css +355 -6
- package/client/src/utils/inputLabels.ts +25 -0
- package/client/test-results/.last-run.json +4 -0
- package/client/tsconfig.tsbuildinfo +1 -1
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -1
- package/dist/config/inputBindings.js +40 -0
- package/dist/config/inputBindings.js.map +1 -0
- package/dist/config/manage.js +1 -0
- package/dist/config/manage.js.map +1 -1
- package/dist/handle-room/version-1/game-launcher/binaries.js +36 -0
- package/dist/handle-room/version-1/game-launcher/binaries.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/listing.js +24 -0
- package/dist/handle-room/version-1/game-launcher/listing.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/preview.js +38 -0
- package/dist/handle-room/version-1/game-launcher/preview.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/process.js +52 -0
- package/dist/handle-room/version-1/game-launcher/process.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/router.js +32 -0
- package/dist/handle-room/version-1/game-launcher/router.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/settings.js +49 -0
- package/dist/handle-room/version-1/game-launcher/settings.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/shared.js +28 -0
- package/dist/handle-room/version-1/game-launcher/shared.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/start.js +76 -0
- package/dist/handle-room/version-1/game-launcher/start.js.map +1 -0
- package/dist/handle-room/version-1/game-launcher/version.js +63 -0
- package/dist/handle-room/version-1/game-launcher/version.js.map +1 -0
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.js +22 -9
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.js.map +1 -1
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.js +50 -1
- package/dist/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.js.map +1 -1
- package/dist/handle-room/version-1/index.js +15 -15
- package/dist/handle-room/version-1/index.js.map +1 -1
- package/dist/handle-room/version-1/input-bindings.js +22 -0
- package/dist/handle-room/version-1/input-bindings.js.map +1 -0
- package/dist/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.js +2 -1
- package/dist/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/http-handler.js +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/http-handler.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/steps.js +2 -1
- package/dist/handle-room/version-1/room-handler-bridge/steps.js.map +1 -1
- package/dist/handle-room/version-1/room-handler-bridge/ws-handler.js +2 -2
- package/dist/handle-room/version-1/room-handler-bridge/ws-handler.js.map +1 -1
- package/dist/index.js +18 -4
- package/dist/index.js.map +1 -1
- package/dist/util-routers/file-system/env.js +3 -0
- package/dist/util-routers/file-system/env.js.map +1 -0
- package/dist/util-routers/file-system/index.js +31 -0
- package/dist/util-routers/file-system/index.js.map +1 -0
- package/dist/util-routers/file-system/list.js +50 -0
- package/dist/util-routers/file-system/list.js.map +1 -0
- package/dist/util-routers/file-system/resolve-within-root.js +18 -0
- package/dist/util-routers/file-system/resolve-within-root.js.map +1 -0
- package/dist/util-routers/file-system/roster-lock.js +56 -0
- package/dist/util-routers/file-system/roster-lock.js.map +1 -0
- package/dist/utils/pick-file.js +82 -0
- package/dist/utils/pick-file.js.map +1 -0
- package/dist/utils/pick-folder.js +84 -0
- package/dist/utils/pick-folder.js.map +1 -0
- package/package.json +10 -7
- package/src/config/index.ts +8 -3
- package/src/config/inputBindings.ts +56 -0
- package/src/config/manage.ts +7 -0
- package/src/handle-room/version-1/game-launcher/binaries.ts +52 -0
- package/src/handle-room/version-1/game-launcher/listing.ts +36 -0
- package/src/handle-room/version-1/game-launcher/preview.ts +54 -0
- package/src/handle-room/version-1/game-launcher/process.ts +75 -0
- package/src/handle-room/version-1/game-launcher/router.ts +42 -0
- package/src/handle-room/version-1/game-launcher/settings.ts +58 -0
- package/src/handle-room/version-1/game-launcher/shared.ts +34 -0
- package/src/handle-room/version-1/game-launcher/start.ts +113 -0
- package/src/handle-room/version-1/game-launcher/version.ts +85 -0
- package/src/handle-room/version-1/globals/FolderDB/SQLFolderDB/ensurePieceExists.ts +39 -14
- package/src/handle-room/version-1/globals/FolderDB/SQLFolderDB/schema.ts +83 -1
- package/src/handle-room/version-1/globals/FolderDB/types.ts +24 -0
- package/src/handle-room/version-1/globals/types.ts +24 -2
- package/src/handle-room/version-1/index.ts +27 -25
- package/src/handle-room/version-1/input-bindings.ts +19 -0
- package/src/handle-room/version-1/room-handler-bridge/exchange-and-download-selections.ts +4 -1
- package/src/handle-room/version-1/room-handler-bridge/http-handler.ts +4 -1
- package/src/handle-room/version-1/room-handler-bridge/steps.ts +8 -0
- package/src/handle-room/version-1/room-handler-bridge/ws-handler.ts +2 -2
- package/src/index.ts +42 -10
- package/src/util-routers/file-system/env.ts +7 -0
- package/src/util-routers/file-system/index.ts +23 -0
- package/src/util-routers/file-system/list.ts +62 -0
- package/src/util-routers/file-system/resolve-within-root.ts +25 -0
- package/src/util-routers/file-system/roster-lock.ts +74 -0
- package/src/utils/pick-file.ts +81 -0
- package/src/utils/pick-folder.ts +92 -0
- package/tests/integration/filesystem.test.ts +117 -0
- package/tests/integration/game-launcher-binaries.test.ts +263 -0
- package/tests/integration/game-launcher-game-complete.test.ts +111 -0
- package/tests/integration/game-launcher-preview.test.ts +107 -0
- package/tests/integration/game-launcher-settings.test.ts +123 -8
- package/tests/integration/game-processes-ws.test.ts +122 -0
- package/tests/integration/helpers/fakeFolderDB.ts +16 -0
- package/tests/integration/helpers/fakePluginRuntime.ts +10 -1
- package/tests/integration/helpers/relayHarness.ts +24 -0
- package/tests/integration/helpers/server.ts +23 -3
- package/tests/integration/input-bindings.test.ts +94 -0
- package/tests/integration/piece-ensure-ws.test.ts +1 -1
- package/tests/integration/piece-files.test.ts +1 -1
- package/tests/integration/relay-protocol.test.ts +100 -0
- package/tests/integration/roster-lock.test.ts +86 -0
- package/tests/integration/sync-dl.test.ts +1 -1
- package/vitest.config.ts +7 -1
- package/client/dist/assets/index-DeYshswm.js +0 -125
- package/client/dist/assets/index-gxAuIfGQ.css +0 -1
- package/client/node_modules/.vite/deps/chunk-IIDO6PKU.js.map +0 -7
- package/client/node_modules/.vite/deps_temp_ab51ebe9/package.json +0 -3
- package/dist/handle-room/version-1/game-launcher.js +0 -181
- package/dist/handle-room/version-1/game-launcher.js.map +0 -1
- package/src/handle-room/version-1/game-launcher.ts +0 -263
- /package/client/node_modules/.vite/deps/{chunk-I55GDNGV.js.map → chunk-7DRNB6P7.js.map} +0 -0
- /package/client/node_modules/.vite/deps/{chunk-77TXW5ZT.js.map → chunk-FFVS6IUX.js.map} +0 -0
- /package/client/node_modules/.vite/deps/{chunk-5KLROPUR.js.map → chunk-G44QLPFQ.js.map} +0 -0
|
@@ -0,0 +1,340 @@
|
|
|
1
|
+
import { createContext, useContext, useEffect, useMemo, useRef, useState, ReactNode } from "react";
|
|
2
|
+
import { useNavigate } from "react-router-dom";
|
|
3
|
+
import { isTyping } from "../hooks/useCursorInput";
|
|
4
|
+
import { useMatchAgent } from "./MatchAgentContext";
|
|
5
|
+
import {
|
|
6
|
+
NavAction, KeyboardBindings, GamepadBindings, InputBindings,
|
|
7
|
+
getInputBindings, setInputBindings as putInputBindings,
|
|
8
|
+
} from "../api/matchAgent";
|
|
9
|
+
import { FullScreenMenu } from "../components/FullScreenMenu";
|
|
10
|
+
import { OnScreenKeyboard } from "../components/OnScreenKeyboard";
|
|
11
|
+
import { OnScreenFileDialog } from "../components/OnScreenFileDialog";
|
|
12
|
+
import { InputHints } from "../components/InputHints";
|
|
13
|
+
|
|
14
|
+
export type { NavAction, KeyboardBindings, GamepadBindings };
|
|
15
|
+
export type InputDevice = "keyboard" | "gamepad";
|
|
16
|
+
|
|
17
|
+
export type FileDialogOptions = {
|
|
18
|
+
// Defaults to match-agent's own host home directory when omitted.
|
|
19
|
+
startPath?: string,
|
|
20
|
+
// e.g. ".roster-lock.json" - filters which files show up without hiding
|
|
21
|
+
// any folders, so navigation is never blocked.
|
|
22
|
+
extension?: string,
|
|
23
|
+
// Shown in the dialog's own footer, e.g. "roster-lock files".
|
|
24
|
+
extensionLabel?: string,
|
|
25
|
+
};
|
|
26
|
+
export type FileDialogState = FileDialogOptions & { resolve: (path: string | null) => void };
|
|
27
|
+
|
|
28
|
+
// Move (arrow keys / d-pad+stick) isn't offered for rebinding - it's a
|
|
29
|
+
// pair of directions rather than a single press, and every other axis- or
|
|
30
|
+
// d-pad-shaped control a user might want to use for it already reports
|
|
31
|
+
// through the same two inputs.
|
|
32
|
+
export const DEFAULT_KEYBOARD_BINDINGS: KeyboardBindings = { select: "Enter", back: "Backspace", menu: " " };
|
|
33
|
+
export const DEFAULT_GAMEPAD_BINDINGS: GamepadBindings = { select: 0, back: 1, menu: 9 };
|
|
34
|
+
const DEFAULT_BINDINGS: InputBindings = { keyboard: DEFAULT_KEYBOARD_BINDINGS, gamepad: DEFAULT_GAMEPAD_BINDINGS };
|
|
35
|
+
|
|
36
|
+
const AXIS_THRESHOLD = 0.5;
|
|
37
|
+
const DPAD_UP = 12;
|
|
38
|
+
const DPAD_DOWN = 13;
|
|
39
|
+
const DPAD_LEFT = 14;
|
|
40
|
+
const DPAD_RIGHT = 15;
|
|
41
|
+
|
|
42
|
+
type Held = { up: boolean, down: boolean, left: boolean, right: boolean, confirm: boolean, back: boolean, menu: boolean };
|
|
43
|
+
const NONE_HELD: Held = {
|
|
44
|
+
up: false, down: false, left: false, right: false, confirm: false, back: false, menu: false,
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const FOCUSABLE_SELECTOR =
|
|
48
|
+
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
|
|
49
|
+
|
|
50
|
+
const TEXT_ENTRY_TYPES = ["text", "search", "password", "email", "url", "tel", "number"];
|
|
51
|
+
|
|
52
|
+
function isTextEntry(el: Element): el is HTMLInputElement | HTMLTextAreaElement {
|
|
53
|
+
if (el instanceof HTMLTextAreaElement) return true;
|
|
54
|
+
return el instanceof HTMLInputElement && TEXT_ENTRY_TYPES.includes(el.type);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// A rebound action key might be a letter, whose KeyboardEvent.key differs
|
|
58
|
+
// by case depending on whether Shift is held ("r" vs "R") - the binding
|
|
59
|
+
// itself is stored without that distinction, so a single-character match
|
|
60
|
+
// is case-insensitive while multi-character keys ("Enter", "Backspace")
|
|
61
|
+
// still match exactly.
|
|
62
|
+
function matchesKey(key: string, binding: string): boolean {
|
|
63
|
+
if (key.length === 1 && binding.length === 1) return key.toLowerCase() === binding.toLowerCase();
|
|
64
|
+
return key === binding;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function focusableIn(container: HTMLElement | null): Array<HTMLElement> {
|
|
68
|
+
if (!container) return [];
|
|
69
|
+
return Array.from(container.querySelectorAll<HTMLElement>(FOCUSABLE_SELECTOR));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function moveFocus(container: HTMLElement | null, delta: number) {
|
|
73
|
+
const items = focusableIn(container);
|
|
74
|
+
if (items.length === 0) return;
|
|
75
|
+
const index = items.indexOf(document.activeElement as HTMLElement);
|
|
76
|
+
const nextIndex = index === -1 ? (delta > 0 ? 0 : items.length - 1) : (index + delta + items.length) % items.length;
|
|
77
|
+
items[nextIndex].focus();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// A <select>'s own focused-native-arrow-key behaviour (cycle its options)
|
|
81
|
+
// is what a keyboard user already gets for free from the browser - a
|
|
82
|
+
// controller has no such thing, so Left/Right repurpose the press to step
|
|
83
|
+
// the select's value instead of moving page focus when one is focused
|
|
84
|
+
// (Up/Down always move page focus instead, specifically so a select can
|
|
85
|
+
// never trap Move entirely - see GlobalNavProvider's up/down vs left/right
|
|
86
|
+
// split below).
|
|
87
|
+
function stepSelect(select: HTMLSelectElement, delta: number) {
|
|
88
|
+
const nextIndex = select.selectedIndex + delta;
|
|
89
|
+
if (nextIndex < 0 || nextIndex >= select.options.length) return;
|
|
90
|
+
select.selectedIndex = nextIndex;
|
|
91
|
+
select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
type GlobalNavContextValue = {
|
|
95
|
+
inputDevice: InputDevice,
|
|
96
|
+
keyboardBindings: KeyboardBindings,
|
|
97
|
+
gamepadBindings: GamepadBindings,
|
|
98
|
+
setKeyboardBinding: (action: NavAction, key: string) => void,
|
|
99
|
+
setGamepadBinding: (action: NavAction, buttonIndex: number) => void,
|
|
100
|
+
resetBindings: () => void,
|
|
101
|
+
// Opens OnScreenFileDialog and resolves with the picked absolute path,
|
|
102
|
+
// or null if cancelled - a DOM-rendered alternative to a native OS file
|
|
103
|
+
// dialog (see pick-file.ts/api/matchAgent's pickRosterLockFile), which a
|
|
104
|
+
// gamepad has no way to reach at all since it's a window outside the
|
|
105
|
+
// browser entirely. Promise-based so a caller can just `await` it like
|
|
106
|
+
// it would a native picker.
|
|
107
|
+
pickFile: (options?: FileDialogOptions) => Promise<string | null>,
|
|
108
|
+
// Lets a component with its own gamepad/keyboard-driven interaction (the
|
|
109
|
+
// piece selection grid - see useCursorInput; the Controls page's own
|
|
110
|
+
// rebind capture) take exclusive control of input while it's mounted, so
|
|
111
|
+
// the same press isn't also read as menu navigation.
|
|
112
|
+
setNavSuspended: (suspended: boolean) => void,
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const GlobalNavContext = createContext<GlobalNavContextValue | null>(null);
|
|
116
|
+
|
|
117
|
+
export function useGlobalNav(): GlobalNavContextValue {
|
|
118
|
+
const ctx = useContext(GlobalNavContext);
|
|
119
|
+
if (!ctx) throw new Error("useGlobalNav must be used within a GlobalNavProvider");
|
|
120
|
+
return ctx;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Makes the whole shell navigable by keyboard or gamepad through one
|
|
124
|
+
// shared scheme of four actions, each with its own on-screen hint (see
|
|
125
|
+
// InputHints) that swaps to match whichever device was used most recently,
|
|
126
|
+
// and each (bar Move) rebindable from the Controls page:
|
|
127
|
+
// Select - Enter / A - clicks the focused control, or - for a
|
|
128
|
+
// text field, which a controller has no other way to type into
|
|
129
|
+
// - opens the on-screen keyboard
|
|
130
|
+
// Back - Backspace / B - closes the menu/keyboard, else goes back a page
|
|
131
|
+
// Menu - Space / Start - the full-screen menu, so a controller can
|
|
132
|
+
// reach NavBar's links without roving-focusing all the way up
|
|
133
|
+
// Move - Arrow keys / d-pad+stick - steps focus among the current
|
|
134
|
+
// page's own controls (see #page-content in App.tsx)
|
|
135
|
+
// This intentionally does NOT layer on top of native Tab - Space's default
|
|
136
|
+
// (scroll the page / activate a focused button) is reassigned to Menu, so
|
|
137
|
+
// every focused control is reached and activated through this scheme on
|
|
138
|
+
// both devices alike, rather than keyboard getting one set of behaviours
|
|
139
|
+
// and gamepad another. The selection grid (useCursorInput) keeps its own
|
|
140
|
+
// separate Enter/Space-to-pick scheme, since it's per-player-slot and
|
|
141
|
+
// already shipped before this existed.
|
|
142
|
+
export function GlobalNavProvider({ children }: { children: ReactNode }) {
|
|
143
|
+
const navigate = useNavigate();
|
|
144
|
+
const { settings } = useMatchAgent();
|
|
145
|
+
const [menuOpen, setMenuOpen] = useState(false);
|
|
146
|
+
const [oskTarget, setOskTarget] = useState<HTMLInputElement | HTMLTextAreaElement | null>(null);
|
|
147
|
+
const [fileDialog, setFileDialog] = useState<FileDialogState | null>(null);
|
|
148
|
+
const [inputDevice, setInputDevice] = useState<InputDevice>("keyboard");
|
|
149
|
+
const [bindings, setBindings] = useState<InputBindings>(DEFAULT_BINDINGS);
|
|
150
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
151
|
+
const oskRef = useRef<HTMLDivElement>(null);
|
|
152
|
+
const fileDialogRef = useRef<HTMLDivElement>(null);
|
|
153
|
+
const suspendedRef = useRef(false);
|
|
154
|
+
const heldRef = useRef<Held>(NONE_HELD);
|
|
155
|
+
|
|
156
|
+
// Fetched from match-agent (not localStorage) so rebound controls live
|
|
157
|
+
// on whatever USB/install the player is actually using, not the browser
|
|
158
|
+
// profile of whichever host happens to be running it - see
|
|
159
|
+
// core/match-agent/src/config/inputBindings.ts.
|
|
160
|
+
useEffect(() => {
|
|
161
|
+
let cancelled = false;
|
|
162
|
+
getInputBindings(settings.url, settings.authCode)
|
|
163
|
+
.then((loaded) => { if (!cancelled) setBindings(loaded); })
|
|
164
|
+
.catch((e) => console.error("Failed to load input bindings from match-agent", e));
|
|
165
|
+
return () => { cancelled = true; };
|
|
166
|
+
}, [settings.url, settings.authCode]);
|
|
167
|
+
|
|
168
|
+
const persistBindings = (next: InputBindings) => {
|
|
169
|
+
setBindings(next);
|
|
170
|
+
putInputBindings(settings.url, settings.authCode, next)
|
|
171
|
+
.catch((e) => console.error("Failed to save input bindings to match-agent", e));
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const activeContainer = (): HTMLElement | null => {
|
|
175
|
+
if (fileDialog) return fileDialogRef.current;
|
|
176
|
+
if (oskTarget) return oskRef.current;
|
|
177
|
+
if (menuOpen) return menuRef.current;
|
|
178
|
+
return document.getElementById("page-content");
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
// Up/Down always move focus - a focused <select> never captures them, so
|
|
182
|
+
// Move can always step past one. Left/Right adjust a focused select's
|
|
183
|
+
// value instead (see stepSelect), falling back to moving focus for every
|
|
184
|
+
// other control, same as before.
|
|
185
|
+
const moveFocusOnly = (delta: number) => moveFocus(activeContainer(), delta);
|
|
186
|
+
const adjustOrMoveFocus = (delta: number) => {
|
|
187
|
+
const activeEl = document.activeElement;
|
|
188
|
+
if (activeEl instanceof HTMLSelectElement) stepSelect(activeEl, delta);
|
|
189
|
+
else moveFocus(activeContainer(), delta);
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
const confirmActive = () => {
|
|
193
|
+
const activeEl = document.activeElement;
|
|
194
|
+
if (!(activeEl instanceof HTMLElement)) return;
|
|
195
|
+
if (isTextEntry(activeEl)) setOskTarget(activeEl);
|
|
196
|
+
else activeEl.click();
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
const toggleMenu = () => {
|
|
200
|
+
setOskTarget(null);
|
|
201
|
+
setMenuOpen((open) => !open);
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
const goBack = () => {
|
|
205
|
+
if (fileDialog) fileDialog.resolve(null);
|
|
206
|
+
else if (oskTarget) setOskTarget(null);
|
|
207
|
+
else if (menuOpen) setMenuOpen(false);
|
|
208
|
+
else navigate(-1);
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
const pickFile = (options: FileDialogOptions = {}): Promise<string | null> => new Promise((resolvePromise) => {
|
|
212
|
+
setMenuOpen(false);
|
|
213
|
+
setOskTarget(null);
|
|
214
|
+
setFileDialog({
|
|
215
|
+
...options,
|
|
216
|
+
resolve: (path) => { setFileDialog(null); resolvePromise(path); },
|
|
217
|
+
});
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
useEffect(() => {
|
|
221
|
+
const onKeyDown = (e: KeyboardEvent) => {
|
|
222
|
+
if (suspendedRef.current) return;
|
|
223
|
+
setInputDevice("keyboard");
|
|
224
|
+
if (isTyping(e.target)) return;
|
|
225
|
+
if (matchesKey(e.key, bindings.keyboard.select)) {
|
|
226
|
+
e.preventDefault();
|
|
227
|
+
confirmActive();
|
|
228
|
+
} else if (matchesKey(e.key, bindings.keyboard.back)) {
|
|
229
|
+
e.preventDefault();
|
|
230
|
+
goBack();
|
|
231
|
+
} else if (matchesKey(e.key, bindings.keyboard.menu)) {
|
|
232
|
+
e.preventDefault();
|
|
233
|
+
toggleMenu();
|
|
234
|
+
} else if (e.key === "ArrowUp") {
|
|
235
|
+
e.preventDefault();
|
|
236
|
+
moveFocusOnly(-1);
|
|
237
|
+
} else if (e.key === "ArrowDown") {
|
|
238
|
+
e.preventDefault();
|
|
239
|
+
moveFocusOnly(1);
|
|
240
|
+
} else if (e.key === "ArrowLeft") {
|
|
241
|
+
e.preventDefault();
|
|
242
|
+
adjustOrMoveFocus(-1);
|
|
243
|
+
} else if (e.key === "ArrowRight") {
|
|
244
|
+
e.preventDefault();
|
|
245
|
+
adjustOrMoveFocus(1);
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
window.addEventListener("keydown", onKeyDown);
|
|
249
|
+
return () => window.removeEventListener("keydown", onKeyDown);
|
|
250
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
251
|
+
}, [oskTarget, menuOpen, fileDialog, bindings]);
|
|
252
|
+
|
|
253
|
+
useEffect(() => {
|
|
254
|
+
let frame: number;
|
|
255
|
+
const poll = () => {
|
|
256
|
+
frame = requestAnimationFrame(poll);
|
|
257
|
+
if (suspendedRef.current) {
|
|
258
|
+
heldRef.current = NONE_HELD;
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const pads = navigator.getGamepads?.() ?? [];
|
|
263
|
+
let next: Held = NONE_HELD;
|
|
264
|
+
for (const pad of pads) {
|
|
265
|
+
if (!pad) continue;
|
|
266
|
+
const horizontal = pad.axes[0] ?? 0;
|
|
267
|
+
const vertical = pad.axes[1] ?? 0;
|
|
268
|
+
next = {
|
|
269
|
+
up: next.up || vertical < -AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_UP]?.pressed),
|
|
270
|
+
down: next.down || vertical > AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_DOWN]?.pressed),
|
|
271
|
+
left: next.left || horizontal < -AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_LEFT]?.pressed),
|
|
272
|
+
right: next.right || horizontal > AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_RIGHT]?.pressed),
|
|
273
|
+
confirm: next.confirm || Boolean(pad.buttons[bindings.gamepad.select]?.pressed),
|
|
274
|
+
back: next.back || Boolean(pad.buttons[bindings.gamepad.back]?.pressed),
|
|
275
|
+
menu: next.menu || Boolean(pad.buttons[bindings.gamepad.menu]?.pressed),
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
const held = heldRef.current;
|
|
280
|
+
const anyEdge = (next.up && !held.up) || (next.down && !held.down) || (next.left && !held.left)
|
|
281
|
+
|| (next.right && !held.right) || (next.confirm && !held.confirm) || (next.back && !held.back)
|
|
282
|
+
|| (next.menu && !held.menu);
|
|
283
|
+
if (anyEdge) setInputDevice("gamepad");
|
|
284
|
+
|
|
285
|
+
if (next.menu && !held.menu) {
|
|
286
|
+
toggleMenu();
|
|
287
|
+
} else {
|
|
288
|
+
if (next.up && !held.up) moveFocusOnly(-1);
|
|
289
|
+
if (next.down && !held.down) moveFocusOnly(1);
|
|
290
|
+
if (next.left && !held.left) adjustOrMoveFocus(-1);
|
|
291
|
+
if (next.right && !held.right) adjustOrMoveFocus(1);
|
|
292
|
+
if (next.confirm && !held.confirm) confirmActive();
|
|
293
|
+
if (next.back && !held.back) goBack();
|
|
294
|
+
}
|
|
295
|
+
heldRef.current = next;
|
|
296
|
+
};
|
|
297
|
+
frame = requestAnimationFrame(poll);
|
|
298
|
+
return () => cancelAnimationFrame(frame);
|
|
299
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
300
|
+
}, [menuOpen, oskTarget, fileDialog, bindings]);
|
|
301
|
+
|
|
302
|
+
// Opening the menu, the keyboard, or the file dialog should land focus
|
|
303
|
+
// inside it immediately, so the very next move-press has something to
|
|
304
|
+
// move from. (Re-focusing the first row after navigating *within* an
|
|
305
|
+
// already-open file dialog is OnScreenFileDialog's own job, since the
|
|
306
|
+
// current folder's listing lives in its state, not here.)
|
|
307
|
+
useEffect(() => {
|
|
308
|
+
if (menuOpen) focusableIn(menuRef.current)[0]?.focus();
|
|
309
|
+
}, [menuOpen]);
|
|
310
|
+
useEffect(() => {
|
|
311
|
+
if (oskTarget) focusableIn(oskRef.current)[0]?.focus();
|
|
312
|
+
}, [oskTarget]);
|
|
313
|
+
useEffect(() => {
|
|
314
|
+
if (fileDialog) focusableIn(fileDialogRef.current)[0]?.focus();
|
|
315
|
+
}, [fileDialog]);
|
|
316
|
+
|
|
317
|
+
const value = useMemo<GlobalNavContextValue>(() => ({
|
|
318
|
+
inputDevice,
|
|
319
|
+
keyboardBindings: bindings.keyboard,
|
|
320
|
+
gamepadBindings: bindings.gamepad,
|
|
321
|
+
setKeyboardBinding: (action, key) => persistBindings({ ...bindings, keyboard: { ...bindings.keyboard, [action]: key } }),
|
|
322
|
+
setGamepadBinding: (action, buttonIndex) => persistBindings({
|
|
323
|
+
...bindings, gamepad: { ...bindings.gamepad, [action]: buttonIndex },
|
|
324
|
+
}),
|
|
325
|
+
resetBindings: () => persistBindings({ keyboard: { ...DEFAULT_KEYBOARD_BINDINGS }, gamepad: { ...DEFAULT_GAMEPAD_BINDINGS } }),
|
|
326
|
+
pickFile,
|
|
327
|
+
setNavSuspended: (suspended: boolean) => { suspendedRef.current = suspended; },
|
|
328
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
329
|
+
}), [inputDevice, bindings]);
|
|
330
|
+
|
|
331
|
+
return (
|
|
332
|
+
<GlobalNavContext.Provider value={value}>
|
|
333
|
+
{children}
|
|
334
|
+
<FullScreenMenu containerRef={menuRef} open={menuOpen} onClose={() => setMenuOpen(false)} />
|
|
335
|
+
<OnScreenKeyboard containerRef={oskRef} target={oskTarget} onClose={() => setOskTarget(null)} />
|
|
336
|
+
<OnScreenFileDialog containerRef={fileDialogRef} state={fileDialog} />
|
|
337
|
+
<InputHints />
|
|
338
|
+
</GlobalNavContext.Provider>
|
|
339
|
+
);
|
|
340
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createContext, useContext, ReactNode } from "react";
|
|
2
|
+
import { RosterLockV1Config } from "@roster-lock/types";
|
|
3
|
+
|
|
4
|
+
// Everything under SelectionBoard needs to ask match-agent about the same
|
|
5
|
+
// game launcher and the same engine - which pieces are downloaded, what a
|
|
6
|
+
// piece's preview looks like. It's context rather than props because the
|
|
7
|
+
// deepest consumer is PieceCard, one per roster entry and three components
|
|
8
|
+
// below where these values are actually known.
|
|
9
|
+
export type SelectionSource = {
|
|
10
|
+
pluginName: string,
|
|
11
|
+
engine: RosterLockV1Config["engine"],
|
|
12
|
+
matchAgentUrl: string,
|
|
13
|
+
matchAgentAuth: string,
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
const SelectionSourceContext = createContext<SelectionSource | null>(null);
|
|
17
|
+
|
|
18
|
+
export function SelectionSourceProvider({ value, children }: { value: SelectionSource, children: ReactNode }) {
|
|
19
|
+
return <SelectionSourceContext.Provider value={value}>{children}</SelectionSourceContext.Provider>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function useSelectionSource(): SelectionSource {
|
|
23
|
+
const ctx = useContext(SelectionSourceContext);
|
|
24
|
+
if (!ctx) throw new Error("useSelectionSource must be used within a SelectionSourceProvider");
|
|
25
|
+
return ctx;
|
|
26
|
+
}
|
|
@@ -2,33 +2,67 @@ import { useEffect, useRef } from "react";
|
|
|
2
2
|
import { InputSource } from "../context/JoinSettingsContext";
|
|
3
3
|
|
|
4
4
|
const AXIS_THRESHOLD = 0.5;
|
|
5
|
+
const DPAD_UP = 12;
|
|
6
|
+
const DPAD_DOWN = 13;
|
|
5
7
|
const DPAD_LEFT = 14;
|
|
6
8
|
const DPAD_RIGHT = 15;
|
|
7
9
|
const CONFIRM_BUTTON = 0;
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
//
|
|
13
|
-
//
|
|
14
|
-
// a
|
|
15
|
-
export function
|
|
16
|
-
|
|
17
|
-
)
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
type Held = { left: boolean, right: boolean, up: boolean, down: boolean, confirm: boolean };
|
|
12
|
+
const NONE_HELD: Held = { left: false, right: false, up: false, down: false, confirm: false };
|
|
13
|
+
|
|
14
|
+
// A keyboard slot's listener is on window, so it would otherwise fire while
|
|
15
|
+
// the player is typing in the roster filter box - where Space and Enter mean
|
|
16
|
+
// "type a space"/"submit", not "pick the cursored piece".
|
|
17
|
+
export function isTyping(target: EventTarget | null): boolean {
|
|
18
|
+
if (!(target instanceof HTMLElement)) return false;
|
|
19
|
+
if (target.isContentEditable) return true;
|
|
20
|
+
return ["INPUT", "TEXTAREA", "SELECT"].includes(target.tagName);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Drives one player slot's cursor over a grid of cards: arrow keys for a
|
|
24
|
+
// keyboard-assigned slot, d-pad/left-stick + button 0 for a gamepad-assigned
|
|
25
|
+
// one. Column moves (left/right) and row moves (up/down) are reported
|
|
26
|
+
// separately because only the caller knows how many columns its grid
|
|
27
|
+
// currently resolves to - see useGridColumns. Edge-detected (fires once per
|
|
28
|
+
// press, not once per frame) so a held direction doesn't spam the callbacks.
|
|
29
|
+
// Plain mouse clicks on a card work regardless of this hook - it's an
|
|
30
|
+
// addition on top of that, not a replacement for it (see components/Selection).
|
|
31
|
+
export function useCursorInput({ source, enabled, onMoveColumn, onMoveRow, onConfirm }: {
|
|
32
|
+
source: InputSource,
|
|
33
|
+
enabled: boolean,
|
|
34
|
+
onMoveColumn: (delta: number) => void,
|
|
35
|
+
onMoveRow: (delta: number) => void,
|
|
36
|
+
onConfirm: () => void,
|
|
37
|
+
}) {
|
|
38
|
+
// Held through a ref so the two effects below depend only on the input
|
|
39
|
+
// source, not on callback identity: the caller's onConfirm closes over the
|
|
40
|
+
// current cursor position and so is a new function every render, and
|
|
41
|
+
// re-running the gamepad effect that often would tear down and restart its
|
|
42
|
+
// requestAnimationFrame loop (losing the edge-detection state with it).
|
|
43
|
+
const handlersRef = useRef({ onMoveColumn, onMoveRow, onConfirm });
|
|
44
|
+
useEffect(() => { handlersRef.current = { onMoveColumn, onMoveRow, onConfirm }; });
|
|
45
|
+
|
|
46
|
+
const heldRef = useRef<Held>(NONE_HELD);
|
|
21
47
|
|
|
22
48
|
useEffect(() => {
|
|
23
49
|
if (!enabled || source.type !== "keyboard") return;
|
|
24
50
|
const onKeyDown = (e: KeyboardEvent) => {
|
|
25
|
-
if (e.
|
|
26
|
-
|
|
51
|
+
if (isTyping(e.target)) return;
|
|
52
|
+
const { onMoveColumn, onMoveRow, onConfirm } = handlersRef.current;
|
|
53
|
+
if (e.key === "ArrowLeft") onMoveColumn(-1);
|
|
54
|
+
else if (e.key === "ArrowRight") onMoveColumn(1);
|
|
55
|
+
else if (e.key === "ArrowUp") onMoveRow(-1);
|
|
56
|
+
else if (e.key === "ArrowDown") onMoveRow(1);
|
|
27
57
|
else if (e.key === "Enter" || e.key === " ") onConfirm();
|
|
58
|
+
else return;
|
|
59
|
+
// Arrow keys and Space scroll the page by default, which fights the
|
|
60
|
+
// grid's own scroll-the-cursor-into-view.
|
|
61
|
+
e.preventDefault();
|
|
28
62
|
};
|
|
29
63
|
window.addEventListener("keydown", onKeyDown);
|
|
30
64
|
return () => window.removeEventListener("keydown", onKeyDown);
|
|
31
|
-
}, [enabled, source
|
|
65
|
+
}, [enabled, source]);
|
|
32
66
|
|
|
33
67
|
useEffect(() => {
|
|
34
68
|
if (!enabled || source.type !== "gamepad") return;
|
|
@@ -36,20 +70,28 @@ export function useCursorInput(
|
|
|
36
70
|
const poll = () => {
|
|
37
71
|
const pad = navigator.getGamepads?.()[source.index];
|
|
38
72
|
if (pad) {
|
|
39
|
-
const
|
|
40
|
-
const
|
|
41
|
-
const
|
|
42
|
-
|
|
73
|
+
const horizontal = pad.axes[0] ?? 0;
|
|
74
|
+
const vertical = pad.axes[1] ?? 0;
|
|
75
|
+
const next: Held = {
|
|
76
|
+
left: horizontal < -AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_LEFT]?.pressed),
|
|
77
|
+
right: horizontal > AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_RIGHT]?.pressed),
|
|
78
|
+
up: vertical < -AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_UP]?.pressed),
|
|
79
|
+
down: vertical > AXIS_THRESHOLD || Boolean(pad.buttons[DPAD_DOWN]?.pressed),
|
|
80
|
+
confirm: Boolean(pad.buttons[CONFIRM_BUTTON]?.pressed),
|
|
81
|
+
};
|
|
43
82
|
|
|
44
|
-
|
|
45
|
-
if (
|
|
46
|
-
if (
|
|
83
|
+
const handlers = handlersRef.current;
|
|
84
|
+
if (next.left && !heldRef.current.left) handlers.onMoveColumn(-1);
|
|
85
|
+
if (next.right && !heldRef.current.right) handlers.onMoveColumn(1);
|
|
86
|
+
if (next.up && !heldRef.current.up) handlers.onMoveRow(-1);
|
|
87
|
+
if (next.down && !heldRef.current.down) handlers.onMoveRow(1);
|
|
88
|
+
if (next.confirm && !heldRef.current.confirm) handlers.onConfirm();
|
|
47
89
|
|
|
48
|
-
heldRef.current =
|
|
90
|
+
heldRef.current = next;
|
|
49
91
|
}
|
|
50
92
|
frame = requestAnimationFrame(poll);
|
|
51
93
|
};
|
|
52
94
|
frame = requestAnimationFrame(poll);
|
|
53
95
|
return () => cancelAnimationFrame(frame);
|
|
54
|
-
}, [enabled, source
|
|
96
|
+
}, [enabled, source]);
|
|
55
97
|
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
import { RosterLockV1Config } from "@roster-lock/types";
|
|
3
|
+
import { listDownloadedPiecesFromConfig } from "@roster-lock/ts-client";
|
|
4
|
+
import { useSelectionSource } from "../context/SelectionSourceContext";
|
|
5
|
+
|
|
6
|
+
const PAGE_SIZE = 200;
|
|
7
|
+
|
|
8
|
+
// Which of a piece type's roster entries match-agent already has on disk, as
|
|
9
|
+
// a set of logic hashes. null until the first answer arrives - that's what
|
|
10
|
+
// lets a card show no download status at all on mount rather than flashing
|
|
11
|
+
// "Not downloaded" at every piece before the list comes back.
|
|
12
|
+
export function useDownloadedPieceVersions(
|
|
13
|
+
rosterConfig: RosterLockV1Config, pieceType: string
|
|
14
|
+
): ReadonlySet<string> | null {
|
|
15
|
+
const { matchAgentUrl, matchAgentAuth } = useSelectionSource();
|
|
16
|
+
const [downloaded, setDownloaded] = useState<ReadonlySet<string> | null>(null);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
let cancelled = false;
|
|
20
|
+
setDownloaded(null);
|
|
21
|
+
// Paged rather than one large limit: a big roster can hold more entries
|
|
22
|
+
// than any single page returns, and a silently truncated answer reads as
|
|
23
|
+
// "the rest of the roster isn't downloaded".
|
|
24
|
+
(async () => {
|
|
25
|
+
const logic = new Set<string>();
|
|
26
|
+
for (let page = 0; !cancelled; page += 1) {
|
|
27
|
+
const rows = await listDownloadedPiecesFromConfig(
|
|
28
|
+
{ version: 1, rosterConfig, pieceType }, { page, limit: PAGE_SIZE }, matchAgentAuth, matchAgentUrl
|
|
29
|
+
);
|
|
30
|
+
for (const row of rows) logic.add(row.version.logic);
|
|
31
|
+
if (rows.length < PAGE_SIZE) break;
|
|
32
|
+
}
|
|
33
|
+
if (!cancelled) setDownloaded(logic);
|
|
34
|
+
})().catch(() => {});
|
|
35
|
+
return () => { cancelled = true; };
|
|
36
|
+
}, [rosterConfig, pieceType, matchAgentUrl, matchAgentAuth]);
|
|
37
|
+
|
|
38
|
+
return downloaded;
|
|
39
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { RefObject, useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
// How many columns a CSS grid currently resolves to. The card grid's column
|
|
4
|
+
// count is decided by the browser (repeat(auto-fill, ...) against whatever
|
|
5
|
+
// width the panel ended up with), not by us, so the only honest way to move a
|
|
6
|
+
// cursor up/down a row is to read it back off the resolved style and re-read
|
|
7
|
+
// it whenever the element resizes.
|
|
8
|
+
export function useGridColumns(ref: RefObject<HTMLElement | null>): number {
|
|
9
|
+
const [columns, setColumns] = useState(1);
|
|
10
|
+
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
const element = ref.current;
|
|
13
|
+
if (!element) return;
|
|
14
|
+
const measure = () => {
|
|
15
|
+
const template = getComputedStyle(element).gridTemplateColumns;
|
|
16
|
+
// "none" for a grid with no tracks yet (empty/hidden) - keep 1 so
|
|
17
|
+
// row movement degrades to single-step rather than to zero-step.
|
|
18
|
+
const count = template === "none" ? 0 : template.split(" ").filter(Boolean).length;
|
|
19
|
+
setColumns(Math.max(count, 1));
|
|
20
|
+
};
|
|
21
|
+
measure();
|
|
22
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
23
|
+
const observer = new ResizeObserver(measure);
|
|
24
|
+
observer.observe(element);
|
|
25
|
+
return () => observer.disconnect();
|
|
26
|
+
}, [ref]);
|
|
27
|
+
|
|
28
|
+
return columns;
|
|
29
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
// Latches on first intersection and never flips back: this exists to gate
|
|
4
|
+
// *starting* work for an element (a card's preview fetch), and "un-starting"
|
|
5
|
+
// one once the player scrolls past would only mean paying for it again when
|
|
6
|
+
// they scroll back. rootMargin pre-loads a screenful ahead so cards are
|
|
7
|
+
// already filled in by the time they're actually looked at.
|
|
8
|
+
export function useInView<T extends Element>(rootMargin = "300px"): [(node: T | null) => void, boolean] {
|
|
9
|
+
const [inView, setInView] = useState(false);
|
|
10
|
+
const observerRef = useRef<IntersectionObserver | null>(null);
|
|
11
|
+
|
|
12
|
+
const ref = useCallback((node: T | null) => {
|
|
13
|
+
observerRef.current?.disconnect();
|
|
14
|
+
observerRef.current = null;
|
|
15
|
+
if (!node || inView) return;
|
|
16
|
+
// No observer (non-browser render, very old webview) - fall back to
|
|
17
|
+
// "everything is visible" rather than to "nothing ever loads".
|
|
18
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
19
|
+
setInView(true);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
const observer = new IntersectionObserver((entries) => {
|
|
23
|
+
if (!entries.some((entry) => entry.isIntersecting)) return;
|
|
24
|
+
setInView(true);
|
|
25
|
+
observer.disconnect();
|
|
26
|
+
}, { rootMargin });
|
|
27
|
+
observer.observe(node);
|
|
28
|
+
observerRef.current = observer;
|
|
29
|
+
}, [inView, rootMargin]);
|
|
30
|
+
|
|
31
|
+
return [ref, inView];
|
|
32
|
+
}
|