Skip to content

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

refactor: cleanup stats components to only request useStats once!

Marc Seitz5fe314cf486a
diff --git a/components/documents/stats-card.tsx b/components/documents/stats-card.tsx
index e33bddb2..2a9425c8 100644
--- a/components/documents/stats-card.tsx
+++ b/components/documents/stats-card.tsx
@@ -1,10 +1,14 @@
-import { useStats } from "@/lib/swr/use-stats";
+import { TStatsData } from "@/lib/swr/use-stats";
import ErrorPage from "next/error";
import StatsElement from "./stats-element";
import { Skeleton } from "@/components/ui/skeleton";
-export default function StatsCard() {
- const { stats, loading, error } = useStats();
+export default function StatsCard({
+ statsData,
+}: {
+ statsData: { stats: TStatsData | undefined; loading: boolean; error: any };
+}) {
+ const { stats, loading, error } = statsData;
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
diff --git a/components/documents/stats-chart.tsx b/components/documents/stats-chart.tsx
index 56d1cb4d..b503e150 100644
--- a/components/documents/stats-chart.tsx
+++ b/components/documents/stats-chart.tsx
@@ -1,4 +1,4 @@
-import { useStats } from "@/lib/swr/use-stats";
+import { TStatsData } from "@/lib/swr/use-stats";
import ErrorPage from "next/error";
import BarChartComponent from "../charts/bar-chart";
import StatsChartSkeleton from "./stats-chart-skeleton";
@@ -6,12 +6,14 @@ import StatsChartDummy from "./stats-chart-dummy";
export default function StatsChart({
documentId,
+ statsData,
totalPagesMax = 0,
}: {
documentId: string;
+ statsData: { stats: TStatsData | undefined; loading: boolean; error: any };
totalPagesMax?: number;
}) {
- const { stats, loading, error } = useStats();
+ const { stats, loading, error } = statsData;
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
diff --git a/components/documents/stats.tsx b/components/documents/stats.tsx
new file mode 100644
index 00000000..191fd1c8
--- /dev/null
+++ b/components/documents/stats.tsx
@@ -0,0 +1,46 @@
+import { useStats } from "@/lib/swr/use-stats";
+import StatsCard from "./stats-card";
+import StatsChart from "./stats-chart";
+import { useState } from "react";
+import { Switch } from "@/components/ui/switch";
+import { Label } from "@/components/ui/label";
+
+export const StatsComponent = ({
+ documentId,
+ numPages,
+}: {
+ documentId: string;
+ numPages: number;
+}) => {
+ const [excludeTeamMembers, setExcludeTeamMembers] = useState<boolean>(false);
+ const statsData = useStats({ excludeTeamMembers });
+
+ return (
+ <>
+ <div className="flex items-center justify-end space-x-2">
+ <Switch
+ disabled={statsData.loading || statsData.error}
+ id="toggle-stats"
+ checked={excludeTeamMembers}
+ onCheckedChange={setExcludeTeamMembers}
+ />
+ <Label
+ htmlFor="toggle-stats"
+ className={excludeTeamMembers ? "" : "text-muted-foreground"}
+ >
+ Exclude internal visits
+ </Label>
+ </div>
+
+ {/* Stats Chart */}
+ <StatsChart
+ documentId={documentId}
+ totalPagesMax={numPages}
+ statsData={statsData}
+ />
+
+ {/* Stats Card */}
+ <StatsCard statsData={statsData} />
+ </>
+ );
+};
diff --git a/pages/documents/[id]/index.tsx b/pages/documents/[id]/index.tsx
index 792f4163..b19fa774 100644
--- a/pages/documents/[id]/index.tsx
+++ b/pages/documents/[id]/index.tsx
@@ -1,8 +1,6 @@
import { getExtension } from "@/lib/utils";
import { useDocument } from "@/lib/swr/use-document";
import ErrorPage from "next/error";
-import StatsCard from "@/components/documents/stats-card";
-import StatsChart from "@/components/documents/stats-chart";
import AppLayout from "@/components/layouts/app";
import LinkSheet from "@/components/links/link-sheet";
import Image from "next/image";
@@ -31,7 +29,10 @@ import PapermarkSparkle from "@/components/shared/icons/papermark-sparkle";
import { Document } from "@prisma/client";
import { usePlausible } from "next-plausible";
import { mutate } from "swr";
-import { TrashIcon, Sparkles, LinkIcon } from "lucide-react";
+import { TrashIcon, Sparkles } from "lucide-react";
+import { StatsComponent } from "@/components/documents/stats";
+import { Switch } from "@/components/ui/switch";
+import { Label } from "@/components/ui/label";
export default function DocumentPage() {
const { document: prismaDocument, primaryVersion, error } = useDocument();
@@ -41,6 +42,7 @@ export default function DocumentPage() {
const [isEditingName, setIsEditingName] = useState<boolean>(false);
const [isFirstClick, setIsFirstClick] = useState<boolean>(false);
const [menuOpen, setMenuOpen] = useState<boolean>(false);
+ const [excludeTeamMembers, setExcludeTeamMembers] = useState<boolean>(false);
const nameRef = useRef<HTMLHeadingElement>(null);
const enterPressedRef = useRef<boolean>(false);
@@ -393,15 +395,10 @@ export default function DocumentPage() {
</header>
{/* Stats */}
- {prismaDocument.numPages !== null && (
- <StatsChart
- documentId={prismaDocument.id}
- totalPagesMax={primaryVersion.numPages!}
- />
- )}
-
- {/* Stats Card */}
- <StatsCard />
+ <StatsComponent
+ documentId={prismaDocument.id}
+ numPages={primaryVersion.numPages!}
+ />
{/* Links */}
<LinksTable primaryVersion={primaryVersion} />