feat: Add link column to CSV file for org members (#17695)
* improve spacing * feat: Add link column to CSV file for org members * update tests
This commit is contained in:
1 parent
fc1d553b74
commit
f9afca2939
3 files changed
+96
-62
No files matched your search
@@ -15,7 +15,11 @@ import { useMemo, useReducer, useRef, useState } from "react";
|
||||
|
||||
import { useOrgBranding } from "@calcom/features/ee/organizations/context/provider";
|
||||
import { WEBAPP_URL } from "@calcom/lib/constants";
|
||||
import { downloadAsCsv, generateCsvRaw, generateHeaderFromReactTable } from "@calcom/lib/csvUtils";
|
||||
import {
|
||||
downloadAsCsv,
|
||||
generateCsvRawForMembersTable,
|
||||
generateHeaderFromReactTable,
|
||||
} from "@calcom/lib/csvUtils";
|
||||
import { getUserAvatarUrl } from "@calcom/lib/getAvatarUrl";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc";
|
||||
@@ -418,9 +422,11 @@ export function UserListTable() {
|
||||
|
||||
const handleDownload = async () => {
|
||||
try {
|
||||
if (!org?.slug || !org?.name) {
|
||||
throw new Error("Org slug or name is missing.");
|
||||
}
|
||||
setIsDownloading(true);
|
||||
const HEADER_IDS_TO_EXCLUDE = ["select", "actions"];
|
||||
const headers = generateHeaderFromReactTable(table, HEADER_IDS_TO_EXCLUDE);
|
||||
const headers = generateHeaderFromReactTable(table);
|
||||
if (!headers || !headers.length) {
|
||||
throw new Error("Header is missing.");
|
||||
}
|
||||
@@ -443,12 +449,12 @@ export function UserListTable() {
|
||||
}
|
||||
|
||||
const ATTRIBUTE_IDS = attributes?.map((attr) => attr.id) ?? [];
|
||||
const csvRaw = generateCsvRaw(headers, allMembers as UserTableUser[], ATTRIBUTE_IDS);
|
||||
const csvRaw = generateCsvRawForMembersTable(headers, allMembers as UserTableUser[], ATTRIBUTE_IDS);
|
||||
if (!csvRaw) {
|
||||
throw new Error("Generating CSV file failed.");
|
||||
}
|
||||
|
||||
const filename = `${org?.name ?? "Org"}_${new Date().toISOString().split("T")[0]}.csv`;
|
||||
const filename = `${org.name}_${new Date().toISOString().split("T")[0]}.csv`;
|
||||
downloadAsCsv(csvRaw, filename);
|
||||
} catch (error) {
|
||||
showToast(`Error: ${error}`, "error");
|
||||
@@ -475,7 +481,7 @@ export function UserListTable() {
|
||||
className="sm:max-w-64 max-w-full"
|
||||
/>
|
||||
</div>
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<div className="flex flex-wrap items-center justify-between gap-2">
|
||||
<DataTableToolbar.CTA
|
||||
type="button"
|
||||
color="secondary"
|
||||
|
||||
+37
-18
@@ -62,19 +62,15 @@ export const sanitizeValue = (value: string) => {
|
||||
return value;
|
||||
};
|
||||
|
||||
export const generateHeaderFromReactTable = (
|
||||
table: Table<UserTableUser>,
|
||||
HEADER_IDS_TO_EXCLUDE?: string[]
|
||||
): string[] | null => {
|
||||
export const generateHeaderFromReactTable = (table: Table<UserTableUser>): string[] | null => {
|
||||
const headerGroups = table.getHeaderGroups();
|
||||
if (!headerGroups.length) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { headers } = headerGroups[0];
|
||||
const filteredHeaders = HEADER_IDS_TO_EXCLUDE
|
||||
? headers.filter((header) => !HEADER_IDS_TO_EXCLUDE.includes(header.id))
|
||||
: headers;
|
||||
const HEADER_IDS_TO_EXCLUDE = ["select", "actions"]; // these columns only make sense in web page
|
||||
const filteredHeaders = headers.filter((header) => !HEADER_IDS_TO_EXCLUDE.includes(header.id));
|
||||
const headerNames = filteredHeaders.map((header) => {
|
||||
const h = header.column.columnDef.header;
|
||||
if (typeof h === "string") {
|
||||
@@ -86,20 +82,38 @@ export const generateHeaderFromReactTable = (
|
||||
return "Unknown";
|
||||
});
|
||||
|
||||
// Add "Link" column (member's public page)
|
||||
const MEMBERS_COLUMN = "Members";
|
||||
const LINK_COLUMN = "Link";
|
||||
const memberIndex = headerNames.findIndex((name) => name === MEMBERS_COLUMN);
|
||||
if (memberIndex > -1) {
|
||||
headerNames.splice(memberIndex + 1, 0, LINK_COLUMN);
|
||||
}
|
||||
|
||||
return headerNames;
|
||||
};
|
||||
|
||||
export const generateCsvRaw = (
|
||||
export const generateCsvRawForMembersTable = (
|
||||
headers: string[],
|
||||
rows: UserTableUser[],
|
||||
ATTRIBUTE_IDS: string[]
|
||||
): string | null => {
|
||||
): string => {
|
||||
if (!headers.length) {
|
||||
return null;
|
||||
throw new Error("The header is empty.");
|
||||
}
|
||||
|
||||
const REQUIRED_HEADERS = ["Members", "Link", "Role", "Teams"] as const;
|
||||
// Validate required headers are present and in correct order
|
||||
const firstFourHeaders = headers.slice(0, REQUIRED_HEADERS.length);
|
||||
if (!REQUIRED_HEADERS.every((header, index) => header === firstFourHeaders[index])) {
|
||||
throw new Error(
|
||||
`Invalid headers structure. Expected headers to start with: ${JSON.stringify(REQUIRED_HEADERS)}`
|
||||
);
|
||||
}
|
||||
|
||||
// Body formation
|
||||
const csvRows = rows.map((row) => {
|
||||
const { email, role, teams, attributes } = row;
|
||||
const { email, role, teams, username, attributes } = row;
|
||||
|
||||
// Create a map of attributeId to array of values
|
||||
const attributeMap = attributes.reduce((acc, attr) => {
|
||||
@@ -110,14 +124,19 @@ export const generateCsvRaw = (
|
||||
return acc;
|
||||
}, {} as Record<string, string[]>);
|
||||
|
||||
return [
|
||||
email,
|
||||
role,
|
||||
sanitizeValue(teams.map((team) => team.name).join(",")),
|
||||
...ATTRIBUTE_IDS.map((attrId) => {
|
||||
return attributeMap[attrId] ? sanitizeValue(attributeMap[attrId].join(",")) : "";
|
||||
}),
|
||||
const requiredColumns = [
|
||||
email, // Members column
|
||||
`${window.location.origin}/${username}`, // Link column
|
||||
role, // Role column
|
||||
sanitizeValue(teams.map((team) => team.name).join(",")), // Teams column
|
||||
];
|
||||
|
||||
// Add attribute columns
|
||||
const attributeColumns = ATTRIBUTE_IDS.map((attrId) =>
|
||||
attributeMap[attrId] ? sanitizeValue(attributeMap[attrId].join(",")) : ""
|
||||
);
|
||||
|
||||
return [...requiredColumns, ...attributeColumns];
|
||||
});
|
||||
|
||||
return [headers.join(","), ...csvRows.map((row) => row.join(","))].join("\n");
|
||||
|
||||
Reference in new issue
Block a user