Files
133ec073be Add CSV export for member list (#10803)
* Initial plan

* Add CSV export functionality to members page

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Align Export CSV button to the right

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Address code review feedback: improve type safety, error handling, and date formatting

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Improve CSV utility and date handling consistency

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Improve error messages and fix useCallback dependencies

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Add comprehensive tests for CSV utility

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Refactor: reduce limit to 100, replace lastActiveIp with role, extract ExportCSV component

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* Improve type safety and extract pagination constant

Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>

* refactor

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: tommoor <380914+tommoor@users.noreply.github.com>
Co-authored-by: Tom Moor <tom@getoutline.com>
2025-12-05 09:42:36 -05:00

87 lines
2.2 KiB
TypeScript

import { useCallback, useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import Button from "~/components/Button";
import { CSVHelper } from "@shared/utils/csv";
import download from "~/utils/download";
import useStores from "~/hooks/useStores";
import usePolicy from "~/hooks/usePolicy";
import useCurrentTeam from "~/hooks/useCurrentTeam";
type Props = {
/** Request parameters for filtering users */
reqParams: {
query?: string;
filter?: string;
role?: string;
sort?: string;
direction?: "ASC" | "DESC";
};
};
/**
* A button that exports all members to a CSV file.
*/
export function ExportCSV({ reqParams }: Props) {
const { t } = useTranslation();
const { users } = useStores();
const team = useCurrentTeam();
const can = usePolicy(team);
const [isExporting, setIsExporting] = useState(false);
const handleExportCSV = useCallback(async () => {
setIsExporting(true);
try {
const allUsers = await users.fetchAll({
...reqParams,
limit: 100,
});
// Convert to CSV format with formatted dates
const csvData = allUsers.map((user) => ({
id: user.id,
name: user.name,
email: user.email || "",
role: user.role,
lastActiveAt: user.lastActiveAt
? new Date(user.lastActiveAt).toISOString()
: "",
createdAt: user.createdAt ? new Date(user.createdAt).toISOString() : "",
}));
const headers: (keyof (typeof csvData)[0])[] = [
"id",
"name",
"email",
"role",
"lastActiveAt",
"createdAt",
];
const csv = CSVHelper.convertToCSV(csvData, headers);
// Trigger download
download(csv, "members.csv", "text/csv");
toast.success(t("Members exported successfully"));
} catch {
toast.error(t("Failed to export members"));
} finally {
setIsExporting(false);
}
}, [users, reqParams, t]);
if (!can.createExport) {
return null;
}
return (
<Button
type="button"
onClick={handleExportCSV}
disabled={isExporting}
neutral
>
{isExporting ? t("Exporting") + "…" : t("Download CSV")}
</Button>
);
}