// سجل المشاريع — Saved-project history for a module tool (programme / cashflow). // Lists the user's saved projects and loads one back into the tool on click. import React, { useEffect, useState } from 'react'; import { History, Trash2, Loader2, Download } from 'lucide-react'; export default function ModuleHistory({ feature, token, isArabic, onLoad, refreshKey, onExport }) { const t = (en, ar) => (isArabic ? ar : en); const [items, setItems] = useState([]); const [loading, setLoading] = useState(false); const [open, setOpen] = useState(false); const fetchList = async () => { setLoading(true); try { const res = await fetch(`/api/modules/${feature}`, { headers: { Authorization: `Bearer ${token}` } }); if (res.ok) setItems(await res.json()); } catch { /* ignore */ } finally { setLoading(false); } }; useEffect(() => { fetchList(); /* eslint-disable-next-line */ }, [feature, token, refreshKey]); const load = async (id) => { try { const res = await fetch(`/api/modules/${feature}/item/${id}`, { headers: { Authorization: `Bearer ${token}` } }); if (res.ok) { const data = await res.json(); onLoad({ id: data.id, config: data.config }); } } catch { /* ignore */ } }; const remove = async (id, e) => { e.stopPropagation(); try { await fetch(`/api/modules/${feature}/item/${id}`, { method: 'DELETE', headers: { Authorization: `Bearer ${token}` } }); fetchList(); } catch { /* ignore */ } }; if (!items.length && !loading) return null; return (
{open && (
{loading && } {items.map((it) => (
{it.name} {it.date} {onExport && ( { e.stopPropagation(); try { const res = await fetch(`/api/modules/${feature}/item/${it.id}`, { headers: { Authorization: `Bearer ${token}` } }); if (res.ok) { const data = await res.json(); onExport({ id: data.id, config: data.config }); } } catch { /* ignore */ } }} title={isArabic ? 'تنزيل التقرير (Excel)' : 'Download Report (Excel)'} /> )} remove(it.id, e)} />
))}
)}
); }