* fix(invoices): anchor the PDF logo to the top-left of its header cell The logo box is always the full 240x80pt reserved area (any larger logo is clamped to exactly that), so objectFit: 'contain' placed the image inside it with the default 50% 50% centering. A wide banner logo fills the width and lands on the left margin, but a near-square logo scaled down to the 80pt height cap is only ~117pt wide and got pushed ~60pt in from the margin, which reads as a misaligned logo and forced companies to reshape their artwork. Anchor the image top-left so every aspect ratio starts at the margin. Covered by a test that renders the real PDF and reads the image placement matrix out of the content stream, for both a wide and a near-square logo. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * feat(invoices): show the real delivery outcome in the send history "Skickad" only meant the email provider accepted the message, so a bounced invoice looked identical to one that arrived. Resend reports the outcome asynchronously; that report now lands on the delivery row and drives the history: green is reserved for a confirmed delivery, bounce/blocked reads red, delayed and spam-marked read amber, and an accepted-but-unconfirmed send is neutral instead of falsely green. The report arrives on a signed webhook and may only touch the three new provider status columns of an already sent, unredacted row: the WORM trigger proves nothing else changed, and a lower ranked or older report can never downgrade an observed failure. The provider reason text can quote the failing address, so it is masked on read and cleared by the daily PII redaction job. Timestamps also formatted in Europe/Stockholm instead of falling back to the runtime zone, which rendered a 14:05 send as 12:05 on Vercel. Delivery reports are per message, never per recipient: Resend sends one event for the whole message, so splitting a send per recipient would be the only way to get finer granularity, at the cost of CC. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * feat(stripe): make the integration feed-only Stripe sync now only imports balance transactions into the transactions inbox, like any bank feed; nothing auto-books. The event/settlement sync (lib/sync.ts, lib/payouts.ts) stays in the repo but is no longer wired to any route or cron: the 15-min sync cron is removed from vercel.json. Payment links on invoice send are unchanged; their payments arrive as feed rows and are matched manually. - /sync runs only syncStripeBalanceTransactions; response is { success, transactions } - connecting via OAuth enables the nightly feed by default (toggle stays as opt-out) - panel: needs-review section and plumbing removed, copy rewritten to transactions-first (sv + en), toast reports fetched/imported/linked and calls out an empty result instead of silent all-zeros Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix(api): return the article currency from the v1 article list The dashboard, importer, export and MCP article surfaces all learned to carry a non-SEK article price (#1166, #1183, #1184), but the v1 projection still omitted currency. An API or agent caller therefore read price_excl_vat with nothing marking it as EUR and would copy the number straight onto a SEK invoice line, at a nine-to-one error. Adds currency to the projection, the response shape and the example, plus a pitfall stating the price is not always SEK and that this endpoint does no FX conversion. Additive field only; no migration (articles.currency already exists). Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * feat(settings): replace the settings modal with a routed panel sheet Settings now renders as a sheet that fills the main panel, sliding up over the page the user came from and back down on close, with the sidebar and frame left visible and usable. Behind it sits one shared master-detail surface: underline search across every section and subsection, the grouped section rail, and the active section as a direct-editing accordion. All 11 sections are decomposed into subsections, and the legacy *SettingsContent components compose the same pieces so the stacked and accordion layouts cannot drift. The sheet is the only presentation, on every entry path. The intercepting route handles in-app navigation and closes by popping the history entry, landing back on the page underneath. @settingsModal/default.tsx handles cold loads (refresh, deep link, new tab), where interception never fires; nothing is mounted underneath there, so it closes to the dashboard. Both branch on one shared predicate, isSheetSection, together with the settings layout, which must render nothing for those sections or the surface would stack twice behind the sheet and run every section's fetches twice. Closing is deliberate rather than incidental: the X, Esc, or navigating away. The dialog is non-modal so the sidebar's account popover and company switcher keep working with settings up, and an outside click no longer dismisses it. Sections land fully collapsed, and the scroll position of the page behind survives opening and closing the sheet. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * feat: enhance article management and settings UI - Add PATCH test for toggling article active state without other fields. - Remove unused MessageCircle icon from DashboardContent. - Refactor AccountingFrameworkForm to use SettingsFieldRow for better help text display. - Update CompanyInfoForm, DimensionsToggle, and various settings forms to replace description with help text. - Remove redundant headings and intros in several settings components to streamline UI. - Improve help text for various settings in English and Swedish translations. - Update structured error messages for better clarity on article deletion. * refactor(ArticleDetailPage): remove unused imports and duplicate state variable * fix(settings): own deep-linked settings routes by route list, not nav visibility Review fixes from the settings panel sheet work: * isSheetSection reads the full settings route list so a hidden-but-deep-linked section (assistant before BankID, banking in sandbox, api without MCP) is claimed by the sheet instead of rendering the legacy shell around an empty panel * keep 503 on the Resend delivery webhook when the signing secret is unset, with a test pinning the behaviour * stripe callback route test coverage Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * refactor: update salary, tax, and templates settings components - Refactored SalarySettingsContent to use a form wrapper and improved payment settings UI. - Enhanced TaxSettingsContent with new signals for EU sales, KU obligations, and ROT/RUT deductions. - Updated TemplatesSettingsContent to remove legacy comments and improve readability. - Simplified navigation items by removing unnecessary constants and directly using hrefs. - Cleaned up translation files by removing deprecated keys and adding new descriptions for clarity. --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
623 lines
24 KiB
TypeScript
623 lines
24 KiB
TypeScript
'use client'
|
|
|
|
import { useState, useEffect, useRef } from 'react'
|
|
import Link from 'next/link'
|
|
import { useSearchParams } from 'next/navigation'
|
|
import { Button } from '@/components/ui/button'
|
|
import { useToast } from '@/components/ui/use-toast'
|
|
import { DestructiveConfirmDialog, useDestructiveConfirm } from '@/components/ui/destructive-confirm-dialog'
|
|
import { CheckCircle, Loader2, Upload } from 'lucide-react'
|
|
import { createClient } from '@/lib/supabase/client'
|
|
import { notifyBankSyncUpdated } from '@/lib/transactions/bank-sync-signal'
|
|
import { useCompany, useCapability } from '@/contexts/CompanyContext'
|
|
import { CAPABILITY } from '@/lib/entitlements/keys'
|
|
import { UpgradeNote } from '@/components/billing/UpgradeNote'
|
|
import { SettingsGroup, SettingsRow, SettingsSeg } from '@/components/settings/SettingsRows'
|
|
import { BankSelector, type Bank } from './BankSelector'
|
|
import { BankConnectionStatus } from './BankConnectionStatus'
|
|
import { AccountPickerDialog } from './AccountPickerDialog'
|
|
import type { BankConnection } from '@/types'
|
|
import type { StoredAccount } from '../types'
|
|
|
|
/**
|
|
* Self-contained banking settings panel for the enable-banking extension.
|
|
* Loaded dynamically by the settings panel registry.
|
|
*/
|
|
export default function BankingSettingsPanel() {
|
|
const { toast } = useToast()
|
|
const supabase = createClient()
|
|
// The OAuth callback lands here with ?select_accounts=<id> once a bank is
|
|
// successfully connected. Read via useSearchParams (SSR/hydration-safe) so
|
|
// the first-load spinner can say "bank connected, fetching accounts"
|
|
// instead of an anonymous spinner. The param itself is consumed and
|
|
// stripped by the auto-open effect below.
|
|
const searchParams = useSearchParams()
|
|
const arrivedFromBankCallback = !!searchParams?.get('select_accounts')
|
|
|
|
const { dialogProps, confirm } = useDestructiveConfirm()
|
|
const { company } = useCompany()
|
|
const hasBankSync = useCapability(CAPABILITY.bank_sync)
|
|
|
|
const [bankConnections, setBankConnections] = useState<BankConnection[]>([])
|
|
const [syncingConnectionId, setSyncingConnectionId] = useState<string | null>(null)
|
|
const [isConnecting, setIsConnecting] = useState(false)
|
|
const [connectingBankName, setConnectingBankName] = useState<string | null>(null)
|
|
const connectingRef = useRef(false)
|
|
const releaseTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null)
|
|
const [isLoading, setIsLoading] = useState(true)
|
|
const [loadError, setLoadError] = useState(false)
|
|
const hasLoadedRef = useRef(false)
|
|
const [showCsvFallback, setShowCsvFallback] = useState(false)
|
|
const [psuType, setPsuType] = useState<'personal' | 'business'>('business')
|
|
const [pickerConnectionId, setPickerConnectionId] = useState<string | null>(null)
|
|
|
|
// Must match STALE_THRESHOLD_MS in extensions/general/enable-banking/index.ts
|
|
const PENDING_LOCK_MS = 30 * 1000
|
|
|
|
useEffect(() => {
|
|
fetchConnections()
|
|
return () => {
|
|
if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current)
|
|
}
|
|
}, [])
|
|
|
|
// Latest-ref so the visibility listener below (subscribed once) always
|
|
// calls the current render's fetchConnections, which closes over company
|
|
// context that may resolve after mount.
|
|
const fetchConnectionsRef = useRef(fetchConnections)
|
|
useEffect(() => {
|
|
fetchConnectionsRef.current = fetchConnections
|
|
})
|
|
|
|
// Safety net for completion signals that never reach this tab: a mobile
|
|
// BankID app-switch can land the bank's redirect in a different browser
|
|
// tab, the user can close the finalize page before its redirect, and a
|
|
// bfcache-restored page shows a pre-connection snapshot. Refetch when the
|
|
// tab regains visibility (background refresh, no spinner: fetchConnections
|
|
// only blanks the panel on first load), throttled so rapid tab toggling
|
|
// doesn't hammer the API.
|
|
const lastVisibilityFetchRef = useRef(0)
|
|
useEffect(() => {
|
|
function onVisible() {
|
|
if (document.visibilityState !== 'visible') return
|
|
const now = Date.now()
|
|
if (now - lastVisibilityFetchRef.current < 5_000) return
|
|
lastVisibilityFetchRef.current = now
|
|
void fetchConnectionsRef.current()
|
|
}
|
|
document.addEventListener('visibilitychange', onVisible)
|
|
return () => document.removeEventListener('visibilitychange', onVisible)
|
|
}, [])
|
|
|
|
// Auto-open the picker when the user lands here from the OAuth callback
|
|
// (URL: /settings/banking?select_accounts=<id>). The query param is stripped
|
|
// afterwards so a refresh doesn't keep reopening it.
|
|
useEffect(() => {
|
|
if (isLoading) return
|
|
if (typeof window === 'undefined') return
|
|
|
|
const params = new URLSearchParams(window.location.search)
|
|
const targetId = params.get('select_accounts')
|
|
if (!targetId) return
|
|
|
|
const match = bankConnections.find(c => c.id === targetId)
|
|
if (match) {
|
|
setPickerConnectionId(targetId)
|
|
}
|
|
|
|
params.delete('select_accounts')
|
|
const newQuery = params.toString()
|
|
const newUrl = `${window.location.pathname}${newQuery ? `?${newQuery}` : ''}`
|
|
window.history.replaceState({}, '', newUrl)
|
|
}, [isLoading, bankConnections])
|
|
|
|
function releaseConnectingLock() {
|
|
connectingRef.current = false
|
|
setIsConnecting(false)
|
|
setConnectingBankName(null)
|
|
}
|
|
|
|
async function fetchConnections() {
|
|
// Only the first load blanks the panel to a spinner. Later refetches (after
|
|
// a sync, disconnect, or account save) refresh in the background so the
|
|
// panel doesn't flash back to a full-height spinner and lose scroll
|
|
// position on every action.
|
|
if (!hasLoadedRef.current) setIsLoading(true)
|
|
setLoadError(false)
|
|
try {
|
|
const { data: { user } } = await supabase.auth.getUser()
|
|
if (!user || !company) {
|
|
setBankConnections([])
|
|
return
|
|
}
|
|
|
|
const { data: connections, error } = await supabase
|
|
.from('bank_connections')
|
|
.select('*')
|
|
.eq('company_id', company.id)
|
|
.order('created_at', { ascending: false })
|
|
|
|
if (error) {
|
|
// Surface the failure instead of rendering an empty panel: an empty
|
|
// panel reads as "your bank got disconnected" when it's really a
|
|
// transient fetch/RLS error.
|
|
setLoadError(true)
|
|
return
|
|
}
|
|
|
|
setBankConnections(connections || [])
|
|
|
|
// If a pending connection exists from a recent attempt (e.g. user bounced back from
|
|
// the bank's auth page), keep the connect button disabled until the server-side lock expires.
|
|
const freshPending = (connections || []).find((c) => c.status === 'pending')
|
|
if (freshPending) {
|
|
const age = Date.now() - new Date(freshPending.created_at).getTime()
|
|
const remaining = PENDING_LOCK_MS - age
|
|
if (remaining > 0) {
|
|
connectingRef.current = true
|
|
setIsConnecting(true)
|
|
setConnectingBankName(freshPending.bank_name)
|
|
if (releaseTimerRef.current) clearTimeout(releaseTimerRef.current)
|
|
releaseTimerRef.current = setTimeout(releaseConnectingLock, remaining)
|
|
}
|
|
}
|
|
} finally {
|
|
// Always clear the spinner, even on the early `!user || !company` return,
|
|
// so an expired session can't leave the panel spinning forever.
|
|
hasLoadedRef.current = true
|
|
setIsLoading(false)
|
|
}
|
|
}
|
|
|
|
async function handleConnectBank(bank: Bank, psuTypeOverride?: 'personal' | 'business') {
|
|
if (connectingRef.current) return
|
|
connectingRef.current = true
|
|
setIsConnecting(true)
|
|
setConnectingBankName(bank.name)
|
|
|
|
try {
|
|
console.log('[enable-banking] Initiating bank connection', {
|
|
bankName: bank.name,
|
|
bankCountry: bank.country,
|
|
psuTypeOverride,
|
|
})
|
|
|
|
const body: Record<string, string> = { aspsp_name: bank.name, aspsp_country: bank.country }
|
|
if (psuTypeOverride) body.psu_type = psuTypeOverride
|
|
|
|
const response = await fetch('/api/extensions/ext/enable-banking/connect', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(body),
|
|
})
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
console.error('[enable-banking] Connect request failed', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: data.error,
|
|
bankName: bank.name,
|
|
})
|
|
throw new Error(data.error)
|
|
}
|
|
|
|
console.log('[enable-banking] Redirecting to bank authorization', {
|
|
connectionId: data.connection_id,
|
|
hasAuthUrl: !!data.authorization_url,
|
|
})
|
|
window.location.href = data.authorization_url
|
|
} catch (error) {
|
|
console.error('[enable-banking] Connect flow failed', {
|
|
message: error instanceof Error ? error.message : String(error),
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
bankName: bank.name,
|
|
})
|
|
toast({
|
|
title: 'Fel',
|
|
description: error instanceof Error ? error.message : 'Kunde inte ansluta bank',
|
|
variant: 'destructive',
|
|
})
|
|
connectingRef.current = false
|
|
setIsConnecting(false)
|
|
setConnectingBankName(null)
|
|
setShowCsvFallback(true)
|
|
}
|
|
}
|
|
|
|
// Re-authorize an existing connection in place: no disconnect required.
|
|
// Posts to /connect with the existing connection_id so the server reuses the
|
|
// same row (revoking the dead session, issuing fresh authorization), then
|
|
// hands off to the bank's consent screen. The OAuth callback drives the row
|
|
// back through account selection to active.
|
|
async function handleReconnect(connection: BankConnection, psuTypeOverride?: 'personal' | 'business') {
|
|
if (connectingRef.current) return
|
|
connectingRef.current = true
|
|
setIsConnecting(true)
|
|
setConnectingBankName(connection.bank_name)
|
|
|
|
try {
|
|
const country = (connection.provider as string)?.split('-').pop()?.toUpperCase() || 'SE'
|
|
const response = await fetch('/api/extensions/ext/enable-banking/connect', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
connection_id: connection.id,
|
|
aspsp_name: connection.bank_name,
|
|
aspsp_country: country,
|
|
// Omitted → server reuses the connection's stored psu_type (falling
|
|
// back to entity_type). Set → switch account type in place.
|
|
...(psuTypeOverride ? { psu_type: psuTypeOverride } : {}),
|
|
}),
|
|
})
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
throw new Error(data.error)
|
|
}
|
|
|
|
window.location.href = data.authorization_url
|
|
} catch (error) {
|
|
console.error('[enable-banking] Reconnect flow failed', {
|
|
message: error instanceof Error ? error.message : String(error),
|
|
connectionId: connection.id,
|
|
})
|
|
toast({
|
|
title: 'Fel',
|
|
description: error instanceof Error ? error.message : 'Kunde inte förnya anslutningen',
|
|
variant: 'destructive',
|
|
})
|
|
connectingRef.current = false
|
|
setIsConnecting(false)
|
|
setConnectingBankName(null)
|
|
}
|
|
}
|
|
|
|
async function handleSyncTransactions(connectionId: string) {
|
|
setSyncingConnectionId(connectionId)
|
|
|
|
// A slow bank can hold the request open up to the route's 300s budget.
|
|
// Cap the client wait so the spinner can't hang indefinitely; the sync is
|
|
// idempotent (imports dedup), so a background completion or manual retry is
|
|
// safe.
|
|
const controller = new AbortController()
|
|
const timeout = setTimeout(() => controller.abort(), 180_000)
|
|
|
|
try {
|
|
console.log('[enable-banking] Starting sync', { connectionId })
|
|
|
|
const response = await fetch('/api/extensions/ext/enable-banking/sync', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ connection_id: connectionId }),
|
|
signal: controller.signal,
|
|
})
|
|
|
|
const data = await response.json()
|
|
|
|
if (!response.ok) {
|
|
console.error('[enable-banking] Sync request failed', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: data.error,
|
|
connectionId,
|
|
})
|
|
throw new Error(data.error)
|
|
}
|
|
|
|
console.log('[enable-banking] Sync completed', {
|
|
connectionId,
|
|
imported: data.imported,
|
|
duplicates: data.duplicates,
|
|
})
|
|
|
|
toast({
|
|
title: 'Synkronisering klar',
|
|
description: `${data.imported} nya transaktioner importerade`,
|
|
})
|
|
|
|
setShowCsvFallback(false)
|
|
notifyBankSyncUpdated()
|
|
fetchConnections()
|
|
} catch (error) {
|
|
if (controller.signal.aborted) {
|
|
toast({
|
|
title: 'Synkronisering tar längre tid än vanligt',
|
|
description: 'Transaktionerna hämtas i bakgrunden. Uppdatera sidan om en stund.',
|
|
})
|
|
fetchConnections()
|
|
} else {
|
|
console.error('[enable-banking] Sync flow failed', {
|
|
message: error instanceof Error ? error.message : String(error),
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
connectionId,
|
|
})
|
|
toast({
|
|
title: 'Fel',
|
|
description: error instanceof Error ? error.message : 'Synkronisering misslyckades',
|
|
variant: 'destructive',
|
|
})
|
|
setShowCsvFallback(true)
|
|
// Refresh so a now-expired connection (e.g. closed PSD2 session) moves
|
|
// into "Åtgärd krävs" and surfaces the "Förnya anslutning" button.
|
|
fetchConnections()
|
|
}
|
|
} finally {
|
|
clearTimeout(timeout)
|
|
setSyncingConnectionId(null)
|
|
}
|
|
}
|
|
|
|
async function handleDisconnectBank(connectionId: string) {
|
|
const ok = await confirm({
|
|
title: 'Koppla bort bank?',
|
|
description: 'PSD2-samtycket kommer återkallas. Befintliga transaktioner påverkas inte.',
|
|
confirmLabel: 'Koppla bort',
|
|
variant: 'warning',
|
|
})
|
|
if (!ok) return
|
|
|
|
try {
|
|
console.log('[enable-banking] Disconnecting bank', { connectionId })
|
|
|
|
const response = await fetch('/api/extensions/ext/enable-banking/disconnect', {
|
|
method: 'DELETE',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ connection_id: connectionId }),
|
|
})
|
|
|
|
if (!response.ok) {
|
|
const data = await response.json()
|
|
console.error('[enable-banking] Disconnect request failed', {
|
|
status: response.status,
|
|
statusText: response.statusText,
|
|
error: data.error,
|
|
connectionId,
|
|
})
|
|
throw new Error(data.error || 'Disconnect failed')
|
|
}
|
|
|
|
console.log('[enable-banking] Bank disconnected', { connectionId })
|
|
toast({
|
|
title: 'Bank bortkopplad',
|
|
description: 'Bankanslutningen och PSD2-samtycket har återkallats',
|
|
})
|
|
fetchConnections()
|
|
} catch (error) {
|
|
console.error('[enable-banking] Disconnect flow failed', {
|
|
message: error instanceof Error ? error.message : String(error),
|
|
stack: error instanceof Error ? error.stack : undefined,
|
|
connectionId,
|
|
})
|
|
toast({
|
|
title: 'Fel',
|
|
description: error instanceof Error ? error.message : 'Kunde inte koppla bort bank',
|
|
variant: 'destructive',
|
|
})
|
|
}
|
|
}
|
|
|
|
if (isLoading) {
|
|
// Coming back from the bank's consent flow the connection already exists,
|
|
// so tell the user that instead of showing an anonymous spinner: this is
|
|
// the last silent gap between "approved at the bank" and the account
|
|
// picker opening.
|
|
if (arrivedFromBankCallback) {
|
|
return (
|
|
<div className="flex h-32 flex-col items-center justify-center gap-3">
|
|
<div className="flex items-center gap-2 text-sm font-medium">
|
|
<CheckCircle className="h-4 w-4 text-success" />
|
|
<span>Banken är ansluten</span>
|
|
</div>
|
|
<div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
<Loader2 className="h-4 w-4 animate-spin" />
|
|
<span>Hämtar dina konton…</span>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
return (
|
|
<div className="flex items-center justify-center h-32">
|
|
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
|
|
</div>
|
|
)
|
|
}
|
|
|
|
// First-load failure: show a recoverable error instead of an empty panel (a
|
|
// blank panel misreads as "no banks connected"). A background-refetch failure
|
|
// keeps the already-loaded connections visible instead of wiping them.
|
|
if (loadError && bankConnections.length === 0) {
|
|
return (
|
|
<div className="px-1 pt-8">
|
|
<p className="text-sm font-medium">Kunde inte ladda bankanslutningar</p>
|
|
<p className="mt-1 max-w-[56ch] text-xs leading-relaxed text-muted-foreground">
|
|
Något gick fel när dina bankanslutningar skulle hämtas. Dina anslutningar
|
|
och transaktioner är oförändrade.
|
|
</p>
|
|
<div className="mt-3 flex flex-wrap items-center gap-3">
|
|
<Button variant="outline" size="sm" onClick={() => fetchConnections()}>
|
|
Försök igen
|
|
</Button>
|
|
<Button variant="ghost" size="sm" asChild>
|
|
<Link href="/import?mode=bank">Importera bankfil istället</Link>
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
const activeConnections = bankConnections.filter((c) => c.status === 'active')
|
|
const pendingSelectionConnections = bankConnections.filter((c) => c.status === 'pending_selection')
|
|
const actionRequiredConnections = bankConnections.filter((c) => ['expired', 'error'].includes(c.status))
|
|
|
|
const pickerConnection = pickerConnectionId
|
|
? bankConnections.find(c => c.id === pickerConnectionId)
|
|
: null
|
|
const pickerAccounts = pickerConnection
|
|
? ((pickerConnection.accounts_data as StoredAccount[] | null) || [])
|
|
: []
|
|
|
|
return (
|
|
<div>
|
|
<DestructiveConfirmDialog {...dialogProps} />
|
|
|
|
{pickerConnection && (
|
|
<AccountPickerDialog
|
|
open={!!pickerConnection}
|
|
onOpenChange={(open) => {
|
|
if (!open) setPickerConnectionId(null)
|
|
}}
|
|
connectionId={pickerConnection.id}
|
|
bankName={pickerConnection.bank_name}
|
|
accounts={pickerAccounts}
|
|
isInitialSelection={pickerConnection.status === 'pending_selection'}
|
|
onSaved={() => fetchConnections()}
|
|
/>
|
|
)}
|
|
|
|
{/* Persistent CSV fallback after connection/sync failure: a live hint,
|
|
kept visible as a compact line instead of a boxed strip. */}
|
|
{showCsvFallback && (
|
|
<div className="flex items-start gap-2 px-1 pt-6">
|
|
<Upload className="mt-0.5 h-3.5 w-3.5 shrink-0 text-muted-foreground" />
|
|
<p className="text-[12.5px] leading-relaxed text-muted-foreground">
|
|
Har du problem med bankanslutningen? Du kan{' '}
|
|
<Link href="/import?mode=bank" className="underline underline-offset-2 hover:text-foreground">
|
|
importera transaktioner manuellt via bankfil
|
|
</Link>
|
|
.
|
|
</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Pending account selection: new connections waiting for the user to pick accounts */}
|
|
{pendingSelectionConnections.length > 0 && (
|
|
<SettingsGroup
|
|
label="Välj konton att synka"
|
|
help="Banken har gett åtkomst till flera konton. Välj vilka du vill synka innan några transaktioner hämtas."
|
|
>
|
|
{pendingSelectionConnections.map((connection) => {
|
|
const accountsList = (connection.accounts_data as StoredAccount[] | null) || []
|
|
return (
|
|
<div
|
|
key={connection.id}
|
|
className="flex flex-wrap items-center gap-x-3 gap-y-1 border-b border-border px-1 py-3"
|
|
>
|
|
<span className="text-sm font-medium">{connection.bank_name}</span>
|
|
<span className="text-xs text-muted-foreground">
|
|
{accountsList.length} konton tillgängliga: inga transaktioner synkas ännu
|
|
</span>
|
|
<span className="ml-auto flex shrink-0 items-center gap-2">
|
|
<Button size="sm" onClick={() => setPickerConnectionId(connection.id)}>
|
|
Välj konton
|
|
</Button>
|
|
<Button
|
|
variant="ghost"
|
|
size="sm"
|
|
className="text-muted-foreground hover:text-foreground"
|
|
onClick={() => handleDisconnectBank(connection.id)}
|
|
>
|
|
Avbryt
|
|
</Button>
|
|
</span>
|
|
</div>
|
|
)
|
|
})}
|
|
</SettingsGroup>
|
|
)}
|
|
|
|
{/* Action required: expired/error connections */}
|
|
{actionRequiredConnections.length > 0 && (
|
|
<SettingsGroup label="Åtgärd krävs" help="Dessa anslutningar behöver uppmärksamhet.">
|
|
{actionRequiredConnections.map((connection) => (
|
|
<BankConnectionStatus
|
|
key={connection.id}
|
|
connection={connection}
|
|
onSync={handleSyncTransactions}
|
|
onDisconnect={handleDisconnectBank}
|
|
onReconnect={handleReconnect}
|
|
onManageAccounts={() => setPickerConnectionId(connection.id)}
|
|
isSyncing={syncingConnectionId === connection.id}
|
|
/>
|
|
))}
|
|
</SettingsGroup>
|
|
)}
|
|
|
|
{/* Connected banks */}
|
|
{activeConnections.length > 0 && (
|
|
<SettingsGroup label="Anslutna banker">
|
|
{activeConnections.map((connection) => (
|
|
<BankConnectionStatus
|
|
key={connection.id}
|
|
connection={connection}
|
|
onSync={handleSyncTransactions}
|
|
onDisconnect={handleDisconnectBank}
|
|
onManageAccounts={() => setPickerConnectionId(connection.id)}
|
|
isSyncing={syncingConnectionId === connection.id}
|
|
/>
|
|
))}
|
|
</SettingsGroup>
|
|
)}
|
|
|
|
{/* Connect new bank. Non-payers keep seeing the group (conversion
|
|
surface) but the bank list is replaced by an upgrade note: the
|
|
server gate would 403 the connect anyway. The former "Om
|
|
bankintegration (PSD2)" card lives on as group-level help. */}
|
|
<SettingsGroup
|
|
label="Anslut ny bank"
|
|
help={
|
|
<div className="space-y-2">
|
|
<p>Välj din bank nedan för att koppla ditt konto via PSD2.</p>
|
|
<p className="font-medium">Om bankintegration (PSD2)</p>
|
|
<p>
|
|
Automatisk import av transaktioner via PSD2 open banking.
|
|
Samtycket gäller i 90 dagar och behöver sedan förnyas.
|
|
</p>
|
|
<p>
|
|
Vi använder säker bankintegration (PSD2). Vi kan endast läsa transaktioner,
|
|
aldrig flytta pengar. Du kan också importera transaktioner manuellt via
|
|
bankfiler på importsidan.
|
|
</p>
|
|
</div>
|
|
}
|
|
>
|
|
{!hasBankSync ? (
|
|
<div className="px-1 pt-3">
|
|
<UpgradeNote>
|
|
Automatisk banksynk kräver ett abonnemang. Du kan fortfarande importera
|
|
transaktioner manuellt via bankfiler på importsidan.
|
|
</UpgradeNote>
|
|
</div>
|
|
) : (
|
|
<>
|
|
<SettingsRow
|
|
label="Kontotyp"
|
|
help="Välj Privatkonto om du använder ditt personliga bankkonto för din verksamhet (vanligt för enskild firma)."
|
|
>
|
|
<SettingsSeg
|
|
value={psuType}
|
|
onChange={setPsuType}
|
|
aria-label="Kontotyp"
|
|
options={[
|
|
{ value: 'business', label: 'Företagskonto' },
|
|
{ value: 'personal', label: 'Privatkonto' },
|
|
]}
|
|
/>
|
|
</SettingsRow>
|
|
<div className="px-1 pt-4">
|
|
<BankSelector
|
|
onConnect={(bank) => handleConnectBank(bank, psuType)}
|
|
onPsuTypeDetected={setPsuType}
|
|
isConnecting={isConnecting}
|
|
connectingBankName={connectingBankName}
|
|
/>
|
|
</div>
|
|
</>
|
|
)}
|
|
</SettingsGroup>
|
|
</div>
|
|
)
|
|
}
|