'use client'
import { useState } from 'react'
import Link from 'next/link'
import { useTranslations } from 'next-intl'
import { Badge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { EmptyState } from '@/components/ui/empty-state'
import { useToast } from '@/components/ui/use-toast'
import { cn, formatCurrency, formatDate } from '@/lib/utils'
import { getErrorMessage } from '@/lib/errors/get-error-message'
import { useCapability } from '@/contexts/CompanyContext'
import { CAPABILITY } from '@/lib/entitlements/keys'
import { visibleWorklistTotal } from '@/lib/worklist/visible-total'
import {
ArrowLeftRight,
ArrowRight,
CalendarClock,
CheckCircle2,
ChevronRight,
Eye,
FileWarning,
Inbox,
Landmark,
Loader2,
ReceiptText,
ShieldCheck,
Stamp,
} from 'lucide-react'
import type { SuggestedMatch, WorklistCounts } from '@/lib/worklist/types'
/**
* AttGoraSection: the dashboard's unified worklist ("Att göra").
*
* One flat ledger of everything actionable, grouped into three bands by
* session intent: Bokför (the daily loop), Granska & komplettera (close the
* gaps), Bevaka (time-driven). Every count comes from lib/worklist (the same
* source as the sidebar badges) so the numbers can never disagree.
*
* Suggested transaction↔invoice matches render inline with one-click confirm:
* the row posts to the existing match endpoints, fades out optimistically,
* and the counts refetch from /api/worklist/counts.
*/
interface ExpiringBankConnection {
id: string
bank_name: string
days_left: number
}
interface AttGoraSectionProps {
worklist: WorklistCounts
suggestedMatches: SuggestedMatch[]
expiringBankConnections?: ExpiringBankConnection[]
}
interface WorklistRowProps {
href: string
icon: React.ComponentType<{ className?: string }>
label: string
detail?: string
count: number
badge?: React.ReactNode
}
function WorklistRow({ href, icon: Icon, label, detail, count, badge }: WorklistRowProps) {
return (
{label}
{detail &&
{detail}
}
{badge}
{count}
)
}
function BandHeader({ children }: { children: React.ReactNode }) {
return (
{children}
)
}
export default function AttGoraSection({
worklist,
suggestedMatches,
expiringBankConnections = [],
}: AttGoraSectionProps) {
const t = useTranslations('dashboard')
const { toast } = useToast()
// The Dokumentinkorg is a paid (AI) surface: a non-payer's home to-do list
// must not offer a row that jumps to the gated workspace. Mirrors the sidebar
// + command palette gate; the page itself enforces it server-side.
const hasAi = useCapability(CAPABILITY.ai)
const [counts, setCounts] = useState(worklist.counts)
const [total, setTotal] = useState(worklist.total)
const [matches, setMatches] = useState(suggestedMatches)
const [leavingIds, setLeavingIds] = useState>(new Set())
const [confirmingId, setConfirmingId] = useState(null)
async function refetchCounts() {
try {
const res = await fetch('/api/worklist/counts')
if (!res.ok) throw new Error(`worklist counts refetch failed: ${res.status}`)
const json = (await res.json().catch(() => ({}))) as { data?: WorklistCounts }
if (json.data) {
setCounts(json.data.counts)
setTotal(json.data.total)
}
} catch (err) {
// Stale counts self-correct on the next page load: never block the
// flow. Note this is a browser console.error and nothing collects it:
// the observability sink (lib/observability) is server-side and has no
// client adapter, so a systematically broken counts endpoint currently
// hides behind silently frozen numbers until someone reports it.
console.error('[att-gora] worklist counts refetch failed', err)
}
}
async function handleConfirmMatch(match: SuggestedMatch) {
setConfirmingId(match.transaction_id)
try {
const url =
match.kind === 'invoice'
? `/api/transactions/${match.transaction_id}/match-invoice`
: `/api/transactions/${match.transaction_id}/match-supplier-invoice`
const body =
match.kind === 'invoice'
? { invoice_id: match.candidate_id }
: { supplier_invoice_id: match.candidate_id }
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
})
const result = await res.json().catch(() => ({}))
if (!res.ok || result.error) {
toast({
title: t('suggested_failed_toast'),
description: getErrorMessage(result, { context: 'transaction', statusCode: res.status }),
variant: 'destructive',
})
return
}
toast({ title: t('suggested_confirmed_toast') })
// Fade the row out, drop it, then re-sync every count from the source
// of truth (the match also booked a transaction, so several numbers move).
setLeavingIds((prev) => new Set(prev).add(match.transaction_id))
setTimeout(() => {
setMatches((prev) => prev.filter((m) => m.transaction_id !== match.transaction_id))
setLeavingIds((prev) => {
const next = new Set(prev)
next.delete(match.transaction_id)
return next
})
}, 200)
void refetchCounts()
} catch {
toast({ title: t('suggested_failed_toast'), variant: 'destructive' })
} finally {
setConfirmingId(null)
}
}
const showInboxDocuments = hasAi && counts.inbox_document > 0
const bokforRows = counts.book_transaction > 0 || showInboxDocuments || matches.length > 0
const granskaRows =
counts.supplier_invoice_approval > 0 ||
counts.verifikat_missing_document > 0 ||
counts.pending_operations > 0
const bevakaRows =
counts.overdue_invoice > 0 ||
counts.deadline_action > 0 ||
expiringBankConnections.length > 0
const allClear = !bokforRows && !granskaRows && !bevakaRows
// The header total must equal what the section actually shows, computed off
// the same visibleWorklistTotal helper as the dashboard KPI tile so the two
// can never drift: the hidden paid inbox row is subtracted for non-payers,
// else the header would count work the section no longer renders.
const displayTotal = visibleWorklistTotal({
total,
inboxDocumentCount: counts.inbox_document,
hasAi,
extra: expiringBankConnections.length,
})
return (
{/* Pane header: Geist title + quiet count over a hairline */}
{t('att_gora_title')}
{allClear ? t('all_done') : t('att_gora_left', { count: displayTotal })}
{allClear ? (
) : (
{bokforRows && (
{t('band_bokfor')}
{counts.book_transaction > 0 && (
)}
{matches.length > 0 && (
{t('suggested_title')}
{matches.map((match) => {
const isLeaving = leavingIds.has(match.transaction_id)
const isConfirming = confirmingId === match.transaction_id
return (
{match.transaction_description}
{' '}
· {formatCurrency(
Math.abs(match.transaction_amount),
match.transaction_currency,
)}
{match.kind === 'invoice'
? t('suggested_kind_invoice')
: t('suggested_kind_supplier_invoice')}
{match.candidate_number ? ` ${match.candidate_number}` : ''}
{match.counterparty_name ? ` · ${match.counterparty_name}` : ''}
{' · '}
{formatDate(match.transaction_date)}
)
})}
)}
{showInboxDocuments && (
)}
)}
{granskaRows && (
{t('band_granska')}
{counts.supplier_invoice_approval > 0 && (
)}
{counts.verifikat_missing_document > 0 && (
)}
{counts.pending_operations > 0 && (
)}
)}
{bevakaRows && (
{t('band_bevaka')}
{counts.overdue_invoice > 0 && (
)}
{counts.deadline_action > 0 && (
)}
{expiringBankConnections.length > 0 && (
)}
)}
)}
)
}