All files / app-dashboard/src page.ts

94.73% Statements 18/19
97.82% Branches 45/46
88.88% Functions 8/9
94.73% Lines 18/19

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181                                                            10x 10x         4x     2x         1x               2x   1x                 1x                                 10x 10x 10x       10x                   10x         10x   10x             10x 20x                 10x                                                                                                                        
import {
  html,
  nothing,
  unsafeHTML,
  renderHTML,
  serviceHeaderTemplate,
  serviceFooterTemplate,
  serviceSkipLinkTemplate,
} from '@be-music/service-web-theme/server';
import {
  resolveLocale,
  SERVICE_CSS,
  SERVICE_FOOTER_LINK_CLASS,
  serviceOrigins,
  type BuildInfo,
  type Locale,
} from '@be-music/service-web-theme';
 
/** The public profile combined with the Accounts session's guest status. */
export interface DashboardAccount {
  handle: string;
  displayName: string;
  anonymous: boolean;
  avatarURL?: string;
}
 
export type { Locale };
 
/** Play and Leaderboard next to this Dashboard host. Preview stays on preview. */
export function serviceLinks(hostname: string): { play: string; leaderboard: string } {
  const origins = serviceOrigins(hostname);
  return { play: origins.play, leaderboard: origins.leaderboard };
}
 
/** Japanese when the browser prefers it, English otherwise. `?lang=` and the language cookie win in `resolveLocale`. */
export function pickLocale(acceptLanguage: string | null): Locale {
  return resolveLocale({ acceptLanguage: acceptLanguage ?? undefined });
}
 
const TEXT = {
  ja: {
    title: 'Be-Music Dashboard',
    signedIn: (name: string, handle: string) => `${name}(@${handle})でログイン中です。`,
    anonymous: (handle: string) =>
      `ゲストアカウント @${handle} でログイン中です。パスキー・Google・Discord で保存すると、ほかの端末でも使えます。`,
    signedOut: 'ログインしていません。',
    play: 'プレイする',
    leaderboard: 'ランキングを見る',
    note: 'このページは仮のダッシュボードです。',
  },
  en: {
    title: 'Be-Music Dashboard',
    signedIn: (name: string, handle: string) => `Signed in as ${name} (@${handle}).`,
    anonymous: (handle: string) =>
      `Signed in as a guest, @${handle}. Keep the account with a passkey, Google or Discord to use it on other devices.`,
    signedOut: 'Not signed in.',
    play: 'Play',
    leaderboard: 'Leaderboard',
    note: 'This is a temporary dashboard.',
  },
} as const;
 
export function escapeHTML(value: string): string {
  return value
    .replaceAll('&', '&')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;')
    .replaceAll('"', '&quot;')
    .replaceAll("'", '&#39;');
}
 
