Feature/quick review scroll (#110)
* fix: allow scrolling in QuickReviewDialog when content overflows viewport The dialog had no max-height or overflow handling, so tall content (e.g. extern representation with warnings + journal preview) was clipped with no way to scroll. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add detailed error logging to Enable Banking integration Adds structured console logging with full context (status codes, response bodies, connection IDs, account UIDs, stack traces) to all Enable Banking operations: connect, callback, sync, disconnect, and the API client retry/error paths. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: add detailed error logging to login and register flows Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> * feat: implement KPI settings management with preferences and dynamic KPI display --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.6
parent
4d4a9a40c9
commit
c1630068b3
+168
-58
@@ -1,73 +1,183 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Info, X } from 'lucide-react'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { formatCurrency } from '@/lib/utils'
|
||||
import type { KPIReport } from '@/types'
|
||||
import { KPI_DEFINITIONS, getDefaultPreferences } from '@/lib/reports/kpi-definitions'
|
||||
import type { KPIReport, KPIPreferences } from '@/types'
|
||||
|
||||
interface KPIHeroCardsProps {
|
||||
report: KPIReport
|
||||
preferences?: KPIPreferences
|
||||
}
|
||||
|
||||
export function KPIHeroCards({ report }: KPIHeroCardsProps) {
|
||||
function getKPIValue(
|
||||
report: KPIReport,
|
||||
id: string
|
||||
): { value: number | null; subtitle: string } {
|
||||
switch (id) {
|
||||
case 'netResult':
|
||||
return { value: report.netResult, subtitle: 'netto' }
|
||||
case 'cashPosition':
|
||||
return { value: report.cashPosition, subtitle: 'likvida medel' }
|
||||
case 'outstandingReceivables':
|
||||
return {
|
||||
value: report.outstandingReceivables,
|
||||
subtitle:
|
||||
report.overdueReceivables > 0
|
||||
? `varav förfallet: ${formatCurrency(report.overdueReceivables)}`
|
||||
: 'utestående',
|
||||
}
|
||||
case 'vatLiability':
|
||||
return {
|
||||
value: report.vatLiability,
|
||||
subtitle:
|
||||
report.vatLiability > 0
|
||||
? 'att betala'
|
||||
: report.vatLiability < 0
|
||||
? 'att återfå'
|
||||
: 'jämnt',
|
||||
}
|
||||
case 'grossMargin':
|
||||
return { value: report.grossMargin, subtitle: 'av intäkter' }
|
||||
case 'expenseRatio':
|
||||
return { value: report.expenseRatio, subtitle: 'av intäkter' }
|
||||
case 'avgPaymentDays':
|
||||
return { value: report.avgPaymentDays, subtitle: 'snitt' }
|
||||
default:
|
||||
return { value: null, subtitle: '' }
|
||||
}
|
||||
}
|
||||
|
||||
function formatKPIValue(value: number | null, format: string, id: string): string {
|
||||
if (value === null) return '—'
|
||||
if (format === 'currency') {
|
||||
if (id === 'vatLiability') return formatCurrency(Math.abs(value))
|
||||
return formatCurrency(value)
|
||||
}
|
||||
if (format === 'percentage') return `${value}%`
|
||||
if (format === 'days') return `${value} dagar`
|
||||
return String(value)
|
||||
}
|
||||
|
||||
function getValueColor(
|
||||
value: number | null,
|
||||
colorLogic: string
|
||||
): string {
|
||||
if (value === null) return 'text-muted-foreground'
|
||||
if (colorLogic === 'neutral') return ''
|
||||
if (colorLogic === 'positive-good') {
|
||||
return value >= 0
|
||||
? 'text-[hsl(var(--chart-1))]'
|
||||
: 'text-[hsl(var(--chart-2))]'
|
||||
}
|
||||
// negative-good (e.g. VAT: negative = refund = good, expense ratio: lower = better)
|
||||
if (colorLogic === 'negative-good') {
|
||||
return value <= 0
|
||||
? 'text-[hsl(var(--chart-1))]'
|
||||
: 'text-[hsl(var(--chart-2))]'
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export function KPIHeroCards({ report, preferences }: KPIHeroCardsProps) {
|
||||
const prefs = preferences ?? getDefaultPreferences()
|
||||
const [infoOpen, setInfoOpen] = useState<string | null>(null)
|
||||
|
||||
// Build ordered, visible list
|
||||
const visibleDefs = prefs.kpiOrder
|
||||
.map((id) => KPI_DEFINITIONS.find((d) => d.id === id))
|
||||
.filter((d) => d && prefs.visibleKpis.includes(d.id)) as typeof KPI_DEFINITIONS
|
||||
|
||||
if (visibleDefs.length === 0) {
|
||||
return (
|
||||
<Card>
|
||||
<CardContent className="py-8 text-center text-muted-foreground text-sm">
|
||||
Inga nyckeltal valda. Klicka på "Anpassa" för att välja vilka som ska visas.
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
}
|
||||
|
||||
// Responsive grid: 2 cols on mobile, up to 4 on desktop
|
||||
const gridCols =
|
||||
visibleDefs.length <= 2
|
||||
? 'grid-cols-2'
|
||||
: visibleDefs.length === 3
|
||||
? 'grid-cols-2 md:grid-cols-3'
|
||||
: 'grid-cols-2 md:grid-cols-4'
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-2 md:grid-cols-4 gap-4">
|
||||
{/* Net result */}
|
||||
<Card>
|
||||
<CardContent className="p-5">
|
||||
<p className="text-xs text-muted-foreground mb-1">Resultat</p>
|
||||
<p className={`font-display text-xl tabular-nums tracking-tight ${
|
||||
report.netResult >= 0 ? 'text-[hsl(var(--chart-1))]' : 'text-[hsl(var(--chart-2))]'
|
||||
}`}>
|
||||
{formatCurrency(report.netResult)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">netto</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
<div className={`grid ${gridCols} gap-4`}>
|
||||
{visibleDefs.map((def) => {
|
||||
const { value, subtitle } = getKPIValue(report, def.id)
|
||||
const formatted = formatKPIValue(value, def.format, def.id)
|
||||
const color = getValueColor(value, def.colorLogic)
|
||||
const showInfo = infoOpen === def.id
|
||||
const hasOverride =
|
||||
prefs.accountOverrides[def.id] &&
|
||||
prefs.accountOverrides[def.id].length > 0
|
||||
|
||||
{/* Cash position */}
|
||||
<Card>
|
||||
<CardContent className="p-5">
|
||||
<p className="text-xs text-muted-foreground mb-1">Kassa</p>
|
||||
<p className={`font-display text-xl tabular-nums tracking-tight ${
|
||||
report.cashPosition > 0 ? 'text-[hsl(var(--chart-1))]' : 'text-[hsl(var(--chart-2))]'
|
||||
}`}>
|
||||
{formatCurrency(report.cashPosition)}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">likvida medel</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
return (
|
||||
<Card key={def.id} className="relative">
|
||||
<CardContent className="p-5">
|
||||
<div className="flex items-start justify-between">
|
||||
<p className="text-xs text-muted-foreground mb-1">
|
||||
{def.label}
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
setInfoOpen(showInfo ? null : def.id)
|
||||
}
|
||||
className="text-muted-foreground/50 hover:text-muted-foreground transition-colors -mt-0.5 -mr-1 p-1"
|
||||
aria-label={`Visa formel för ${def.label}`}
|
||||
>
|
||||
{showInfo ? (
|
||||
<X className="h-3 w-3" />
|
||||
) : (
|
||||
<Info className="h-3 w-3" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* Outstanding receivables */}
|
||||
<Card>
|
||||
<CardContent className="p-5">
|
||||
<p className="text-xs text-muted-foreground mb-1">Kundfordringar</p>
|
||||
<p className="font-display text-xl tabular-nums tracking-tight">
|
||||
{formatCurrency(report.outstandingReceivables)}
|
||||
</p>
|
||||
{report.overdueReceivables > 0 ? (
|
||||
<p className="text-xs text-[hsl(var(--chart-2))] mt-1">
|
||||
varav förfallet: {formatCurrency(report.overdueReceivables)}
|
||||
</p>
|
||||
) : (
|
||||
<p className="text-xs text-muted-foreground mt-1">utestående</p>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
|
||||
{/* VAT liability */}
|
||||
<Card>
|
||||
<CardContent className="p-5">
|
||||
<p className="text-xs text-muted-foreground mb-1">Moms</p>
|
||||
<p className={`font-display text-xl tabular-nums tracking-tight ${
|
||||
report.vatLiability > 0 ? 'text-[hsl(var(--chart-2))]' : 'text-[hsl(var(--chart-1))]'
|
||||
}`}>
|
||||
{formatCurrency(Math.abs(report.vatLiability))}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
{report.vatLiability > 0 ? 'att betala' : report.vatLiability < 0 ? 'att återfå' : 'jämnt'}
|
||||
</p>
|
||||
</CardContent>
|
||||
</Card>
|
||||
{showInfo ? (
|
||||
<div className="space-y-1.5 text-[11px] text-muted-foreground mt-1">
|
||||
<p className="text-xs text-foreground/80">{def.description}</p>
|
||||
<div>
|
||||
<span className="font-medium">Formel: </span>
|
||||
<span className="font-mono">{def.formula}</span>
|
||||
</div>
|
||||
<div>
|
||||
<span className="font-medium">Konton: </span>
|
||||
{def.accountDescription}
|
||||
</div>
|
||||
{hasOverride && (
|
||||
<div className="text-primary">
|
||||
<span className="font-medium">Anpassade konton: </span>
|
||||
<span className="font-mono">
|
||||
{prefs.accountOverrides[def.id].join(', ')}
|
||||
</span>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
<p
|
||||
className={`font-display text-xl tabular-nums tracking-tight ${color}`}
|
||||
>
|
||||
{formatted}
|
||||
</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">
|
||||
{subtitle}
|
||||
</p>
|
||||
</>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
'use client'
|
||||
|
||||
import { useState } from 'react'
|
||||
import { Settings2, ChevronDown, ChevronRight, RotateCcw } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
DialogDescription,
|
||||
DialogTrigger,
|
||||
DialogClose,
|
||||
} from '@/components/ui/dialog'
|
||||
import { Switch } from '@/components/ui/switch'
|
||||
import { KPI_DEFINITIONS, getDefaultPreferences } from '@/lib/reports/kpi-definitions'
|
||||
import type { KPIPreferences } from '@/types'
|
||||
|
||||
interface KPISettingsDialogProps {
|
||||
preferences: KPIPreferences
|
||||
onSave: (prefs: KPIPreferences) => void
|
||||
saving: boolean
|
||||
}
|
||||
|
||||
export function KPISettingsDialog({ preferences, onSave, saving }: KPISettingsDialogProps) {
|
||||
const [draft, setDraft] = useState<KPIPreferences>(preferences)
|
||||
const [expandedKpi, setExpandedKpi] = useState<string | null>(null)
|
||||
const [open, setOpen] = useState(false)
|
||||
|
||||
function handleOpen(isOpen: boolean) {
|
||||
if (isOpen) setDraft(preferences)
|
||||
setOpen(isOpen)
|
||||
}
|
||||
|
||||
function toggleKpi(id: string) {
|
||||
setDraft((prev) => {
|
||||
const visible = prev.visibleKpis.includes(id)
|
||||
? prev.visibleKpis.filter((k) => k !== id)
|
||||
: [...prev.visibleKpis, id]
|
||||
return { ...prev, visibleKpis: visible }
|
||||
})
|
||||
}
|
||||
|
||||
function setAccountOverride(kpiId: string, value: string) {
|
||||
const accounts = value
|
||||
.split(',')
|
||||
.map((s) => s.trim())
|
||||
.filter((s) => /^\d{4}$/.test(s))
|
||||
|
||||
setDraft((prev) => ({
|
||||
...prev,
|
||||
accountOverrides: {
|
||||
...prev.accountOverrides,
|
||||
[kpiId]: accounts,
|
||||
},
|
||||
}))
|
||||
}
|
||||
|
||||
function clearAccountOverride(kpiId: string) {
|
||||
setDraft((prev) => {
|
||||
const overrides = { ...prev.accountOverrides }
|
||||
delete overrides[kpiId]
|
||||
return { ...prev, accountOverrides: overrides }
|
||||
})
|
||||
}
|
||||
|
||||
function handleReset() {
|
||||
setDraft(getDefaultPreferences())
|
||||
}
|
||||
|
||||
function handleSave() {
|
||||
onSave(draft)
|
||||
setOpen(false)
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={handleOpen}>
|
||||
<DialogTrigger asChild>
|
||||
<Button variant="outline" size="sm" className="gap-1.5">
|
||||
<Settings2 className="h-3.5 w-3.5" />
|
||||
Anpassa
|
||||
</Button>
|
||||
</DialogTrigger>
|
||||
<DialogContent className="max-w-md max-h-[85vh] overflow-y-auto">
|
||||
<DialogHeader>
|
||||
<DialogTitle>Anpassa nyckeltal</DialogTitle>
|
||||
<DialogDescription>
|
||||
Välj vilka nyckeltal som visas och justera beräkningarna.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="space-y-1 mt-2">
|
||||
{KPI_DEFINITIONS.map((def) => {
|
||||
const isVisible = draft.visibleKpis.includes(def.id)
|
||||
const isExpanded = expandedKpi === def.id
|
||||
const hasOverride =
|
||||
def.customizableAccounts &&
|
||||
draft.accountOverrides[def.id] &&
|
||||
draft.accountOverrides[def.id].length > 0
|
||||
const overrideValue =
|
||||
draft.accountOverrides[def.id]?.join(', ') ?? ''
|
||||
|
||||
return (
|
||||
<div
|
||||
key={def.id}
|
||||
className="rounded-lg border border-border/60 p-3"
|
||||
>
|
||||
<div className="flex items-center justify-between">
|
||||
<button
|
||||
type="button"
|
||||
className="flex items-center gap-2 text-left flex-1 min-w-0"
|
||||
onClick={() =>
|
||||
setExpandedKpi(isExpanded ? null : def.id)
|
||||
}
|
||||
>
|
||||
{isExpanded ? (
|
||||
<ChevronDown className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
||||
) : (
|
||||
<ChevronRight className="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
||||
)}
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium truncate">
|
||||
{def.label}
|
||||
{hasOverride && (
|
||||
<span className="ml-1.5 text-xs text-muted-foreground font-normal">
|
||||
(anpassad)
|
||||
</span>
|
||||
)}
|
||||
</p>
|
||||
</div>
|
||||
</button>
|
||||
<Switch
|
||||
checked={isVisible}
|
||||
onCheckedChange={() => toggleKpi(def.id)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
{isExpanded && (
|
||||
<div className="mt-3 ml-5.5 space-y-2.5 text-xs text-muted-foreground">
|
||||
<p>{def.description}</p>
|
||||
<div>
|
||||
<p className="font-medium text-foreground/80 mb-0.5">
|
||||
Formel
|
||||
</p>
|
||||
<p className="font-mono text-[11px] bg-muted/50 rounded px-2 py-1">
|
||||
{def.formula}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-medium text-foreground/80 mb-0.5">
|
||||
Konton
|
||||
</p>
|
||||
<p>{def.accountDescription}</p>
|
||||
</div>
|
||||
|
||||
{def.customizableAccounts && (
|
||||
<div className="pt-1">
|
||||
<label className="font-medium text-foreground/80 block mb-1">
|
||||
Anpassa konton
|
||||
</label>
|
||||
<input
|
||||
type="text"
|
||||
value={overrideValue}
|
||||
onChange={(e) =>
|
||||
setAccountOverride(def.id, e.target.value)
|
||||
}
|
||||
placeholder={def.defaultAccounts.join(', ')}
|
||||
className="w-full rounded-md border border-input bg-background px-2.5 py-1.5 text-xs font-mono tabular-nums placeholder:text-muted-foreground/50"
|
||||
/>
|
||||
<p className="mt-1 text-[10px] text-muted-foreground/70">
|
||||
Ange kontonummer separerade med komma (t.ex.{' '}
|
||||
{def.defaultAccounts.slice(0, 3).join(', ')})
|
||||
</p>
|
||||
{hasOverride && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => clearAccountOverride(def.id)}
|
||||
className="mt-1 text-[10px] text-primary hover:underline"
|
||||
>
|
||||
Återställ till standard
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between pt-2 border-t mt-2">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleReset}
|
||||
className="flex items-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
|
||||
>
|
||||
<RotateCcw className="h-3 w-3" />
|
||||
Återställ allt
|
||||
</button>
|
||||
<div className="flex gap-2">
|
||||
<DialogClose asChild>
|
||||
<Button variant="outline" size="sm">
|
||||
Avbryt
|
||||
</Button>
|
||||
</DialogClose>
|
||||
<Button size="sm" onClick={handleSave} disabled={saving}>
|
||||
{saving ? 'Sparar...' : 'Spara'}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user