Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
48 changes: 42 additions & 6 deletions src/components/newPdfViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,20 @@ export default function PDFViewer({
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" })
.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 @@ -571,7 +585,19 @@ export default function PDFViewer({
}),
], [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 @@ -655,7 +681,7 @@ if (isLoading || !engine) {
dismissReadingCoachmark={dismissReadingCoachmark}
/>}
<DocumentContent documentId={activeDocumentId}>
{({ isLoaded }) => (
{({ isLoaded, isError }) => (
<>
<div
className="absolute inset-0 z-50 flex items-center justify-center bg-[#070114]"
Expand All @@ -666,10 +692,20 @@ if (isLoading || !engine) {
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
17 changes: 15 additions & 2 deletions src/lib/posthog-server.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
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;

export function getPostHogClient(): PostHog | null {
if (posthogClient) return posthogClient;

const token = process.env.NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN;
if (!token) {
if (process.env.NODE_ENV !== "production") {
Expand All @@ -12,7 +21,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.

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
8 changes: 7 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,11 @@ 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);
if (typeof navigator !== "undefined" && !navigator.onLine) {
toast.error("You are offline. Please check your internet connection.");
} else {
toast.error("This paper is no longer available on the server.");
}
}
};
Loading