Skip to content

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

feat: add create link sheet

Marc Seitz80fa1f72eb2e
diff --git a/components/links/email-protection-section.tsx b/components/links/email-protection-section.tsx
new file mode 100644
index 00000000..28e863fd
--- /dev/null
+++ b/components/links/email-protection-section.tsx
@@ -0,0 +1,37 @@
+import { Dispatch, SetStateAction, useState } from "react";
+import { Switch } from "@/components/ui/switch";
+import { motion } from "framer-motion";
+import { Link } from "@prisma/client";
+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/contants";
+import { cn, getDateTimeLocal } from "@/lib/utils";
+
+
+
+export default function EmailProtectionSection({data, setData}: {data: Link, setData: Dispatch<SetStateAction<Link>>}) {
+ const [enabled, setEnabled] = useState(true);
+
+ function handleCheckedChange() {
+ setData({ ...data, emailProtected: enabled });
+ setEnabled(!enabled);
+ }
+ return (
+ <div className="pb-5">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between space-x-2">
+ <h2
+ className={cn(
+ "text-sm font-medium leading-6",
+ enabled ? "text-white" : "text-gray-400"
+ )}
+ >
+ Email Protection
+ </h2>
+ </div>
+ <Switch
+ checked={enabled}
+ onCheckedChange={(e) => handleCheckedChange()}
+ />
+ </div>
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/components/links/expiration-section.tsx b/components/links/expiration-section.tsx
new file mode 100644
index 00000000..a8071be7
--- /dev/null
+++ b/components/links/expiration-section.tsx
@@ -0,0 +1,50 @@
+import { Dispatch, SetStateAction, useState } from "react";
+import { Switch } from "@/components/ui/switch";
+import { motion } from "framer-motion";
+import { Link } from "@prisma/client";
+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/contants";
+import { cn, getDateTimeLocal } from "@/lib/utils";
+
+
+
+export default function ExpirationSection({data, setData}: {data: Link, setData: Dispatch<SetStateAction<Link>>}) {
+ const { expiresAt } = data;
+ const [enabled, setEnabled] = useState(!!expiresAt);
+
+ return (
+ <div className="pb-5">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between space-x-2">
+ <h2
+ className={cn(
+ "text-sm font-medium leading-6",
+ enabled ? "text-white" : "text-gray-400"
+ )}
+ >
+ Expiration Date
+ </h2>
+ </div>
+ <Switch
+ checked={enabled}
+ onCheckedChange={(checked) => setEnabled(!enabled)}
+ />
+ </div>
+ {enabled && (
+ <motion.div className="mt-3" {...FADE_IN_ANIMATION_SETTINGS}>
+ <input
+ type="datetime-local"
+ id="expiresAt"
+ name="expiresAt"
+ min={getDateTimeLocal()}
+ value={expiresAt ? getDateTimeLocal(expiresAt) : ""}
+ step="60" // need to add step to prevent weird date bug (https://stackoverflow.com/q/19284193/10639526)
+ onChange={(e) => {
+ setData({ ...data, expiresAt: new Date(e.target.value) });
+ }}
+ className="flex w-full rounded-md border-0 py-1.5 text-white bg-black shadow-sm ring-1 ring-inset ring-gray-600 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-gray-300 sm:text-sm sm:leading-6 dark:[color-scheme:dark]"
+ />
+ </motion.div>
+ )}
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/components/links/link-sheet.tsx b/components/links/link-sheet.tsx
new file mode 100644
index 00000000..6d797125
--- /dev/null
+++ b/components/links/link-sheet.tsx
@@ -0,0 +1,152 @@
+import {
+ Sheet,
+ SheetClose,
+ SheetContent,
+ SheetDescription,
+ SheetFooter,
+ SheetHeader,
+ SheetTitle,
+ SheetTrigger,
+} from "@/components/ui/sheet";
+import { Separator } from "@/components/ui/separator";
+import { useState } from "react";
+import { Link } from "@prisma/client"
+import PasswordSection from "./password-section"
+import ExpirationSection from "./expiration-section";
+import EmailProtectionSection from "./email-protection-section";
+import { useRouter } from "next/router";
+import { useDocumentLinks } from "@/lib/swr/use-document";
+import { mutate } from "swr";
+
+
+
+export default function LinkSheet({ children }: { children: React.ReactNode }) {
+ const { links } = useDocumentLinks();
+ const [data, setData] = useState<Link>({});
+ const [isLoading, setIsLoading] = useState<boolean>(false);
+
+ const router = useRouter();
+ const documentId = router.query.id as string;
+
+ const handleSubmit = async (event: any) => {
+ event.preventDefault();
+
+ setIsLoading(true);
+ const response = await fetch("/api/links", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ ...data,
+ documentId: documentId,
+ }),
+ });
+
+ if (!response.ok) {
+ throw new Error(`HTTP error! status: ${response.status}`);
+ }
+
+ const newLink = await response.json();
+
+ // Update local data with the new link
+ mutate(
+ `/api/documents/${encodeURIComponent(documentId)}/links`,
+ [...(links || []), newLink],
+ false
+ );
+
+ setIsLoading(false);
+
+ }
+
+
+
+ return (
+ <Sheet>
+ <SheetTrigger>{children}</SheetTrigger>
+ <SheetContent className="bg-black text-white flex flex-col justify-between">
+ <SheetHeader>
+ <SheetTitle className="text-white">Create a new link</SheetTitle>
+ <SheetDescription className="text-white">
+ Make changes to your profile here. Click save when you're done.
+ </SheetDescription>
+ </SheetHeader>
+ <form
+ className="flex flex-col grow"
+ onSubmit={handleSubmit}
+ >
+ <div className="h-0 flex-1">
+ <div className="flex flex-1 flex-col justify-between">
+ <div className="divide-y divide-gray-200">
+ <div className="space-y-6 pb-5 pt-6">
+ <div>
+ <label
+ htmlFor="project-name"
+ className="block text-sm font-medium leading-6 text-white"
+ >
+ Link name
+ </label>
+ <div className="mt-2">
+ <input
+ type="text"
+ name="link-name"
+ id="link-name"
+ placeholder="Recipient's Organization"
+ value={data.name ? data.name : null}
+ className="block w-full rounded-md border-0 py-1.5 text-white bg-black shadow-sm ring-1 ring-inset ring-gray-600 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-gray-300 sm:text-sm sm:leading-6"
+ onChange={(e) =>
+ setData({ ...data, name: e.target.value })
+ }
+ />
+ </div>
+ </div>
+
+ <div className="flex items-center relative">
+ <Separator className="bg-gray-500 absolute" />
+ <div className="relative mx-auto">
+ <span className="px-2 bg-black text-gray-500 text-sm">
+ Optional
+ </span>
+ </div>
+ </div>
+
+ <div>
+ <PasswordSection {...{ data, setData }} />
+ <ExpirationSection {...{ data, setData }} />
+ <EmailProtectionSection {...{ data, setData }} />
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <SheetFooter>
+ <div className="flex items-center">
+ {/* <div className="flex text-sm">
+ <a
+ href="#"
+ className="group inline-flex items-center text-gray-500 hover:text-gray-400"
+ >
+ <QuestionMarkCircleIcon
+ className="h-5 w-5 text-gray-400 group-hover:text-gray-300"
+ aria-hidden="true"
+ />
+ <span className="ml-2">Learn more about sharing</span>
+ </a>
+ </div> */}
+ <SheetClose asChild>
+ <button
+ type="submit"
+ className="ml-4 inline-flex justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-gray-950 shadow-sm hover:bg-gray-200 w-24"
+ >
+ Save
+ </button>
+ </SheetClose>
+ </div>
+ </SheetFooter>
+ </form>
+ </SheetContent>
+ </Sheet>
+ );
+}
diff --git a/components/links/password-section.tsx b/components/links/password-section.tsx
new file mode 100644
index 00000000..3a21c2cf
--- /dev/null
+++ b/components/links/password-section.tsx
@@ -0,0 +1,61 @@
+import { Dispatch, SetStateAction, useState } from "react";
+import { Switch } from "@/components/ui/switch";
+import { motion } from "framer-motion";
+import { Link } from "@prisma/client";
+import { FADE_IN_ANIMATION_SETTINGS } from "@/lib/contants";
+import Eye from "@/components/shared/icons/eye"
+import { cn } from "@/lib/utils";
+
+
+
+export default function PasswordSection({data, setData}: {data: Link, setData: Dispatch<SetStateAction<Link>>}) {
+ const { password } = data;
+ const [enabled, setEnabled] = useState(!!password);
+ const [showPassword, setShowPassword] = useState(false);
+
+ return (
+ <div className="pb-5">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center justify-between space-x-2">
+ <h2 className={cn("text-sm font-medium leading-6", enabled ? "text-white" : "text-gray-400")}>
+ Password Protection
+ </h2>
+ </div>
+ <Switch
+ checked={enabled}
+ onCheckedChange={(checked) => setEnabled(!enabled)}
+ />
+ </div>
+ {enabled && (
+ <motion.div
+ className="relative mt-4 rounded-md shadow-sm"
+ {...FADE_IN_ANIMATION_SETTINGS}
+ >
+ <input
+ name="password"
+ id="password"
+ type={showPassword ? "text" : "password"}
+ className="flex w-full rounded-md border-0 py-1.5 text-white bg-black shadow-sm ring-1 ring-inset ring-gray-600 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-gray-300 sm:text-sm sm:leading-6"
+ value={password || ""}
+ placeholder="Enter password"
+ onChange={(e) => {
+ setData({ ...data, password: e.target.value });
+ }}
+ aria-invalid="true"
+ />
+ <button
+ type="button"
+ onClick={() => setShowPassword(!showPassword)}
+ className="absolute inset-y-0 right-0 flex items-center pr-3"
+ >
+ {showPassword ? (
+ <Eye className="h-4 w-4 text-gray-400" aria-hidden="true" />
+ ) : (
+ <Eye className="h-4 w-4 text-gray-400" aria-hidden="true" />
+ )}
+ </button>
+ </motion.div>
+ )}
+ </div>
+ );
+}
\ No newline at end of file
diff --git a/components/shared/icons/eye.tsx b/components/shared/icons/eye.tsx
new file mode 100644
index 00000000..11abaf3d
--- /dev/null
+++ b/components/shared/icons/eye.tsx
@@ -0,0 +1,19 @@
+export default function Eye({ className }: { className?: string }) {
+ return (
+ <svg
+ width="14"
+ height="14"
+ viewBox="0 0 24 24"
+ fill="none"
+ stroke="currentColor"
+ stroke-width="2"
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ shapeRendering="geometricPrecision"
+ className={className}
+ >
+ <path d="M2 12s3-7 10-7 10 7 10 7-3 7-10 7-10-7-10-7Z" />
+ <circle cx="12" cy="12" r="3" />
+ </svg>
+ );
+}
\ No newline at end of file
diff --git a/components/ui/sheet.tsx b/components/ui/sheet.tsx
new file mode 100644
index 00000000..25157ea0
--- /dev/null
+++ b/components/ui/sheet.tsx
@@ -0,0 +1,144 @@
+"use client";
+
+import * as React from "react";
+import * as SheetPrimitive from "@radix-ui/react-dialog";
+import { cva, type VariantProps } from "class-variance-authority";
+import { X } from "lucide-react";
+
+import { cn } from "@/lib/utils";
+
+const Sheet = SheetPrimitive.Root;
+
+const SheetTrigger = SheetPrimitive.Trigger;
+
+const SheetClose = SheetPrimitive.Close;
+
+const SheetPortal = ({
+ className,
+ ...props
+}: SheetPrimitive.DialogPortalProps) => (
+ <SheetPrimitive.Portal className={cn(className)} {...props} />
+);
+SheetPortal.displayName = SheetPrimitive.Portal.displayName;
+
+const SheetOverlay = React.forwardRef<
+ React.ElementRef<typeof SheetPrimitive.Overlay>,
+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>
+>(({ className, ...props }, ref) => (
+ <SheetPrimitive.Overlay
+ className={cn(
+ "fixed inset-0 z-50 bg-background/80 backdrop-blur-sm data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
+ className
+ )}
+ {...props}
+ ref={ref}
+ />
+));
+SheetOverlay.displayName = SheetPrimitive.Overlay.displayName;
+
+const sheetVariants = cva(
+ "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500",
+ {
+ variants: {
+ side: {
+ top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top",
+ bottom:
+ "inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom",
+ left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-lg",
+ right:
+ "inset-y-0 right-0 h-full w-3/4 border-l border-white/10 data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-lg",
+ },
+ },
+ defaultVariants: {
+ side: "right",
+ },
+ }
+);
+
+interface SheetContentProps
+ extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,
+ VariantProps<typeof sheetVariants> {}
+
+const SheetContent = React.forwardRef<
+ React.ElementRef<typeof SheetPrimitive.Content>,
+ SheetContentProps
+>(({ side = "right", className, children, ...props }, ref) => (
+ <SheetPortal>
+ <SheetOverlay />
+ <SheetPrimitive.Content
+ ref={ref}
+ className={cn(sheetVariants({ side }), className)}
+ {...props}
+ >
+ {children}
+ <SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">
+ <X className="h-4 w-4" />
+ <span className="sr-only">Close</span>
+ </SheetPrimitive.Close>
+ </SheetPrimitive.Content>
+ </SheetPortal>
+));
+SheetContent.displayName = SheetPrimitive.Content.displayName;
+
+const SheetHeader = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) => (
+ <div
+ className={cn(
+ "flex flex-col space-y-2 text-center sm:text-left",
+ className
+ )}
+ {...props}
+ />
+);
+SheetHeader.displayName = "SheetHeader";
+
+const SheetFooter = ({
+ className,
+ ...props
+}: React.HTMLAttributes<HTMLDivElement>) => (
+ <div
+ className={cn(
+ "flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
+ className
+ )}
+ {...props}
+ />
+);
+SheetFooter.displayName = "SheetFooter";
+
+const SheetTitle = React.forwardRef<
+ React.ElementRef<typeof SheetPrimitive.Title>,
+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>
+>(({ className, ...props }, ref) => (
+ <SheetPrimitive.Title
+ ref={ref}
+ className={cn("text-lg font-semibold text-foreground", className)}
+ {...props}
+ />
+));
+SheetTitle.displayName = SheetPrimitive.Title.displayName;
+
+const SheetDescription = React.forwardRef<
+ React.ElementRef<typeof SheetPrimitive.Description>,
+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>
+>(({ className, ...props }, ref) => (
+ <SheetPrimitive.Description
+ ref={ref}
+ className={cn("text-sm text-muted-foreground", className)}
+ {...props}
+ />
+));
+SheetDescription.displayName = SheetPrimitive.Description.displayName;
+
+export {
+ Sheet,
+ SheetTrigger,
+ SheetClose,
+ SheetContent,
+ SheetHeader,
+ SheetFooter,
+ SheetTitle,
+ SheetDescription,
+};
diff --git a/components/ui/switch.tsx b/components/ui/switch.tsx
new file mode 100644
index 00000000..73eb890f
--- /dev/null
+++ b/components/ui/switch.tsx
@@ -0,0 +1,29 @@
+"use client";
+
+import * as React from "react";
+import * as SwitchPrimitives from "@radix-ui/react-switch";
+
+import { cn } from "@/lib/utils";
+
+const Switch = React.forwardRef<
+ React.ElementRef<typeof SwitchPrimitives.Root>,
+ React.ComponentPropsWithoutRef<typeof SwitchPrimitives.Root>
+>(({ className, ...props }, ref) => (
+ <SwitchPrimitives.Root
+ className={cn(
+ "peer inline-flex h-5 w-10 shrink-0 cursor-pointer items-center rounded-full border-2 border-transparent transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 focus-visible:ring-offset-background disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-gray-200 data-[state=unchecked]:bg-gray-700",
+ className
+ )}
+ {...props}
+ ref={ref}
+ >
+ <SwitchPrimitives.Thumb
+ className={cn(
+ "pointer-events-none block h-4 w-4 rounded-full bg-black shadow-lg ring-0 transition-transform data-[state=checked]:translate-x-5 data-[state=unchecked]:translate-x-0"
+ )}
+ />
+ </SwitchPrimitives.Root>
+));
+Switch.displayName = SwitchPrimitives.Root.displayName;
+
+export { Switch };
diff --git a/components/welcome/intro.tsx b/components/welcome/intro.tsx
index 738fc7de..e75274fd 100644
--- a/components/welcome/intro.tsx
+++ b/components/welcome/intro.tsx
@@ -1,10 +1,6 @@
import { useRouter } from "next/router";
import { motion } from "framer-motion";
-
-const STAGGER_CHILD_VARIANTS = {
- hidden: { opacity: 0, y: 20 },
- show: { opacity: 1, y: 0, transition: { duration: 0.4, type: "spring" } },
-};
+import { STAGGER_CHILD_VARIANTS } from "@/lib/contants"
export default function Intro() {
const router = useRouter();
diff --git a/components/welcome/next.tsx b/components/welcome/next.tsx
index 62851c5d..fda475e9 100644
--- a/components/welcome/next.tsx
+++ b/components/welcome/next.tsx
@@ -1,11 +1,7 @@
import { useRouter } from "next/router";
import { motion } from "framer-motion";
import { DocumentIcon, PresentationChartBarIcon } from "@heroicons/react/24/outline";
-
-const STAGGER_CHILD_VARIANTS = {
- hidden: { opacity: 0, y: 20 },
- show: { opacity: 1, y: 0, transition: { duration: 0.4, type: "spring" } },
-};
+import { STAGGER_CHILD_VARIANTS } from "@/lib/contants";
export default function Next() {
const router = useRouter();
diff --git a/components/welcome/upload.tsx b/components/welcome/upload.tsx
index e1689494..279b0480 100644
--- a/components/welcome/upload.tsx
+++ b/components/welcome/upload.tsx
@@ -8,11 +8,7 @@ import { ArrowRightIcon, DocumentDuplicateIcon } from "@heroicons/react/24/outli
import toast from "react-hot-toast";
import Notification from "../Notification";
import Skeleton from "../Skeleton";
-
-const STAGGER_CHILD_VARIANTS = {
- hidden: { opacity: 0, y: 20 },
- show: { opacity: 1, y: 0, transition: { duration: 0.4, type: "spring" } },
-};
+import { STAGGER_CHILD_VARIANTS } from "@/lib/contants";
export default function Upload() {
const router = useRouter();
diff --git a/lib/contants.ts b/lib/contants.ts
new file mode 100644
index 00000000..14a07fff
--- /dev/null
+++ b/lib/contants.ts
@@ -0,0 +1,11 @@
+export const FADE_IN_ANIMATION_SETTINGS = {
+ initial: { opacity: 0 },
+ animate: { opacity: 1 },
+ exit: { opacity: 0 },
+ transition: { duration: 0.2 },
+};
+
+export const STAGGER_CHILD_VARIANTS = {
+ hidden: { opacity: 0, y: 20 },
+ show: { opacity: 1, y: 0, transition: { duration: 0.4, type: "spring" } },
+};
\ No newline at end of file
diff --git a/lib/utils.ts b/lib/utils.ts
index a7e4e173..09ccf58a 100644
--- a/lib/utils.ts
+++ b/lib/utils.ts
@@ -1,6 +1,7 @@
import { clsx, type ClassValue } from "clsx";
import { twMerge } from "tailwind-merge";
import ms from "ms"
+import bcrypt from "bcryptjs";
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
@@ -143,4 +144,29 @@ export function nFormatter(num?: number, digits?: number) {
return item
? (num / item.value).toFixed(digits || 1).replace(rx, "$1") + item.symbol
: "0";
+}
+
+export const getDateTimeLocal = (timestamp?: Date): string => {
+ const d = timestamp ? new Date(timestamp) : new Date();
+ if (d.toString() === "Invalid Date") return "";
+ return new Date(d.getTime() - d.getTimezoneOffset() * 60000)
+ .toISOString()
+ .split(":")
+ .slice(0, 2)
+ .join(":");
+};
+
+
+export async function hashPassword(password: string): Promise<string> {
+ const saltRounds = 10;
+ const hashedPassword = await bcrypt.hash(password, saltRounds);
+ return hashedPassword;
+}
+
+export async function checkPassword(
+ password: string,
+ hashedPassword: string
+): Promise<boolean> {
+ const match = await bcrypt.compare(password, hashedPassword);
+ return match;
}
\ No newline at end of file
diff --git a/package-lock.json b/package-lock.json
index ea62a366..767d01a8 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -26,6 +26,7 @@
"autoprefixer": "10.4.14",
"base-x": "^4.0.0",
"class-variance-authority": "^0.6.0",
+ "bcryptjs": "^2.4.3",
"clsx": "^1.2.1",
"date-fns": "^2.30.0",
"eslint": "8.41.0",
@@ -54,6 +55,7 @@
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.3",
+ "@types/bcryptjs": "^2.4.2",
"@types/ms": "^0.7.31",
"@types/ua-parser-js": "^0.7.36",
"prisma": "^4.15.0"
@@ -1950,6 +1952,12 @@
"react-dom": ">=16.6.0"
}
},
+ "node_modules/@types/bcryptjs": {
+ "version": "2.4.2",
+ "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz",
+ "integrity": "sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==",
+ "dev": true
+ },
"node_modules/@types/d3-array": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.0.5.tgz",
@@ -2538,6 +2546,11 @@
}
]
},
+ "node_modules/bcryptjs": {
+ "version": "2.4.3",
+ "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
+ "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ=="
+ },
"node_modules/before-after-hook": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz",
@@ -10160,6 +10173,12 @@
"tailwind-merge": "^1.9.1"
}
},
+ "@types/bcryptjs": {
+ "version": "2.4.2",
+ "resolved": "https://registry.npmjs.org/@types/bcryptjs/-/bcryptjs-2.4.2.tgz",
+ "integrity": "sha512-LiMQ6EOPob/4yUL66SZzu6Yh77cbzJFYll+ZfaPiPPFswtIlA/Fs1MzdKYA7JApHU49zQTbJGX3PDmCpIdDBRQ==",
+ "dev": true
+ },
"@types/d3-array": {
"version": "3.0.5",
"resolved": "https://registry.npmjs.org/@types/d3-array/-/d3-array-3.0.5.tgz",
@@ -10588,6 +10607,11 @@
"resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz",
"integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA=="
},
+ "bcryptjs": {
+ "version": "2.4.3",
+ "resolved": "https://registry.npmjs.org/bcryptjs/-/bcryptjs-2.4.3.tgz",
+ "integrity": "sha512-V/Hy/X9Vt7f3BbPJEi8BdVFMByHi+jNXrYkW3huaybV/kQ0KJg0Y6PkEMbn+zeT+i+SiKZ/HMqJGIIt4LZDqNQ=="
+ },
"before-after-hook": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/before-after-hook/-/before-after-hook-2.2.3.tgz",
diff --git a/package.json b/package.json
index 884c93ba..06c3b827 100644
--- a/package.json
+++ b/package.json
@@ -31,6 +31,7 @@
"autoprefixer": "10.4.14",
"base-x": "^4.0.0",
"class-variance-authority": "^0.6.0",
+ "bcryptjs": "^2.4.3",
"clsx": "^1.2.1",
"date-fns": "^2.30.0",
"eslint": "8.41.0",
@@ -59,6 +60,7 @@
},
"devDependencies": {
"@tailwindcss/forms": "^0.5.3",
+ "@types/bcryptjs": "^2.4.2",
"@types/ms": "^0.7.31",
"@types/ua-parser-js": "^0.7.36",
"prisma": "^4.15.0"
diff --git a/pages/api/links/index.ts b/pages/api/links/index.ts
index d9d81950..ba37d67d 100644
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@ -2,6 +2,7 @@ import { NextApiRequest, NextApiResponse } from "next";
import prisma from "@/lib/prisma";
import { getServerSession } from "next-auth/next";
import { authOptions } from "../auth/[...nextauth]";
+import { hashPassword } from "@/lib/utils";
export default async function handler(
req: NextApiRequest,
@@ -13,12 +14,19 @@ export default async function handler(
return res.status(401).end("Unauthorized");
}
- const { documentId } = req.body;
+ const { documentId, password, expiresAt, ...linkData } = req.body;
+
+ const hashedPassword = password && password.length > 0 ? await hashPassword(password) : null
+ const exat = expiresAt ? new Date(expiresAt) : null
// Fetch the link and its related document from the database
const link = await prisma.link.create({
data: {
documentId: documentId,
+ password: hashedPassword,
+ name: linkData.name || null,
+ emailProtected: linkData.emailProtected || false,
+ expiresAt: exat
},
});
diff --git a/pages/documents/[id].tsx b/pages/documents/[id].tsx
index 0aacc7b8..e57a1f93 100644
--- a/pages/documents/[id].tsx
+++ b/pages/documents/[id].tsx
@@ -5,7 +5,9 @@ import LinksContainer from "@/components/links/links-container";
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"
+import LinksTable from "@/components/links/links-table";
export default function DocumentPage() {
const { document, error } = useDocument();
@@ -37,6 +39,14 @@ export default function DocumentPage() {
</h2>
</div>
</div>
+ <LinkSheet>
+ <button
+ type="button"
+ className="inline-flex items-center px-4 py-2 border border-transparent shadow-sm text-sm font-medium rounded-md text-gray-950 bg-gray-300 hover:bg-gray-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-gray-500"
+ >
+ Create Link
+ </button>
+ </LinkSheet>
</div>
{/* Stats */}
{document.numPages !== null && (
@@ -48,6 +58,7 @@ export default function DocumentPage() {
<StatsCard />
{/* Links */}
<LinksContainer />
+ <LinksTable />
</>
) : (
<div>Loading...</div>
diff --git a/prisma/schema.prisma b/prisma/schema.prisma
index a2fa556b..c868adaa 100644
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@ -75,10 +75,12 @@ model Link {
id String @id @default(cuid())
document Document @relation(fields: [documentId], references: [id])
documentId String
- url String? @unique
+ url String? @unique
+ name String? // Link name
expiresAt DateTime? // Optional expiration date
password String? // Optional password for link protection
allowedEmails String[] // Array of emails allowed to view the document
+ emailProtected Boolean? @default(true) // Optional email protection
views View[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt