|
1 | | -function devRuntimeAllowed() { |
2 | | - const host = window.location.hostname; |
3 | | - return window.GameFoundryDevRuntime?.enabled === true || |
4 | | - window.location.protocol === "file:" || |
5 | | - host === "localhost" || |
6 | | - host === "127.0.0.1"; |
| 1 | +import { |
| 2 | + getSessionCurrent, |
| 3 | + getSessionModes, |
| 4 | + getSessionUsers, |
| 5 | + setSessionMode, |
| 6 | + setSessionUser, |
| 7 | +} from "../../../src/engine/api/session-api-client.js"; |
| 8 | + |
| 9 | +const modeButtons = Array.from(document.querySelectorAll("[data-login-mode]")); |
| 10 | +const modeTitle = document.querySelector("[data-login-mode-title]"); |
| 11 | +const modeDescription = document.querySelector("[data-login-mode-description]"); |
| 12 | +const modeStatus = document.querySelector("[data-login-mode-status]"); |
| 13 | +const userControls = document.querySelector("[data-login-user-controls]"); |
| 14 | +const userStatus = document.querySelector("[data-login-user-status]"); |
| 15 | +const continueLink = document.querySelector("[data-login-continue]"); |
| 16 | + |
| 17 | +function currentReturnTo() { |
| 18 | + const params = new URLSearchParams(window.location.search); |
| 19 | + const value = params.get("returnTo") || ""; |
| 20 | + if (!value || value.startsWith("/") || value.includes("://") || value.includes("..")) { |
| 21 | + return "toolbox/index.html"; |
| 22 | + } |
| 23 | + return value; |
| 24 | +} |
| 25 | + |
| 26 | +function updateContinueLink() { |
| 27 | + if (continueLink) { |
| 28 | + continueLink.href = currentReturnTo(); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function dispatchSessionChanged() { |
| 33 | + window.dispatchEvent(new CustomEvent("gamefoundry:mock-db-session-user-changed", { |
| 34 | + detail: getSessionCurrent(), |
| 35 | + })); |
| 36 | +} |
| 37 | + |
| 38 | +function dispatchModeChanged() { |
| 39 | + window.dispatchEvent(new CustomEvent("gamefoundry:mock-db-session-mode-changed", { |
| 40 | + detail: getSessionCurrent(), |
| 41 | + })); |
7 | 42 | } |
8 | 43 |
|
9 | | -async function loadLocalLoginSession() { |
10 | | - if (!devRuntimeAllowed()) { |
| 44 | +function setSelectedButton(button, selected) { |
| 45 | + button.classList.toggle("primary", selected); |
| 46 | + button.setAttribute("aria-pressed", String(selected)); |
| 47 | + if (selected) { |
| 48 | + button.setAttribute("aria-current", "true"); |
| 49 | + } else { |
| 50 | + button.removeAttribute("aria-current"); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +function renderUserButtons(mode) { |
| 55 | + if (!userControls) { |
11 | 56 | return; |
12 | 57 | } |
13 | | - await import("../../../src/dev-runtime/auth/login-session.js"); |
| 58 | + |
| 59 | + userControls.replaceChildren(); |
| 60 | + if (mode.id === "dev") { |
| 61 | + userControls.hidden = true; |
| 62 | + if (userStatus) { |
| 63 | + userStatus.textContent = "DEV mode uses read-only/demo JSON access. No users are selectable."; |
| 64 | + } |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + userControls.hidden = false; |
| 69 | + const sessionUser = getSessionCurrent(); |
| 70 | + getSessionUsers().forEach((user) => { |
| 71 | + const button = document.createElement("button"); |
| 72 | + button.className = "btn btn--compact"; |
| 73 | + button.type = "button"; |
| 74 | + button.textContent = user.label; |
| 75 | + button.dataset.loginUser = user.userKey || ""; |
| 76 | + setSelectedButton(button, (user.userKey || "") === (sessionUser.userKey || "")); |
| 77 | + userControls.append(button); |
| 78 | + }); |
| 79 | + if (userStatus) { |
| 80 | + userStatus.textContent = sessionUser.userKey |
| 81 | + ? `Selected local user: ${sessionUser.label}.` |
| 82 | + : "Guest is unauthenticated and is not stored in the users table."; |
| 83 | + } |
14 | 84 | } |
15 | 85 |
|
16 | | -loadLocalLoginSession().catch((error) => { |
17 | | - console.error("Unable to load Local login session.", error); |
| 86 | +function renderModeButtons(mode) { |
| 87 | + modeButtons.forEach((button) => { |
| 88 | + setSelectedButton(button, button.dataset.loginMode === mode.id); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +function renderError(error) { |
| 93 | + const message = error instanceof Error ? error.message : String(error || "Session API unavailable."); |
| 94 | + modeButtons.forEach((button) => { |
| 95 | + button.disabled = true; |
| 96 | + button.setAttribute("aria-disabled", "true"); |
| 97 | + }); |
| 98 | + if (userControls) { |
| 99 | + userControls.replaceChildren(); |
| 100 | + userControls.hidden = true; |
| 101 | + } |
| 102 | + if (modeTitle) { |
| 103 | + modeTitle.textContent = "Session API unavailable"; |
| 104 | + } |
| 105 | + if (modeDescription) { |
| 106 | + modeDescription.textContent = "Start the Local/DEV server API to select a session."; |
| 107 | + } |
| 108 | + if (modeStatus) { |
| 109 | + modeStatus.textContent = `Login/session diagnostic: ${message}`; |
| 110 | + } |
| 111 | + if (userStatus) { |
| 112 | + userStatus.textContent = "No local users are available until /api/session responds."; |
| 113 | + } |
| 114 | + updateContinueLink(); |
| 115 | +} |
| 116 | + |
| 117 | +function render() { |
| 118 | + try { |
| 119 | + const session = getSessionCurrent(); |
| 120 | + const mode = getSessionModes().find((item) => item.id === session.mode) || { |
| 121 | + description: "", |
| 122 | + id: session.mode, |
| 123 | + label: session.mode, |
| 124 | + }; |
| 125 | + renderModeButtons(mode); |
| 126 | + if (modeTitle) { |
| 127 | + modeTitle.textContent = mode.label; |
| 128 | + } |
| 129 | + if (modeDescription) { |
| 130 | + modeDescription.textContent = mode.description; |
| 131 | + } |
| 132 | + if (modeStatus) { |
| 133 | + modeStatus.textContent = `${mode.label} mode selected.`; |
| 134 | + } |
| 135 | + renderUserButtons(mode); |
| 136 | + updateContinueLink(); |
| 137 | + } catch (error) { |
| 138 | + renderError(error); |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +modeButtons.forEach((button) => { |
| 143 | + button.addEventListener("click", () => { |
| 144 | + try { |
| 145 | + setSessionMode(button.dataset.loginMode || "local"); |
| 146 | + dispatchModeChanged(); |
| 147 | + render(); |
| 148 | + } catch (error) { |
| 149 | + renderError(error); |
| 150 | + } |
| 151 | + }); |
18 | 152 | }); |
| 153 | + |
| 154 | +userControls?.addEventListener("click", (event) => { |
| 155 | + const button = event.target.closest("[data-login-user]"); |
| 156 | + if (!button) { |
| 157 | + return; |
| 158 | + } |
| 159 | + try { |
| 160 | + setSessionMode("local"); |
| 161 | + setSessionUser(button.dataset.loginUser || ""); |
| 162 | + dispatchSessionChanged(); |
| 163 | + render(); |
| 164 | + } catch (error) { |
| 165 | + renderError(error); |
| 166 | + } |
| 167 | +}); |
| 168 | + |
| 169 | +render(); |
0 commit comments