All files / app-leaderboard/src/pages settings.ts

90.9% Statements 10/11
55% Branches 11/20
100% Functions 2/2
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133              5x                           3x 3x 3x 3x 3x 2x                               1x 1x 1x                                                                                                                                                                                
import type { PageContent } from './template.ts';
import { html, nothing, renderHTML } from '@be-music/service-web-theme/server';
import { Avatar } from './components/basics.ts';
import { Layout, PageHeader } from './layout.ts';
import { meta, PAGE_CACHE, pageMessages, type PageContext } from './page.ts';
 
function Card(props: { title: string; description?: string; children: PageContent }) {
  return html`<section class="card gap-4" data-size="sm">
    <header>
      <h2>${props.title}</h2>
      ${props.description !== undefined ? html`<p>${props.description}</p>` : null}
    </header>
    <div class="space-y-4 px-4">${props.children}</div>
  </section>`;
}
 
/**
 * The signed-in player's settings: handle, display name, avatar and passkeys. The forms and buttons are wired by the
 * client script (client/main.ts), which calls the account API; signed out, the page offers the passkey sign-in.
 */
export async function settingsPage(c: PageContext<'/settings'>) {
  const viewer = c.get('viewer');
  c.header('cache-control', PAGE_CACHE);
  const t = pageMessages(c).settings;
  const pageMeta = meta(c, t.title, t.description, [{ label: t.title, kind: 'settings' }]);
  if (viewer === null) {
    return c.html(
      renderHTML(
        Layout({
          meta: pageMeta,
          children: html`${PageHeader({ title: t.title, kind: 'settings', children: html`${t.signedOutLead}` })}${Card({
            title: t.signIn,
            description: t.signInLead,
            children: html`<button type="button" class="btn" data-account-action="sign-in">
                ${t.signInWithPasskey}
              </button>
              <p class="text-muted-foreground text-sm">${t.noPasskey}</p>`,
          })}`,
        }),
      ),
    );
  }
  Iif (c.env.AUTH_ISSUER) return c.redirect(new URL('/account', c.env.AUTH_ISSUER).href, 302);
  const hasUpload = !viewer.avatarUrl.endsWith('?v=g');
  return c.html(
    renderHTML(
      Layout({
        meta: pageMeta,
        children: html`${PageHeader({
            title: t.title,
            kind: 'settings',
            children: html`${t.signedInAs(viewer.handle)}${viewer.anonymous ? t.anonymousSuffix : ''}`,
          })}<be-music-status
            class="text-destructive text-sm"
            data-settings-status
            role="status"
            hidden
          ></be-music-status
          >${Card({
            title: t.profile,
            description: t.profileLead,
            children: html`<form class="grid gap-4 sm:max-w-md" data-profile-form>
              <div class="grid gap-2">
                <label class="label" for="settings-handle">${t.handle}</label
                ><input
                  class="input"
                  id="settings-handle"
                  name="handle"
                  value=${viewer.handle ?? nothing}
                  required
                  minlength=${3}
                  maxlength=${15}
                  pattern="[A-Za-z0-9_]{3,15}"
                  autocomplete="username"
                  spellcheck=${false}
                />
                <p class="text-muted-foreground text-xs">${t.handleHint}</p>
              </div>
              <div class="grid gap-2">
                <label class="label" for="settings-name">${t.name}</label
                ><input
                  class="input"
                  id="settings-name"
                  name="displayName"
                  value=${viewer.displayName ?? nothing}
                  required
                  maxlength=${64}
                  autocomplete="nickname"
                />
                <p class="text-muted-foreground text-xs">${t.nameHint}</p>
              </div>
              <div><button type="submit" class="btn">${t.save}</button></div>
            </form>`,
          })}${Card({
            title: t.avatar,
            description: t.avatarLead,
            children: html`<div class="flex flex-wrap items-center gap-4">
              ${Avatar({ src: viewer.avatarUrl, size: 96 })}
              <div class="flex flex-wrap items-center gap-2">
                <label class="btn" data-variant="outline"
                  >${t.upload}<input
                    type="file"
                    class="sr-only"
                    accept="image/png,image/jpeg,image/webp,image/*"
                    data-avatar-input /></label
                >${
                  hasUpload
                    ? html`<button type="button" class="btn" data-variant="ghost" data-account-action="remove-avatar">
                        ${t.useGenerated}
                      </button>`
                    : null
                }
              </div>
            </div>`,
          })}${Card({
            title: t.passkeys,
            description: viewer.anonymous ? t.passkeysAnonymous : t.passkeysCount(viewer.passkeys),
            children: html`<div>
              <button
                type="button"
                class="btn"
                data-variant=${viewer.anonymous ? 'primary' : 'outline'}
                data-account-action="create-passkey"
              >
                ${viewer.anonymous ? t.keepAccount : t.addPasskey}
              </button>
            </div>`,
          })}`,
      }),
    ),
  );
}