import React, { useState, useEffect } from 'react'; import { History, RotateCcw, ChevronDown, ChevronRight, User, Calendar, Tag } from 'lucide-react'; import { getHistory, HistoryEntry } from '../lib/supabase'; import { ExcelRow, COLUMNS } from '../types'; import { cn } from '../lib/utils'; interface HistoryViewProps { headers: string[]; onRevert: (articleNo: string, oldData: ExcelRow) => void; } export function HistoryView({ headers, onRevert }: HistoryViewProps) { const [history, setHistory] = useState([]); const [loading, setLoading] = useState(true); const [expandedId, setExpandedId] = useState(null); const [search, setSearch] = useState(''); useEffect(() => { loadHistory(); }, []); const loadHistory = async () => { setLoading(true); const data = await getHistory(); setHistory(data); setLoading(false); }; const getChangedFields = (oldData: ExcelRow, newData: ExcelRow) => { const changes: { header: string; old: any; new: any; index: number }[] = []; const maxLen = Math.max(oldData.length, newData.length); for (let i = 0; i < maxLen; i++) { if (oldData[i] !== newData[i]) { changes.push({ header: headers[i] || `Col ${i}`, old: oldData[i], new: newData[i], index: i }); } } return changes; }; const formatDate = (dateStr: string) => { const date = new Date(dateStr); return new Intl.DateTimeFormat('en-GB', { day: '2-digit', month: '2-digit', year: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit' }).format(date); }; const filteredHistory = history.filter(entry => entry.article_name?.toLowerCase().includes(search.toLowerCase()) || entry.product_id.toLowerCase().includes(search.toLowerCase()) || entry.changed_by?.toLowerCase().includes(search.toLowerCase()) ); if (loading) { return (

Fetching history...

); } return (

Change History

Review and revert any changes made to products.

setSearch(e.target.value)} className="pl-10 pr-4 py-2 bg-slate-800 border border-slate-700 rounded-md text-sm text-slate-200 focus:outline-none focus:border-blue-500 transition-colors w-64" />
{filteredHistory.length === 0 ? (

No history records found.

{search ? "Try adjusting your search filters." : "Changes are recorded when you 'Save All' pending modifications."}

) : (
{filteredHistory.map((entry) => { const isExpanded = expandedId === entry.id; const changes = getChangedFields(entry.old_data, entry.new_data); return (
{/* Summary Row */}
setExpandedId(isExpanded ? null : entry.id)} > {isExpanded ? : }
{entry.product_id.slice(0, 2).toUpperCase()}
{entry.article_name}
ID: {entry.product_id}
{entry.changed_by}
{formatDate(entry.changed_at)}
{changes.length} {changes.length === 1 ? 'change' : 'changes'}
{/* Details View */} {isExpanded && (
{changes.map((change, idx) => ( ))}
Field Original Value New Value
{change.header} {String(change.old ?? '-')} {String(change.new ?? '-')}
Row Index Reference: {entry.product_id}

Reverting will move this record to 'Pending Validation' for final approval before re-syncing.

)}
); })}
)}
); }