Files
accounted/components/settings/BankIdSettings.tsx
T
Jakob Wennberg 6d9846b1e7 feat(settings): Fönster redesign - flat rows, ? help, dirty save bar (#1193)
* feat(settings): Fönster redesign - flat rows, help behind ?, dirty save bar

Founder-approved concept (2026-07-25) applied to the whole settings
surface, modal and full-page variants alike:

- New primitives in components/settings/SettingsRows.tsx: section header
  (serif title + one-line intro), eyebrow groups, hairline label/control
  rows, flat inputs/selects/textareas, segmented control, animated
  reveal for gated settings, danger zone.
- Every static explanation paragraph moved behind a "?" popover
  (HelpPopover) at row or group level; dynamic status stays visible.
- Modal chrome: company kicker over serif title, fixed 920x680 window.
- SettingsFormWrapper: save is a sticky bar that appears only when the
  form is dirty; collapses to zero height when clean.
- All 11 sections converted (Konto, Abonnemang, Företag, Bokföring,
  Skatt, Löner, Fakturering, Mallar, Bank incl. Enable Banking-panel,
  Assistenten, API) with handlers, validation, role/entitlement/sandbox
  gates and i18n keys preserved; checkboxes became switches, cards
  dissolved into groups.
- Fix: Escape with an open help popover closed the whole settings
  modal; it now closes the popover first.
- New i18n keys: settings_intro.*, group labels, wrapper_unsaved
  (sv+en).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(settings): founder feedback round 1 on the Fönster redesign

- Abonnemang paying state: status and manage split into two rows so the
  row no longer wraps awkwardly; the included-features list now shows
  for paying companies too.
- Logos where the counterpart has one: BankID mark on the security row
  and on the Koppla BankID button, Skatteverket mark on the connection
  rows.
- Buttons are unmistakably buttons: 27 text-labeled row actions went
  from ghost to outline pills; icon-only actions stay quiet.
- The agent-knowledge view (Regler & profil: Dina regler, Momsprofil,
  Konventioner) converted to the flat row language; it was the last
  old-style surface inside settings. Descriptions moved behind "?",
  rules render as hairline rows, the per-row "Regel" chip demoted to
  muted text.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

* fix(settings): address review-bot findings on the Fönster redesign

- SettingsFormWrapper marks the form dirty on switch clicks too: Radix
  Switch is a button and fires no input event, so switch-only changes
  (f-skatt, KU, ROT/RUT, OSS...) never revealed the save bar.
- i18n: the migrated hardcoded strings got keys in both locales
  (fiscal-period start date/range/months, security set-password trio);
  dates in ApiKeysPanel/OAuthClientsPanel/CalendarFeedSettings now pass
  the active locale to formatDateLong.
- A11y: member remove/revoke buttons and the invite role select got
  correct accessible names; BankNameCombobox accepts aria-label wired
  from its row; the pinned-fact icon exposes role img.
- BankIdSettings: explicit Avbryt under the QR block so a cancelled
  BankID flow cannot strand isLinking.
- VoucherSeriesManager: clear the skeleton when no company is resolved.

Verified end to end in sandbox: switch-only dirty bar, PUT /api/settings
200 for text and switch saves, persistence across hard reload.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-25 23:55:08 +02:00

167 lines
5.1 KiB
TypeScript

'use client'
import Image from 'next/image'
import { useTranslations } from 'next-intl'
import { useState, useEffect, useCallback } from 'react'
import { createClient } from '@/lib/supabase/client'
import { BankIdAuth } from '@/components/auth/BankIdAuth'
import type { BankIdResult } from '@/components/auth/BankIdAuth'
import { Button } from '@/components/ui/button'
import { Loader2 } from 'lucide-react'
import { useToast } from '@/components/ui/use-toast'
import { formatDateLong } from '@/lib/utils'
import {
SettingsRow,
SettingsRowEnd,
SettingsRowNote,
} from '@/components/settings/SettingsRows'
interface BankIdIdentity {
given_name: string | null
surname: string | null
linked_at: string
}
export function BankIdSettings() {
const t = useTranslations('settings_bankid')
const [identity, setIdentity] = useState<BankIdIdentity | null>(null)
const [isLoading, setIsLoading] = useState(true)
const [isLinking, setIsLinking] = useState(false)
const [isUnlinking, setIsUnlinking] = useState(false)
const { toast } = useToast()
const fetchIdentity = useCallback(async () => {
const supabase = createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) { setIsLoading(false); return }
const { data } = await supabase
.from('bankid_identities')
.select('given_name, surname, linked_at')
.eq('user_id', user.id)
.maybeSingle()
setIdentity(data)
setIsLoading(false)
}, [])
useEffect(() => {
fetchIdentity()
}, [fetchIdentity])
const handleLinkComplete = async (result: BankIdResult) => {
if (result.error) {
const message = result.error === 'already_linked'
? t('toast_already_linked')
: t('toast_link_failed')
toast({ title: message, variant: 'destructive' })
setIsLinking(false)
return
}
toast({ title: t('toast_linked') })
setIsLinking(false)
fetchIdentity()
}
const handleUnlink = async () => {
if (!confirm(t('confirm_unlink'))) return
setIsUnlinking(true)
try {
const res = await fetch('/api/extensions/ext/tic/bankid/unlink', { method: 'POST' })
if (!res.ok) throw new Error('Unlink failed')
setIdentity(null)
toast({ title: t('toast_unlinked') })
} catch {
toast({ title: t('toast_unlink_failed'), variant: 'destructive' })
} finally {
setIsUnlinking(false)
}
}
if (isLoading) {
return (
<SettingsRow label={t('title')}>
<Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />
</SettingsRow>
)
}
return (
<>
<SettingsRow
label={
<span className="inline-flex items-center gap-2">
<Image
src="/logos/bankid-seeklogo.svg"
alt=""
aria-hidden="true"
width={16}
height={16}
className="dark:invert"
/>
{t('title')}
</span>
}
help={identity ? t('linked_description') : t('not_linked_description')}
borderless={isLinking}
>
{identity ? (
<>
<span className="text-sm font-medium">
{identity.given_name} {identity.surname}
</span>
<SettingsRowNote>
{t('linked_on', { date: formatDateLong(identity.linked_at) })}
</SettingsRowNote>
<SettingsRowEnd>
<Button
variant="outline"
size="sm"
onClick={handleUnlink}
disabled={isUnlinking}
className="text-destructive hover:text-destructive"
>
{isUnlinking ? t('unlinking') : t('unlink_button')}
</Button>
</SettingsRowEnd>
</>
) : isLinking ? (
// Active flow: the scan instruction is the actionable content and
// stays visible while the QR block below is open.
<SettingsRowNote>{t('link_bankid_description')}</SettingsRowNote>
) : (
<SettingsRowEnd>
<Button variant="outline" size="sm" onClick={() => setIsLinking(true)}>
<Image
src="/logos/bankid-seeklogo.svg"
alt=""
aria-hidden="true"
width={16}
height={16}
className="mr-2 dark:invert"
/>
{t('link_button')}
</Button>
</SettingsRowEnd>
)}
</SettingsRow>
{/* QR flow: an expanding block below the row. Mounted only while
linking so the BankID session starts exactly when requested. */}
{isLinking && (
<div className="flex flex-col items-center gap-3 border-b border-border px-1 py-4">
<BankIdAuth mode="link" onComplete={handleLinkComplete} />
{/* BankIdAuth's own Avbryt only resets its internal session; give
the row an exit so isLinking can't get stuck. */}
<Button variant="outline" size="sm" onClick={() => setIsLinking(false)}>
{t('cancel_linking')}
</Button>
</div>
)}
</>
)
}