Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Merge branch 'staging' into fix/faster
Marc Seitz97ced4682897
diff --cc components/view/PagesViewer.tsxindex f049b936,2479cb04..dbb59a7f--- a/components/view/PagesViewer.tsx+++ b/components/view/PagesViewer.tsx@@@ -1,10 -1,6 +1,9 @@@import { ChevronLeftIcon, ChevronRightIcon } from "@heroicons/react/20/solid";import { useEffect, useRef, useState } from "react";--import { BlurImage } from "@/components/shared/blur-image";+import Image from "next/image";+import LoadingSpinner from "../ui/loading-spinner";++const DEFAULT_PRELOADED_IMAGES_NUM = 10;export default function PagesViewer({pages,@@@ -59,37 -53,40 +58,58 @@@};}, []);+ useEffect(() => {+ setLoadedImages((prev) =>+ prev.map((loaded, index) =>+ index < DEFAULT_PRELOADED_IMAGES_NUM ? true : loaded,+ ),+ );+ }, []);++ useEffect(() => {+ const handleKeyDown = (event: KeyboardEvent) => {+ switch (event.key) {+ case "ArrowRight":+ goToNextPage();+ break;+ case "ArrowLeft":+ goToPreviousPage();+ break;+ default:+ break;+ }+ };++ // when the component mounts, attach the event listener+ document.addEventListener("keydown", handleKeyDown);++ // when the component unmounts, detach the event listener+ return () => {+ document.removeEventListener("keydown", handleKeyDown);+ };+ }, [pageNumber]);+- // Go to next page- function goToNextPage() {- if (pageNumber >= numPages) return;- setPageNumber((prevPageNumber) => prevPageNumber + 1);- }-- // Go to previous page- function goToPreviousPage() {- if (pageNumber <= 1) return;+ // Function to preload next image+ const preloadImage = (index: number) => {+ if (index < numPages && !loadedImages[index]) {+ const newLoadedImages = [...loadedImages];+ newLoadedImages[index] = true;+ setLoadedImages(newLoadedImages);+ }+ };++ // Navigate to previous page+ const goToPreviousPage = () => {- if (pageNumber > 1) {- setPageNumber(pageNumber - 1);- }++ if (pageNumber > 1) return;+ setPageNumber((prevPageNumber) => prevPageNumber - 1);- }+ };++ // Navigate to next page and preload next image+ const goToNextPage = () => {- if (pageNumber < numPages) {- preloadImage(DEFAULT_PRELOADED_IMAGES_NUM - 1 + pageNumber); // Preload the next image- setPageNumber(pageNumber + 1);- }++ if (pageNumber < numPages) return;++ preloadImage(DEFAULT_PRELOADED_IMAGES_NUM - 1 + pageNumber); // Preload the next image++ setPageNumber((prevPageNumber) => prevPageNumber + 1);+ };async function trackPageView(duration: number = 0) {await fetch("/api/record_view", {diff --cc components/view/document-view.tsxindex dca4e3dd,ec19aa21..b7303c76--- a/components/view/document-view.tsx+++ b/components/view/document-view.tsx@@@ -6,9 -7,10 +6,11 @@@ import AccessForm,import { usePlausible } from "next-plausible";import { toast } from "sonner";import { LinkWithDocument } from "@/lib/types";-import LoadingSpinner from "../ui/loading-spinner";-import PagesViewer from "@/components/PagesViewer";+import LoadingSpinner from "@/components/ui/loading-spinner";+import PagesViewer from "@/components/view/PagesViewer";+import PDFViewer from "@/components/view/PDFViewer";+ import { NotionPage } from "../NotionPage";+ import { ExtendedRecordMap } from "notion-types";export type DEFAULT_DOCUMENT_VIEW_TYPE = {viewId: string;@@@ -19,13 -21,16 +21,18 @@@export default function DocumentView({link,userEmail,+ userId,isProtected,+ notionData,}: {link: LinkWithDocument;userEmail: string | null | undefined;+ userId: string | null | undefined;isProtected: boolean;+ notionData: {+ rootNotionPageId: string | null;+ recordMap: ExtendedRecordMap | null;+ };}) {const { document, emailProtected, password: linkPassword } = link;diff --cc pages/api/links/[id]/index.tsindex 15d5caaa,3a2ab4fc..99202810--- a/pages/api/links/[id]/index.ts+++ b/pages/api/links/[id]/index.ts@@@ -31,7 -30,7 +31,12 @@@ export default async function handleid: true,versions: {where: { isPrimary: true },- select: { id: true, versionNumber: true, hasPages: true },- select: { versionNumber: true, type: true },++ select: {++ id: true,++ versionNumber: true,++ type: true,++ hasPages: true,++ },take: 1,},},diff --cc pages/view/domains/[domain]/[slug]/index.tsxindex f59094f7,b406dfb9..d155a3e6--- a/pages/view/domains/[domain]/[slug]/index.tsx+++ b/pages/view/domains/[domain]/[slug]/index.tsx@@@ -1,22 -1,128 +1,130 @@@import LoadingSpinner from "@/components/ui/loading-spinner";import DocumentView from "@/components/view/document-view";import { useDomainLink } from "@/lib/swr/use-link";+import { CustomUser, LinkWithDocument } from "@/lib/types";import NotFound from "@/pages/404";+import { GetStaticPropsContext } from "next";import { useSession } from "next-auth/react";+import { useRouter } from "next/router";+ import prisma from "@/lib/prisma";- export default function ViewPage({ link }: { link: LinkWithDocument }) {- const router = useRouter();- // const { link, error } = useDomainLink();- const { data: session, status } = useSession();+ import { ExtendedRecordMap } from "notion-types";+ import notion from "@/lib/notion";-import { LinkWithDocument } from "@/lib/types";+ import { parsePageId } from "notion-utils";++ export const getStaticProps = async (context: {+ params: { domain: string; slug: string };+ }) => {+ const domain = context.params.domain as string;+ const slug = context.params.slug as string;++ console.log("domain", domain);+ console.log("slug", slug);++ const link = await prisma.link.findUnique({+ where: {+ domainSlug_slug: {+ slug: slug,+ domainSlug: domain,+ },+ },+ select: {+ id: true,+ expiresAt: true,+ emailProtected: true,+ allowDownload: true,+ password: true,+ isArchived: true,+ document: {+ select: {+ id: true,+ team: { select: { plan: true } },+ versions: {+ where: { isPrimary: true },+ select: { versionNumber: true, type: true, file: true },+ take: 1,+ },+ },+ },+ },+ });++ if (!link || !link.document.team) {+ return {+ notFound: true,+ };+ }++ console.log("plan", link.document.team.plan);++ // if owner of document is on free plan, return 404+ if (link.document.team.plan === "free") {+ return {+ notFound: true,+ };+ }++ let pageId = null;+ let recordMap = null;++ const { type, file, ...versionWithoutTypeAndFile } =+ link.document.versions[0];++ console.log("type", type);+ console.log("file", file);++ if (type === "notion") {+ // regex match to get the page id from the notion url+ const notionPageId = parsePageId(file, { uuid: false });+ if (!notionPageId) {+ return {+ notFound: true,+ };+ }++ pageId = notionPageId;+ recordMap = await notion.getPage(pageId);+ }++ return {+ props: {+ // return link without file and type to avoid sending the file to the client+ link: {+ ...link,+ document: {+ ...link.document,+ versions: [versionWithoutTypeAndFile],+ },+ },+ notionData: {+ rootNotionPageId: pageId,+ recordMap,+ },+ },+ revalidate: 10,+ };+ };++ export async function getStaticPaths() {+ return {+ paths: [],+ fallback: true,+ };+ }- // if (error && error.status === 404) {- // return <NotFound />;- // }+ export default function ViewPage({+ link,+ notionData,+ }: {+ link: LinkWithDocument;+ notionData: {+ rootNotionPageId: string | null;+ recordMap: ExtendedRecordMap | null;+ };+ }) {+ const { data: session, status } = useSession();- if (!link || status === "loading" || router.isFallback) {+ if (!link || status === "loading") {return (<div className="h-screen flex items-center justify-center"><LoadingSpinner className="h-20 w-20" />@@@ -49,8 -157,8 +159,9 @@@<DocumentViewlink={link}userEmail={userEmail}+ userId={userId}isProtected={true}+ notionData={notionData}/>);}@@@ -59,45 -167,8 +170,46 @@@<DocumentViewlink={link}userEmail={userEmail}+ userId={userId}isProtected={false}+ notionData={notionData}/>);}+- export async function getStaticProps(context: GetStaticPropsContext) {- // const router = useRouter()- // const { domain, slug } = router.query as {- // domain: string;- // slug: string;- // };- const { domain, slug } = context.params as { domain: string; slug: string };-- console.log(context.params);-- // Fetch the link- const res = await fetch(- `${process.env.NEXTAUTH_URL}/api/links/domains/${encodeURIComponent(- domain,- )}/${encodeURIComponent(slug)}`,- );- if (!res.ok) {- return { notFound: true };- }- const link = (await res.json()) as LinkWithDocument;++// export async function getStaticProps(context: GetStaticPropsContext) {++// // const router = useRouter()++// // const { domain, slug } = router.query as {++// // domain: string;++// // slug: string;++// // };++// const { domain, slug } = context.params as { domain: string; slug: string };+- console.log(link);++// console.log(context.params);+- return {- props: {- link,- },- };- }++// // Fetch the link++// const res = await fetch(++// `${process.env.NEXTAUTH_URL}/api/links/domains/${encodeURIComponent(++// domain,++// )}/${encodeURIComponent(slug)}`,++// );++// if (!res.ok) {++// return { notFound: true };++// }++// const link = (await res.json()) as LinkWithDocument;+- export async function getStaticPaths() {- return {- paths: [],- fallback: true,- };- }++// console.log(link);++++// return {++// props: {++// link,++// },++// };++// }++++// export async function getStaticPaths() {++// return {++// paths: [],++// fallback: true,++// };++// }diff --cc prisma/schema.prismaindex 063698eb,2a986541..0b83b4a1--- a/prisma/schema.prisma+++ b/prisma/schema.prisma@@@ -97,25 -98,24 +98,27 @@@ model VerificationToken@@unique([identifier, token])}--model Document {- id String @id @default(cuid())- name String- description String?- file String // This should be a reference to where the file is stored (S3, Google Cloud Storage, etc.)- type String? // This should be a reference to the file type (pdf, docx, etc.)- numPages Int? // This should be a reference to the number of pages in the document- owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)- teamId String?- team Team? @relation(fields: [teamId], references: [id], onDelete: SetNull)- ownerId String // This field holds the foreign key.- createdAt DateTime @default(now())- updatedAt DateTime @updatedAt- links Link[]- views View[]- versions DocumentVersion[]++model Document {+ id String @id @default(cuid())+ name String+ description String?+ file String // This should be a reference to where the file is stored (S3, Google Cloud Storage, etc.)+ type String? // This should be a reference to the file type (pdf, docx, etc.)+ numPages Int? // This should be a reference to the number of pages in the document+ owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)+ teamId String?+ team Team? @relation(fields: [teamId], references: [id], onDelete: SetNull)+ ownerId String // This field holds the foreign key.+ assistantEnabled Boolean @default(false) // This indicates if assistant is enabled for this document+ createdAt DateTime @default(now())+ updatedAt DateTime @updatedAt+ links Link[]+ views View[]+ versions DocumentVersion[]+ conversations Conversation[]++ @@index([ownerId])+ @@index([teamId])}model DocumentVersion {