Skip to content

Commit

Permalink
Merge pull request #1420 from mfts/feat/dataroom-location
Browse files Browse the repository at this point in the history
feat: add dataroom visits exportable
  • Loading branch information
mfts authored Dec 22, 2024
2 parents 815d767 + 207b216 commit cc5b614
Show file tree
Hide file tree
Showing 4 changed files with 425 additions and 69 deletions.
37 changes: 3 additions & 34 deletions components/documents/document-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -294,40 +294,8 @@ export default function DocumentHeader({
}
const data = await response.json();

// Converting the json Array into CSV without using parser.
const csvString = [
[
"Viewed at",
"Name",
"Email",
"Link Name",
"Total Visit Duration (s)",
"Total Document Completion (%)",
"Document version",
"Downloaded at",
"Verified",
"Agreement accepted",
"Viewed from dataroom",
],
...data.visits.map((item: any) => [
item.viewedAt,
item.viewerName,
item.viewerEmail,
item.linkName,
item.totalVisitDuration / 1000.0,
item.visitCompletion,
item.documentVersion,
item.downloadedAt,
item.verified,
item.agreement,
item.dataroom,
]),
]
.map((row) => row.join(","))
.join("\n");

// Creating csv as per the time stamp.
const blob = new Blob([csvString], { type: "text/csv;charset=utf-8;" });
// Create and download the CSV file
const blob = new Blob([data.visits], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = window.document.createElement("a");
link.href = url;
Expand All @@ -339,6 +307,7 @@ export default function DocumentHeader({
link.click();
window.document.body.removeChild(link);
URL.revokeObjectURL(url);

toast.success("CSV file downloaded successfully");
} catch (error) {
console.error("Error:", error);
Expand Down
45 changes: 44 additions & 1 deletion components/visitors/dataroom-visitors-table.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { useTeam } from "@/context/team-context";
import {
BadgeCheckIcon,
BadgeInfoIcon,
DownloadCloudIcon,
FileBadgeIcon,
MailOpenIcon,
} from "lucide-react";
import { toast } from "sonner";

import ChevronDown from "@/components/shared/icons/chevron-down";
import { Button } from "@/components/ui/button";
import {
Collapsible,
CollapsibleContent,
Expand Down Expand Up @@ -35,12 +38,52 @@ export default function DataroomVisitorsTable({
}: {
dataroomId: string;
}) {
const teamInfo = useTeam();
const teamId = teamInfo?.currentTeam?.id;
const { views } = useDataroomVisits({ dataroomId });

const exportVisitCounts = async (dataroomId: string) => {
const formattedTime = new Date().toISOString().replace(/[-:Z]/g, "");
try {
const response = await fetch(
`/api/teams/${teamId}/datarooms/${dataroomId}/export-visits`,
{ method: "GET" },
);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();

// Create blob and download
const blob = new Blob([data.visits], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.setAttribute(
"download",
`${data.dataroomName}_visits_${formattedTime}.csv`,
);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(url);

toast.success("CSV file downloaded successfully");
} catch (error) {
console.error("Error:", error);
toast.error(
"An error occurred while downloading the CSV. Please try again.",
);
}
};

return (
<div className="w-full">
<div>
<div className="flex justify-between">
<h2 className="mb-2 md:mb-4">All visitors</h2>
<Button size="sm" onClick={() => exportVisitCounts(dataroomId)}>
Export visits
</Button>
</div>
<div className="rounded-md border">
<Table>
Expand Down
Loading

0 comments on commit cc5b614

Please sign in to comment.