* refactor(ui): normalize dense pages to the locked design system Sweep of the info-dense surfaces against .claude/rules/design.md; no behavior changes, classNames and primitive adoption only. - Replace hand-rolled h1s with PageHeader (import, suppliers, kpi, salary/employees, skattekonto, settings layout) and drop the one double title (SalarySettingsContent under the settings h1) - Replace hand-rolled empty states with EmptyState (skattekonto, banking/api-keys/oauth/counterparty settings) and hand-rolled pulse divs with Skeleton (deadlines, report view loaders) - Remove semantic colors used as chrome: amber/emerald banners in AGIPanel and SkatteverketPanel, success/warning tints in kassaflodesanalys, arsredovisning and import become neutral surfaces with the tint kept on the icon only - Full-opacity borders everywhere (border-border/30-60, border-destructive/20-40, border-foreground/30, text-destructive/80) - Snap off-scale spacing (p-5 to p-6, p-2.5 to p-3, gap/mt-x.5 to scale values); KPI metric tiles p-6 to p-4 per the tile rule - Remove the mobile Select that duplicated the invoices status Tabs (TabsList already scrolls horizontally); single Tabs now serves both breakpoints - supplier-invoices: shared formatCurrency instead of a local formatAmount helper; skattekonto: formatDate/formatDateLong/ formatDateTime instead of raw dates and toLocaleString - arsredovisning flerarsoversikt converted to the Table primitive with right-aligned tabular-nums cells - Settings: CardTitle text-base on section cards, one heading idiom in AccountSettingsContent, h3 to h2 in CompanyProfileView Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * refactor(i18n): trim text bloat and fix an untranslated sv string - Fix invoice_credit.create_failed_fallback: sv catalog carried the English "Failed to create credit note"; now "Kunde inte skapa kreditfaktura". Translate new_user_checklist.step3_title in en - Drop descriptions that paraphrase their own title (design.md forbidden pattern): invoice_detail.credited_description, invoice_credit.original_card_description, invoice_editor customer/notes card descriptions (keys deleted from both catalogs, zero remaining usages); the transaction booking DialogDescription becomes sr-only so screen readers keep it - Trim redundant sentences from settings_salary.info_payroll_scope, settings_backup.intro, ext_cloud_backup_long_description, settings.name_description, salary_payments.open_payments_note and shorten invoice_credit.reason_card_description; statutory BFL/tax prose untouched - Normalize toast punctuation (dimensions/self_billing created_description lose the trailing period like their siblings) - common.delete "Radera" to "Ta bort" (zero live call sites; Radera stays reserved for irreversible account/company deletion) Catalogs verified key-identical (4795 keys each) and JSON-parseable. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
259 lines
9.9 KiB
TypeScript
259 lines
9.9 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect } from 'react'
|
|
import { useTranslations } from 'next-intl'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Card, CardContent } from '@/components/ui/card'
|
|
import { Input } from '@/components/ui/input'
|
|
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from '@/components/ui/dialog'
|
|
import { EmptyState } from '@/components/ui/empty-state'
|
|
import { Skeleton } from '@/components/ui/skeleton'
|
|
import { PageHeader } from '@/components/ui/page-header'
|
|
import { ReportExportMenu } from '@/components/reports/ReportExportMenu'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { Plus, Search, Building2, Lock } from 'lucide-react'
|
|
import SupplierForm from '@/components/suppliers/SupplierForm'
|
|
import Link from 'next/link'
|
|
import { useCompany } from '@/contexts/CompanyContext'
|
|
import { useCanWrite } from '@/lib/hooks/use-can-write'
|
|
import type { Supplier, SupplierType, CreateSupplierInput } from '@/types'
|
|
|
|
const SUPPLIER_TYPE_KEYS: Record<SupplierType, string> = {
|
|
swedish_business: 'type_swedish_business',
|
|
eu_business: 'type_eu_business',
|
|
non_eu_business: 'type_non_eu_business',
|
|
}
|
|
|
|
function getPaymentInfo(supplier: Supplier, t: (key: string) => string): { label: string; value: string } | null {
|
|
if (supplier.bankgiro) return { label: t('label_bg'), value: supplier.bankgiro }
|
|
if (supplier.plusgiro) return { label: t('label_pg'), value: supplier.plusgiro }
|
|
if (supplier.iban) return { label: t('label_iban'), value: supplier.iban }
|
|
if (supplier.bank_account) return { label: t('label_bank_account'), value: supplier.bank_account }
|
|
return null
|
|
}
|
|
|
|
function formatLocation(supplier: Supplier): string | null {
|
|
if (supplier.city && supplier.country) return `${supplier.city}, ${supplier.country}`
|
|
if (supplier.city) return supplier.city
|
|
return null
|
|
}
|
|
|
|
export default function SuppliersPage() {
|
|
const { company } = useCompany()
|
|
const { canWrite } = useCanWrite()
|
|
const t = useTranslations('suppliers')
|
|
const [suppliers, setSuppliers] = useState<Supplier[]>([])
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [searchTerm, setSearchTerm] = useState('')
|
|
const [isDialogOpen, setIsDialogOpen] = useState(false)
|
|
const [isCreating, setIsCreating] = useState(false)
|
|
const { toast } = useToast()
|
|
const supabase = createClient()
|
|
|
|
async function fetchSuppliers() {
|
|
if (!company) return
|
|
setIsLoading(true)
|
|
const { data, error } = await supabase
|
|
.from('suppliers')
|
|
.select('*')
|
|
.eq('company_id', company.id)
|
|
.order('name', { ascending: true })
|
|
|
|
if (error) {
|
|
toast({
|
|
title: t('load_failed_title'),
|
|
description: t('load_failed_description'),
|
|
variant: 'destructive',
|
|
})
|
|
} else {
|
|
setSuppliers(data || [])
|
|
}
|
|
setIsLoading(false)
|
|
}
|
|
|
|
useEffect(() => {
|
|
fetchSuppliers()
|
|
}, [])
|
|
|
|
async function handleCreateSupplier(data: CreateSupplierInput) {
|
|
setIsCreating(true)
|
|
|
|
const response = await fetch('/api/suppliers', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(data),
|
|
})
|
|
|
|
const result = await response.json()
|
|
|
|
if (!response.ok) {
|
|
const fieldErrors = result.errors?.map((e: { field: string; message: string }) => `${e.field}: ${e.message}`).join(', ')
|
|
toast({
|
|
title: t('create_failed_title'),
|
|
description: fieldErrors || result.error || t('create_failed_retry'),
|
|
variant: 'destructive',
|
|
})
|
|
} else {
|
|
toast({
|
|
title: t('created_title'),
|
|
description: t('created_description', { name: data.name }),
|
|
})
|
|
setSuppliers([...suppliers, result.data])
|
|
setIsDialogOpen(false)
|
|
}
|
|
|
|
setIsCreating(false)
|
|
}
|
|
|
|
const filteredSuppliers = suppliers.filter((s) =>
|
|
s.name.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
s.email?.toLowerCase().includes(searchTerm.toLowerCase()) ||
|
|
s.org_number?.includes(searchTerm)
|
|
)
|
|
|
|
return (
|
|
<div className="space-y-8">
|
|
<PageHeader
|
|
title={t('title')}
|
|
action={
|
|
<div className="flex items-center gap-2">
|
|
<ReportExportMenu
|
|
size="default"
|
|
items={[
|
|
{ format: 'xlsx', href: '/api/export/suppliers' },
|
|
{ format: 'csv', href: '/api/export/suppliers?format=csv' },
|
|
]}
|
|
/>
|
|
<Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
|
|
<DialogTrigger asChild>
|
|
<Button
|
|
disabled={!canWrite}
|
|
title={!canWrite ? t('viewer_disabled_tooltip') : undefined}
|
|
>
|
|
{canWrite ? (
|
|
<Plus className="mr-2 h-4 w-4" />
|
|
) : (
|
|
<Lock className="mr-2 h-4 w-4" />
|
|
)}
|
|
{t('new_supplier')}
|
|
</Button>
|
|
</DialogTrigger>
|
|
<DialogContent className="sm:max-w-2xl max-h-[95dvh] sm:max-h-[90vh] overflow-y-auto">
|
|
<DialogHeader>
|
|
<DialogTitle>{t('add_supplier')}</DialogTitle>
|
|
</DialogHeader>
|
|
<SupplierForm
|
|
onSubmit={handleCreateSupplier}
|
|
isLoading={isCreating}
|
|
/>
|
|
</DialogContent>
|
|
</Dialog>
|
|
</div>
|
|
}
|
|
/>
|
|
|
|
{/* Search */}
|
|
<div className="relative">
|
|
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
|
|
<Input
|
|
placeholder={t('search_placeholder')}
|
|
value={searchTerm}
|
|
onChange={(e) => setSearchTerm(e.target.value)}
|
|
className="pl-10"
|
|
/>
|
|
</div>
|
|
|
|
{/* Supplier list */}
|
|
{isLoading ? (
|
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
|
{[1, 2, 3].map((i) => (
|
|
<Card key={i}>
|
|
<CardContent className="p-6 space-y-3">
|
|
<Skeleton className="h-5 w-1/2" />
|
|
<Skeleton className="h-3 w-2/3" />
|
|
<div className="h-px bg-muted" />
|
|
<Skeleton className="h-3 w-1/2" />
|
|
<Skeleton className="h-3 w-1/3" />
|
|
</CardContent>
|
|
</Card>
|
|
))}
|
|
</div>
|
|
) : filteredSuppliers.length === 0 ? (
|
|
<Card>
|
|
<CardContent className="p-0">
|
|
{searchTerm ? (
|
|
<EmptyState
|
|
icon={Building2}
|
|
title={t('no_search_results_title')}
|
|
description={t('no_search_results_description', { term: searchTerm })}
|
|
/>
|
|
) : (
|
|
<EmptyState
|
|
icon={Building2}
|
|
title={t('empty_title')}
|
|
description={t('empty_description')}
|
|
actionLabel={canWrite ? t('new_supplier') : undefined}
|
|
onAction={canWrite ? () => setIsDialogOpen(true) : undefined}
|
|
/>
|
|
)}
|
|
</CardContent>
|
|
</Card>
|
|
) : (
|
|
<div className="grid gap-4 md:grid-cols-2 lg:grid-cols-3">
|
|
{filteredSuppliers.map((supplier) => {
|
|
const payment = getPaymentInfo(supplier, t)
|
|
const location = formatLocation(supplier)
|
|
return (
|
|
<Link key={supplier.id} href={`/suppliers/${supplier.id}`} className="group">
|
|
<Card className="h-full cursor-pointer transition-colors duration-150 hover:border-foreground/20 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2">
|
|
<CardContent className="p-6 flex flex-col h-full">
|
|
<div className="space-y-1 mb-4">
|
|
<h3 className="text-[15px] font-semibold tracking-tight leading-tight truncate group-hover:text-primary transition-colors">
|
|
{supplier.name}
|
|
</h3>
|
|
<p className="text-xs text-muted-foreground leading-snug">
|
|
{t(SUPPLIER_TYPE_KEYS[supplier.supplier_type])}
|
|
</p>
|
|
</div>
|
|
|
|
<dl className="mt-auto space-y-2 text-sm border-t pt-3">
|
|
{supplier.org_number && (
|
|
<div className="flex items-baseline justify-between gap-3">
|
|
<dt className="text-xs text-muted-foreground shrink-0">{t('label_org_number')}</dt>
|
|
<dd className="tabular-nums truncate">{supplier.org_number}</dd>
|
|
</div>
|
|
)}
|
|
{payment && (
|
|
<div className="flex items-baseline justify-between gap-3">
|
|
<dt className="text-xs text-muted-foreground shrink-0">{payment.label}</dt>
|
|
<dd className="tabular-nums truncate">{payment.value}</dd>
|
|
</div>
|
|
)}
|
|
{supplier.email && (
|
|
<div className="flex items-baseline justify-between gap-3">
|
|
<dt className="text-xs text-muted-foreground shrink-0">{t('label_email')}</dt>
|
|
<dd className="truncate">{supplier.email}</dd>
|
|
</div>
|
|
)}
|
|
{location && (
|
|
<div className="flex items-baseline justify-between gap-3">
|
|
<dt className="text-xs text-muted-foreground shrink-0">{t('label_location')}</dt>
|
|
<dd className="truncate">{location}</dd>
|
|
</div>
|
|
)}
|
|
{!supplier.org_number && !payment && !supplier.email && !location && (
|
|
<p className="text-xs text-muted-foreground italic">{t('no_contact_info')}</p>
|
|
)}
|
|
</dl>
|
|
</CardContent>
|
|
</Card>
|
|
</Link>
|
|
)
|
|
})}
|
|
</div>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|