Skip to content

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

feat: add limits to documents and links

Marc Seitz148531ede136
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsx
index a04dcadd..d494397b 100644
--- a/components/Sidebar.tsx
+++ b/components/Sidebar.tsx
@@ -17,7 +17,8 @@ import MenuIcon from "@/components/shared/icons/menu";
import { Sheet, SheetContent, SheetTrigger } from "@/components/ui/sheet";
import { usePlan } from "@/lib/swr/use-billing";
-import { cn } from "@/lib/utils";
+import useLimits from "@/lib/swr/use-limits";
+import { cn, nFormatter } from "@/lib/utils";
import Banner from "./banner";
import ProBanner from "./billing/pro-banner";
@@ -26,6 +27,7 @@ import ProfileMenu from "./profile-menu";
import SiderbarFolders from "./sidebar-folders";
import { AddTeamModal } from "./teams/add-team-modal";
import SelectTeam from "./teams/select-team";
+import { Progress } from "./ui/progress";
import { ScrollArea } from "./ui/scroll-area";
export default function Sidebar() {
@@ -66,6 +68,9 @@ export const SidebarComponent = ({ className }: { className?: string }) => {
const { data: session, status } = useSession();
const { plan: userPlan, trial: userTrial, loading } = usePlan();
const isTrial = !!userTrial;
+ const { limits } = useLimits();
+ const linksLimit = limits?.links;
+ const documentsLimit = limits?.documents;
const router = useRouter();
const { currentTeam, teams, isLoading }: TeamContextType =
@@ -281,6 +286,25 @@ export const SidebarComponent = ({ className }: { className?: string }) => {
<ProBanner setShowProBanner={setShowProBanner} />
) : null}
+ <div className="mb-2">
+ {linksLimit ? (
+ <UsageProgress
+ title="Links"
+ unit="links"
+ usage={limits?.usage?.links}
+ usageLimit={linksLimit}
+ />
+ ) : null}
+ {documentsLimit ? (
+ <UsageProgress
+ title="Documents"
+ unit="documents"
+ usage={limits?.usage?.documents}
+ usageLimit={documentsLimit}
+ />
+ ) : null}
+ </div>
+
<div className="hidden w-full lg:block">
<ProfileMenu size="large" />
</div>
@@ -289,3 +313,50 @@ export const SidebarComponent = ({ className }: { className?: string }) => {
</div>
);
};
+
+function UsageProgress(data: {
+ title: string;
+ unit: string;
+ usage?: number;
+ usageLimit?: number;
+}) {
+ let { title, unit, usage, usageLimit } = data;
+ let usagePercentage = 0;
+ if (usage !== undefined && usageLimit !== undefined) {
+ usagePercentage = (usage / usageLimit) * 100;
+ }
+
+ return (
+ <div className="p-2">
+ {/* <div className="flex items-center space-x-2">
+ <h3 className="font-medium">{title}</h3>
+ </div> */}
+
+ <div className="mt-2 flex flex-col space-y-2">
+ {usage !== undefined && usageLimit !== undefined ? (
+ <p className="text-sm text-foreground">
+ <span
+ className={cn(
+ usagePercentage > 100 && "text-destructive dark:text-red-600",
+ )}
+ >
+ {nFormatter(usage)}
+ </span>{" "}
+ / {nFormatter(usageLimit)} {unit}{" "}
+ <span
+ className={cn(
+ usagePercentage > 100 && "text-destructive dark:text-red-600",
+ )}
+ >
+ ({usagePercentage.toFixed(0)}
+ %)
+ </span>
+ </p>
+ ) : (
+ <div className="h-5 w-32 animate-pulse rounded-md bg-muted" />
+ )}
+ <Progress value={usagePercentage} className="h-2 bg-muted" max={100} />
+ </div>
+ </div>
+ );
+}
diff --git a/components/documents/add-document-modal.tsx b/components/documents/add-document-modal.tsx
index 9bcf51f9..9e734880 100644
--- a/components/documents/add-document-modal.tsx
+++ b/components/documents/add-document-modal.tsx
@@ -28,9 +28,12 @@ import {
createNewDocumentVersion,
} from "@/lib/documents/create-document";
import { putFile } from "@/lib/files/put-file";
+import useLimits from "@/lib/swr/use-limits";
import { copyToClipboard } from "@/lib/utils";
import { getSupportedContentType } from "@/lib/utils/get-content-type";
+import { UpgradePlanModal } from "../billing/upgrade-plan-modal";
+
export function AddDocumentModal({
newVersion,
children,
@@ -52,6 +55,7 @@ export function AddDocumentModal({
const [currentFile, setCurrentFile] = useState<File | null>(null);
const [notionLink, setNotionLink] = useState<string | null>(null);
const teamInfo = useTeam();
+ const { canAddDocuments } = useLimits();
const teamId = teamInfo?.currentTeam?.id as string;
@@ -69,6 +73,11 @@ export function AddDocumentModal({
return; // prevent form from submitting
}
+ if (!canAddDocuments) {
+ toast.error("You have reached the maximum number of documents.");
+ return;
+ }
+
try {
setUploading(true);
@@ -251,6 +260,12 @@ export function AddDocumentModal({
event: FormEvent<HTMLFormElement>,
): Promise<void> => {
event.preventDefault();
+
+ if (!canUploadDocuments) {
+ toast.error("You have reached the maximum number of documents.");
+ return;
+ }
+
const validateNotionPageURL = parsePageId(notionLink);
// Check if it's a valid URL or not by Regx
const isValidURL =
@@ -353,6 +368,24 @@ export function AddDocumentModal({
setAddDocumentModalOpen && setAddDocumentModalOpen(!isOpen);
};
+ if (!canAddDocuments && children) {
+ if (newVersion) {
+ return (
+ <UpgradePlanModal
+ clickedPlan="Pro"
+ trigger={"limit_upload_document_version"}
+ >
+ {children}
+ </UpgradePlanModal>
+ );
+ }
+ return (
+ <UpgradePlanModal clickedPlan="Pro" trigger={"limit_upload_documents"}>
+ <Button>Upgrade to Add Documents</Button>
+ </UpgradePlanModal>
+ );
+ }
+
return (
<Dialog open={isOpen} onOpenChange={clearModelStates}>
<DialogTrigger asChild>{children}</DialogTrigger>
diff --git a/components/ui/progress.tsx b/components/ui/progress.tsx
index 4b26873f..03267e24 100644
--- a/components/ui/progress.tsx
+++ b/components/ui/progress.tsx
@@ -27,7 +27,10 @@ const Progress = React.forwardRef<
{...props}
>
<ProgressPrimitive.Indicator
- className="h-full w-full flex-1 bg-primary transition-all"
+ className={cn(
+ "h-full w-full flex-1 bg-primary transition-all",
+ value && value > 100 ? "bg-destructive dark:bg-red-600" : "",
+ )}
style={{ transform: `translateX(-${100 - (value || 0)}%)` }}
/>
{text && !error ? (
diff --git a/components/upload-zone.tsx b/components/upload-zone.tsx
index ecede438..a2930013 100644
--- a/components/upload-zone.tsx
+++ b/components/upload-zone.tsx
@@ -6,12 +6,14 @@ import { useTeam } from "@/context/team-context";
import { DocumentStorageType } from "@prisma/client";
import { useSession } from "next-auth/react";
import { FileRejection, useDropzone } from "react-dropzone";
+import { toast } from "sonner";
import { mutate } from "swr";
import { useAnalytics } from "@/lib/analytics";
import { DocumentData, createDocument } from "@/lib/documents/create-document";
import { resumableUpload } from "@/lib/files/tus-upload";
import { usePlan } from "@/lib/swr/use-billing";
+import useLimits from "@/lib/swr/use-limits";
import { CustomUser } from "@/lib/types";
import { cn } from "@/lib/utils";
import { getSupportedContentType } from "@/lib/utils/get-content-type";
@@ -70,6 +72,10 @@ export default function UploadZone({
const isTrial = !!trial;
const maxSize = plan === "business" || plan === "datarooms" ? 250 : 30;
const maxNumPages = plan === "business" || plan === "datarooms" ? 500 : 100;
+ const { limits, canAddDocuments } = useLimits();
+ const remainingDocuments = limits?.documents
+ ? limits?.documents - limits?.usage?.documents
+ : 0;
const [progress, setProgress] = useState<number>(0);
const [showProgress, setShowProgress] = useState(false);
@@ -77,6 +83,10 @@ export default function UploadZone({
const onDrop = useCallback(
(acceptedFiles: FileWithPath[]) => {
+ if (!canAddDocuments && acceptedFiles.length > remainingDocuments) {
+ toast.error("You have reached the maximum number of documents.");
+ return;
+ }
const newUploads = acceptedFiles.map((file) => ({
fileName: file.name,
progress: 0,
diff --git a/pages/documents/[id]/index.tsx b/pages/documents/[id]/index.tsx
index 00e9ad1e..cdceee95 100644
--- a/pages/documents/[id]/index.tsx
+++ b/pages/documents/[id]/index.tsx
@@ -4,6 +4,7 @@ import { useState } from "react";
import { useTeam } from "@/context/team-context";
+import { UpgradePlanModal } from "@/components/billing/upgrade-plan-modal";
import DocumentHeader from "@/components/documents/document-header";
import { StatsComponent } from "@/components/documents/stats";
import AppLayout from "@/components/layouts/app";
@@ -15,23 +16,47 @@ import LoadingSpinner from "@/components/ui/loading-spinner";
import VisitorsTable from "@/components/visitors/visitors-table";
import { useDocument, useDocumentLinks } from "@/lib/swr/use-document";
+import useLimits from "@/lib/swr/use-limits";
export default function DocumentPage() {
const { document: prismaDocument, primaryVersion, error } = useDocument();
-
const { links } = useDocumentLinks();
const teamInfo = useTeam();
-
+
+ const { canAddLinks } = useLimits();
+
const [isLinkSheetOpen, setIsLinkSheetOpen] = useState<boolean>(false);
if (error && error.status === 404) {
return <ErrorPage statusCode={404} />;
}
-
+
if (error && error.status === 400) {
return <ErrorPage statusCode={400} />;
}
+ const AddLinkButton = () => {
+ if (!canAddLinks) {
+ return (
+ <UpgradePlanModal clickedPlan="Pro" trigger={"limit_add_link"}>
+ <Button className="flex h-8 whitespace-nowrap text-xs lg:h-9 lg:text-sm">
+ Upgrade to Create Link
+ </Button>
+ </UpgradePlanModal>
+ );
+ } else {
+ return (
+ <Button
+ key={"create-link"}
+ className="flex h-8 whitespace-nowrap text-xs lg:h-9 lg:text-sm"
+ onClick={() => setIsLinkSheetOpen(true)}
+ >
+ Create Link
+ </Button>
+ );
+ }
+ };
+
return (
<AppLayout>
<main className="relative mx-2 mb-10 mt-4 space-y-8 overflow-hidden px-1 sm:mx-3 md:mx-5 md:mt-5 lg:mx-7 lg:mt-8 xl:mx-10">
@@ -42,15 +67,7 @@ export default function DocumentPage() {
primaryVersion={primaryVersion}
prismaDocument={prismaDocument}
teamId={teamInfo?.currentTeam?.id!}
- actions={[
- <Button
- key={"create-link"}
- className="flex h-8 whitespace-nowrap text-xs lg:h-9 lg:text-sm"
- onClick={() => setIsLinkSheetOpen(true)}
- >
- Create Link
- </Button>,
- ]}
+ actions={[<AddLinkButton />]}
/>
{/* <NavMenu
diff --git a/pages/documents/index.tsx b/pages/documents/index.tsx
index c5c4f4c1..4fc01903 100644
--- a/pages/documents/index.tsx
+++ b/pages/documents/index.tsx
@@ -1,5 +1,5 @@
import { useTeam } from "@/context/team-context";
-import { FileIcon, FolderIcon, FolderPlusIcon, PlusIcon } from "lucide-react";
+import { FolderPlusIcon, PlusIcon } from "lucide-react";
import { AddDocumentModal } from "@/components/documents/add-document-modal";
import { DocumentsList } from "@/components/documents/documents-list";