Add Svelte web client and service worker
Mobile-first PWA: groups via key-in-fragment links, encrypted outbox and sync over SSE, all split modes, currencies with ECB rate prefill, photos, comments, history and rewind, stats, recurring expenses, CSV and backup, EN/CS, dark/light theme, Web Push decrypted in the service worker. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013FYo6JSV1fDU1fm3LU77iF
This commit is contained in:
parent
f67add3660
commit
8fbbb4933e
53 changed files with 4558 additions and 37 deletions
143
apps/web/src/components/Stats.svelte
Normal file
143
apps/web/src/components/Stats.svelte
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
<script lang="ts">
|
||||
import { computeStats } from '@oweme/core';
|
||||
import { addDays, CATEGORY_ICONS, fmtMonth, money, today } from '../lib/format.ts';
|
||||
import type { MsgKey } from '../lib/i18n.ts';
|
||||
import type { GroupSession } from '../lib/session.svelte.ts';
|
||||
import { t } from '../lib/settings.svelte.ts';
|
||||
|
||||
let { session }: { session: GroupSession } = $props();
|
||||
type Range = 'all' | 'month' | '30' | 'year';
|
||||
let range = $state<Range>('all');
|
||||
let hoverMonth = $state<string | null>(null);
|
||||
|
||||
const bounds = $derived.by((): [string | null, string | null] => {
|
||||
const d = today();
|
||||
if (range === 'month') return [`${d.slice(0, 7)}-01`, d];
|
||||
if (range === '30') return [addDays(d, -29), d];
|
||||
if (range === 'year') return [`${d.slice(0, 4)}-01-01`, d];
|
||||
return [null, null];
|
||||
});
|
||||
const st = $derived(computeStats(session.state, bounds[0], bounds[1]));
|
||||
const base = $derived(session.base);
|
||||
const maxCat = $derived(Math.max(1, ...st.byCategory.map((c) => c.amount)));
|
||||
const maxMonth = $derived(Math.max(1, ...st.byMonth.map((m) => m.amount)));
|
||||
const peakMonth = $derived(st.byMonth.reduce((a, m) => (m.amount > (a?.amount ?? -1) ? m : a), null as null | { month: string; amount: number }));
|
||||
const shownMonth = $derived(st.byMonth.find((m) => m.month === hoverMonth) ?? peakMonth);
|
||||
const members = $derived(st.perMember.filter((p) => p.paid || p.consumed));
|
||||
const maxMember = $derived(Math.max(1, ...members.flatMap((p) => [p.paid, p.consumed])));
|
||||
const mine = $derived(st.perMember.find((p) => p.member === session.me));
|
||||
// Label every month on short ranges, fewer on long ones, so labels never collide.
|
||||
const labelEvery = $derived(Math.ceil(st.byMonth.length / 8));
|
||||
</script>
|
||||
|
||||
<div class="stack">
|
||||
<div class="seg" role="group" aria-label={t('stats')}>
|
||||
{#each [['all', 'range_all'], ['year', 'range_year'], ['month', 'range_month'], ['30', 'range_30']] as const as [r, label]}
|
||||
<button aria-pressed={range === r} onclick={() => (range = r)}>{t(label)}</button>
|
||||
{/each}
|
||||
</div>
|
||||
|
||||
<div class="tiles">
|
||||
<div class="card">
|
||||
<div class="muted small">{t('total_spent')}</div>
|
||||
<div class="hero num">{money(st.total, base)}</div>
|
||||
</div>
|
||||
{#if mine}
|
||||
<div class="card">
|
||||
<div class="muted small">{t('my_spending')}</div>
|
||||
<div class="hero num">{money(mine.consumed, base)}</div>
|
||||
</div>
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
{#if st.total === 0}
|
||||
<p class="empty">{t('no_data')}</p>
|
||||
{:else}
|
||||
<section class="card stack">
|
||||
<h2>{t('by_category')}</h2>
|
||||
<ul class="bars">
|
||||
{#each st.byCategory as c (c.category)}
|
||||
<li title="{t(`cat_${c.category}` as MsgKey)}: {money(c.amount, base)}">
|
||||
<span class="label"><span aria-hidden="true">{CATEGORY_ICONS[c.category]}</span> {t(`cat_${c.category}` as MsgKey)}</span>
|
||||
<span class="track"><span class="hbar s1" style:width="{(c.amount / maxCat) * 100}%"></span></span>
|
||||
<span class="value num">{money(c.amount, base)}</span>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</section>
|
||||
|
||||
{#if st.byMonth.length > 1}
|
||||
<section class="card stack">
|
||||
<div class="row">
|
||||
<h2 class="grow" style:flex="1">{t('by_month')}</h2>
|
||||
{#if shownMonth}<span class="small num" aria-live="polite">{fmtMonth(shownMonth.month)}: <strong>{money(shownMonth.amount, base)}</strong></span>{/if}
|
||||
</div>
|
||||
<div class="months">
|
||||
{#each st.byMonth as m, i (m.month)}
|
||||
<button
|
||||
type="button" class="month"
|
||||
aria-label="{fmtMonth(m.month)}: {money(m.amount, base)}"
|
||||
onmouseenter={() => (hoverMonth = m.month)} onmouseleave={() => (hoverMonth = null)}
|
||||
onfocus={() => (hoverMonth = m.month)} onblur={() => (hoverMonth = null)}
|
||||
>
|
||||
<span class="col"><span class="vbar s1" class:active={shownMonth?.month === m.month} style:height="{(m.amount / maxMonth) * 100}%"></span></span>
|
||||
<span class="mlabel small muted">{i % labelEvery === 0 ? fmtMonth(m.month) : ''}</span>
|
||||
</button>
|
||||
{/each}
|
||||
</div>
|
||||
</section>
|
||||
{/if}
|
||||
|
||||
{#if members.length}
|
||||
<section class="card stack">
|
||||
<h2>{t('per_member')}</h2>
|
||||
<div class="legend small muted row">
|
||||
<span class="row" style:gap="6px"><span class="swatch s1"></span>{t('legend_paid')}</span>
|
||||
<span class="row" style:gap="6px"><span class="swatch s2"></span>{t('legend_share')}</span>
|
||||
</div>
|
||||
<ul class="bars grouped">
|
||||
{#each members as p (p.member)}
|
||||
<li>
|
||||
<span class="label">{session.memberName(p.member)}</span>
|
||||
<span class="pair">
|
||||
<span class="line" title="{t('legend_paid')}: {money(p.paid, base)}">
|
||||
<span class="track"><span class="hbar s1" style:width="{(p.paid / maxMember) * 100}%"></span></span>
|
||||
<span class="value num">{money(p.paid, base)}</span>
|
||||
</span>
|
||||
<span class="line" title="{t('legend_share')}: {money(p.consumed, base)}">
|
||||
<span class="track"><span class="hbar s2" style:width="{(p.consumed / maxMember) * 100}%"></span></span>
|
||||
<span class="value num">{money(p.consumed, base)}</span>
|
||||
</span>
|
||||
</span>
|
||||
</li>
|
||||
{/each}
|
||||
</ul>
|
||||
</section>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
|
||||
.hero { font-size: 1.6rem; font-weight: 700; }
|
||||
.s1 { background: var(--chart-1); }
|
||||
.s2 { background: var(--chart-2); }
|
||||
.bars { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
|
||||
.bars li { display: grid; grid-template-columns: minmax(90px, 34%) 1fr auto; align-items: center; gap: 10px; }
|
||||
.label { font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
||||
.track { display: block; height: 12px; }
|
||||
.hbar { display: block; height: 100%; min-width: 2px; border-radius: 0 4px 4px 0; }
|
||||
.value { font-size: 0.85rem; color: var(--text); text-align: right; }
|
||||
.grouped li { grid-template-columns: minmax(70px, 28%) 1fr; }
|
||||
.pair { display: flex; flex-direction: column; gap: 2px; }
|
||||
.line { display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: center; }
|
||||
.legend { gap: 16px; }
|
||||
.swatch { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
|
||||
.months { display: flex; align-items: stretch; gap: 2px; height: 160px; }
|
||||
.month { flex: 1; display: flex; padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: default; flex-direction: column; min-width: 0; outline-offset: 2px; }
|
||||
.month:focus-visible { outline: 2px solid var(--accent); border-radius: 4px; }
|
||||
.col { flex: 1; display: flex; align-items: flex-end; border-bottom: 1px solid var(--border); }
|
||||
.vbar { display: block; width: 100%; max-width: 28px; margin: 0 auto; min-height: 2px; border-radius: 4px 4px 0 0; opacity: 0.85; }
|
||||
.vbar.active { opacity: 1; }
|
||||
.mlabel { text-align: center; white-space: nowrap; overflow: visible; height: 1.4em; font-size: 0.7rem; }
|
||||
</style>
|
||||
Loading…
Add table
Add a link
Reference in a new issue