Skip to content

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

fix: loading time and spinner with no email present

Marc Seitzfff557a6b90d
diff --git a/components/PagesViewer.tsx b/components/PagesViewer.tsx
index 23815d96..2e321768 100644
--- a/components/PagesViewer.tsx
+++ b/components/PagesViewer.tsx
@@ -115,11 +115,27 @@ export default function PagesViewer({pages, linkId, documentId, viewId}: {pages:
className="object-contain mx-auto"
src={pages[pageNumber - 1].file}
alt={`Page ${pageNumber}`}
+ sizes="100vw"
fill
priority={true}
quality={100}
/>
</div>
+
+ {/* Preload the next few images off-screen */}
+ {/* <div className="absolute top-0 left-full">
+ {pages.slice(pageNumber, pageNumber + 3).map((page, idx) => (
+ <BlurImage
+ key={idx}
+ src={page.file}
+ alt={`Preload Page ${page.pageNumber}`}
+ quality={100}
+ sizes="100vw"
+ fill
+ className="object-contain"
+ />
+ ))}
+ </div> */}
</div>
</>
);
diff --git a/components/view/access-form/index.tsx b/components/view/access-form/index.tsx
index d161589a..c2b45a59 100644
--- a/components/view/access-form/index.tsx
+++ b/components/view/access-form/index.tsx
@@ -1,25 +1,45 @@
-import { Dispatch, SetStateAction } from "react";
+import { useEffect } from "react";
import PasswordSection from "./password-section";
import EmailSection from "./email-section";
import { Button } from "@/components/ui/button";
-import { DEFAULT_ACCESS_FORM_TYPE } from "../document-view";
+export const DEFAULT_ACCESS_FORM_DATA = {
+ email: null,
+ password: null,
+};
+
+export type DEFAULT_ACCESS_FORM_TYPE = {
+ email: string | null;
+ password: string | null;
+};
export default function AccessForm({
- onSubmitHandler,
data,
+ email,
setData,
+ onSubmitHandler,
requireEmail,
requirePassword,
isLoading,
}: {
- onSubmitHandler: React.FormEventHandler<HTMLFormElement>;
data: DEFAULT_ACCESS_FORM_TYPE;
- setData: Dispatch<SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;
+ email: string | null | undefined;
+ setData: React.Dispatch<React.SetStateAction<DEFAULT_ACCESS_FORM_TYPE>>;
+ onSubmitHandler: React.FormEventHandler<HTMLFormElement>;
requireEmail: boolean;
requirePassword: boolean;
isLoading: boolean;
}) {
+
+ useEffect(() => {
+ const userEmail = email;
+ if (userEmail) {
+ setData((prevData: DEFAULT_ACCESS_FORM_TYPE) => ({
+ ...prevData,
+ email: userEmail || prevData.email,
+ }));
+ }
+ }, [email]);
return (
<>
diff --git a/components/view/document-view.tsx b/components/view/document-view.tsx
index dc32e27f..1be215c2 100644
--- a/components/view/document-view.tsx
+++ b/components/view/document-view.tsx
@@ -1,24 +1,12 @@
import React, { useEffect, useRef, useState } from "react";
-import ErrorPage from "next/error";
import PDFViewer from "@/components/PDFViewer";
-import AccessForm from "@/components/view/access-form";
-import { useSession } from "next-auth/react";
+import AccessForm, { DEFAULT_ACCESS_FORM_DATA, DEFAULT_ACCESS_FORM_TYPE } from "@/components/view/access-form";
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";
-export const DEFAULT_ACCESS_FORM_DATA = {
- email: null,
- password: null,
-};
-
-export type DEFAULT_ACCESS_FORM_TYPE = {
- email: string | null;
- password: string | null;
-};
-
export type DEFAULT_DOCUMENT_VIEW_TYPE = {
viewId: string;
file: string | null;
@@ -27,54 +15,32 @@ export type DEFAULT_DOCUMENT_VIEW_TYPE = {
export default function DocumentView({
link,
- error,
+ userEmail,
+ isProtected,
}: {
link: LinkWithDocument;
- error: any;
+ userEmail: string | null | undefined;
+ isProtected: boolean;
}) {
- const { data: session } = useSession();
+ const { document, emailProtected, password: linkPassword } = link;
+
const plausible = usePlausible();
+ const didMount = useRef<boolean>(false);
const [submitted, setSubmitted] = useState<boolean>(false);
const [isLoading, setIsLoading] = useState<boolean>(false);
- const hasInitiatedSubmit = useRef(false);
const [viewData, setViewData] = useState<DEFAULT_DOCUMENT_VIEW_TYPE>({
viewId: "",
file: null,
pages: null,
});
-
const [data, setData] = useState<DEFAULT_ACCESS_FORM_TYPE>(
DEFAULT_ACCESS_FORM_DATA
);
- useEffect(() => {
- const userEmail = session?.user?.email;
- if (userEmail) {
- setData((prevData) => ({
- ...prevData,
- email: userEmail || prevData.email,
- }));
- }
- }, [session]);
-
- if (error && error.status === 404) {
- return <ErrorPage statusCode={404} />;
- }
-
- if (!link) {
- return <LoadingSpinner className="mr-1 h-5 w-5" />;
- }
-
- const { document, expiresAt, emailProtected, password: linkPassword } = link;
-
- // Check if link is expired
- const isExpired = expiresAt && new Date(expiresAt) < new Date();
- if (isExpired) {
- return <div>Link is expired</div>;
- }
- const handleSubmission = async () => {
+ const handleSubmission = async (): Promise<void> => {
+ console.log("handle submission to get views");
setIsLoading(true);
const response = await fetch("/api/views", {
method: "POST",
@@ -83,6 +49,7 @@ export default function DocumentView({
},
body: JSON.stringify({
...data,
+ email: data.email || userEmail,
linkId: link.id,
documentId: document.id,
}),
@@ -104,17 +71,33 @@ export default function DocumentView({
const handleSubmit: React.FormEventHandler<HTMLFormElement> = async (
event: React.FormEvent
- ) => {
+ ): Promise<void> => {
event.preventDefault();
+ console.log("handle submit");
await handleSubmission();
};
- if ((!submitted && emailProtected) || (!submitted && linkPassword)) {
+ console.log("Render with", { submitted, isProtected });
+ // If link is not submitted and does not have email / password protection, show the access form
+ useEffect(() => {
+ if (!didMount.current) {
+ if (!submitted && !isProtected) {
+ console.log("submitting directly");
+ handleSubmission();
+ }
+ didMount.current = true;
+ }
+ }, [submitted, isProtected]);
+
+ // If link is not submitted and does not have email / password protection, show the access form
+ if (!submitted && isProtected) {
+ console.log("calling access form");
return (
<AccessForm
- onSubmitHandler={handleSubmit}
data={data}
+ email={userEmail}
setData={setData}
+ onSubmitHandler={handleSubmit}
requireEmail={emailProtected}
requirePassword={!!linkPassword}
isLoading={isLoading}
@@ -122,38 +105,39 @@ export default function DocumentView({
);
}
- if (
- !emailProtected &&
- !linkPassword &&
- !submitted &&
- !hasInitiatedSubmit.current
- ) {
- hasInitiatedSubmit.current = true;
- handleSubmission();
- }
-
- if (viewData.viewId === "") {
- return <LoadingSpinner className="mr-1 h-5 w-5" />;
+ if (isLoading) {
+ console.log("loading");
+ return (
+ <div className="h-screen flex items-center justify-center">
+ <LoadingSpinner className="h-20 w-20" />
+ </div>
+ );
}
return (
<div className="bg-gray-950">
- {viewData.pages ? (
- <PagesViewer
- pages={viewData.pages}
- viewId={viewData.viewId}
- linkId={link.id}
- documentId={document.id}
- />
+ {submitted ? (
+ viewData.pages ? (
+ <PagesViewer
+ pages={viewData.pages}
+ viewId={viewData.viewId}
+ linkId={link.id}
+ documentId={document.id}
+ />
+ ) : (
+ <PDFViewer
+ file={viewData.file}
+ viewId={viewData.viewId}
+ linkId={link.id}
+ documentId={document.id}
+ name={document.name}
+ allowDownload={link.allowDownload}
+ />
+ )
) : (
- <PDFViewer
- file={viewData.file}
- viewId={viewData.viewId}
- linkId={link.id}
- documentId={document.id}
- name={document.name}
- allowDownload={link.allowDownload}
- />
+ <div className="h-screen flex items-center justify-center">
+ <LoadingSpinner className="h-20 w-20" />
+ </div>
)}
</div>
);
diff --git a/lib/swr/use-link.ts b/lib/swr/use-link.ts
index cb03be95..b7616319 100644
--- a/lib/swr/use-link.ts
+++ b/lib/swr/use-link.ts
@@ -11,6 +11,7 @@ export function useLink() {
linkId: string;
};
+ // only fetch data once when linkId is present
const { data: link, error } = useSWR<LinkWithDocument>(
linkId && `/api/links/${encodeURIComponent(linkId)}`,
fetcher,
diff --git a/pages/view/[linkId].tsx b/pages/view/[linkId].tsx
index c918e880..395b1d5b 100644
--- a/pages/view/[linkId].tsx
+++ b/pages/view/[linkId].tsx
@@ -2,21 +2,38 @@ import LoadingSpinner from "@/components/ui/loading-spinner";
import DocumentView from "@/components/view/document-view";
import { useLink } from "@/lib/swr/use-link";
import NotFound from "@/pages/404";
+import { useSession } from "next-auth/react";
export default function ViewPage() {
const { link, error } = useLink();
+ const { data: session, status } = useSession();
if (error && error.status === 404) {
return <NotFound />;
}
- if (!link) {
+ if (!link || status === "loading") {
return (
<div className="h-screen flex items-center justify-center">
- <LoadingSpinner className="mr-1 h-20 w-20" />
+ <LoadingSpinner className="h-20 w-20" />
</div>
);
}
- return <DocumentView link={link} error={error} />;
+ const { expiresAt, emailProtected, password: linkPassword } = link;
+
+ const { email: userEmail } = session?.user || {};
+
+ // If the link is expired, show a 404 page
+ if (expiresAt && new Date(expiresAt) < new Date()) {
+ return <NotFound />;
+ }
+
+ if (emailProtected || linkPassword) {
+ return (
+ <DocumentView link={link} userEmail={userEmail} isProtected={true} />
+ );
+ }
+
+ return <DocumentView link={link} userEmail={userEmail} isProtected={false} />;
}