Admin dashboard, Site design 2x2 grid, show password on login #25

Merged
reudy merged 3 commits from session/visual-tweaks into main 2026-10-27 10:35:56 +01:00
Showing only changes of commit 05d8659685 - Show all commits

View file

@ -1,6 +1,6 @@
<script lang="ts"> <script lang="ts">
import IncomeChart from '$lib/admin/IncomeChart.svelte'; import IncomeChart from '$lib/admin/IncomeChart.svelte';
import { DISPLAY_LOCALE, formatDateTime, formatInstant, formatPrice } from '$lib/calendar'; import { DISPLAY_LOCALE, formatDateTime, formatInstant, formatPrice, zonedDateKey } from '$lib/calendar';
import { formatDuration } from '$shared/items'; import { formatDuration } from '$shared/items';
let { data } = $props(); let { data } = $props();
@ -20,22 +20,32 @@
// Mid-month noon UTC is inside the same month in every timezone. // Mid-month noon UTC is inside the same month in every timezone.
const monthLabel = (month: string, options: Intl.DateTimeFormatOptions = { month: 'long', year: 'numeric' }) => const monthLabel = (month: string, options: Intl.DateTimeFormatOptions = { month: 'long', year: 'numeric' }) =>
formatInstant(`${month}-15T12:00:00Z`, data.timezone, options); formatInstant(`${month}-15T12:00:00Z`, data.timezone, options);
const dayLabel = (value: string) => formatInstant(value, data.timezone, { weekday: 'short', day: 'numeric', month: 'short' }); const dayLabel = (value: string) => formatInstant(value, data.timezone, { weekday: 'long', day: 'numeric', month: 'short' });
const timeLabel = (value: string) => formatInstant(value, data.timezone, { hour: '2-digit', minute: '2-digit' }); const timeLabel = (value: string) => formatInstant(value, data.timezone, { hour: '2-digit', minute: '2-digit' });
let thisMonthName = $derived(monthLabel(dashboard.thisMonth.month, { month: 'long' })); let thisMonthName = $derived(monthLabel(dashboard.thisMonth.month, { month: 'long' }));
let incomeChange = $derived(dashboard.thisMonth.income - dashboard.lastMonth.income); let incomeChange = $derived(dashboard.thisMonth.income - dashboard.lastMonth.income);
let chartMonths = $derived(dashboard.months.map((m) => ({ ...m, label: monthLabel(m.month, { month: 'short' }), name: monthLabel(m.month) }))); let chartMonths = $derived(dashboard.months.map((m) => ({ ...m, label: monthLabel(m.month, { month: 'short' }), name: monthLabel(m.month) })));
let customersThisMonth = $derived(dashboard.newCustomers + dashboard.returningCustomers);
let utilisationPercent = $derived(dashboard.utilisation ? Math.round((dashboard.utilisation.booked / dashboard.utilisation.open) * 100) : null); let utilisationPercent = $derived(dashboard.utilisation ? Math.round((dashboard.utilisation.booked / dashboard.utilisation.open) * 100) : null);
let todayKey = $derived(zonedDateKey(new Date().toISOString(), data.timezone));
// Coming up, one group per day (bookings arrive sorted by start).
let upcomingDays = $derived.by(() => {
const days: { key: string; label: string; bookings: typeof dashboard.upcoming }[] = [];
for (const booking of dashboard.upcoming) {
const key = zonedDateKey(booking.start, data.timezone);
if (days.at(-1)?.key !== key) days.push({ key, label: dayLabel(booking.start), bookings: [] });
days.at(-1)!.bookings.push(booking);
}
return days;
});
</script> </script>
<svelte:head><title>Dashboard | CMS</title></svelte:head> <svelte:head><title>Dashboard | CMS</title></svelte:head>
{#snippet tile(label: string, value: string, note: string)} {#snippet tile(label: string, value: string, note: string)}
<div class="rounded-box bg-base-100 p-5 shadow"> <div class="rounded-box bg-base-100 p-4 shadow sm:p-5">
<p class="text-sm text-base-content/60">{label}</p> <p class="text-sm text-base-content/60">{label}</p>
<p class="mt-1 text-3xl font-bold tabular-nums">{value}</p> <p class="mt-1 text-2xl font-bold tabular-nums sm:text-3xl">{value}</p>
<p class="mt-1 text-sm text-base-content/60">{note}</p> <p class="mt-1 text-sm text-base-content/60">{note}</p>
</div> </div>
{/snippet} {/snippet}
@ -60,90 +70,126 @@
<p class="text-base-content/60">How the studio is doing. Income is paid orders, in the month they were paid; hours are booked appointment time, with group sessions counted once.</p> <p class="text-base-content/60">How the studio is doing. Income is paid orders, in the month they were paid; hours are booked appointment time, with group sessions counted once.</p>
</div> </div>
<div class="grid gap-4 sm:grid-cols-2 xl:grid-cols-4"> <!-- What's next and what needs doing first; the figures follow. -->
{@render tile(`Income in ${thisMonthName}`, money(dashboard.thisMonth.income), `${incomeChange >= 0 ? '+' : '−'}${money(Math.abs(incomeChange))} on ${monthLabel(dashboard.lastMonth.month, { month: 'long' })}`)} <div class="grid gap-6 xl:grid-cols-2">
{@render tile( <section class="rounded-box bg-base-100 p-4 shadow sm:p-5">
`Income in ${dashboard.thisMonth.month.slice(0, 4)}`, <div class="mb-3 flex flex-wrap items-baseline justify-between gap-x-4">
money(dashboard.incomeThisYear), <h2 class="text-2xl font-bold">Coming up</h2>
`${dashboard.thisMonth.orders} ${dashboard.thisMonth.orders === 1 ? 'order' : 'orders'} this month${dashboard.thisMonth.orders ? `, ${money(dashboard.thisMonth.income / dashboard.thisMonth.orders)} on average` : ''}` <p class="text-sm text-base-content/60">
)} Next {dashboard.upcomingDays} days: {dashboard.upcoming.length} {dashboard.upcoming.length === 1 ? 'booking' : 'bookings'}, {hours(dashboard.upcomingMinutes)}
{@render tile(`Hours worked in ${thisMonthName}`, hours(dashboard.minutesDoneThisMonth), `${hours(dashboard.minutesAheadThisMonth)} still booked this month`)} </p>
{@render tile(
'Opened time booked',
utilisationPercent === null ? '—' : `${utilisationPercent} %`,
dashboard.utilisation ? `${hours(dashboard.utilisation.booked)} of ${hours(dashboard.utilisation.open)} opened in Slots this month` : 'No slots opened this month'
)}
{@render tile(`Next ${dashboard.upcomingDays} days`, `${dashboard.upcoming.length} ${dashboard.upcoming.length === 1 ? 'booking' : 'bookings'}`, `${hours(dashboard.upcomingMinutes)} of appointments`)}
{@render tile(`Customers in ${thisMonthName}`, String(customersThisMonth), `${dashboard.newCustomers} new, ${dashboard.returningCustomers} returning`)}
{@render tile('Vouchers & codes owed', money(dashboard.valueOutstanding), `${dashboard.codesOutstanding} ${dashboard.codesOutstanding === 1 ? 'code' : 'codes'} with sessions still to book`)}
{@render tile('Needs attention', String(dashboard.attention.length), dashboard.attention.length ? 'See the list below' : 'Nothing right now')}
</div> </div>
{#if upcomingDays.length}
{#if dashboard.attention.length} <div class="space-y-4">
<section class="mt-6 rounded-box bg-base-100 p-5 shadow"> {#each upcomingDays as day (day.key)}
<h2 class="mb-4 text-2xl font-bold">Needs attention</h2> <div>
<ul class="divide-y divide-base-300"> <h3 class="mb-1 text-sm font-semibold">{day.key === todayKey ? 'Today' : day.label}</h3>
{#each dashboard.attention as item, i (i)} <ul class="divide-y divide-base-300 rounded-box bg-base-200/60 px-3">
<li class="flex flex-wrap items-center gap-x-3 gap-y-1 py-2"> {#each day.bookings as booking, i (i)}
{#if item.kind === 'email'} <li class="flex gap-3 py-2">
<span class="badge badge-warning badge-sm">⚠ Email failed</span> <span class="w-14 shrink-0 font-semibold tabular-nums">{timeLabel(booking.start)}</span>
<span class="order-last basis-full sm:order-none sm:basis-0 sm:flex-1">Booking email for {item.customer}{#if item.created}<span class="text-base-content/60">&nbsp;· ordered {formatDateTime(item.created, data.timezone)}</span>{/if}</span>
<a class="btn btn-ghost btn-xs ml-auto" href="/orders">Resend in Orders</a>
{:else if item.kind === 'session'}
<span class="badge badge-info badge-sm">◔ Nearly full</span>
<span class="order-last basis-full sm:order-none sm:basis-0 sm:flex-1">{item.name || 'Group session'} · {formatDateTime(item.start, data.timezone)}<span class="text-base-content/60">&nbsp;· {item.taken} of {item.capacity} places taken</span></span>
<a class="btn btn-ghost btn-xs ml-auto" href="/slots">Slots</a>
{:else}
<span class="badge badge-sm">⏱ Expires soon</span>
<span class="order-last basis-full sm:order-none sm:basis-0 sm:flex-1">{item.code} ({item.name}) · {item.left} {item.left === 1 ? 'session' : 'sessions'} left<span class="text-base-content/60">&nbsp;· expires {formatDateTime(item.expiresAt, data.timezone)}</span></span>
<a class="btn btn-ghost btn-xs ml-auto" href="/passes?q={encodeURIComponent(item.code)}">Vouchers & codes</a>
{/if}
</li>
{/each}
</ul>
<p class="mt-2 text-sm text-base-content/60">Sessions and codes in the next {dashboard.attentionDays} days.</p>
</section>
{/if}
<div class="mt-6 grid gap-6 xl:grid-cols-2">
<section class="rounded-box bg-base-100 p-5 shadow">
<h2 class="mb-4 text-2xl font-bold">Income per month</h2>
<IncomeChart months={chartMonths} format={money} tickFormat={roundMoney} current={dashboard.thisMonth.month} />
</section>
<section class="rounded-box bg-base-100 p-5 shadow">
<h2 class="mb-4 text-2xl font-bold">Coming up</h2>
{#if dashboard.upcoming.length}
<ul class="divide-y divide-base-300">
{#each dashboard.upcoming as booking, i (i)}
<li class="flex flex-wrap items-baseline gap-x-3 py-2">
<span class="w-28 shrink-0 font-semibold">{dayLabel(booking.start)}</span>
<span class="w-28 shrink-0 tabular-nums">{timeLabel(booking.start)} · {formatDuration(booking.minutes)}</span>
<span class="min-w-0 flex-1"> <span class="min-w-0 flex-1">
{booking.name}{#if booking.quantity > 1}<span class="text-base-content/60">&nbsp;× {booking.quantity}</span>{/if} <span class="block">{booking.name}{#if booking.quantity > 1}<span class="text-base-content/60">&nbsp;× {booking.quantity}</span>{/if}</span>
{#if booking.customer}<span class="block text-sm text-base-content/60">{booking.customer}</span>{/if} <span class="block text-sm text-base-content/60">{formatDuration(booking.minutes)}{#if booking.customer}&nbsp;· {booking.customer}{/if}</span>
</span> </span>
</li> </li>
{/each} {/each}
</ul> </ul>
</div>
{/each}
</div>
{:else} {:else}
<p class="text-base-content/60">Nothing booked in the next {dashboard.upcomingDays} days.</p> <p class="text-base-content/60">Nothing booked in the next {dashboard.upcomingDays} days.</p>
{/if} {/if}
<a class="btn btn-sm mt-4" href="/orders">All orders</a> <a class="btn btn-sm mt-4" href="/orders">All orders</a>
</section> </section>
<section class="rounded-box bg-base-100 p-4 shadow sm:p-5">
<div class="mb-3 flex flex-wrap items-baseline justify-between gap-x-4">
<h2 class="text-2xl font-bold">Needs attention</h2>
<p class="text-sm text-base-content/60">Sessions and codes: next {dashboard.attentionDays} days</p>
</div>
{#if dashboard.attention.length}
<ul class="divide-y divide-base-300">
{#each dashboard.attention as item, i (i)}
<li class="flex flex-wrap items-center gap-x-3 gap-y-1 py-2">
{#if item.kind === 'email'}
<span class="badge badge-warning badge-sm">⚠ Email failed</span>
<span class="order-last basis-full">Booking email for {item.customer}{#if item.created}<span class="text-base-content/60">&nbsp;· ordered {formatDateTime(item.created, data.timezone)}</span>{/if}</span>
<a class="btn btn-ghost btn-xs ml-auto" href="/orders">Resend in Orders</a>
{:else if item.kind === 'session'}
<span class="badge badge-info badge-sm">◔ Nearly full</span>
<span class="order-last basis-full">{item.name || 'Group session'} · {formatDateTime(item.start, data.timezone)}<span class="text-base-content/60">&nbsp;· {item.taken} of {item.capacity} places taken</span></span>
<a class="btn btn-ghost btn-xs ml-auto" href="/slots">Slots</a>
{:else}
<span class="badge badge-sm">⏱ Expires soon</span>
<span class="order-last basis-full">{item.code} ({item.name}) · {item.left} {item.left === 1 ? 'session' : 'sessions'} left<span class="text-base-content/60">&nbsp;· expires {formatDateTime(item.expiresAt, data.timezone)}</span></span>
<a class="btn btn-ghost btn-xs ml-auto" href="/passes?q={encodeURIComponent(item.code)}">Vouchers & codes</a>
{/if}
</li>
{/each}
</ul>
{:else}
<p class="text-base-content/60">✓ All clear: no failed emails, nearly full sessions or codes about to expire.</p>
{/if}
</section>
</div> </div>
<div class="mt-6 grid gap-6 xl:grid-cols-3"> <div class="mt-6 grid grid-cols-2 gap-3 sm:gap-4 xl:grid-cols-4">
<section class="rounded-box bg-base-100 p-5 shadow"> {@render tile(`Income in ${thisMonthName}`, money(dashboard.thisMonth.income), `${incomeChange >= 0 ? '+' : '−'}${money(Math.abs(incomeChange))} on ${monthLabel(dashboard.lastMonth.month, { month: 'long' })}`)}
<h2 class="mb-4 text-2xl font-bold">What sells</h2> {@render tile(`Hours worked in ${thisMonthName}`, hours(dashboard.minutesDoneThisMonth), `${hours(dashboard.minutesAheadThisMonth)} still booked`)}
{@render salesTable(dashboard.sales, (row) => row.name, 'Nothing sold yet.')} {@render tile(
'Opened time booked',
utilisationPercent === null ? '—' : `${utilisationPercent} %`,
dashboard.utilisation ? `${hours(dashboard.utilisation.booked)} of ${hours(dashboard.utilisation.open)} in Slots this month` : 'No slots opened this month'
)}
{@render tile('Codes owed', money(dashboard.valueOutstanding), `${dashboard.codesOutstanding} ${dashboard.codesOutstanding === 1 ? 'code' : 'codes'} with sessions to book`)}
</div>
<section class="mt-6 rounded-box bg-base-100 p-4 shadow sm:p-5">
<div class="mb-4 flex flex-wrap items-end justify-between gap-x-6 gap-y-2">
<div>
<h2 class="text-2xl font-bold">Income</h2>
<p class="text-sm text-base-content/60">Per month, last {dashboard.months.length} months</p>
</div>
<div class="sm:text-right">
<p class="text-sm text-base-content/60">Income in {dashboard.thisMonth.month.slice(0, 4)}</p>
<p class="text-2xl font-bold tabular-nums sm:text-3xl">{money(dashboard.incomeThisYear)}</p>
<p class="text-sm text-base-content/60">
{dashboard.thisMonth.orders} {dashboard.thisMonth.orders === 1 ? 'order' : 'orders'} this month{#if dashboard.thisMonth.orders}, {money(dashboard.thisMonth.income / dashboard.thisMonth.orders)} on average{/if}
</p>
</div>
</div>
<IncomeChart months={chartMonths} format={money} tickFormat={roundMoney} current={dashboard.thisMonth.month} />
<details class="mt-2">
<summary class="cursor-pointer text-sm font-medium">Show as table</summary>
<div class="mt-2 overflow-x-auto">
<table class="table table-sm">
<thead>
<tr><th>Month</th><th class="text-right">Orders</th><th class="text-right">Income</th><th class="text-right">Hours</th></tr>
</thead>
<tbody>
{#each [...dashboard.months].reverse() as row (row.month)}
<tr class={row.month === dashboard.thisMonth.month ? 'bg-primary/10' : ''}>
<td>{monthLabel(row.month, { month: 'short', year: 'numeric' })}</td>
<td class="text-right tabular-nums">{row.orders}</td>
<td class="text-right tabular-nums">{money(row.income)}</td>
<td class="text-right tabular-nums">{hours(row.minutes)}</td>
</tr>
{/each}
</tbody>
</table>
</div>
</details>
</section> </section>
<section class="rounded-box bg-base-100 p-5 shadow">
<h2 class="mb-4 text-2xl font-bold">Popular lengths</h2> <div class="mt-6 grid gap-6 lg:grid-cols-2 xl:grid-cols-3">
{@render salesTable(dashboard.lengths, (row) => `${row.name} · ${formatDuration(row.minutes)}`, 'No bookings yet.')} <section class="rounded-box bg-base-100 p-4 shadow sm:p-5">
</section> <h2 class="mb-1 text-2xl font-bold">Customers</h2>
<section class="rounded-box bg-base-100 p-5 shadow"> <p class="mb-3 text-sm text-base-content/60">
<h2 class="mb-4 text-2xl font-bold">Regulars</h2> {dashboard.newCustomers + dashboard.returningCustomers} in {thisMonthName}: {dashboard.newCustomers} new, {dashboard.returningCustomers} returning
</p>
<h3 class="font-semibold">Regulars</h3>
{#if dashboard.regulars.length} {#if dashboard.regulars.length}
<table class="table table-sm"> <table class="table table-sm">
<thead><tr><th></th><th class="text-right">Orders</th><th class="text-right">Spent</th></tr></thead> <thead><tr><th></th><th class="text-right">Orders</th><th class="text-right">Spent</th></tr></thead>
@ -157,26 +203,13 @@
<p class="text-base-content/60">Nobody has ordered more than once yet.</p> <p class="text-base-content/60">Nobody has ordered more than once yet.</p>
{/if} {/if}
</section> </section>
</div> <section class="rounded-box bg-base-100 p-4 shadow sm:p-5">
<p class="mt-2 text-sm text-base-content/60">What sells, popular lengths and regulars cover the last {dashboard.months.length} months.</p> <h2 class="mb-3 text-2xl font-bold">What sells</h2>
{@render salesTable(dashboard.sales, (row) => row.name, 'Nothing sold yet.')}
<section class="mt-6 rounded-box bg-base-100 p-5 shadow">
<h2 class="mb-4 text-2xl font-bold">Month by month</h2>
<div class="overflow-x-auto">
<table class="table table-sm">
<thead>
<tr><th>Month</th><th class="text-right">Orders</th><th class="text-right">Income</th><th class="text-right">Hours</th></tr>
</thead>
<tbody>
{#each [...dashboard.months].reverse() as row (row.month)}
<tr class={row.month === dashboard.thisMonth.month ? 'bg-primary/10' : ''}>
<td>{monthLabel(row.month)}</td>
<td class="text-right tabular-nums">{row.orders}</td>
<td class="text-right tabular-nums">{money(row.income)}</td>
<td class="text-right tabular-nums">{hours(row.minutes)}</td>
</tr>
{/each}
</tbody>
</table>
</div>
</section> </section>
<section class="rounded-box bg-base-100 p-4 shadow sm:p-5">
<h2 class="mb-3 text-2xl font-bold">Popular lengths</h2>
{@render salesTable(dashboard.lengths, (row) => `${row.name} · ${formatDuration(row.minutes)}`, 'No bookings yet.')}
</section>
</div>
<p class="mt-2 text-sm text-base-content/60">Regulars, what sells and popular lengths cover the last {dashboard.months.length} months.</p>