|
| 1 | +"use client" |
| 2 | + |
| 3 | +import { useEffect, useRef, useState } from "react" |
| 4 | +import { useI18n } from "@/contexts/i18n-context" |
| 5 | +import * as XLSX from "xlsx" |
| 6 | + |
| 7 | +interface ExcelPreviewRendererProps { |
| 8 | + base64Content: string |
| 9 | +} |
| 10 | + |
| 11 | +export function ExcelPreviewRenderer({ base64Content }: ExcelPreviewRendererProps) { |
| 12 | + const containerRef = useRef<HTMLDivElement | null>(null) |
| 13 | + const [error, setError] = useState<string | null>(null) |
| 14 | + const [activeSheet, setActiveSheet] = useState<string | null>(null) |
| 15 | + const [sheets, setSheets] = useState<{ [key: string]: string }>({}) |
| 16 | + const { t } = useI18n() |
| 17 | + |
| 18 | + useEffect(() => { |
| 19 | + const render = async () => { |
| 20 | + if (!base64Content) { |
| 21 | + return |
| 22 | + } |
| 23 | + |
| 24 | + try { |
| 25 | + let workbook; |
| 26 | + |
| 27 | + // Check if content is likely base64 |
| 28 | + const isBase64 = /^[A-Za-z0-9+/=]+$/.test(base64Content.replace(/\s/g, '')); |
| 29 | + |
| 30 | + if (isBase64) { |
| 31 | + try { |
| 32 | + const binary = atob(base64Content) |
| 33 | + const bytes = new Uint8Array(binary.length) |
| 34 | + for (let i = 0; i < binary.length; i++) { |
| 35 | + bytes[i] = binary.charCodeAt(i) |
| 36 | + } |
| 37 | + workbook = XLSX.read(bytes, { type: "array" }) |
| 38 | + } catch (e) { |
| 39 | + // Fallback for non-base64 text |
| 40 | + workbook = XLSX.read(base64Content, { type: "string" }) |
| 41 | + } |
| 42 | + } else { |
| 43 | + workbook = XLSX.read(base64Content, { type: "string" }) |
| 44 | + } |
| 45 | + |
| 46 | + const sheetData: { [key: string]: string } = {} |
| 47 | + |
| 48 | + workbook.SheetNames.forEach((sheetName) => { |
| 49 | + const worksheet = workbook.Sheets[sheetName] |
| 50 | + const html = XLSX.utils.sheet_to_html(worksheet) |
| 51 | + sheetData[sheetName] = html |
| 52 | + }) |
| 53 | + |
| 54 | + setSheets(sheetData) |
| 55 | + if (workbook.SheetNames.length > 0) { |
| 56 | + setActiveSheet(workbook.SheetNames[0]) |
| 57 | + } |
| 58 | + setError(null) |
| 59 | + } catch (e) { |
| 60 | + console.error(e) |
| 61 | + setError(t("files.previewDialog.errors.excelRenderFailed") || "Failed to render Excel file") |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + render() |
| 66 | + }, [base64Content, t]) |
| 67 | + |
| 68 | + if (error) { |
| 69 | + return <div className="p-4 text-sm text-destructive">{error}</div> |
| 70 | + } |
| 71 | + |
| 72 | + if (!activeSheet || !sheets[activeSheet]) { |
| 73 | + return null |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <div className="flex flex-col h-full overflow-hidden bg-background"> |
| 78 | + {Object.keys(sheets).length > 1 && ( |
| 79 | + <div className="flex border-b overflow-x-auto bg-muted/30 p-2 gap-2 flex-shrink-0"> |
| 80 | + {Object.keys(sheets).map((sheetName) => ( |
| 81 | + <button |
| 82 | + key={sheetName} |
| 83 | + onClick={() => setActiveSheet(sheetName)} |
| 84 | + className={`px-3 py-1.5 text-sm rounded-md transition-colors whitespace-nowrap ${activeSheet === sheetName |
| 85 | + ? "bg-background shadow-sm border border-border font-medium" |
| 86 | + : "text-muted-foreground hover:bg-muted" |
| 87 | + }`} |
| 88 | + > |
| 89 | + {sheetName} |
| 90 | + </button> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | + )} |
| 94 | + |
| 95 | + <div |
| 96 | + className="flex-1 overflow-auto p-4 excel-preview-container" |
| 97 | + ref={containerRef} |
| 98 | + > |
| 99 | + <div |
| 100 | + className="bg-background rounded-md shadow-sm border min-w-max" |
| 101 | + dangerouslySetInnerHTML={{ __html: sheets[activeSheet] }} |
| 102 | + /> |
| 103 | + </div> |
| 104 | + |
| 105 | + <style dangerouslySetInnerHTML={{ |
| 106 | + __html: ` |
| 107 | + .excel-preview-container table { |
| 108 | + border-collapse: collapse; |
| 109 | + width: 100%; |
| 110 | + } |
| 111 | + .excel-preview-container th, |
| 112 | + .excel-preview-container td { |
| 113 | + border: 1px solid hsl(var(--border)); |
| 114 | + padding: 8px; |
| 115 | + min-width: 80px; |
| 116 | + text-align: left; |
| 117 | + } |
| 118 | + .excel-preview-container td[data-t="n"] { |
| 119 | + text-align: right; |
| 120 | + } |
| 121 | + .excel-preview-container tr:nth-child(even) { |
| 122 | + background-color: hsl(var(--muted) / 0.3); |
| 123 | + } |
| 124 | + `}} /> |
| 125 | + </div> |
| 126 | + ) |
| 127 | +} |
0 commit comments