"use client"; import { ChevronDown, Plus } from "lucide-react"; import { Fragment, useState } from "react"; import { toast } from "sonner"; import { MoneyInput } from "@/components/money-input"; import { ResponsiveDialog } from "@/components/responsive-dialog"; import { Button } from "@/components/ui/button"; import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { tr } from "@/i18n/tr"; import { cn } from "@/lib/utils"; import { ASSET_KEYS, ASSETS, type AssetKey } from "@/shared/assets"; import { todayIstanbul } from "@/shared/dates"; import { formatDate, formatMoney } from "@/shared/format"; import { formatMoneyInput } from "@/shared/money"; import { savePriceAction } from "../actions"; import type { PriceRow } from "../service"; const t = tr.prices; const d = (ymd: string) => formatDate(`${ymd}T12:00:00+03:00`); export function PricesView({ rows, canWrite }: { rows: PriceRow[]; canWrite: boolean }) { const [editing, setEditing] = useState(null); const [openRow, setOpenRow] = useState(null); return (
{canWrite ? (
) : null}
{rows.map((r) => ( {openRow === r.assetKey ? r.history.slice(1).map((h) => ( )) : null} ))}
{t.asset} {t.buy} {t.sell} {t.date}
{r.latest ? formatMoney(r.latest.buyMinor) : "—"} {r.latest ? formatMoney(r.latest.sellMinor) : "—"} {r.latest ? d(r.latest.date) : t.noPrice} {canWrite ? ( ) : null}
{t.history} {formatMoney(h.buyMinor)} {formatMoney(h.sellMinor)} {d(h.date)}
{editing ? ( r.assetKey === editing)?.latest ?? null} onClose={() => setEditing(null)} /> ) : null}
); } function PriceForm({ assetKey: initial, latest, onClose, }: { assetKey: AssetKey; latest: PriceRow["latest"]; onClose: () => void; }) { const [v, setV] = useState({ assetKey: initial, date: todayIstanbul(), buy: latest ? formatMoneyInput(latest.buyMinor) : "", sell: latest ? formatMoneyInput(latest.sellMinor) : "", }); const [errors, setErrors] = useState>({}); const [busy, setBusy] = useState(false); async function submit(e: React.FormEvent) { e.preventDefault(); setBusy(true); const r = await savePriceAction(v); setBusy(false); if (r.ok) { toast.success(t.saved); onClose(); } else setErrors({ ...r.fieldErrors, root: r.fieldErrors ? undefined : r.error }); } return ( !o && onClose()} title={t.add} footer={ <> } >
{t.asset} {t.date} setV((s) => ({ ...s, date: e.target.value }))} /> {errors.date ? {errors.date} : null}
{t.buy} setV((s) => ({ ...s, buy: x }))} autoFocus /> {errors.buy ? {errors.buy} : null} {t.sell} setV((s) => ({ ...s, sell: x }))} />
{t.sellHint} {errors.root ? {errors.root} : null}
); }