137 lines
4.1 KiB
Svelte
137 lines
4.1 KiB
Svelte
<script lang="ts">
|
|
import Amount from '#lib/components/Amount.svelte';
|
|
import Button from '#lib/components/Button.svelte';
|
|
import Card from '#lib/components/Card.svelte';
|
|
import DataTable, { type Column } from '#lib/components/DataTable.svelte';
|
|
import EmptyState from '#lib/components/EmptyState.svelte';
|
|
import PageHeader from '#lib/components/PageHeader.svelte';
|
|
import Pagination from '#lib/components/Pagination.svelte';
|
|
import SearchInput from '#lib/components/SearchInput.svelte';
|
|
import SegmentedControl from '#lib/components/SegmentedControl.svelte';
|
|
import SelectField from '#lib/components/SelectField.svelte';
|
|
import { formatDate, signedCents } from '#lib/format.ts';
|
|
import { accounts, transactions } from '#lib/mock/data.ts';
|
|
|
|
const PAGE_SIZE = 8;
|
|
|
|
const columns: Column[] = [
|
|
{ key: 'date', label: 'Date' },
|
|
{ key: 'description', label: 'Description' },
|
|
{ key: 'account', label: 'Account' },
|
|
{ key: 'amount', label: 'Amount / EUR', align: 'right' }
|
|
];
|
|
|
|
let query = $state('');
|
|
let accountId = $state('all');
|
|
let direction = $state('all');
|
|
let page = $state(1);
|
|
let selected = $state<string[]>([]);
|
|
|
|
const accountName = (id: string) => accounts.find((account) => account.id === id)?.name ?? '';
|
|
|
|
const filtered = $derived(
|
|
transactions
|
|
.filter(
|
|
(transaction) =>
|
|
(accountId === 'all' || transaction.accountId === accountId) &&
|
|
(direction === 'all' || transaction.direction === direction) &&
|
|
transaction.description.toLowerCase().includes(query.trim().toLowerCase())
|
|
)
|
|
.toSorted((a, b) => b.date.localeCompare(a.date))
|
|
);
|
|
const rows = $derived(filtered.slice((page - 1) * PAGE_SIZE, page * PAGE_SIZE));
|
|
const selectedTotal = $derived(
|
|
transactions
|
|
.filter((transaction) => selected.includes(transaction.id))
|
|
.reduce((sum, t) => sum + signedCents(t.amountCents, t.direction), 0)
|
|
);
|
|
|
|
// Filters change the result set, so return to the first page.
|
|
$effect(() => {
|
|
void [query, accountId, direction];
|
|
page = 1;
|
|
});
|
|
</script>
|
|
|
|
<svelte:head>
|
|
<title>Transaction history | Expenses</title>
|
|
</svelte:head>
|
|
|
|
<PageHeader title="Transaction history" description="Every payment in and out of your accounts.">
|
|
{#snippet actions()}
|
|
<Button icon="download">Export</Button>
|
|
{/snippet}
|
|
</PageHeader>
|
|
|
|
<div class="mb-4 grid gap-4 md:grid-cols-[minmax(0,1fr)_16rem] md:items-end">
|
|
<SearchInput
|
|
bind:value={query}
|
|
label="Search transactions"
|
|
placeholder="Search transactions..."
|
|
/>
|
|
<SelectField
|
|
bind:value={accountId}
|
|
label="Account"
|
|
options={[
|
|
{ value: 'all', label: 'All accounts' },
|
|
...accounts.map((account) => ({ value: account.id, label: account.name }))
|
|
]}
|
|
/>
|
|
</div>
|
|
<div class="mb-6">
|
|
<SegmentedControl
|
|
bind:value={direction}
|
|
label="Direction"
|
|
options={[
|
|
{ value: 'all', label: 'All' },
|
|
{ value: 'in', label: 'Money in' },
|
|
{ value: 'out', label: 'Money out' }
|
|
]}
|
|
/>
|
|
</div>
|
|
|
|
<Card>
|
|
<DataTable
|
|
caption="Transaction history"
|
|
{columns}
|
|
{rows}
|
|
rowId={(transaction) => transaction.id}
|
|
selectable
|
|
bind:selected
|
|
>
|
|
{#snippet cell(transaction, column)}
|
|
{#if column.key === 'date'}
|
|
<span class="text-neutral-400 tabular-nums">{formatDate(transaction.date)}</span>
|
|
{:else if column.key === 'description'}
|
|
{transaction.description}
|
|
{:else if column.key === 'account'}
|
|
<span class="text-neutral-400">{accountName(transaction.accountId)}</span>
|
|
{:else}
|
|
<Amount cents={transaction.amountCents} direction={transaction.direction} />
|
|
{/if}
|
|
{/snippet}
|
|
{#snippet empty()}
|
|
<EmptyState
|
|
icon="search"
|
|
title="No transactions found"
|
|
description="Try a different search or filter."
|
|
/>
|
|
{/snippet}
|
|
{#snippet footer()}
|
|
{#if selected.length}
|
|
<span class="text-neutral-400">{selected.length} selected</span>
|
|
<span>
|
|
Total
|
|
<Amount cents={selectedTotal} direction={selectedTotal < 0 ? 'out' : 'in'} showCurrency />
|
|
</span>
|
|
{:else}
|
|
<span class="text-neutral-500">Select transactions to see their total.</span>
|
|
{/if}
|
|
{/snippet}
|
|
</DataTable>
|
|
</Card>
|
|
|
|
<div class="mt-6">
|
|
<Pagination bind:page pageSize={PAGE_SIZE} total={filtered.length} noun="transactions" />
|
|
</div>
|