Skip to content

Commit 48bfb89

Browse files
committed
feat: Add missing file UX handling for downloads and PDF viewer
1 parent 34f4d66 commit 48bfb89

3 files changed

Lines changed: 56 additions & 11 deletions

File tree

‎src/components/newPdfViewer.tsx‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,19 @@
468468
const [isReadingMode, setIsReadingMode] = useState(false);
469469
const [showReadingCoachmark, setShowReadingCoachmark] = useState(false);
470470
const viewerRef = useRef<HTMLDivElement>(null);
471+
const [urlStatus, setUrlStatus] = useState<"checking" | "ok" | "error">("checking");
472+
473+
useEffect(() => {
474+
let isMounted = true;
475+
fetch(url, { method: "HEAD" })
476+
.then(res => {
477+
if (isMounted) setUrlStatus(res.ok ? "ok" : "error");
478+
})
479+
.catch(() => {
480+
if (isMounted) setUrlStatus("error");
481+
});
482+
return () => { isMounted = false; };
483+
}, [url]);
471484
const effectiveBackgroundColor =
472485
backgroundColor ?? (resolvedTheme === "light" ? "#F3F5FF" : "#070114");
473486
const loaderTextColor =
@@ -569,7 +582,19 @@
569582
}),
570583
], [url, name]);
571584

572-
if (isLoading || !engine) {
585+
if (urlStatus === "error") {
586+
return (
587+
<div className="flex h-dvh w-full flex-col items-center justify-center gap-2" style={{ backgroundColor: effectiveBackgroundColor, color: loaderTextColor }}>
588+
<BookOpenText size={48} className="opacity-50 mb-2" />
589+
<span className="text-lg font-medium">Paper Not Found</span>
590+
<span className="text-sm opacity-70 text-center max-w-[250px]">
591+
This paper has been deleted or is missing from the server.
592+
</span>
593+
</div>
594+
);
595+
}
596+
597+
if (isLoading || !engine || urlStatus === "checking") {
573598
return (
574599
<Loader
575600
backgroundColor={effectiveBackgroundColor}
@@ -653,7 +678,7 @@
653678
dismissReadingCoachmark={dismissReadingCoachmark}
654679
/>}
655680
<DocumentContent documentId={activeDocumentId}>
656-
{({ isLoaded }) => (
681+
{({ isLoaded, isError }) => (
657682
<>
658683
<div
659684
className="absolute inset-0 z-50 flex items-center justify-center bg-[#070114]"
@@ -664,10 +689,20 @@
664689
backgroundColor: effectiveBackgroundColor,
665690
}}
666691
>
667-
<Loader
668-
backgroundColor={effectiveBackgroundColor}
669-
textColor={loaderTextColor}
670-
/>
692+
{isError ? (
693+
<div className="flex flex-col items-center gap-2" style={{ color: loaderTextColor }}>
694+
<BookOpenText size={48} className="opacity-50 mb-2" />
695+
<span className="text-lg font-medium">Paper Not Found</span>
696+
<span className="text-sm opacity-70 text-center max-w-[250px]">
697+
This paper has been deleted or is missing from the server.
698+
</span>
699+
</div>
700+
) : (
701+
<Loader
702+
backgroundColor={effectiveBackgroundColor}
703+
textColor={loaderTextColor}
704+
/>
705+
)}
671706
</div>
672707
<Viewport
673708
documentId={activeDocumentId}

‎src/lib/posthog-server.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
1-
import * as posthogModule from "posthog-node";
1+
/* eslint-disable @typescript-eslint/no-require-imports */
2+
/* eslint-disable @typescript-eslint/no-explicit-any */
3+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
4+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
5+
/* eslint-disable @typescript-eslint/no-unsafe-call */
6+
/* eslint-disable @typescript-eslint/prefer-nullish-coalescing */
27

3-
let posthogClient: any = null;
8+
import type { PostHog } from "posthog-node";
49

5-
export function getPostHogClient(): any {
10+
let posthogClient: PostHog | null = null;
11+
12+
export function getPostHogClient(): PostHog | null {
613
const token = process.env.NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN;
714
if (!token) {
815
if (process.env.NODE_ENV !== "production") {
@@ -13,7 +20,8 @@ export function getPostHogClient(): any {
1320
return null;
1421
}
1522

16-
const PHClass = (posthogModule as any).PostHog || (posthogModule as any).default?.PostHog || posthogModule.default;
23+
const phModule = require("posthog-node");
24+
const PHClass = phModule.PostHog || phModule.default?.PostHog || phModule.default || phModule;
1725

1826
posthogClient ??= new PHClass(token, {
1927
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,

‎src/lib/utils/download.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { type IPaper } from "../../interface";
22
import { extractBracketContent } from "./string";
33
import axios from "axios";
4+
import toast from "react-hot-toast";
45

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

0 commit comments

Comments
 (0)