|
| 1 | +import { getSessionCurrent } from "../../../../src/api/session-api-client.js"; |
| 2 | +import { |
| 3 | + createServerRepositoryClient, |
| 4 | + readServerToolConstants, |
| 5 | + requireServerConstant, |
| 6 | +} from "../../../../src/api/server-api-client.js"; |
| 7 | + |
| 8 | +const constants = readServerToolConstants("game-crew"); |
| 9 | + |
| 10 | +export const GAME_CREW_TABLES = Object.freeze(requireServerConstant(constants, "GAME_CREW_TABLES", "game-crew")); |
| 11 | +export const GAME_CREW_MEMBER_ROLES = Object.freeze(requireServerConstant(constants, "GAME_CREW_MEMBER_ROLES", "game-crew")); |
| 12 | + |
| 13 | +export function createGameCrewApiRepository(options = {}) { |
| 14 | + return createServerRepositoryClient("game-crew", options); |
| 15 | +} |
| 16 | + |
| 17 | +const repository = createGameCrewApiRepository(); |
| 18 | + |
| 19 | +const elements = { |
| 20 | + action: document.querySelector("[data-game-crew-action]"), |
| 21 | + add: document.querySelector("[data-game-crew-add]"), |
| 22 | + count: document.querySelector("[data-game-crew-count]"), |
| 23 | + guidance: document.querySelector("[data-game-crew-guidance]"), |
| 24 | + log: document.querySelector("[data-game-crew-log]"), |
| 25 | + outputStatus: document.querySelector("[data-game-crew-output-status]"), |
| 26 | + owner: document.querySelector("[data-game-crew-owner]"), |
| 27 | + projectName: document.querySelector("[data-game-crew-project-name]"), |
| 28 | + refresh: document.querySelector("[data-game-crew-refresh]"), |
| 29 | + selected: document.querySelector("[data-game-crew-selected]"), |
| 30 | + status: document.querySelector("[data-game-crew-status]"), |
| 31 | + table: document.querySelector("[data-game-crew-table]"), |
| 32 | + tableCounts: document.querySelector("[data-game-crew-table-counts]"), |
| 33 | +}; |
| 34 | + |
| 35 | +function setText(target, value) { |
| 36 | + if (target) { |
| 37 | + target.textContent = value; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function createCell(text) { |
| 42 | + const cell = document.createElement("td"); |
| 43 | + cell.textContent = text; |
| 44 | + return cell; |
| 45 | +} |
| 46 | + |
| 47 | +function createButton(label, datasetName, value, options = {}) { |
| 48 | + const button = document.createElement("button"); |
| 49 | + button.className = options.secondary ? "btn btn--secondary btn--compact" : "btn btn--compact"; |
| 50 | + button.type = "button"; |
| 51 | + button.dataset[datasetName] = value; |
| 52 | + button.textContent = label; |
| 53 | + if (options.disabled) { |
| 54 | + button.disabled = true; |
| 55 | + } |
| 56 | + return button; |
| 57 | +} |
| 58 | + |
| 59 | +function currentSession() { |
| 60 | + try { |
| 61 | + return getSessionCurrent(); |
| 62 | + } catch { |
| 63 | + return { authenticated: false }; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function redirectGuestWriteAction() { |
| 68 | + if (currentSession()?.authenticated === true) { |
| 69 | + return false; |
| 70 | + } |
| 71 | + setText(elements.log, "Sign in before changing project crew membership."); |
| 72 | + window.location.href = new URL("/account/sign-in.html", window.location.href).href; |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +function renderTableCounts(snapshot) { |
| 77 | + if (!elements.tableCounts) { |
| 78 | + return; |
| 79 | + } |
| 80 | + elements.tableCounts.replaceChildren(); |
| 81 | + snapshot.tableCounts.forEach((count) => { |
| 82 | + const row = document.createElement("tr"); |
| 83 | + row.append(createCell(count.table), createCell(String(count.rows))); |
| 84 | + elements.tableCounts.append(row); |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +function renderMembers(snapshot) { |
| 89 | + if (!elements.table) { |
| 90 | + return; |
| 91 | + } |
| 92 | + elements.table.replaceChildren(); |
| 93 | + |
| 94 | + if (!snapshot.members.length) { |
| 95 | + const row = document.createElement("tr"); |
| 96 | + const cell = document.createElement("td"); |
| 97 | + cell.colSpan = 5; |
| 98 | + cell.textContent = "No crew members yet."; |
| 99 | + row.append(cell); |
| 100 | + elements.table.append(row); |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + snapshot.members.forEach((member) => { |
| 105 | + const row = document.createElement("tr"); |
| 106 | + row.dataset.gameCrewMemberRow = member.userKey; |
| 107 | + const actions = document.createElement("div"); |
| 108 | + actions.className = "action-group action-group--tight"; |
| 109 | + actions.append(createButton( |
| 110 | + member.role === "Owner" ? "Owner locked" : "Remove", |
| 111 | + "gameCrewRemove", |
| 112 | + member.userKey, |
| 113 | + { disabled: member.role === "Owner", secondary: true }, |
| 114 | + )); |
| 115 | + const actionCell = document.createElement("td"); |
| 116 | + actionCell.append(actions); |
| 117 | + row.append( |
| 118 | + createCell(member.displayName), |
| 119 | + createCell(member.role), |
| 120 | + createCell(member.status), |
| 121 | + createCell(member.joinedAt ? new Date(member.joinedAt).toLocaleDateString() : "Ready"), |
| 122 | + actionCell, |
| 123 | + ); |
| 124 | + elements.table.append(row); |
| 125 | + }); |
| 126 | +} |
| 127 | + |
| 128 | +function normalizeSnapshot(value) { |
| 129 | + return value && typeof value === "object" && Array.isArray(value.members) |
| 130 | + ? value |
| 131 | + : { |
| 132 | + activeProject: null, |
| 133 | + guidance: "Project crew is temporarily unavailable. Refresh the page.", |
| 134 | + members: [], |
| 135 | + owner: null, |
| 136 | + status: "Unavailable", |
| 137 | + tableCounts: GAME_CREW_TABLES.map((table) => ({ rows: 0, table })), |
| 138 | + }; |
| 139 | +} |
| 140 | + |
| 141 | +function render() { |
| 142 | + const snapshot = normalizeSnapshot(repository.getSnapshot()); |
| 143 | + setText(elements.status, snapshot.status); |
| 144 | + setText(elements.outputStatus, snapshot.status); |
| 145 | + setText(elements.owner, snapshot.owner?.displayName || snapshot.activeProject?.ownerDisplayName || "Unknown"); |
| 146 | + setText(elements.count, String(snapshot.members.length)); |
| 147 | + setText(elements.projectName, snapshot.activeProject?.name || "No project selected"); |
| 148 | + setText(elements.guidance, snapshot.guidance); |
| 149 | + renderMembers(snapshot); |
| 150 | + renderTableCounts(snapshot); |
| 151 | +} |
| 152 | + |
| 153 | +elements.add?.addEventListener("click", () => { |
| 154 | + if (redirectGuestWriteAction()) { |
| 155 | + return; |
| 156 | + } |
| 157 | + const result = repository.addMember(); |
| 158 | + setText(elements.action, result.message); |
| 159 | + setText(elements.log, result.message); |
| 160 | + render(); |
| 161 | +}); |
| 162 | + |
| 163 | +elements.refresh?.addEventListener("click", () => { |
| 164 | + setText(elements.log, "Project crew refreshed from the API."); |
| 165 | + render(); |
| 166 | +}); |
| 167 | + |
| 168 | +elements.table?.addEventListener("click", (event) => { |
| 169 | + const remove = event.target.closest("[data-game-crew-remove]"); |
| 170 | + if (!remove || remove.disabled) { |
| 171 | + return; |
| 172 | + } |
| 173 | + if (redirectGuestWriteAction()) { |
| 174 | + return; |
| 175 | + } |
| 176 | + const result = repository.removeMember(remove.dataset.gameCrewRemove); |
| 177 | + setText(elements.selected, result.member?.displayName || "Selected member"); |
| 178 | + setText(elements.action, result.message); |
| 179 | + setText(elements.log, result.message); |
| 180 | + render(); |
| 181 | +}); |
| 182 | + |
| 183 | +render(); |
0 commit comments