fixing remote assessment modal

This commit is contained in:
Khalim Conn-Kowlessar 2025-05-06 20:10:21 +01:00
parent 45551fa5db
commit 978aed6397

View file

@ -332,16 +332,29 @@ function generateS3Keys(userId: string, portfolioId: string) {
type GenericObject = Record<string, any>;
const convertToCSV = <T extends GenericObject>(data: T[]): string => {
// Get headers (keys from the first object)
const convertToCSV = <T extends Record<string, any>>(data: T[]): string => {
if (data.length === 0) return "";
const headers = Object.keys(data[0]) as (keyof T)[];
// Create CSV rows
const escape = (value: any): string => {
if (value == null) return "";
const str = String(value);
// Check if field contains special characters
if (/[",\n]/.test(str)) {
// Escape double quotes and wrap in quotes
return `"${str.replace(/"/g, '""')}"`;
}
return str;
};
const rows = data.map((row) =>
headers.map((header) => row[header]).join(",")
headers.map((header) => escape(row[header])).join(",")
);
// Combine headers and rows into CSV string
return [headers.join(","), ...rows].join("\n");
};
@ -414,6 +427,8 @@ function useCreateRemoteAssessment({
},
];
console.log("HELLO WORLD", convertToCSV(assetList));
csvFile = new Blob([convertToCSV(assetList)], {
type: "text/csv",
});