Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions src/components/newPdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,19 @@
const [isReadingMode, setIsReadingMode] = useState(false);
const [showReadingCoachmark, setShowReadingCoachmark] = useState(false);
const viewerRef = useRef<HTMLDivElement>(null);
const [urlStatus, setUrlStatus] = useState<"checking" | "ok" | "error">("checking");

useEffect(() => {
let isMounted = true;
fetch(url, { method: "HEAD" })

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we already handle that error, still do we need this ? will it delay the paper fetching by any chance?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually still need the HEAD request! The issue is that the PDF library's built-in error handler completely fails when GCS returns a 404 (because GCS returns an XML page, and the viewer gets stuck trying to parse it as a PDF, causing an infinite loading spinner). The HEAD request is just a tiny pre-flight check that catches the 404 instantly before the viewer can hang.
And don't worry about delays! Because it's a HEAD request, it only grabs a few bytes of server headers without downloading the actual PDF payload. It takes less than 50ms and happens in the background before the heavy WASM engine even initializes, so there's zero noticeable delay in paper fetching so no worries

.then(res => {
if (isMounted) setUrlStatus(res.ok ? "ok" : "error");
})
.catch(() => {
if (isMounted) setUrlStatus("error");
});
return () => { isMounted = false; };
}, [url]);
const effectiveBackgroundColor =
backgroundColor ?? (resolvedTheme === "light" ? "#F3F5FF" : "#070114");
const loaderTextColor =
Expand Down Expand Up @@ -569,7 +582,19 @@
}),
], [url, name]);

if (isLoading || !engine) {
if (urlStatus === "error") {
return (
<div className="flex h-dvh w-full flex-col items-center justify-center gap-2" style={{ backgroundColor: effectiveBackgroundColor, color: loaderTextColor }}>
<BookOpenText size={48} className="opacity-50 mb-2" />
<span className="text-lg font-medium">Paper Not Found</span>
<span className="text-sm opacity-70 text-center max-w-[250px]">
This paper has been deleted or is missing from the server.
</span>
</div>
);
}

if (isLoading || !engine || urlStatus === "checking") {
return (
<Loader
backgroundColor={effectiveBackgroundColor}
Expand Down Expand Up @@ -653,7 +678,7 @@
dismissReadingCoachmark={dismissReadingCoachmark}
/>}
<DocumentContent documentId={activeDocumentId}>
{({ isLoaded }) => (
{({ isLoaded, isError }) => (
<>
<div
className="absolute inset-0 z-50 flex items-center justify-center bg-[#070114]"
Expand All @@ -664,10 +689,20 @@
backgroundColor: effectiveBackgroundColor,
}}
>
<Loader
backgroundColor={effectiveBackgroundColor}
textColor={loaderTextColor}
/>
{isError ? (
<div className="flex flex-col items-center gap-2" style={{ color: loaderTextColor }}>
<BookOpenText size={48} className="opacity-50 mb-2" />
<span className="text-lg font-medium">Paper Not Found</span>
<span className="text-sm opacity-70 text-center max-w-[250px]">
This paper has been deleted or is missing from the server.
</span>
</div>
) : (
<Loader
backgroundColor={effectiveBackgroundColor}
textColor={loaderTextColor}
/>
)}
</div>
<Viewport
documentId={activeDocumentId}
Expand Down
4 changes: 2 additions & 2 deletions src/context/filterContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
const a = document.createElement("a");
a.href = url;

a.download = getDownloadName(searchParams, "subject");
a.download = `${getDownloadName(searchParams, "subject")}.zip`;

document.body.appendChild(a);
a.click();
Expand Down Expand Up @@ -292,7 +292,7 @@ export const FilterProvider: React.FC<FilterProviderProps> = ({
() =>
Math.ceil(
(appliedFilters ? filteredPapers.length : papers.length) /
papersPerPage,
papersPerPage,
),
[appliedFilters, filteredPapers.length, papers.length, papersPerPage],
);
Expand Down
15 changes: 13 additions & 2 deletions src/lib/posthog-server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { PostHog } from "posthog-node";
/* eslint-disable @typescript-eslint/no-require-imports */
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-call */
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */

import type { PostHog } from "posthog-node";

let posthogClient: PostHog | null = null;

Expand All @@ -12,7 +19,11 @@ export function getPostHogClient(): PostHog | null {
}
return null;
}
posthogClient ??= new PostHog(token, {

const phModule = require("posthog-node");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it gets build on every upload or an api call...may be do something like If(posthogClient)return it ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch brooooo You're totally right, it was hitting that require on every single upload(i didnt see tht)
I just pushed a commit that adds an early return at the very top so it caches the client immediately after the first call.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!

const PHClass = phModule.PostHog || phModule.default?.PostHog || phModule.default || phModule;

posthogClient ??= new PHClass(token, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
flushAt: 1,
flushInterval: 0,
Expand Down
4 changes: 3 additions & 1 deletion src/lib/utils/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type IPaper } from "../../interface";
import { extractBracketContent } from "./string";
import axios from "axios";
import toast from "react-hot-toast";

export const getSecureUrl = (url: string): string =>
url.startsWith("http://") ? url.replace("http://", "https://") : url;
Expand All @@ -23,6 +24,7 @@ export const downloadFile = async (
link.click();
window.URL.revokeObjectURL(link.href);
} catch (error) {
console.error("Download failed:", error);
console.error("Download failed (File missing or network error):", error);
toast.error("This paper is no longer available on the server.");

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make sure this error is shown only for a particular error response status, else even for a no internet situation, this might be shown to the uses..kind of misleading

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! If their internet drops, they shouldn't see the "paper is missing" error. I just updated the catch block to check navigator.onLine. Now, if it's an actual network failure, it gives a specific "You are offline" toast instead!

}
};
Loading