export function renderDashboard(input: {
  account: DashboardAccount | undefined;
  hostname: string;
  locale: Locale;
  showDonateLink?: boolean;
  buildInfo?: BuildInfo;
  requestURL?: string;
  links?: { play: string; leaderboard: string; donate: string };
}): string {
  const t = TEXT[input.locale];
  const links = input.links ?? { ...serviceLinks(input.hostname), donate: `https://donate.${input.hostname}/` };
  const donate = input.showDonateLink
    ? html` ·
        <a class=${SERVICE_FOOTER_LINK_CLASS} href=${links.donate}>${input.locale === 'ja' ? '寄付' : 'Donate'}</a>`
    : nothing;
  const footer = serviceFooterTemplate({
    current: 'dashboard',
    hostname: input.hostname,
    origins: { play: links.play, leaderboard: links.leaderboard },
    navLabel: input.locale === 'ja' ? 'サービス' : 'Services',
    siteHTML: renderHTML(html`Be-Music${donate}`),
    buildInfo: input.buildInfo,
    locale: input.locale,
  });
  const status =
    input.account === undefined
      ? t.signedOut
      : input.account.anonymous
        ? t.anonymous(input.account.handle)
        : t.signedIn(input.account.displayName, input.account.handle);
  const accountHref = input.account === undefined ? '/login' : '/account';
  const accountLabel =
    input.account === undefined
      ? input.locale === 'ja'
        ? 'ログイン'
        : 'Sign in'
      : input.locale === 'ja'
        ? 'アカウント管理'
        : 'Manage account';
  const serviceCard = (href: string, title: string, description: string, open: string) =>
    html` <a
      class="card hover:bg-accent focus-visible:ring-ring/50 focus-visible:ring-3 gap-3 px-4 no-underline outline-none"
      data-size="sm"
      href=${href}
    >
      <h3 class="font-medium">${title}</h3>
      <p class="text-muted-foreground flex-1">${description}</p>
      <span class="font-medium">${open} <span aria-hidden="true">→</span></span>
    </a>`;
  return renderHTML(
    html`<!doctype html>
      <html lang=${input.locale}>
        <head>
          <meta charset="utf-8" />
          <meta name="viewport" content="width=device-width, initial-scale=1" />
          <meta name="robots" content="noindex" />
          <title>${t.title}</title>
          ${unsafeHTML(`<style>${SERVICE_CSS}</style>`)}
        </head>
        <body class="bg-background text-foreground min-h-svh antialiased">
          ${serviceSkipLinkTemplate(input.locale)}
          ${serviceHeaderTemplate({
            locale: input.locale,
            title: 'Be-Music Dashboard',
            homeHref: '/',
            playHref: links.play,
            accountHref,
            account: input.account,
            requestURL: input.requestURL ?? '/',
          })}
          <main class="mx-auto max-w-5xl px-4 pt-8 pb-12" id="main">
            <div class="mb-6">
              <h1 class="text-2xl font-semibold tracking-tight">
                ${input.locale === 'ja' ? 'ダッシュボード' : 'Dashboard'}
              </h1>
              <p class="text-muted-foreground text-sm">
                ${input.locale === 'ja' ? 'アカウントと、それぞれの Be-Music サービスへ。' : 'Your account and Be-Music services, in one place.'}
              </p>
            </div>
            <section class="card mb-8" data-size="sm" aria-labelledby="account-heading">
              <header><h2 id="account-heading">${input.locale === 'ja' ? 'アカウント' : 'Account'}</h2></header>
              <section class="grid gap-4">
                <p
                  class="text-muted-foreground text-sm"
                  data-state=${input.account === undefined ? 'signed-out' : input.account.anonymous ? 'anonymous' : 'signed-in'}
                >
                  ${status}
                </p>
                <div class="flex flex-wrap gap-2">
                  <a class="btn no-underline" data-size="sm" href=${accountHref}>${accountLabel}</a>
                  ${input.account === undefined ? nothing : html`<form method="post" action="/sign-out"><button class="btn" data-size="sm" data-variant="outline" type="submit">${input.locale === 'ja' ? 'ログアウト' : 'Sign out'}</button></form>`}
                </div>
              </section>
            </section>
            <section aria-labelledby="services-heading">
              <h2 class="mb-4 text-lg font-semibold tracking-tight" id="services-heading">
                ${input.locale === 'ja' ? 'サービス' : 'Services'}
              </h2>
              <nav class="grid gap-4 sm:grid-cols-2" aria-label=${input.locale === 'ja' ? 'サービス' : 'Services'}>
                ${serviceCard(links.play, 'Play', input.locale === 'ja' ? 'BMS・bmson の譜面をブラウザでプレイ。' : 'Play BMS and bmson charts in your browser.', t.play)}
                ${serviceCard(links.leaderboard, 'Leaderboard', input.locale === 'ja' ? 'スコアやプレイヤーの記録をチェック。' : 'Explore scores, rankings and player records.', t.leaderboard)}
              </nav>
            </section>
          </main>
          ${footer}
        </body>
      </html>`,
  );
}