Skip to content

Import a 3D Model and automatically assign and export animations

TypeScript
18.0 KB
Raw Blame
<!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;
      --border-strong: #b9c0cc;
      --accent: #2f6fdf;
      --error: #c62828;
      --star: #e8710a;
      --star-empty: #c9ced8;
      --bar-track: #f0f2f5;
      --selected-bg: #eaf1fd;
    }

    @media (prefers-color-scheme: dark) {
      :root {
        --bg: #121722;
        --bg-panel: #1b2230;
        --text: #e4e8ef;
        --text-muted: #9aa3b2;
        --border: #2c3545;
        --border-strong: #46516a;
        --accent: #6b9bff;
        --error: #ff6b6b;
        --star: #ffa94d;
        --star-empty: #3a4456;
        --bar-track: #141a26;
        --selected-bg: #1f2b42;
      }
    }

    * { 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;
    }

    #token-form input, #token-form button {
      font: inherit;
      padding: 0.5rem 0.75rem;
      border-radius: 4px;
      border: 1px solid var(--border);
      background: var(--bg-panel);
      color: var(--text);
    }

    #token-form input { flex: 1; min-width: 12rem; }

    #token-form 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.rating { white-space: nowrap; }
    td.text { white-space: pre-wrap; word-break: break-word; min-width: 16rem; }

    .empty { color: var(--text-muted); font-style: italic; }

    /* Star display. The filled layer is clipped to a percentage for partial stars. */
    .stars {
      position: relative;
      display: inline-block;
      line-height: 1;
      white-space: nowrap;
      color: var(--star-empty);
    }

    .stars-fill {
      position: absolute;
      top: 0;
      left: 0;
      bottom: 0;
      overflow: hidden;
      color: var(--star);
    }

    .stars.large { font-size: 1.75rem; }

    /* Summary on the left (kept in view while scrolling), feedback on the right; stacks on narrow screens */
    .survey-layout {
      display: grid;
      grid-template-columns: minmax(18rem, 24rem) minmax(0, 1fr);
      gap: 2rem;
      align-items: start;
    }

    #rating-summary { position: sticky; top: 1rem; }

    @media (max-width: 56rem) {
      .survey-layout { grid-template-columns: minmax(0, 1fr); gap: 0; }
      #rating-summary { position: static; }
    }

    /* Rating summary (average + distribution bars) */
    .rating-average {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      font-size: 1.25rem;
    }

    .rating-total { color: var(--text-muted); margin: 0.5rem 0 1rem; }

    .rating-rows {
      display: grid;
      gap: 0.25rem;
      max-width: 34rem;
      margin-bottom: 1.5rem;
    }

    .rating-row {
      display: grid;
      grid-template-columns: 3.5rem 1fr 3rem 3rem;
      align-items: center;
      gap: 0.75rem;
      padding: 0.35rem 0.5rem;
      margin: 0 -0.5rem;
      border: 1px solid transparent;
      border-radius: 4px;
      background: none;
      color: var(--accent);
      font: inherit;
      text-align: left;
      cursor: pointer;
    }

    .rating-row:hover:not(:disabled) .rating-label { text-decoration: underline; }
    .rating-row:hover:not(:disabled) .bar { border-color: var(--star); }
    .rating-row:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
    .rating-row[aria-pressed="true"] { background: var(--selected-bg); border-color: var(--accent); }
    .rating-row:disabled { color: var(--text); cursor: default; }

    .bar {
      height: 1.25rem;
      border: 1px solid var(--border-strong);
      border-radius: 4px;
      overflow: hidden;
      background: var(--bar-track);
    }

    .bar-fill { display: block; height: 100%; background: var(--star); }

    .rating-percent { text-align: right; }
    .rating-count { text-align: right; color: var(--text-muted); font-size: 0.9em; }

    .feedback-toolbar {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
      align-items: center;
      gap: 0.5rem 1rem;
      margin-bottom: 0.75rem;
      color: var(--text-muted);
    }

    .feedback-toolbar label {
      display: flex;
      align-items: center;
      gap: 0.4rem;
      cursor: pointer;
    }

    .link-button {
      padding: 0;
      margin-left: 0.5rem;
      border: none;
      background: none;
      color: var(--accent);
      font: inherit;
      text-decoration: underline;
      cursor: pointer;
    }
  </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="survey-layout">
        <div id="rating-summary"></div>
        <div class="survey-feedback">
          <div class="feedback-toolbar" id="feedback-toolbar" hidden>
            <span id="feedback-filter-label"></span>
            <label><input type="checkbox" id="feedback-only" checked /> Only with written feedback</label>
          </div>
          <div class="table-scroll" id="survey-responses"></div>
        </div>
      </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'

    // Question names sent by DownloadSuccessDialog.ts
    const RATING_QUESTION = 'Rating'
    const FEEDBACK_QUESTION = 'Feedback'
    const RATING_VALUES = [5, 4, 3, 2, 1]

    const token_form = document.getElementById('token-form')
    const token_input = document.getElementById('admin-token')
    const status_element = document.getElementById('status')
    const feedback_only_checkbox = document.getElementById('feedback-only')

    // Survey state: one entry per submission, plus the rating clicked in the summary (null = all)
    let survey_submissions = []
    let selected_rating = null

    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.
    // A cell can pass a DOM element instead of text.
    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()
          if (cell.element) {
            td.appendChild(cell.element)
          } else {
            td.textContent = cell.text
          }
          td.className = cell.class_name ?? ''
        })
      })
      return table
    }

    function build_empty_message (message) {
      const empty = document.createElement('p')
      empty.className = 'empty'
      empty.textContent = message
      return empty
    }

    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) {
        container.appendChild(build_empty_message('Nothing submitted yet.'))
        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)
    }

    // Five stars with the filled layer clipped to value / 5, so 4.7 shows a partial last star
    function build_stars (value, size_class = '') {
      const stars = document.createElement('span')
      stars.className = `stars ${size_class}`.trim()
      stars.setAttribute('role', 'img')
      stars.setAttribute('aria-label', `${value} out of 5 stars`)
      stars.textContent = '★★★★★'

      const fill = document.createElement('span')
      fill.className = 'stars-fill'
      fill.style.width = `${(value / 5) * 100}%`
      fill.textContent = '★★★★★'
      stars.appendChild(fill)
      return stars
    }

    function parse_rating (answer) {
      const rating = Number(answer)
      return RATING_VALUES.includes(rating) ? rating : null
    }

    // Each survey submission is several rows (one per question) sharing a session_id
    function group_survey_submissions (responses) {
      const submissions = new Map()
      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
      })

      return Array.from(submissions.values()).map((submission) => ({
        submitted_at: submission.submitted_at,
        rating: parse_rating(submission.answers[RATING_QUESTION]),
        feedback: (submission.answers[FEEDBACK_QUESTION] ?? '').trim()
      }))
    }

    function select_rating (rating) {
      selected_rating = selected_rating === rating ? null : rating
      render_rating_summary()
      render_feedback_list()
    }

    // Average rating plus a clickable bar per star value, like a store review summary
    function render_rating_summary () {
      const container = document.getElementById('rating-summary')
      container.innerHTML = ''

      const rated = survey_submissions.filter((submission) => submission.rating !== null)
      if (rated.length === 0) {
        return
      }

      const average = rated.reduce((sum, submission) => sum + submission.rating, 0) / rated.length
      const rounded_average = Math.round(average * 10) / 10

      const average_row = document.createElement('div')
      average_row.className = 'rating-average'
      const average_text = document.createElement('span')
      average_text.textContent = `${rounded_average} out of 5`
      average_row.append(build_stars(rounded_average, 'large'), average_text)

      const total = document.createElement('p')
      total.className = 'rating-total'
      total.textContent = `${rated.length} ${rated.length === 1 ? 'rating' : 'ratings'}`

      const rows = document.createElement('div')
      rows.className = 'rating-rows'

      RATING_VALUES.forEach((rating) => {
        const count = rated.filter((submission) => submission.rating === rating).length
        const percent = Math.round((count / rated.length) * 100)

        const row = document.createElement('button')
        row.type = 'button'
        row.className = 'rating-row'
        row.disabled = count === 0
        row.setAttribute('aria-pressed', String(selected_rating === rating))
        row.addEventListener('click', () => select_rating(rating))

        const label = document.createElement('span')
        label.className = 'rating-label'
        label.textContent = `${rating} star`

        const bar = document.createElement('span')
        bar.className = 'bar'
        const bar_fill = document.createElement('span')
        bar_fill.className = 'bar-fill'
        bar_fill.style.width = `${percent}%`
        bar.appendChild(bar_fill)

        const percent_text = document.createElement('span')
        percent_text.className = 'rating-percent'
        percent_text.textContent = `${percent}%`

        const count_text = document.createElement('span')
        count_text.className = 'rating-count'
        count_text.textContent = `(${count})`

        row.append(label, bar, percent_text, count_text)
        rows.appendChild(row)
      })

      container.append(average_row, total, rows)
    }

    // Submissions table, filtered by the selected rating and the "written feedback" checkbox
    function render_feedback_list () {
      const container = document.getElementById('survey-responses')
      const toolbar = document.getElementById('feedback-toolbar')
      const filter_label = document.getElementById('feedback-filter-label')
      container.innerHTML = ''
      document.getElementById('survey-responses-count').textContent = `${survey_submissions.length} latest submissions`

      if (survey_submissions.length === 0) {
        toolbar.hidden = true
        container.appendChild(build_empty_message('Nothing submitted yet.'))
        return
      }
      toolbar.hidden = false

      const feedback_only = feedback_only_checkbox.checked
      const visible = survey_submissions.filter((submission) =>
        (selected_rating === null || submission.rating === selected_rating) &&
        (!feedback_only || submission.feedback !== ''))

      const noun = visible.length === 1 ? 'submission' : 'submissions'
      const rating_part = selected_rating === null ? '' : ` rated ${selected_rating} star${selected_rating === 1 ? '' : 's'}`
      const feedback_part = feedback_only ? ' with written feedback' : ''
      filter_label.textContent = `Showing ${visible.length} ${noun}${rating_part}${feedback_part}`

      if (selected_rating !== null) {
        const clear_button = document.createElement('button')
        clear_button.type = 'button'
        clear_button.className = 'link-button'
        clear_button.textContent = 'Show all ratings'
        clear_button.addEventListener('click', () => select_rating(null))
        filter_label.appendChild(clear_button)
      }

      if (visible.length === 0) {
        container.appendChild(build_empty_message('No matching submissions.'))
        return
      }

      const rows = visible.map((submission) => [
        { text: format_date(submission.submitted_at), class_name: 'date' },
        submission.rating === null
          ? { text: '—', class_name: 'rating' }
          : { element: build_stars(submission.rating), class_name: 'rating' },
        { text: submission.feedback, class_name: 'text' }
      ])
      container.appendChild(build_table(['Submitted', 'Rating', 'Feedback'], rows))
    }

    function render_survey_responses (responses) {
      survey_submissions = group_survey_submissions(responses)
      selected_rating = null
      render_rating_summary()
      render_feedback_list()
    }

    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())
    })

    feedback_only_checkbox.addEventListener('change', render_feedback_list)

    // 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>