"use client"; import { useState } from "react"; import { Download } from "lucide-react"; import { Button } from "@/app/components/ui/button"; import { ErrorState } from "@/app/components/ui/states"; import { exportErrorText, fetchAllForExport } from "@/app/lib/exportAll"; import { malaysiaToday } from "@/app/lib/malaysiaPeriod"; import { downloadXlsx, toXlsx } from "@/app/lib/xlsx"; import { useT } from "@/app/lib/i18n/MessagesProvider"; type Col = { key: string; title: string }; /** * Shared by the admin listings: fetches every row of the tab's current query, maps it to the * sheet and downloads `-.xlsx`. `fetchPage` must use the tab's own table query so the * file matches the screen. Returns the filter-bar button and the error line to render. */ export function useListingExport( file: string, sheetName: string, total: number, fetchPage: (page0: number, size: number) => Promise<{ rows: R[]; total: number }>, columns: Col[], toRow: (row: R) => Record, ) { const t = useT(); const [exporting, setExporting] = useState(false); const [error, setError] = useState(null); async function run() { setError(null); setExporting(true); try { const all = await fetchAllForExport(fetchPage); downloadXlsx(`${file}-${malaysiaToday()}.xlsx`, await toXlsx(all.map(toRow), columns, sheetName)); } catch (err) { setError(exportErrorText(err, t)); } finally { setExporting(false); } } const button = ( ); const errorLine = error && ; return { button, errorLine }; }