Files
accounted/components/bookkeeping/NoDocRequiredToggle.tsx
T
Jakob Wennberg ec27228a8e style: remove em/en dashes repo-wide, add CLAUDE.md rule against them (#890)
Em dashes (—) and en dashes (–) had spread across comments, docs, tests,
and a few UI strings, reading as AI-generated boilerplate rather than
house style. Replaced each with punctuation matching its context: colon
for explanatory clauses, comma for asides, plain hyphen for numeric/legal
ranges (e.g. "21-23§"), "to"/"till" for date ranges, parentheses for
paired-dash asides. messages/en.json and messages/sv.json were fixed by
hand together to keep sv/en in sync.

Left untouched where the dash is the functional subject rather than
decorative punctuation: date-range-parser.ts's separator regex,
charset-repair.ts's CP1252 byte-mapping table (and its test), the SIE
encoding mojibake docs, generic-csv.ts's minus-sign normalizer, the
agent system-prompt files that already instruct against em dashes, and
a golden iXBRL test fixture compared byte-for-byte.

Also fixes two bugs surfaced along the way: an off-by-one in
ApiKeysPanel's scope-label split (a leftover from an earlier partial
pass), and a charset-repair test that had lost the literal en-dash it
exists to verify.

Regenerated the agent atom seed migration (skills:generate) since 27
SKILL.md files changed. Added a CLAUDE.md rule against em/en dashes,
with an explicit carve-out for the functional-dash cases above.

Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-04 15:58:06 +02:00

165 lines
5.5 KiB
TypeScript

'use client'
import { useState } from 'react'
import { useTranslations } from 'next-intl'
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
import { Input } from '@/components/ui/input'
import { Button } from '@/components/ui/button'
import { Loader2, CircleSlash, Lock } from 'lucide-react'
import { useToast } from '@/components/ui/use-toast'
interface Props {
entryId: string
initialExempt: boolean
initialReason: string | null
canWrite: boolean
onChange: (exempted: boolean, reason: string | null) => void
}
/**
* Lets the bookkeeper mark a posted verifikation as not needing a separate
* underlag (bankavgift, ränta, intern överföring etc.). Toggling persists via
* /api/bookkeeping/journal-entries/[id]/no-document-required.
*
* The flag lives in journal_entry_no_doc_required (sidecar) so the underlying
* verifikation stays immutable per BFL.
*/
export default function NoDocRequiredToggle({
entryId,
initialExempt,
initialReason,
canWrite,
onChange,
}: Props) {
const t = useTranslations('journal_list')
const { toast } = useToast()
const [exempt, setExempt] = useState(initialExempt)
const [reason, setReason] = useState(initialReason ?? '')
const [saving, setSaving] = useState(false)
const [editingReason, setEditingReason] = useState(false)
const persistExempt = async (
nextExempt: boolean,
nextReason: string | null,
previousReason: string,
) => {
setSaving(true)
try {
const res = nextExempt
? await fetch(`/api/bookkeeping/journal-entries/${entryId}/no-document-required`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ reason: nextReason ?? null }),
})
: await fetch(`/api/bookkeeping/journal-entries/${entryId}/no-document-required`, {
method: 'DELETE',
})
if (!res.ok) {
const body = await res.json().catch(() => ({}))
toast({
title: t('no_doc_required_save_failed'),
description: body.error,
variant: 'destructive',
})
// Roll back UI state on failure: both the toggle AND the reason so
// the rendered state matches the DB row we failed to mutate.
setExempt(!nextExempt)
setReason(previousReason)
return
}
onChange(nextExempt, nextExempt ? nextReason : null)
} catch {
toast({ title: t('no_doc_required_save_failed'), variant: 'destructive' })
setExempt(!nextExempt)
setReason(previousReason)
} finally {
setSaving(false)
}
}
const handleToggle = (checked: boolean) => {
const previousReason = reason
setExempt(checked)
if (!checked) {
setReason('')
setEditingReason(false)
}
persistExempt(checked, checked ? (reason.trim() || null) : null, previousReason)
}
const handleSaveReason = () => {
setEditingReason(false)
persistExempt(true, reason.trim() || null, reason)
}
return (
<div className="mt-4 pt-3 border-t">
<div className="flex items-center gap-2">
<Switch
id={`no-doc-${entryId}`}
checked={exempt}
onCheckedChange={handleToggle}
disabled={!canWrite || saving}
/>
<Label
htmlFor={`no-doc-${entryId}`}
className="text-sm cursor-pointer flex items-center gap-1.5"
>
{!canWrite && <Lock className="h-3 w-3" />}
{saving && <Loader2 className="h-3 w-3 animate-spin" />}
{!saving && exempt && <CircleSlash className="h-3.5 w-3.5 text-muted-foreground" />}
{t('no_doc_required_toggle')}
</Label>
</div>
{exempt && (
<div className="mt-2 ml-10 space-y-1">
{editingReason ? (
<div className="flex items-center gap-2">
<Input
value={reason}
onChange={(e) => setReason(e.target.value)}
placeholder={t('no_doc_required_reason_placeholder')}
list={`no-doc-suggestions-${entryId}`}
maxLength={200}
className="h-8 text-xs flex-1 max-w-sm"
disabled={saving}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault()
handleSaveReason()
}
}}
/>
<datalist id={`no-doc-suggestions-${entryId}`}>
<option value={t('no_doc_required_suggestion_bank_fee')} />
<option value={t('no_doc_required_suggestion_interest')} />
<option value={t('no_doc_required_suggestion_internal_transfer')} />
<option value={t('no_doc_required_suggestion_tax_payment')} />
<option value={t('no_doc_required_suggestion_salary')} />
</datalist>
<Button size="sm" variant="outline" className="h-8" onClick={handleSaveReason} disabled={saving}>
{t('no_doc_required_save_reason')}
</Button>
</div>
) : (
<button
type="button"
onClick={() => canWrite && setEditingReason(true)}
disabled={!canWrite}
className="text-xs text-muted-foreground hover:text-foreground transition-colors text-left"
>
{reason
? `${t('no_doc_required_reason_label')}: ${reason}`
: t('no_doc_required_reason_add')}
</button>
)}
</div>
)}
</div>
)
}