oweme/apps/web/src/components/Stats.svelte
agent b3b57c156e Group page redesign and visual bug fixes
Layout, from the approved mock:
- Group header on Expenses/Balances: my balance as the headline number
  (Money component de-emphasises currency and decimals), total spent, and
  round quick actions Add / Payment / Settle up / Share. The floating +
  shows only once those scroll off screen.
- Expense rows on two lines (title | amount, payer | my part) with round
  icons tinted per category; sentence-case section headings.
- Balances: larger avatars, paired avatars on settle-up rows, tinted button.

Bugs found in a screenshot pass (phone 320/390, desktop 1280; dark, black,
light; English and Czech):
- A long title or member name widened the group page past the screen and
  phones zoomed out (grid tracks without minmax(0, ...)); same in Stats.
- Long titles were clipped mid-word with no ellipsis.
- Category emoji nearly invisible on its grey circle in the light theme.
- Stats period labels wrapped (en) or ran off screen (cs); shortened.
- Icons next to wrapping text were squashed (lock on Home/App settings).
- Dialogs opened with a focus ring on the close button after a tap.
- New expenses always defaulted to Food & drink; now the group's default
  category (settable in group settings), else Other.

e2e: the Balances check now targets the Settle up heading, which the new
quick action shares its text with.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HA82gVKYyo9BHLHPW4qr4x
2026-11-06 20:36:33 +00:00

144 lines
7.2 KiB
Svelte

<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 compact" 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>
.seg.compact :global(button) { font-size: 0.84rem; padding: 0 6px; }
.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%) minmax(0, 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: minmax(0, 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>