feat: monthly dashboard
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2860f2f776
commit
1e91f38fd3
5 files changed
+529
-3
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import Link from "next/link";
|
||||
import { bpToPercentLabel } from "@/domain/money";
|
||||
import type { Op } from "@/domain/types";
|
||||
import { formatDateFr } from "@/lib/dates";
|
||||
import { Amount } from "./ui/Amount";
|
||||
import { Chip } from "./ui/Chip";
|
||||
|
||||
export interface OpListContext {
|
||||
users: { id: string; firstName: string; color: string }[];
|
||||
categories: Record<string, { name: string; emoji: string }>;
|
||||
chargeLabels: Record<string, string>;
|
||||
}
|
||||
|
||||
function splitLabel(op: Op, users: OpListContext["users"]) {
|
||||
const full = users.find((u) => op.split[u.id] === 10000);
|
||||
if (full) return null;
|
||||
return users.map((u) => bpToPercentLabel(op.split[u.id] ?? 0)).join("/");
|
||||
}
|
||||
|
||||
function Chips({ op, ctx }: { op: Op; ctx: OpListContext }) {
|
||||
const account = ctx.users.find((u) => u.id === op.accountUserId);
|
||||
const fullUser = ctx.users.find((u) => op.split[u.id] === 10000);
|
||||
const split = splitLabel(op, ctx.users);
|
||||
const target = op.allocatedToRecurringId ?? op.allocatedToOperationId;
|
||||
return (
|
||||
<div className="flex flex-wrap gap-1">
|
||||
{op.status === "PLANNED" && <Chip tone="planned">prévue</Chip>}
|
||||
{account && (
|
||||
<Chip color={account.color}>
|
||||
{op.kind === "EXPENSE" ? "payé par" : "reçu par"} {account.firstName}
|
||||
</Chip>
|
||||
)}
|
||||
{split && <Chip tone="split">{split}</Chip>}
|
||||
{!split && fullUser && fullUser.id !== op.accountUserId && <Chip color={fullUser.color}>pour {fullUser.firstName}</Chip>}
|
||||
{target && <Chip tone="link">↳ {ctx.chargeLabels[target] ?? "charge"}</Chip>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/** Liste d'opérations : tableau sur ordinateur, cartes sur mobile. Chaque ligne ouvre l'édition. */
|
||||
export function OpList({ ops, ctx, returnTo }: { ops: Op[]; ctx: OpListContext; returnTo: string }) {
|
||||
const href = (op: Op) => `/operations/${op.id}?retour=${encodeURIComponent(returnTo)}`;
|
||||
return (
|
||||
<div className="divide-y divide-line-soft">
|
||||
{ops.map((op) => {
|
||||
const cat = ctx.categories[op.categoryId];
|
||||
return (
|
||||
<Link
|
||||
key={op.id}
|
||||
href={href(op)}
|
||||
className={`flex items-center gap-3 py-2.5 hover:bg-page/60 ${op.status === "PLANNED" ? "opacity-65" : ""}`}
|
||||
>
|
||||
<span className="hidden w-12 shrink-0 text-muted md:block">{formatDateFr(op.date).slice(0, 5)}</span>
|
||||
<span className="grid h-[30px] w-[30px] shrink-0 place-items-center rounded-lg bg-gray-100 text-[15px]">{cat?.emoji}</span>
|
||||
<span className="min-w-0 flex-1">
|
||||
<span className="block truncate font-semibold">{op.label}</span>
|
||||
<span className="block text-[13px] text-muted">
|
||||
<span className="md:hidden">{formatDateFr(op.date).slice(0, 5)} · </span>
|
||||
{cat?.name}
|
||||
</span>
|
||||
<span className="mt-1 block md:hidden">
|
||||
<Chips op={op} ctx={ctx} />
|
||||
</span>
|
||||
</span>
|
||||
<span className="hidden md:block">
|
||||
<Chips op={op} ctx={ctx} />
|
||||
</span>
|
||||
<Amount cents={op.amount} kind={op.kind} className="w-28 shrink-0 text-right" />
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useTransition } from "react";
|
||||
import { decimalFromCents } from "@/domain/money";
|
||||
import { saveSettlementAction } from "@/server/actions/operations";
|
||||
import { Button } from "../ui/Button";
|
||||
import { Card } from "../ui/Card";
|
||||
import { Field, FormError, Input, Select } from "../ui/Field";
|
||||
|
||||
export function SettlementDialog({
|
||||
users,
|
||||
debtorId,
|
||||
creditorId,
|
||||
amount,
|
||||
today,
|
||||
}: {
|
||||
users: { id: string; firstName: string }[];
|
||||
debtorId?: string;
|
||||
creditorId?: string;
|
||||
amount?: number;
|
||||
today: string;
|
||||
}) {
|
||||
const [open, setOpen] = useState(false);
|
||||
const [from, setFrom] = useState(debtorId ?? users[1]?.id ?? "");
|
||||
const [to, setTo] = useState(creditorId ?? users[0]?.id ?? "");
|
||||
const [value, setValue] = useState(amount ? decimalFromCents(amount).replace(".", ",") : "");
|
||||
const [date, setDate] = useState(today);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [pending, startTransition] = useTransition();
|
||||
|
||||
const submit = () =>
|
||||
startTransition(async () => {
|
||||
const r = await saveSettlementAction({ fromUserId: from, toUserId: to, amount: value, date });
|
||||
if (!r.ok) return setError(r.error);
|
||||
setOpen(false);
|
||||
});
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button variant="ghost" onClick={() => setOpen(true)} className="shrink-0">
|
||||
Enregistrer un remboursement
|
||||
</Button>
|
||||
{open && (
|
||||
<div className="fixed inset-0 z-50 grid place-items-center bg-black/30 p-4" onClick={() => setOpen(false)}>
|
||||
<Card className="w-full max-w-sm space-y-4 p-5 shadow-xl" onClick={(e) => e.stopPropagation()}>
|
||||
<h2 className="text-lg font-bold">Remboursement entre vous</h2>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<Field label="De">
|
||||
<Select value={from} onChange={(e) => setFrom(e.target.value)}>
|
||||
{users.map((u) => (
|
||||
<option key={u.id} value={u.id}>
|
||||
{u.firstName}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
<Field label="À">
|
||||
<Select value={to} onChange={(e) => setTo(e.target.value)}>
|
||||
{users.map((u) => (
|
||||
<option key={u.id} value={u.id}>
|
||||
{u.firstName}
|
||||
</option>
|
||||
))}
|
||||
</Select>
|
||||
</Field>
|
||||
</div>
|
||||
<Field label="Montant (€)">
|
||||
<Input inputMode="decimal" value={value} onChange={(e) => setValue(e.target.value)} />
|
||||
</Field>
|
||||
<Field label="Date">
|
||||
<Input type="date" value={date} onChange={(e) => setDate(e.target.value)} />
|
||||
</Field>
|
||||
<FormError message={error} />
|
||||
<div className="flex gap-3">
|
||||
<Button variant="ghost" onClick={() => setOpen(false)} className="flex-1">
|
||||
Annuler
|
||||
</Button>
|
||||
<Button onClick={submit} disabled={pending} className="flex-1">
|
||||
Enregistrer
|
||||
</Button>
|
||||
</div>
|
||||
</Card>
|
||||
</div>
|
||||
)}
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user