Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: add location info to visitors in dataroom

Marc Seitzf6146c471ef2
diff --git a/components/visitors/dataroom-visitor-useragent.tsx b/components/visitors/dataroom-visitor-useragent.tsx
new file mode 100644
index 00000000..d6f04f87
--- /dev/null
+++ b/components/visitors/dataroom-visitor-useragent.tsx
@@ -0,0 +1,17 @@
+import { useDataroomVisitorUserAgent } from "@/lib/swr/use-dataroom-stats";
+
+import VisitorUserAgentBase from "./visitor-useragent-base";
+
+export function DataroomVisitorUserAgent({ viewId }: { viewId: string }) {
+ const { userAgent, error } = useDataroomVisitorUserAgent(viewId);
+
+ if (error) {
+ return <div>No useragent info</div>;
+ }
+
+ if (!userAgent) {
+ return <div>Loading...</div>;
+ }
+
+ return <VisitorUserAgentBase userAgent={userAgent} />;
+}
diff --git a/components/visitors/dataroom-visitors-table.tsx b/components/visitors/dataroom-visitors-table.tsx
index 9cae66bc..c59fc129 100644
--- a/components/visitors/dataroom-visitors-table.tsx
+++ b/components/visitors/dataroom-visitors-table.tsx
@@ -26,6 +26,7 @@ import { BadgeTooltip } from "@/components/ui/tooltip";
import { useDataroomVisits } from "@/lib/swr/use-dataroom";
import { durationFormat, timeAgo } from "@/lib/utils";
+import { DataroomVisitorUserAgent } from "./dataroom-visitor-useragent";
import DataroomVisitHistory from "./dataroom-visitors-history";
import { VisitorAvatar } from "./visitor-avatar";
@@ -155,6 +156,11 @@ export default function DataroomVisitorsTable({
<CollapsibleContent asChild>
<>
+ <TableRow>
+ <TableCell colSpan={3}>
+ <DataroomVisitorUserAgent viewId={view.id} />
+ </TableCell>
+ </TableRow>
<TableRow key={view.id}>
<TableCell>
<div className="flex items-center gap-x-4 overflow-visible">
diff --git a/components/visitors/visitor-useragent-base.tsx b/components/visitors/visitor-useragent-base.tsx
new file mode 100644
index 00000000..845cd938
--- /dev/null
+++ b/components/visitors/visitor-useragent-base.tsx
@@ -0,0 +1,52 @@
+import { COUNTRIES } from "@/lib/constants";
+
+import UAIcon from "../user-agent-icon";
+
+export default function VisitorUserAgentBase({
+ userAgent,
+}: {
+ userAgent: {
+ device: string;
+ browser: string;
+ os: string;
+ country?: string;
+ city?: string;
+ };
+}) {
+ const { device, browser, os, country, city } = userAgent;
+
+ return (
+ <div>
+ <div className="pb-0.5 pl-0.5 md:pb-1 md:pl-1">
+ {city && country ? (
+ <div className="flex items-center">
+ <div className="flex items-center gap-x-1 px-1">
+ <img
+ alt={country}
+ src={`https://flag.vercel.app/m/${country}.svg`}
+ className="h-3 w-4"
+ />
+ <span>{city},</span>
+ <span>{COUNTRIES[country]}</span>
+ </div>
+ </div>
+ ) : null}
+ </div>
+ <div className="pb-0.5 pl-0.5 md:pb-1 md:pl-1">
+ <div className="flex items-center">
+ <div className="flex items-center gap-x-1 px-1">
+ <UAIcon display={device} type="devices" className="size-4" />{" "}
+ {device},
+ </div>
+ <div className="flex items-center gap-x-1 px-1">
+ <UAIcon display={browser} type="browsers" className="size-4" />{" "}
+ {browser},
+ </div>
+ <div className="flex items-center gap-x-1 px-1">
+ <UAIcon display={os} type="os" className="size-4" /> {os}
+ </div>
+ </div>
+ </div>
+ </div>
+ );
+}
diff --git a/components/visitors/visitor-useragent.tsx b/components/visitors/visitor-useragent.tsx
index fb37e2ba..2c5d1de9 100644
--- a/components/visitors/visitor-useragent.tsx
+++ b/components/visitors/visitor-useragent.tsx
@@ -1,55 +1,17 @@
-import ErrorPage from "next/error";
-
-import { COUNTRIES } from "@/lib/constants";
import { useVisitorUserAgent } from "@/lib/swr/use-stats";
-import UAIcon from "../user-agent-icon";
+import VisitorUserAgentBase from "./visitor-useragent-base";
export default function VisitorUserAgent({ viewId }: { viewId: string }) {
const { userAgent, error } = useVisitorUserAgent(viewId);
- if (error && error.status === 404) {
- return <ErrorPage statusCode={404} />;
+ if (error) {
+ return <div>No useragent info</div>;
}
if (!userAgent) {
return <div>Loading...</div>;
}
- const { device, browser, os, country, city } = userAgent;
-
- return (
- <div>
- <div className="pb-0.5 pl-0.5 md:pb-1 md:pl-1">
- {city && country ? (
- <div className="flex items-center">
- <div className="flex items-center gap-x-1 px-1">
- <img
- alt={country}
- src={`https://flag.vercel.app/m/${country}.svg`}
- className="h-3 w-4"
- />
- <span>{city},</span>
- <span>{COUNTRIES[country]}</span>
- </div>
- </div>
- ) : null}
- </div>
- <div className="pb-0.5 pl-0.5 md:pb-1 md:pl-1">
- <div className="flex items-center">
- <div className="flex items-center gap-x-1 px-1">
- <UAIcon display={device} type="devices" className="size-4" />{" "}
- {device},
- </div>
- <div className="flex items-center gap-x-1 px-1">
- <UAIcon display={browser} type="browsers" className="size-4" />{" "}
- {browser},
- </div>
- <div className="flex items-center gap-x-1 px-1">
- <UAIcon display={os} type="os" className="size-4" /> {os}
- </div>
- </div>
- </div>
- </div>
- );
+ return <VisitorUserAgentBase userAgent={userAgent} />;
}
diff --git a/lib/swr/use-dataroom-stats.ts b/lib/swr/use-dataroom-stats.ts
index f62e0542..d657662e 100644
--- a/lib/swr/use-dataroom-stats.ts
+++ b/lib/swr/use-dataroom-stats.ts
@@ -3,6 +3,7 @@ import { useRouter } from "next/router";
import { useTeam } from "@/context/team-context";
import { View } from "@prisma/client";
import useSWR from "swr";
+import useSWRImmutable from "swr/immutable";
import { fetcher } from "@/lib/utils";
@@ -79,3 +80,32 @@ export function useDataroomStats({
// error,
// };
// }
+
+export function useDataroomVisitorUserAgent(viewId: string) {
+ const router = useRouter();
+ const teamInfo = useTeam();
+ const teamId = teamInfo?.currentTeam?.id;
+
+ const { id: dataroomId } = router.query as {
+ id: string;
+ };
+
+ const { data: userAgent, error } = useSWRImmutable<{
+ country: string;
+ city: string;
+ os: string;
+ browser: string;
+ device: string;
+ }>(
+ dataroomId &&
+ viewId &&
+ `/api/teams/${teamId}/datarooms/${dataroomId}/views/${viewId}/user-agent`,
+ fetcher,
+ );
+
+ return {
+ userAgent,
+ loading: !error && !userAgent,
+ error,
+ };
+}
diff --git a/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/user-agent.ts b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/user-agent.ts
new file mode 100644
index 00000000..bd3ffe42
--- /dev/null
+++ b/pages/api/teams/[teamId]/datarooms/[id]/views/[viewId]/user-agent.ts
@@ -0,0 +1,97 @@
+import { NextApiRequest, NextApiResponse } from "next";
+
+import { authOptions } from "@/pages/api/auth/[...nextauth]";
+import { getServerSession } from "next-auth/next";
+
+import { errorhandler } from "@/lib/errorHandler";
+import prisma from "@/lib/prisma";
+import { getViewUserAgent } from "@/lib/tinybird";
+import { CustomUser } from "@/lib/types";
+
+export default async function handle(
+ req: NextApiRequest,
+ res: NextApiResponse,
+) {
+ if (req.method === "GET") {
+ // GET /api/teams/:teamId/datarooms/:id/views/:viewId/user-agent
+ const session = await getServerSession(req, res, authOptions);
+ if (!session) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ const {
+ teamId,
+ id: dataroomId,
+ viewId,
+ } = req.query as {
+ teamId: string;
+ id: string;
+ viewId: string;
+ };
+
+ const userId = (session.user as CustomUser).id;
+
+ try {
+ const team = await prisma.team.findUnique({
+ where: {
+ id: teamId,
+ users: {
+ some: {
+ userId: userId,
+ },
+ },
+ },
+ select: {
+ id: true,
+ plan: true,
+ },
+ });
+
+ if (!team) {
+ return res.status(401).end("Unauthorized");
+ }
+
+ if (team.plan.includes("free")) {
+ return res.status(403).end("Forbidden");
+ }
+
+ const view = await prisma.view.findMany({
+ where: {
+ dataroomId: dataroomId,
+ dataroomViewId: viewId,
+ },
+ select: {
+ id: true,
+ documentId: true,
+ },
+ take: 1,
+ });
+
+ if (!view || !view[0].documentId) {
+ return res.status(404).end("Not Found");
+ }
+
+ const userAgent = await getViewUserAgent({
+ documentId: view[0].documentId,
+ viewId: view[0].id,
+ since: 0,
+ });
+
+ const userAgentData = userAgent.data[0];
+ // Include country and city for business and datarooms plans
+ if (team.plan.includes("business") || team.plan.includes("datarooms")) {
+ return res.status(200).json(userAgentData);
+ } else {
+ // For other plans, exclude country and city
+ const { country, city, ...remainingResponse } = userAgentData;
+ return res.status(200).json(remainingResponse);
+ }
+ } catch (error) {
+ errorhandler(error, res);
+ }
+ } else {
+ // We only allow GET and POST requests
+ res.setHeader("Allow", ["GET"]);
+ return res.status(405).end(`Method ${req.method} Not Allowed`);
+ }
+}