Files
accounted/components/dashboard/SkatteverketPromoCard.tsx
T
d41ef2a909 feat(sandbox): seed payroll, articles and a year of ledger history; calm the connect CTAs (#1437)
* feat(sandbox): seed payroll, articles and a year of ledger history; calm the connect CTAs

The sandbox showed neither Löner nor a usable set of reports, and the
"connect X" surfaces were oversized boxed cards.

Sandbox seed:
- pays_salaries + employer_registered, so Löner and Anställda appear at all
  (an enskild firma is not an employer by default). Both seeded employees are
  employment_type 'employee': an EF may employ staff, just not its own owner.
- Two employees, one booked and one open lönekörning, and the three verifikat
  the booked run must have posted (7210/2710/1930, 7510/2731, 7290+7519/
  2920+2940). Skatteavdrag comes from the real Skatteverket 2026 tables.
- Year-to-date ledger history, January through last month, with the quarterly
  momsredovisning cleared to 2650 and paid on the SFL deadline. Without the
  settlement the demo collected VAT all year and never remitted it, which left
  an implausible bank balance and 155 813 kr of moms "att betala".
- The history is exempted through journal_entry_no_doc_required, the same way
  the SIE-import opt-in treats imported books: its kvitton live in the previous
  system, and unflagged it put 39 "verifikat utan underlag" on the home screen.
- Artikelregister, and the BAS accounts the K1 chart omits for an enskild firma.
- History is numbered before the invoice and payroll vouchers so the series runs
  forwards through the year, and its writes are batched.

Connect CTAs:
- Bank picker: a two-column grid of 95px bordered logo cards becomes flat
  hairline rows, Lucide icons, and a quiet inline connecting state.
- Cloud backup: each provider collapses to one row; the BFL note is shown once
  for the section and names only configured destinations.
- Hem first-run: only the active step argues its case, but every not-done step
  keeps a reachable action. The Skatteverket nudge becomes one quiet sentence.

Mobile assistant FAB: a fresh open is desktop-only, since the bottom nav already
has an Assistent tab. A collapsed session keeps its handle everywhere except
/chat, which is itself the way back to the conversation.

Also closes a real hole: /api/salary/runs/[id]/payslips/send had no sandbox
guard, and a seeded booked run put "Skicka lönebesked" one click from an
anonymous visitor with live Resend behind it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

* fix(sandbox): check the two unchecked Supabase errors and tighten review nits

CodeRabbit review on #1437.

Major: two calls discarded their error and continued with null data. A failed
chart_of_accounts re-select would have written account_id: null onto every
ledger-history and salary voucher line, and a failed next_voucher_number would
have inserted a posted verifikat with no number, which is a hole in the
verifikationsserie (BFNAR 2013:2). Both now throw, and a null voucher number is
rejected explicitly.

Minor: the A-004 note claimed a 10 % markup on numbers that are 11.1 %; the
salary breakdown test's name said the opposite of its assertions after the
switch to the real tax table; the ledger-history doc still said 4 to 6 verifikat
per month before the quarterly momsredovisning added a seventh in March, May and
June.

Bank picker: the spinner is aria-hidden, so loading and connecting had no text
equivalent and a failed bank fetch was never announced. Added role="status" with
an sr-only label, and role="alert" on the error line.

Declined: confirm-before-disconnect on the cloud-backup row. Disconnect was
unconfirmed before this PR too, so adding a dialog is a behaviour change beyond
the redesign rather than a fix to it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-06 15:30:04 +02:00

90 lines
3.9 KiB
TypeScript

'use client'
import { useCallback, useSyncExternalStore } from 'react'
import { useTranslations } from 'next-intl'
import { ENABLED_EXTENSION_IDS } from '@/lib/extensions/_generated/enabled-extensions'
import { useCapability, useCompanyOptional } from '@/contexts/CompanyContext'
import { CAPABILITY } from '@/lib/entitlements/keys'
const dismissKey = (companyId: string) => `erp_skv_promo_dismissed:${companyId}`
// storage events only fire in OTHER tabs; this custom event covers the
// same-tab dismissal so useSyncExternalStore re-reads localStorage.
const DISMISS_EVENT = 'erp-skv-promo-dismissed'
function subscribeToDismissal(onStoreChange: () => void) {
window.addEventListener('storage', onStoreChange)
window.addEventListener(DISMISS_EVENT, onStoreChange)
return () => {
window.removeEventListener('storage', onStoreChange)
window.removeEventListener(DISMISS_EVENT, onStoreChange)
}
}
interface SkatteverketPromoCardProps {
companyId: string
/** True when the active user already has a Skatteverket token. */
connected: boolean
}
/**
* Dismissible dashboard nudge for companies that have never connected
* Skatteverket. New companies meet the connect step in NewUserChecklist;
* this is the equivalent surface for existing companies, which otherwise
* only discover the integration deep inside the VAT/AGI flows.
* Shape: one quiet sentence with the connect link at the end and the same
* "Dölj" text control the checklist uses, not a boxed card. It is an
* optional offer, not an exception, so it stays muted rather than taking
* the ochre .attn tone (convention 6/12: attention colour is for data and
* real exceptions).
* Capability-less users never see it: the paywall upsell lives where the
* intent is (SkatteverketPanel, AGIPanel), not on the dashboard.
* Sandbox companies never see it either: Skatteverket is one of the external
* services the sandbox blocks outright, so the CTA would lead nowhere.
*/
export function SkatteverketPromoCard({ companyId, connected }: SkatteverketPromoCardProps) {
const t = useTranslations('dashboard')
const extensionEnabled = ENABLED_EXTENSION_IDS.has('skatteverket')
const hasCapability = useCapability(CAPABILITY.skatteverket)
const isSandbox = useCompanyOptional()?.isSandbox ?? false
// Server snapshot says dismissed: the card appears only after hydration,
// when localStorage is readable, so server and client never disagree.
const dismissed = useSyncExternalStore(
subscribeToDismissal,
() => localStorage.getItem(dismissKey(companyId)) === 'true',
() => true
)
const dismiss = useCallback(() => {
localStorage.setItem(dismissKey(companyId), 'true')
window.dispatchEvent(new Event(DISMISS_EVENT))
}, [companyId])
if (!extensionEnabled || !hasCapability || isSandbox || connected || dismissed) return null
return (
<section className="flex items-start justify-between gap-4">
<p className="text-[12.5px] leading-5 text-muted-foreground">
{t('skv_promo_description')}{' '}
{/* py-3/-my-3: a 44px tap target on a link that still sits inline in
the sentence; the negative margin keeps the line box at 20px so
nothing shifts. */}
{/* eslint-disable-next-line @next/next/no-html-link-for-pages -- /api route, not a Next page; the authorize endpoint 302s to Skatteverket, which the client router cannot follow */}
<a
href="/api/extensions/ext/skatteverket/authorize?return_to=/"
className="inline-block -my-3 whitespace-nowrap py-3 text-foreground underline decoration-border underline-offset-4 transition-colors hover:decoration-foreground"
>
{t('skv_promo_cta')}
</a>
</p>
<button
type="button"
onClick={dismiss}
className="-my-3 shrink-0 py-3 text-xs text-muted-foreground underline decoration-border underline-offset-4 transition-colors hover:text-foreground"
>
{t('skv_promo_dismiss')}
</button>
</section>
)
}