import { Table, TableBody, TableCell, TableHead, TableRow } from "@/components/ui/table" import { cn } from "@/lib/utils" import { ArrowDownRightIcon, ArrowUpDownIcon, ArrowUpRightIcon, BanknoteIcon, CalendarIcon, DollarSignIcon, FileTextIcon, ListIcon, ReceiptIcon } from "lucide-react" import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip" import _ from "@/lib/translate" import { GetStatementDetailsResponse } from "../import_utils" import { useMemo } from "react" import { BankStatementImportLogColumnMap } from "@/types/Accounts/BankStatementImportLogColumnMap" const CSVRawDataPreview = ({ data }: { data: GetStatementDetailsResponse }) => { const column_mapping: Record = useMemo(() => { const col_map: Record = {} data.doc.column_mapping?.forEach(col => { if (col.maps_to && col.maps_to !== "Do not import") { col_map[col.maps_to] = col.index; } }) return col_map }, [data]) const validColumns = Object.values(column_mapping) // Reverse the column mapping to get a map of column index to variable name const columnIndexMap: Record = Object.fromEntries(Object.entries(column_mapping).map(([variable, columnIndex]) => [columnIndex, variable as StandardColumnTypes])) // Loop over the contents of the CSV file and show a preview - highlight the header row and the transaction rows return ( {data.raw_data.map((row, index) => { const isHeaderRow = index === data.doc.detected_header_index; const isTransactionRow = index >= (data.doc.detected_transaction_starting_index ?? 0) && index <= (data.doc.detected_transaction_ending_index ?? 0); return {isHeaderRow ? {index + 1} : {index + 1} } {row.map((cell, cellIndex) => { const isValidColumn = validColumns.includes(cellIndex); const columnType = columnIndexMap[cellIndex]; const isAmountColumn = ["Amount", "Withdrawal", "Deposit", "Balance"].includes(columnType); if (isHeaderRow) { return
{columnType && {_(columnType)} } {cell}
} else { return
{cell}
} } )}
})}
) } type StandardColumnTypes = BankStatementImportLogColumnMap['maps_to']; const ColumnHeaderIcon = ({ columnType }: { columnType?: StandardColumnTypes }) => { if (!columnType) { return null } if (columnType === 'Amount') { return } if (columnType === 'Withdrawal') { return } if (columnType === 'Deposit') { return } if (columnType === 'Balance') { return } if (columnType === 'Date') { return } if (columnType === 'Description') { return } if (columnType === 'Reference') { return } if (columnType === 'Transaction Type') { return } if (columnType === 'Debit/Credit') { return } return null } export default CSVRawDataPreview