feat: insights routing forms (#17583)

Co-authored-by: Joe Au-Yeung <[email protected]>
This commit is contained in:
sean-brydonandJoe Au-Yeung authored and GitHub committed 2024-11-15 14:54:51 -05:00
1 parent e85c0e1a85
commit 7ce5886b4f
34 files changed
+2762 -870

No files matched your search

+30 -3
View File
@@ -2,9 +2,12 @@ import type { Table } from "@tanstack/react-table";
import type { UserTableUser } from "@calcom/features/users/components/UserTable/types";
export const downloadAsCsv = (csvRaw: string, filename: string) => {
export const downloadAsCsv = (data: string | Record<string, any>[], filename: string) => {
// If data is an array of objects, convert it to CSV string
const csvString = typeof data === "string" ? data : objectsToCsv(data);
// Create a Blob from the text data
const blob = new Blob([csvRaw], { type: "text/plain" });
const blob = new Blob([csvString], { type: "text/plain" });
// Create an Object URL for the Blob
const url = window.URL.createObjectURL(blob);
@@ -12,7 +15,7 @@ export const downloadAsCsv = (csvRaw: string, filename: string) => {
// Create a download link
const a = document.createElement("a");
a.href = url;
a.download = filename; // Specify the filename
a.download = filename;
// Simulate a click event to trigger the download
a.click();
@@ -21,6 +24,30 @@ export const downloadAsCsv = (csvRaw: string, filename: string) => {
window.URL.revokeObjectURL(url);
};
export const objectsToCsv = (data: Record<string, any>[]): string => {
if (!data.length) return "";
// Get headers from the first object
const headers = Object.keys(data[0]);
// Create CSV rows
const csvRows = [
// Header row
headers.map((header) => sanitizeValue(header)).join(","),
// Data rows
...data.map((row) =>
headers
.map((header) => {
const value = row[header];
return sanitizeValue(value?.toString() ?? "");
})
.join(",")
),
];
return csvRows.join("\n");
};
export const sanitizeValue = (value: string) => {
// handling three cases:
// 1. quotes - we need to double quotes for CSV