Skip to content

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

fix: free limit for document views shown properly

Marc Seitz8455c40ecfbf
diff --git a/components/documents/stats-card.tsx b/components/documents/stats-card.tsx
index 2a9425c8..8a90c38d 100644
--- a/components/documents/stats-card.tsx
+++ b/components/documents/stats-card.tsx
@@ -40,7 +40,7 @@ export default function StatsCard({
const statistics = [
{
name: "Number of visits",
- value: stats?.views.length.toString() ?? "0",
+ value: stats?.totalViews.toString() ?? "0",
active: true,
},
{
diff --git a/components/visitors/visitors-table.tsx b/components/visitors/visitors-table.tsx
index e479da20..3d0bc0b2 100644
--- a/components/visitors/visitors-table.tsx
+++ b/components/visitors/visitors-table.tsx
@@ -243,8 +243,13 @@ export default function VisitorsTable({ numPages }: { numPages: number }) {
{/* Pagination Controls */}
<div className="mt-2 w-full flex items-center">
<div className="text-sm w-full">
- Showing <span className="font-semibold">10</span> of{" "}
- {views?.totalViews} visits
+ Showing{" "}
+ <span className="font-semibold">
+ {views?.totalViews && views?.totalViews > 10
+ ? 10
+ : views?.totalViews}
+ </span>{" "}
+ of {views?.totalViews} visits
</div>
<Pagination className="justify-end">
<PaginationContent>
diff --git a/lib/swr/use-stats.ts b/lib/swr/use-stats.ts
index fe5404e8..071200ff 100644
--- a/lib/swr/use-stats.ts
+++ b/lib/swr/use-stats.ts
@@ -11,6 +11,7 @@ export type TStatsData = {
data: { versionNumber: number; pageNumber: string; avg_duration: number }[];
};
total_duration: number;
+ totalViews: number;
};
export function useStats({
diff --git a/pages/api/teams/[teamId]/documents/[id]/stats.ts b/pages/api/teams/[teamId]/documents/[id]/stats.ts
index b7184b82..60079252 100644
--- a/pages/api/teams/[teamId]/documents/[id]/stats.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/stats.ts
@@ -86,6 +86,8 @@ export default async function handle(
});
}
+ const totalViews = views.length;
+
// limit the number of views to 20 on free plan
const limitedViews =
document?.team?.plan === "free" ? views.slice(0, LIMITS.views) : views;
@@ -130,6 +132,7 @@ export default async function handle(
duration,
total_duration,
groupedReactions,
+ totalViews,
};
return res.status(200).json(stats);
diff --git a/pages/api/teams/[teamId]/documents/[id]/views/index.ts b/pages/api/teams/[teamId]/documents/[id]/views/index.ts
index 20b03fb2..c343d2d8 100644
--- a/pages/api/teams/[teamId]/documents/[id]/views/index.ts
+++ b/pages/api/teams/[teamId]/documents/[id]/views/index.ts
@@ -27,6 +27,8 @@ export default async function handle(
const limit = parseInt((req.query.limit as string) || "10", 10);
const offset = (page - 1) * limit;
+ console.log("offset", offset);
+
const userId = (session.user as CustomUser).id;
try {
@@ -117,7 +119,7 @@ export default async function handle(
// filter the last 20 views
const limitedViews =
- team.plan === "free" ? views.slice(0, LIMITS.views) : views;
+ team.plan === "free" && offset >= LIMITS.views ? [] : views;
const durationsPromises = limitedViews?.map((view: { id: string }) => {
return getViewPageDuration({