Import a 3D Model and automatically assign and export animations
TypeScript
try to add survey page to view results
Scott Petrovic7218fadfb81a
diff --git a/src/survey-worker.js b/src/survey-worker.jsindex 3f21a4f..640d3a9 100644--- a/src/survey-worker.js+++ b/src/survey-worker.js@@ -15,6 +15,9 @@* "rig_type": "human",* "description": "A sword slash combo and a ledge climb"* }+ *+ * GET /responses (requires "Authorization: Bearer <ADMIN_TOKEN>") returns the latest+ * survey submissions and animation requests for static/survey/index.html.*/export default {/**@@ -40,7 +43,12 @@ export default {return handleAnimationRequest(request, env);}- // 4) Fallback for unknown routes/methods.+ // 4) Admin read endpoint: latest survey responses and animation requests.+ if (request.method === "GET" && url.pathname === "/responses") {+ return handleGetResponses(request, env);+ }++ // 5) Fallback for unknown routes/methods.return corsResponse({ error: "Not found" }, 404);},};@@ -50,6 +58,7 @@ const MAX_QUESTION_LENGTH = 200;const MAX_ANSWER_LENGTH = 2000;const MAX_RIG_TYPE_LENGTH = 50;const MAX_ANIMATION_REQUEST_LENGTH = 2000;+const RESPONSES_READ_LIMIT = 300; // for report viewing/*** Handles POST /submit by validating payload shape, preparing DB writes,@@ -130,6 +139,65 @@ async function handleAnimationRequest(request, env) {return corsResponse({ success: true }, 201);}+/**+ * Handles GET /responses for the survey viewer page (static/survey/index.html).+ * Requires "Authorization: Bearer <ADMIN_TOKEN>", where ADMIN_TOKEN is a worker secret.+ * Returns the latest survey submissions (all answers grouped by session) and animation requests.+ */+async function handleGetResponses(request, env) {+ if (!(await isAuthorized(request, env))) {+ return corsResponse({ error: "Unauthorized" }, 401);+ }++ // A single survey submission is stored as multiple rows sharing a session_id,+ // so limit by the latest sessions rather than by rows.+ const survey_query = env.DB.prepare(+ `SELECT session_id, question, answer, submitted_at FROM responses+ WHERE session_id IN (+ SELECT session_id FROM responses GROUP BY session_id ORDER BY MAX(id) DESC LIMIT ?+ )+ ORDER BY id DESC`+ ).bind(RESPONSES_READ_LIMIT);++ const animation_requests_query = env.DB.prepare(+ "SELECT id, rig_type, description, submitted_at FROM animation_requests ORDER BY id DESC LIMIT ?"+ ).bind(RESPONSES_READ_LIMIT);++ const [survey_result, animation_requests_result] = await env.DB.batch([survey_query, animation_requests_query]);++ return corsResponse(+ {+ survey_responses: survey_result.results,+ animation_requests: animation_requests_result.results,+ },+ 200+ );+}++/**+ * Compares the bearer token against the ADMIN_TOKEN secret in constant time.+ * Denies everything when the secret has not been configured.+ */+async function isAuthorized(request, env) {+ const admin_token = env.ADMIN_TOKEN;+ if (typeof admin_token !== "string" || admin_token.length === 0) {+ return false;+ }++ const auth_header = request.headers.get("Authorization") || "";+ const provided_token = auth_header.startsWith("Bearer ") ? auth_header.slice(7) : "";++ const encoder = new TextEncoder();+ const provided_bytes = encoder.encode(provided_token);+ const expected_bytes = encoder.encode(admin_token);++ if (provided_bytes.byteLength !== expected_bytes.byteLength) {+ return false;+ }++ return crypto.subtle.timingSafeEqual(provided_bytes, expected_bytes);+}+/*** Normalizes an optional session id from the client.* Generates a UUID when no usable id is provided.@@ -225,7 +293,7 @@ function corsResponse(body, status) {"Content-Type": "application/json","Access-Control-Allow-Origin": "*", // restrict to your domain in production"Access-Control-Allow-Methods": "GET, POST, OPTIONS",- "Access-Control-Allow-Headers": "Content-Type",+ "Access-Control-Allow-Headers": "Content-Type, Authorization",};return new Response(body ? JSON.stringify(body) : null, { status, headers });diff --git a/static/survey/index.html b/static/survey/index.htmlnew file mode 100644index 0000000..f85c341--- /dev/null+++ b/static/survey/index.html@@ -0,0 +1,279 @@+<!DOCTYPE html>+<html lang="en">+<head>+ <meta charset="UTF-8" />+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />+ <meta name="robots" content="noindex, nofollow" />+ <title>Survey Responses</title>+ <style>+ :root {+ --bg: #f5f6f8;+ --bg-panel: #ffffff;+ --text: #1d2330;+ --text-muted: #5d6677;+ --border: #dde1e8;+ --accent: #2f6fdf;+ --error: #c62828;+ }++ @media (prefers-color-scheme: dark) {+ :root {+ --bg: #121722;+ --bg-panel: #1b2230;+ --text: #e4e8ef;+ --text-muted: #9aa3b2;+ --border: #2c3545;+ --accent: #6b9bff;+ --error: #ff6b6b;+ }+ }++ * { box-sizing: border-box; }++ body {+ margin: 0;+ padding: 2rem 1rem;+ background: var(--bg);+ color: var(--text);+ font-family: system-ui, -apple-system, "Segoe UI", sans-serif;+ line-height: 1.4;+ }++ main {+ max-width: 70rem;+ margin: 0 auto;+ }++ h1 { margin: 0 0 1.5rem; }+ h2 { margin: 0; font-size: 1.3rem; }++ form {+ display: flex;+ flex-wrap: wrap;+ gap: 0.5rem;+ margin-bottom: 1rem;+ }++ input, button {+ font: inherit;+ padding: 0.5rem 0.75rem;+ border-radius: 4px;+ border: 1px solid var(--border);+ background: var(--bg-panel);+ color: var(--text);+ }++ input { flex: 1; min-width: 12rem; }++ button {+ background: var(--accent);+ border-color: var(--accent);+ color: #fff;+ cursor: pointer;+ }++ #status { color: var(--text-muted); min-height: 1.4em; margin: 0 0 1.5rem; }+ #status.error { color: var(--error); }++ section {+ background: var(--bg-panel);+ border: 1px solid var(--border);+ border-radius: 6px;+ padding: 1.25rem;+ margin-bottom: 2rem;+ }++ .section-header {+ display: flex;+ justify-content: space-between;+ align-items: baseline;+ margin-bottom: 1rem;+ }++ .count { color: var(--text-muted); }++ .table-scroll { overflow-x: auto; }++ table {+ width: 100%;+ border-collapse: collapse;+ }++ th, td {+ text-align: left;+ vertical-align: top;+ padding: 0.5rem 0.75rem;+ border-bottom: 1px solid var(--border);+ }++ th { color: var(--text-muted); font-weight: 600; white-space: nowrap; }+ td.date { white-space: nowrap; color: var(--text-muted); }+ td.text { white-space: pre-wrap; word-break: break-word; min-width: 16rem; }++ .empty { color: var(--text-muted); font-style: italic; }+ </style>+</head>+<body>+ <main>+ <h1>Survey Responses</h1>++ <form id="token-form">+ <input type="password" id="admin-token" placeholder="Admin token" autocomplete="current-password" required />+ <button type="submit">Load</button>+ </form>+ <p id="status"></p>++ <section>+ <div class="section-header">+ <h2>Animation Requests</h2>+ <span class="count" id="animation-requests-count"></span>+ </div>+ <div class="table-scroll" id="animation-requests"></div>+ </section>++ <section>+ <div class="section-header">+ <h2>Feedback Survey</h2>+ <span class="count" id="survey-responses-count"></span>+ </div>+ <div class="table-scroll" id="survey-responses"></div>+ </section>+ </main>++ <script>+ // Same worker that receives the survey submissions (src/survey-worker.js)+ const WORKER_URL = 'https://mesh2motion-app.scottpetrovic.workers.dev'+ const TOKEN_STORAGE_KEY = 'm2m-survey-admin-token'++ const token_form = document.getElementById('token-form')+ const token_input = document.getElementById('admin-token')+ const status_element = document.getElementById('status')++ function set_status (message, is_error = false) {+ status_element.textContent = message+ status_element.classList.toggle('error', is_error)+ }++ // D1 stores datetime('now') as UTC "YYYY-MM-DD HH:MM:SS"; show it in local time+ function format_date (utc_text) {+ const date = new Date(utc_text.replace(' ', 'T') + 'Z')+ return isNaN(date.getTime()) ? utc_text : date.toLocaleString()+ }++ // Builds a table using textContent so submitted text is never parsed as HTML+ function build_table (headers, rows) {+ const table = document.createElement('table')+ const header_row = table.createTHead().insertRow()+ headers.forEach((header) => {+ const th = document.createElement('th')+ th.textContent = header+ header_row.appendChild(th)+ })++ const tbody = table.createTBody()+ rows.forEach((row) => {+ const tr = tbody.insertRow()+ row.forEach((cell) => {+ const td = tr.insertCell()+ td.textContent = cell.text+ td.className = cell.class_name ?? ''+ })+ })+ return table+ }++ function render_section (container_id, count_id, count_label, headers, rows) {+ const container = document.getElementById(container_id)+ container.innerHTML = ''+ document.getElementById(count_id).textContent = `${rows.length} ${count_label}`++ if (rows.length === 0) {+ const empty = document.createElement('p')+ empty.className = 'empty'+ empty.textContent = 'Nothing submitted yet.'+ container.appendChild(empty)+ return+ }++ container.appendChild(build_table(headers, rows))+ }++ function render_animation_requests (requests) {+ const rows = requests.map((request) => [+ { text: format_date(request.submitted_at), class_name: 'date' },+ { text: request.rig_type },+ { text: request.description, class_name: 'text' }+ ])+ render_section('animation-requests', 'animation-requests-count', 'latest requests',+ ['Submitted', 'Rig', 'Request'], rows)+ }++ // Each survey submission is several rows (one per question) sharing a session_id.+ // Group them into one table row per submission with a column per question.+ function render_survey_responses (responses) {+ const submissions = new Map()+ const questions = []++ responses.forEach((response) => {+ if (!submissions.has(response.session_id)) {+ submissions.set(response.session_id, { submitted_at: response.submitted_at, answers: {} })+ }+ submissions.get(response.session_id).answers[response.question] = response.answer+ if (!questions.includes(response.question)) {+ questions.push(response.question)+ }+ })++ const rows = Array.from(submissions.values()).map((submission) => [+ { text: format_date(submission.submitted_at), class_name: 'date' },+ ...questions.map((question) => ({ text: submission.answers[question] ?? '', class_name: 'text' }))+ ])+ render_section('survey-responses', 'survey-responses-count', 'latest submissions',+ ['Submitted', ...questions], rows)+ }++ async function load_responses (token) {+ set_status('Loading...')+ try {+ const res = await fetch(`${WORKER_URL}/responses`, {+ headers: { Authorization: `Bearer ${token}` }+ })++ if (res.status === 401) {+ set_status('Invalid admin token.', true)+ try { sessionStorage.removeItem(TOKEN_STORAGE_KEY) } catch {}+ return+ }++ if (!res.ok) {+ set_status(`Failed to load responses (${res.status} ${res.statusText}).`, true)+ return+ }++ const data = await res.json()+ try { sessionStorage.setItem(TOKEN_STORAGE_KEY, token) } catch {}++ render_animation_requests(data.animation_requests ?? [])+ render_survey_responses(data.survey_responses ?? [])+ set_status(`Loaded ${new Date().toLocaleTimeString()}`)+ } catch (error) {+ console.error(error)+ set_status('Could not reach the survey worker.', true)+ }+ }++ token_form.addEventListener('submit', (event) => {+ event.preventDefault()+ load_responses(token_input.value.trim())+ })++ // Remember the token for this browser tab only+ let saved_token = null+ try { saved_token = sessionStorage.getItem(TOKEN_STORAGE_KEY) } catch {}+ if (saved_token) {+ token_input.value = saved_token+ load_responses(saved_token)+ }+ </script>+</body>+</html>diff --git a/static/survey/readme.md b/static/survey/readme.mdindex bd32d94..6424ef7 100644--- a/static/survey/readme.md+++ b/static/survey/readme.md@@ -6,10 +6,18 @@ I am using Cloudflare for this since that is just where I am doing things. If so2. Get the UUID of the database using the npx command in command prompt: npx wrangler d1 list3. Run this command to create the table that stores responses:- npx wrangler d1 execute m2m-survey-responses --remote --file=survey/schema.sql+ npx wrangler d1 execute m2m-survey-responses --remote --file=static/survey/schema.sql4. Run this command to actually deploy the new "worker" that will process the survey being sent:npx wrangler deploy-The schema file also creates the `animation_requests` table used by the "Request an animation" dialog (POST /animation-request). The statements use `IF NOT EXISTS`, so re-running step 3 on an existing database only adds missing tables.\ No newline at end of file+The schema file also creates the `animation_requests` table used by the "Request an animation" dialog (POST /animation-request). The statements use `IF NOT EXISTS`, so re-running step 3 on an existing database only adds missing tables.++## Viewing responses++`survey/index.html` shows the latest 50 feedback submissions and animation requests. It reads from the worker's `GET /responses` endpoint, which requires an admin token. Set the token as a worker secret (it is never stored in the repo), then enter it on the page:++ npx wrangler secret put ADMIN_TOKEN++Without the secret set, the endpoint rejects every request.\ No newline at end of file