Skip to content

Commit

Permalink
handle new pdf schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedriad1 committed Dec 12, 2024
1 parent 3d9a9c7 commit 2ae94c9
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 56 deletions.
15 changes: 5 additions & 10 deletions src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,10 @@ import { useTheme } from "next-themes";
import { env } from "@/env";
import { makePdfViewerButtons } from "./buttons";
import { useNavbarStore } from "@/stores/navbar";
import type { TurathContent } from "@/types/api/content/turath";

const isInitializedByUrl = new Map<string, boolean>();

export default function PdfView({
pdf: pdfSource,
}: {
pdf: TurathContent["pdf"];
}) {
export default function PdfView({ pdf: pdfSource }: { pdf: string }) {
const { resolvedTheme = "light" } = useTheme();
const viewerRef = useRef<HTMLDivElement>(null);
const instanceRef = useRef<WebViewerInstance | null>(null);
Expand All @@ -33,18 +28,18 @@ export default function PdfView({

useEffect(() => {
const initialize = async () => {
if (!pdfSource || !("fullBookUrl" in pdfSource)) return;
if (!pdfSource) return;

if (isInitializedByUrl.get(pdfSource.fullBookUrl)) return;
if (isInitializedByUrl.get(pdfSource)) return;

isInitializedByUrl.set(pdfSource.fullBookUrl, true);
isInitializedByUrl.set(pdfSource, true);
setIsLoading(true);
setChapters([]);

const instance: WebViewerInstance = await WebViewer(
{
path: "/pdf-express", // point to where the files you copied are served from
initialDoc: pdfSource.fullBookUrl, // path to your document
initialDoc: pdfSource, // path to your document
enableAnnotations: false,
disabledElements: [
// "selectToolButton",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import type { ButtonProps } from "@/components/ui/button";
import { ArrowDownTrayIcon } from "@heroicons/react/24/solid";
import { useTranslations } from "next-intl";
import ReaderNavigationButton from "./navigation-button";
import type { TurathContent } from "@/types/api/content/turath";

export default function DownloadButton({
pdf,
slug,
}: {
pdf?: TurathContent["pdf"];
pdf?: string;
slug: string;
}) {
const t = useTranslations("reader");
Expand All @@ -19,10 +18,10 @@ export default function DownloadButton({
tooltipProps: { side: "bottom" },
};

if (pdf && "fullBookUrl" in pdf) {
if (pdf) {
return (
<ReaderNavigationButton {...commonProps} asChild>
<a href={pdf.fullBookUrl} download={slug + ".pdf"} target="_blank">
<a href={pdf} download={slug + ".pdf"} target="_blank">
<ArrowDownTrayIcon />
</a>
</ReaderNavigationButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export default function ReaderNavigation({
const versionId = bookResponse.content.id;

const pdf =
bookResponse.content.source === "turath"
? bookResponse.content.pdf
: undefined;
"pdfUrl" in bookResponse.content ? bookResponse.content.pdfUrl : undefined;

return (
<>
Expand All @@ -66,7 +64,7 @@ export default function ReaderNavigation({
</div>

<div className="flex flex-1 justify-center">
<ViewTabs hasPdf={!!pdf && "fullBookUrl" in pdf} />
<ViewTabs hasPdf={!!pdf} />
</div>

<div className="hidden flex-1 items-center gap-2 md:flex md:justify-end">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,21 @@ import ReaderNavigationButton from "./navigation-button";
import { useGetBookUrl, useReaderView } from "./utils";
import { useTranslations } from "next-intl";
import { Link } from "@/navigation";
import type { TurathContent } from "@/types/api/content/turath";

export default function ReaderNavigationMobileActions({
pdf,
slug,
isSinglePage,
}: {
pdf?: TurathContent["pdf"];
pdf?: string;
slug: string;
isSinglePage?: boolean;
}) {
const { view, setView } = useReaderView();
const t = useTranslations("reader");
const bookUrl = useGetBookUrl(isSinglePage ? undefined : 1);

const hasPdfView = !!pdf && "fullBookUrl" in pdf;
const hasPdfView = !!pdf;

return (
<DropdownMenu>
Expand Down Expand Up @@ -62,11 +61,7 @@ export default function ReaderNavigationMobileActions({
<DropdownMenuGroup>
{hasPdfView ? (
<DropdownMenuItem asChild className="gap-2">
<a
href={pdf.fullBookUrl}
download={slug + ".pdf"}
target="_blank"
>
<a href={pdf} download={slug + ".pdf"} target="_blank">
<ArrowDownTrayIcon className="h-4 w-4" />
<span>{t("download-pdf")}</span>
</a>
Expand Down
8 changes: 6 additions & 2 deletions src/app/[locale]/t/[bookId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ export default async function SidebarContent({
<h1>Coming Soon</h1>
</article>
);
} else if (view === "pdf" && "pdf" in response.content) {
readerContent = <PdfView pdf={response.content.pdf} />;
} else if (view === "pdf") {
if (!("pdfUrl" in response.content) || !response.content.pdfUrl) {
notFound();
}

readerContent = <PdfView pdf={response.content.pdfUrl} />;
} else {
readerContent = (
<article>
Expand Down
8 changes: 8 additions & 0 deletions src/types/api/content/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export type BaseContent = {
id: string;
publicationDetails?: PrismaJson.PublicationDetails;
};

export type BaseContentWithPdf = BaseContent & {
pdfUrl?: string;
};
6 changes: 3 additions & 3 deletions src/types/api/content/external.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type ExternalContent = {
id: string;
import type { BaseContent } from "./base";

export type ExternalContent = BaseContent & {
source: "external";
url: string;
publicationDetails?: PrismaJson.PublicationDetails;
};
8 changes: 3 additions & 5 deletions src/types/api/content/openiti.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import type { ParseResult } from "@openiti/markdown-parser";
import type { BaseContentWithPdf } from "./base";

export type OpenitiContent = {
id: string;
version: string;
export type OpenitiContent = BaseContentWithPdf & {
source: "openiti";
version: string;
pages: ParseResult["content"];
publicationDetails?: PrismaJson.PublicationDetails;
headings?: (ParseResult["chapters"][number] & { pageIndex?: number })[];
pdfUrl?: string;
};
6 changes: 3 additions & 3 deletions src/types/api/content/pdf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type PdfContent = {
id: string;
import type { BaseContent } from "./base";

export type PdfContent = BaseContent & {
source: "pdf";
url: string;
publicationDetails?: PrismaJson.PublicationDetails;
};
21 changes: 4 additions & 17 deletions src/types/api/content/turath.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import type { BaseContentWithPdf } from "./base";

interface TurathPage {
text: string;
vol: string;
page: number;
}

type TurathPdf =
| {
fullBookUrl: string;
sizeInMb?: string;
}
| {
volume: string;
url: string;
}[]
| null;

type TurathHeading = {
page:
| {
Expand All @@ -27,13 +18,9 @@ type TurathHeading = {
pageIndex?: number | undefined;
};

export type TurathContent = {
id: string;
version: string;
export type TurathContent = BaseContentWithPdf & {
source: "turath";
version: string;
pages: TurathPage[];
pdf?: TurathPdf;
publicationDetails?: PrismaJson.PublicationDetails;
headings?: TurathHeading[];
pdfUrl?: string;
};

0 comments on commit 2ae94c9

Please sign in to comment.