Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
feat: add counter for pro trial
Marc Seitz7e7f1e4bd524
diff --git a/components/Sidebar.tsx b/components/Sidebar.tsxindex 44622163..a2023f06 100644--- a/components/Sidebar.tsx+++ b/components/Sidebar.tsx@@ -9,13 +9,15 @@ import MenuIcon from "@/components/shared/icons/menu";import ChevronUp from "@/components/shared/icons/chevron-up";import X from "@/components/shared/icons/x";import Link from "next/link";-import { cn } from "@/lib/utils";+import { cn, daysLeft } from "@/lib/utils";import { useRouter } from "next/router";import { ModeToggle } from "./theme-toggle";import { Button } from "./ui/button";+import { CustomUser } from "@/lib/types";+import LoadingSpinner from "./ui/loading-spinner";export default function Sidebar() {- const { data: session } = useSession();+ const { data: session, status } = useSession();const [sidebarOpen, setSidebarOpen] = useState<boolean>(false);const router = useRouter();@@ -50,6 +52,27 @@ export default function Sidebar() {},];+ const cutoffDate = new Date('2023-10-12T00:00:00.000Z');++ const calculateDaysLeft = (accountCreationDate: Date): number => {+ let maxDays;+ if (accountCreationDate < cutoffDate) {+ // If the user signed up before the 12th of October 2023+ maxDays = 30;+ // Override account creation date to 1st of October for correct calculation+ accountCreationDate = new Date('2023-10-01T00:00:00.000Z');+ } else {+ // If the user signed up on or after the 12th of October 2023+ maxDays = 14;+ }+ return daysLeft(accountCreationDate, maxDays);+ }++ if (status === "loading") return <LoadingSpinner className="mr-1 h-5 w-5" />;;++ const userDaysLeft = calculateDaysLeft(new Date((session?.user as CustomUser).createdAt || 0));++return (<><Transition.Root show={sidebarOpen} as={Fragment}>@@ -187,7 +210,7 @@ export default function Sidebar() {<span className="font-bold text-sm">✨ Pro Trial ✨</span></div><p className="my-4 text-sm">- You are on the Pro trial for the next 30 days.+ {`You are on the Pro trial for the next ${userDaysLeft} days.`}</p><Button><Link href="/settings/domains" target="_blank">diff --git a/lib/utils.ts b/lib/utils.tsindex 25f6293e..2602dd68 100644--- a/lib/utils.ts+++ b/lib/utils.ts@@ -229,4 +229,16 @@ export const formattedDate = (date: Date) => {export const nanoid = customAlphabet("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",7,-); // 7-character random string\ No newline at end of file+); // 7-character random string+++export const daysLeft = (accountCreationDate: Date, maxDays: number): number => {+ const now = new Date();+ const endPeriodDate = new Date(accountCreationDate);+ endPeriodDate.setDate(accountCreationDate.getDate() + maxDays);++ const diffInMilliseconds = endPeriodDate.getTime() - now.getTime();++ // Convert milliseconds to days and return+ return Math.ceil(diffInMilliseconds / (1000 * 60 * 60 * 24));+}\ No newline at end of file