Files
accounted/components/import/SIEUploadStep.tsx
T
Jakob Wennberg d23cb4c859 fix: SIE import duplicate check, performance, and UX improvements (#162)
* fix: catch duplicate SIE import early with clear error message

- Add duplicate check in execute route before doing any work (defense in depth)
- Handle duplicate error from execute route in frontend
- Show "Filen har redan importerats" heading instead of generic "Kunde inte läsa filen"

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

* fix: catch duplicate SIE import early and batch account creation for performance

- Add duplicate check in execute route before doing any work (defense in depth)
- Handle duplicate error from execute route in frontend with clear Swedish message
- Replace sequential ensureAccountExists loop (50-100 DB round trips) with single
  batch SELECT + batch INSERT — reduces import time from 3+ min to seconds

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

* fix: update mappings optimistically after creating missing accounts

Previously re-parsed the SIE file after account creation, which could fail
with a 409 duplicate error (leaving the "create accounts" card stuck).
Now optimistically marks created accounts as self-mapped and updates the
preview stats immediately.

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

* fix: address Greptile P2 feedback — error handling and typed errorType prop

- Check batch insert error in executeSIEImport account creation safety net
- Replace brittle string-match error detection with typed errorType prop
- Remove stale file dependency from handleCreateAccounts useCallback

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

---------

Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-02 11:21:51 +02:00

227 lines
8.1 KiB
TypeScript

'use client'
import { useState, useCallback, useEffect } from 'react'
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'
import { Progress } from '@/components/ui/progress'
import { Upload, FileText, AlertCircle, CheckCircle, Loader2 } from 'lucide-react'
const LOADING_PHASES = [
{ message: 'Läser fil...', progress: 10 },
{ message: 'Identifierar teckenkodning...', progress: 30 },
{ message: 'Tolkar SIE-data...', progress: 55 },
{ message: 'Matchar konton mot BAS-kontoplanen...', progress: 85 },
] as const
interface SIEUploadStepProps {
onFileSelect: (file: File) => void
isLoading: boolean
error: string | null
errorType?: 'duplicate' | 'duplicate_period' | 'validation' | 'parse'
}
export default function SIEUploadStep({ onFileSelect, isLoading, error, errorType }: SIEUploadStepProps) {
const [isDragging, setIsDragging] = useState(false)
const [selectedFile, setSelectedFile] = useState<File | null>(null)
const [loadingPhase, setLoadingPhase] = useState(0)
// Cycle through loading phases on timers
useEffect(() => {
if (!isLoading) {
setLoadingPhase(0)
return
}
const timers = [
setTimeout(() => setLoadingPhase(1), 2000),
setTimeout(() => setLoadingPhase(2), 4000),
setTimeout(() => setLoadingPhase(3), 7000),
]
return () => timers.forEach(clearTimeout)
}, [isLoading])
const handleDragOver = useCallback((e: React.DragEvent) => {
e.preventDefault()
setIsDragging(true)
}, [])
const handleDragLeave = useCallback((e: React.DragEvent) => {
e.preventDefault()
setIsDragging(false)
}, [])
const handleDrop = useCallback((e: React.DragEvent) => {
e.preventDefault()
setIsDragging(false)
const files = e.dataTransfer.files
if (files.length > 0) {
const file = files[0]
if (file.name.toLowerCase().endsWith('.sie') || file.name.toLowerCase().endsWith('.se')) {
setSelectedFile(file)
onFileSelect(file)
}
}
}, [onFileSelect])
const handleFileInput = useCallback((e: React.ChangeEvent<HTMLInputElement>) => {
const files = e.target.files
if (files && files.length > 0) {
setSelectedFile(files[0])
onFileSelect(files[0])
}
}, [onFileSelect])
const phase = LOADING_PHASES[loadingPhase]
// Full-card takeover while analyzing the file
if (isLoading && selectedFile) {
return (
<div className="space-y-6">
<Card>
<CardContent className="pt-8 pb-8">
<div className="flex flex-col items-center text-center space-y-6">
<Loader2 className="h-10 w-10 text-primary animate-spin" />
<div className="space-y-1">
<p className="font-medium text-lg">{phase.message}</p>
<p className="text-sm text-muted-foreground">
{selectedFile.name} ({(selectedFile.size / 1024).toFixed(1)} KB)
</p>
</div>
<Progress value={phase.progress} className="w-64 mx-auto transition-all duration-1000" />
<p className="text-xs text-muted-foreground">
Lämna inte sidan förrän analysen är klar
</p>
</div>
</CardContent>
</Card>
</div>
)
}
return (
<div className="space-y-6">
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Upload className="h-5 w-5" />
Ladda upp SIE-fil
</CardTitle>
<CardDescription>
Exportera en SIE4-fil från ditt nuvarande bokföringssystem (Fortnox, Visma, etc.)
och ladda upp den här för att importera din bokföring.
</CardDescription>
</CardHeader>
<CardContent>
{/* Drop zone */}
<div
className={`
relative border-2 border-dashed rounded-lg p-8 text-center transition-colors cursor-pointer hover:border-primary/50
${isDragging ? 'border-primary bg-primary/5' : 'border-muted-foreground/25'}
${error ? 'border-destructive bg-destructive/5' : ''}
`}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
onClick={() => document.getElementById('file-input')?.click()}
>
<input
id="file-input"
type="file"
accept=".sie,.se"
className="hidden"
onChange={handleFileInput}
disabled={isLoading}
/>
{selectedFile ? (
<div className="space-y-4">
<CheckCircle className="mx-auto h-12 w-12 text-success" />
<div>
<p className="font-medium">{selectedFile.name}</p>
<p className="text-sm text-muted-foreground">
{(selectedFile.size / 1024).toFixed(1)} KB
</p>
</div>
</div>
) : (
<div className="space-y-4">
<Upload className="mx-auto h-12 w-12 text-muted-foreground" />
<div>
<p className="font-medium hidden sm:block">Dra och släpp SIE-fil här</p>
<p className="font-medium sm:hidden">Tryck för att välja SIE-fil</p>
<p className="text-sm text-muted-foreground hidden sm:block">eller klicka för att välja fil</p>
<p className="text-sm text-muted-foreground sm:hidden">.sie eller .se-filer</p>
</div>
</div>
)}
</div>
{/* Error display */}
{error && (
<div className="mt-4 p-4 bg-destructive/10 border border-destructive/20 rounded-lg flex gap-3">
<AlertCircle className="h-5 w-5 text-destructive flex-shrink-0 mt-0.5" />
<div>
<p className="font-medium text-destructive">
{errorType === 'duplicate' || errorType === 'duplicate_period'
? 'Filen har redan importerats'
: 'Kunde inte läsa filen'}
</p>
<p className="text-sm text-muted-foreground">{error}</p>
</div>
</div>
)}
</CardContent>
</Card>
{/* Info cards */}
<div className="grid gap-4 md:grid-cols-2">
<Card>
<CardHeader>
<CardTitle className="text-base">Vad är SIE?</CardTitle>
</CardHeader>
<CardContent className="text-sm text-muted-foreground">
<p>
SIE (Standard Import Export) är det svenska standardformatet för att överföra
bokföringsdata mellan system. Det används av alla större bokföringsprogram i Sverige.
</p>
</CardContent>
</Card>
<Card>
<CardHeader>
<CardTitle className="text-base">Vilken SIE-typ?</CardTitle>
</CardHeader>
<CardContent className="text-sm text-muted-foreground">
<ul className="space-y-1">
<li><strong>SIE4</strong> - Full historik med alla verifikationer (rekommenderas)</li>
<li><strong>SIE1</strong> - Endast årssaldon (enklare import)</li>
</ul>
</CardContent>
</Card>
</div>
{/* Export instructions */}
<Card className="bg-muted/50">
<CardHeader>
<CardTitle className="text-base"> exporterar du från...</CardTitle>
</CardHeader>
<CardContent className="text-sm space-y-3">
<div>
<p className="font-medium">Fortnox</p>
<p className="text-muted-foreground">Inställningar Import/Export Exportera SIE</p>
</div>
<div>
<p className="font-medium">Visma eEkonomi</p>
<p className="text-muted-foreground">Rapporter Övrigt Exportera till SIE</p>
</div>
<div>
<p className="font-medium">Bokio</p>
<p className="text-muted-foreground">Inställningar Bokföring Exportera SIE-fil</p>
</div>
</CardContent>
</Card>
</div>
)
}