Files
accounted/components/skattekonto/SkattekontoMatchDialog.tsx
T
9686b54b41 refactor(design): lock the border-radius ladder, one radius per role (#1607)
Seven radii were in circulation (4/5/6/8/12/16px + pill) with no rule for
which went where; one toolbar row on /transactions mixed four shape
languages. This locks a 4-tier ladder (design.md convention 16):

- pill: interactive toolbar controls (buttons, chips, pickers, segmented
  controls, toolbar search, count nubs)
- rounded-xl (12px): overlay tier: page panel, dialogs, slide-overs
- rounded-lg (8px): cards, form fields, popover/menu content, boxes
- rounded-sm (4px): nested leaves (menu items, checkboxes, kbd/code nubs)

Changes:
- New SegmentedControl primitive (pill-in-pill tablist, h-8) replaces the
  hand-rolled bg-muted/70 tablist copied across 11 files
- New ToolbarSearch primitive (pill, h-8) adopted on 9 page toolbars;
  dialog/picker searches keep the rounded-lg Input
- dialog.tsx 8px -> 12px, matching SettingsModal/slide-over/CommandPalette
- ContextPicker chips at the shared h-8 toolbar height
- ~300 rounded-md / bare rounded call sites remapped by role; auth icon
  tiles and the mobile nav sheet come down from 16px to 12px
- rounded-md, bare rounded, rounded-2xl and rounded-[Npx] are dead
  vocabulary, enforced by a new off-ladder-radius check in check:guards

Verified: lint 0 errors, 14422 unit tests pass, check:guards green, tsc
clean on all changed files, sandbox screenshots of transactions/
bookkeeping/granskning toolbars and the Ny verifikation dialog.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-14 08:55:37 +02:00

235 lines
7.5 KiB
TypeScript

'use client'
import { useEffect, useState } from 'react'
import { useTranslations } from 'next-intl'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from '@/components/ui/dialog'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table'
import { useToast } from '@/components/ui/use-toast'
import { formatCurrency, formatDate } from '@/lib/utils'
import { formatVoucher } from '@/lib/bookkeeping/voucher-series-resolver'
import type { StoredSkattekontoTransaction } from '@/types/skatteverket'
import { getErrorMessage as getUserErrorMessage } from '@/lib/errors/get-error-message'
interface MatchCandidate {
journal_entry_id: string
voucher_number: number | null
voucher_series: string | null
entry_date: string
description: string
status: 'draft' | 'posted' | 'reversed'
matched_amount: number
matched_side: 'debit' | 'credit'
}
/**
* Shared dialog for linking a skattekonto_transactions row to an existing
* journal entry. Used by both /skattekonto and /transactions so we don't
* have two copies of the same dialog drifting apart.
*
* The dialog owns its own data fetch: pass the row + open flag and it
* handles the rest. On successful match it calls onMatched(), letting the
* caller refresh its data.
*/
export function SkattekontoMatchDialog({
row,
open,
onClose,
onMatched,
}: {
row: StoredSkattekontoTransaction | null
open: boolean
onClose: () => void
onMatched: () => void
}) {
const t = useTranslations('tx_skattekonto_match')
const { toast } = useToast()
const [candidates, setCandidates] = useState<MatchCandidate[] | null>(null)
const [loading, setLoading] = useState(false)
const [submittingId, setSubmittingId] = useState<string | null>(null)
useEffect(() => {
if (!open || !row) {
setCandidates(null)
return
}
let cancelled = false
setLoading(true)
;(async () => {
try {
const res = await fetch(
`/api/extensions/ext/skatteverket/skattekonto/transaktioner/${row.id}/match-candidates`,
)
const json = await res.json()
if (cancelled) return
if (!res.ok) {
// Map the parsed body plus the status, never `new Error(json.error)`:
// the Error constructor stringifies a non-string body field, and the
// mapper would discard the route's own Swedish reason.
toast({
title: t('fetch_candidates_failed_title'),
description: getUserErrorMessage(json, { statusCode: res.status }),
variant: 'destructive',
})
onClose()
return
}
setCandidates(json.data.candidates as MatchCandidate[])
} catch (err) {
if (cancelled) return
toast({
title: t('fetch_candidates_failed_title'),
description: err instanceof Error ? getUserErrorMessage(err) : undefined,
variant: 'destructive',
})
onClose()
} finally {
if (!cancelled) setLoading(false)
}
})()
return () => {
cancelled = true
}
}, [open, row, toast, onClose, t])
async function confirmMatch(journalEntryId: string) {
if (!row) return
setSubmittingId(journalEntryId)
try {
const res = await fetch(
`/api/extensions/ext/skatteverket/skattekonto/transaktioner/${row.id}/match`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ journal_entry_id: journalEntryId }),
},
)
const json = await res.json()
if (!res.ok) {
toast({
title: t('match_failed_title'),
description: getUserErrorMessage(json, { statusCode: res.status }),
variant: 'destructive',
})
return
}
toast({ title: t('match_success_title') })
onMatched()
onClose()
} catch (err) {
toast({
title: t('match_failed_title'),
description: err instanceof Error ? getUserErrorMessage(err) : undefined,
variant: 'destructive',
})
} finally {
setSubmittingId(null)
}
}
return (
<Dialog open={open} onOpenChange={o => !o && onClose()}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>{t('title')}</DialogTitle>
<DialogDescription>
{row && (
<>
{formatDate(row.transaktionsdatum)} • {row.transaktionstext} •{' '}
<span className="tabular-nums">
{formatCurrency(Number(row.belopp_skatteverket))}
</span>
</>
)}
</DialogDescription>
</DialogHeader>
{loading && (
<p className="py-6 text-center text-sm text-muted-foreground">
{t('searching')}
</p>
)}
{!loading && candidates && candidates.length === 0 && (
<div className="space-y-2 py-4 text-sm">
<p>{t('no_candidates_title')}</p>
<p className="text-muted-foreground">
{t.rich('no_candidates_help', {
strong: (chunks) => <strong>{chunks}</strong>,
})}
</p>
</div>
)}
{!loading && candidates && candidates.length > 0 && (
<div className="max-h-[420px] overflow-y-auto rounded-lg border">
<Table>
<TableHeader>
<TableRow>
<TableHead>{t('th_date')}</TableHead>
<TableHead>{t('th_voucher')}</TableHead>
<TableHead>{t('th_description')}</TableHead>
<TableHead>{t('th_status')}</TableHead>
<TableHead></TableHead>
</TableRow>
</TableHeader>
<TableBody>
{candidates.map(c => (
<TableRow key={c.journal_entry_id}>
<TableCell className="tabular-nums">{formatDate(c.entry_date)}</TableCell>
<TableCell className="tabular-nums">
{formatVoucher(c)}
</TableCell>
<TableCell className="max-w-[260px] truncate">
{c.description}
</TableCell>
<TableCell>
{c.status === 'posted' ? (
<Badge variant="secondary">{t('status_posted')}</Badge>
) : c.status === 'draft' ? (
<Badge variant="outline">{t('status_draft')}</Badge>
) : (
<Badge variant="destructive">{t('status_reversed')}</Badge>
)}
</TableCell>
<TableCell className="text-right">
<Button
size="sm"
onClick={() => confirmMatch(c.journal_entry_id)}
disabled={submittingId === c.journal_entry_id}
>
{submittingId === c.journal_entry_id ? t('linking') : t('link')}
</Button>
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
)}
<DialogFooter>
<Button variant="ghost" onClick={onClose}>
{t('cancel')}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}