|
| 1 | +import { requestCurrentSession } from "./account-auth-service.js"; |
| 2 | + |
| 3 | +const ACCOUNT_DATA_FAILURE_MESSAGE = "Account data is unavailable. Please try again later."; |
| 4 | + |
| 5 | +function text(value) { |
| 6 | + if (value === undefined || value === null || value === "") { |
| 7 | + return "N/A"; |
| 8 | + } |
| 9 | + return String(value); |
| 10 | +} |
| 11 | + |
| 12 | +function titleCase(value) { |
| 13 | + return text(value).replace(/\b\w/g, (letter) => letter.toUpperCase()); |
| 14 | +} |
| 15 | + |
| 16 | +function pageRoots() { |
| 17 | + return Array.from(document.querySelectorAll("[data-account-page]")); |
| 18 | +} |
| 19 | + |
| 20 | +function setStatus(root, message) { |
| 21 | + const status = root.querySelector("[data-account-status]"); |
| 22 | + if (status) { |
| 23 | + status.textContent = message; |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function clearContent(root) { |
| 28 | + const content = root.querySelector("[data-account-content]"); |
| 29 | + if (content) { |
| 30 | + content.replaceChildren(); |
| 31 | + } |
| 32 | + return content; |
| 33 | +} |
| 34 | + |
| 35 | +function accountAccess(session) { |
| 36 | + const roles = Array.isArray(session?.roleSlugs) ? session.roleSlugs : []; |
| 37 | + return roles.length ? roles.map(titleCase).join(", ") : "Account"; |
| 38 | +} |
| 39 | + |
| 40 | +function renderTable(parent, caption, rows, tableName) { |
| 41 | + const wrapper = document.createElement("div"); |
| 42 | + wrapper.className = "table-wrapper"; |
| 43 | + const table = document.createElement("table"); |
| 44 | + table.className = "data-table"; |
| 45 | + table.dataset.accountTable = tableName; |
| 46 | + |
| 47 | + const captionElement = document.createElement("caption"); |
| 48 | + captionElement.textContent = caption; |
| 49 | + table.append(captionElement); |
| 50 | + |
| 51 | + const thead = document.createElement("thead"); |
| 52 | + const headerRow = document.createElement("tr"); |
| 53 | + ["Field", "Value"].forEach((header) => { |
| 54 | + const cell = document.createElement("th"); |
| 55 | + cell.scope = "col"; |
| 56 | + cell.textContent = header; |
| 57 | + headerRow.append(cell); |
| 58 | + }); |
| 59 | + thead.append(headerRow); |
| 60 | + table.append(thead); |
| 61 | + |
| 62 | + const tbody = document.createElement("tbody"); |
| 63 | + rows.forEach((row) => { |
| 64 | + const tableRow = document.createElement("tr"); |
| 65 | + row.forEach((value) => { |
| 66 | + const cell = document.createElement("td"); |
| 67 | + cell.textContent = text(value); |
| 68 | + tableRow.append(cell); |
| 69 | + }); |
| 70 | + tbody.append(tableRow); |
| 71 | + }); |
| 72 | + table.append(tbody); |
| 73 | + wrapper.append(table); |
| 74 | + parent.append(wrapper); |
| 75 | +} |
| 76 | + |
| 77 | +function renderFollowUp(parent, tableName, title, reason, action) { |
| 78 | + const article = document.createElement("article"); |
| 79 | + article.className = "callout"; |
| 80 | + article.dataset.accountFollowUp = tableName; |
| 81 | + |
| 82 | + const heading = document.createElement("h3"); |
| 83 | + heading.textContent = title; |
| 84 | + const status = document.createElement("p"); |
| 85 | + status.className = "status"; |
| 86 | + status.textContent = `FOLLOW-UP REQUIRED: ${reason}`; |
| 87 | + const nextAction = document.createElement("p"); |
| 88 | + nextAction.textContent = action; |
| 89 | + |
| 90 | + article.append(heading, status, nextAction); |
| 91 | + parent.append(article); |
| 92 | +} |
| 93 | + |
| 94 | +function renderAccountContext(parent, session) { |
| 95 | + renderTable(parent, "Current Account", [ |
| 96 | + ["Display Name", session?.displayName || session?.label || "Account"], |
| 97 | + ["Account Key", session?.userKey || session?.id || "N/A"], |
| 98 | + ["Access", accountAccess(session)], |
| 99 | + ["Account State", session?.authenticated ? "Signed in" : "Signed out"], |
| 100 | + ], "current-account"); |
| 101 | +} |
| 102 | + |
| 103 | +function renderAccountHome(root, session) { |
| 104 | + const content = clearContent(root); |
| 105 | + renderAccountContext(content, session); |
| 106 | + setStatus(root, "Loaded account summary from the account service."); |
| 107 | +} |
| 108 | + |
| 109 | +function renderAccountProfile(root, session) { |
| 110 | + const content = clearContent(root); |
| 111 | + renderAccountContext(content, session); |
| 112 | + setStatus(root, "Loaded profile identity from the account service."); |
| 113 | +} |
| 114 | + |
| 115 | +function renderAccountPreferences(root, session) { |
| 116 | + const content = clearContent(root); |
| 117 | + renderAccountContext(content, session); |
| 118 | + renderFollowUp( |
| 119 | + content, |
| 120 | + "account-preferences", |
| 121 | + "Preferences Service", |
| 122 | + "Account preferences are not available yet.", |
| 123 | + "A future account service update is required before this page can save preferences.", |
| 124 | + ); |
| 125 | + setStatus(root, "Loaded current account. Preferences storage is not available yet."); |
| 126 | +} |
| 127 | + |
| 128 | +function renderAccountSecurity(root, session) { |
| 129 | + const content = clearContent(root); |
| 130 | + renderAccountContext(content, session); |
| 131 | + renderFollowUp( |
| 132 | + content, |
| 133 | + "account-security", |
| 134 | + "Security Service", |
| 135 | + "Account security settings are not available yet.", |
| 136 | + "A future account service update is required before this page can show live security controls.", |
| 137 | + ); |
| 138 | + setStatus(root, "Loaded current account. Security settings are not available yet."); |
| 139 | +} |
| 140 | + |
| 141 | +const RENDERERS = Object.freeze({ |
| 142 | + home: renderAccountHome, |
| 143 | + preferences: renderAccountPreferences, |
| 144 | + profile: renderAccountProfile, |
| 145 | + security: renderAccountSecurity, |
| 146 | +}); |
| 147 | + |
| 148 | +async function renderRoot(root) { |
| 149 | + const renderer = RENDERERS[root.dataset.accountPage || ""]; |
| 150 | + if (!renderer) { |
| 151 | + return; |
| 152 | + } |
| 153 | + try { |
| 154 | + const session = await requestCurrentSession(ACCOUNT_DATA_FAILURE_MESSAGE); |
| 155 | + if (!session?.authenticated) { |
| 156 | + throw new Error("Sign in to view account details."); |
| 157 | + } |
| 158 | + renderer(root, session); |
| 159 | + } catch (error) { |
| 160 | + console.warn("[account/operator] Account data service unavailable:", error instanceof Error ? error.message : String(error || "")); |
| 161 | + clearContent(root); |
| 162 | + setStatus(root, ACCOUNT_DATA_FAILURE_MESSAGE); |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +function renderAll() { |
| 167 | + pageRoots().forEach((root) => { |
| 168 | + renderRoot(root); |
| 169 | + }); |
| 170 | +} |
| 171 | + |
| 172 | +window.addEventListener("gamefoundry:mock-db-session-user-changed", renderAll); |
| 173 | +renderAll(); |
0 commit comments