import { useState } from 'react'; import { Download, FileEdit, FileSpreadsheet, Share2, Wand2 } from 'lucide-react'; import type { ToolCallView } from '@/lib/types'; import { downloadFile, shareFile } from '@/lib/api'; import { cellValue } from '@/lib/format'; import { Button } from '@/components/ui/button'; import { useUI } from '@/stores/ui'; import { cn } from '@/lib/cn'; interface SheetPreview { name: string; rows: unknown[][]; max_row?: number; max_col?: number } interface Preview { sheet: string; rows: unknown[][]; sheets: string[]; all?: SheetPreview[] } function colLetter(i: number): string { let s = ''; let n = i; while (n >= 0) { s = String.fromCharCode(65 + (n % 26)) + s; n = Math.floor(n / 26) - 1; } return s; } export function ExcelCard({ call }: { call: ToolCallView }) { const p = call.payload as { filename?: string; file_id?: string; preview?: Preview; sheets?: { name: string; tables: number; inputs: number }[]; changes?: string[]; source?: string }; const pv = p.preview; const all: SheetPreview[] = pv?.all?.length ? pv.all : pv ? [{ name: pv.sheet, rows: pv.rows }] : []; const visible = all.filter((s) => s.name !== 'Lisez-moi').concat(all.filter((s) => s.name === 'Lisez-moi')); const [active, setActive] = useState(0); const setDraft = useUI((s) => s.setDraft); const fileId = p.file_id || call.artifacts?.[0]?.file_id; const filename = p.filename || call.artifacts?.[0]?.filename || 'classeur.xlsx'; const sheet = visible[Math.min(active, Math.max(0, visible.length - 1))]; const isInspect = call.name === 'inspect_excel'; const isEdit = call.name === 'edit_excel'; const modify = () => setDraft(`Modifie le classeur « ${filename} » : `); const ncols = sheet ? Math.max(...sheet.rows.map((r) => r.length), 1) : 0; return (
{isEdit ? : }
{filename}
{isEdit && p.source ? <>Nouvelle version de {p.source} · : null} {visible.length ? `${visible.length} feuille${visible.length > 1 ? 's' : ''}` : 'Classeur Excel'} · formules vivantes
{isEdit && p.changes?.length ? ( ) : null} {visible.length > 1 && (
{visible.map((s, i) => ( ))}
)} {sheet && sheet.rows.length > 0 && (
)} {sheet.rows.map((row, i) => ( {Array.from({ length: ncols }).map((_, j) => { const c = row[j]; const isFormula = typeof c === 'string' && c.startsWith('='); const isNum = typeof c === 'number'; return ( ); })} ))}
{Array.from({ length: ncols }).map((_, j) => {colLetter(j)}
{i + 1} {isFormula ? 'ƒ ' + String(c) : cellValue(c)}
{sheet.max_row && sheet.max_row > sheet.rows.length &&
… {sheet.max_row - sheet.rows.length} ligne(s) de plus dans le fichier
}
)} {fileId && (
{!isInspect && } {typeof navigator !== 'undefined' && !!navigator.share && !isInspect && ( )}
)}

Aperçu statique : les formules (ƒ) sont calculées à l'ouverture dans Excel, Numbers ou LibreOffice.

); }