import { useState, useEffect } from "react"; import { useQuery } from "@tanstack/react-query"; import axios from "axios"; import * as XLSX from "xlsx"; type Props = { url?: string; }; export function ExcelPreview({ url }: Props) { const [sheets, setSheets] = useState<{ name: string; data: any[][] }[]>([]); const [activeSheet, setActiveSheet] = useState(0); const { data, isLoading, error, isError } = useQuery({ queryKey: ["excel", url], queryFn: () => axios.get(url!, { responseType: "arraybuffer" }), enabled: !!url, }); useEffect(() => { if (data?.data) { try { const workbook = XLSX.read(data.data, { type: "array" }); const sheetData = workbook.SheetNames.map((name) => { const worksheet = workbook.Sheets[name]; const jsonData = XLSX.utils.sheet_to_json(worksheet, { header: 1 }); return { name, data: jsonData as any[][] }; }); setSheets(sheetData); } catch (err) { } } }, [data]); if (!url) { return (
| {cell !== null && cell !== undefined ? String(cell) : ""} | ))}