From 2ae94c984d0f8e78689be5c5c39314f57e1fb6cf Mon Sep 17 00:00:00 2001 From: ahmedriad1 Date: Thu, 12 Dec 2024 04:46:52 +0200 Subject: [PATCH] handle new pdf schema --- .../t/[bookId]/_components/pdf-view/index.tsx | 15 +++++-------- .../reader-navigation/download-button.tsx | 7 +++---- .../_components/reader-navigation/index.tsx | 6 ++---- .../reader-navigation/mobile-actions.tsx | 11 +++------- src/app/[locale]/t/[bookId]/page.tsx | 8 +++++-- src/types/api/content/base.ts | 8 +++++++ src/types/api/content/external.ts | 6 +++--- src/types/api/content/openiti.ts | 8 +++---- src/types/api/content/pdf.ts | 6 +++--- src/types/api/content/turath.ts | 21 ++++--------------- 10 files changed, 40 insertions(+), 56 deletions(-) create mode 100644 src/types/api/content/base.ts diff --git a/src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx b/src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx index 4c19c61..e88af75 100644 --- a/src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx +++ b/src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx @@ -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(); -export default function PdfView({ - pdf: pdfSource, -}: { - pdf: TurathContent["pdf"]; -}) { +export default function PdfView({ pdf: pdfSource }: { pdf: string }) { const { resolvedTheme = "light" } = useTheme(); const viewerRef = useRef(null); const instanceRef = useRef(null); @@ -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", diff --git a/src/app/[locale]/t/[bookId]/_components/reader-navigation/download-button.tsx b/src/app/[locale]/t/[bookId]/_components/reader-navigation/download-button.tsx index d6c259c..57b806e 100644 --- a/src/app/[locale]/t/[bookId]/_components/reader-navigation/download-button.tsx +++ b/src/app/[locale]/t/[bookId]/_components/reader-navigation/download-button.tsx @@ -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"); @@ -19,10 +18,10 @@ export default function DownloadButton({ tooltipProps: { side: "bottom" }, }; - if (pdf && "fullBookUrl" in pdf) { + if (pdf) { return ( - + diff --git a/src/app/[locale]/t/[bookId]/_components/reader-navigation/index.tsx b/src/app/[locale]/t/[bookId]/_components/reader-navigation/index.tsx index 8208ef1..85a44cf 100644 --- a/src/app/[locale]/t/[bookId]/_components/reader-navigation/index.tsx +++ b/src/app/[locale]/t/[bookId]/_components/reader-navigation/index.tsx @@ -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 ( <> @@ -66,7 +64,7 @@ export default function ReaderNavigation({
- +
diff --git a/src/app/[locale]/t/[bookId]/_components/reader-navigation/mobile-actions.tsx b/src/app/[locale]/t/[bookId]/_components/reader-navigation/mobile-actions.tsx index fe53664..31f1bfe 100644 --- a/src/app/[locale]/t/[bookId]/_components/reader-navigation/mobile-actions.tsx +++ b/src/app/[locale]/t/[bookId]/_components/reader-navigation/mobile-actions.tsx @@ -13,14 +13,13 @@ 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; }) { @@ -28,7 +27,7 @@ export default function ReaderNavigationMobileActions({ const t = useTranslations("reader"); const bookUrl = useGetBookUrl(isSinglePage ? undefined : 1); - const hasPdfView = !!pdf && "fullBookUrl" in pdf; + const hasPdfView = !!pdf; return ( @@ -62,11 +61,7 @@ export default function ReaderNavigationMobileActions({ {hasPdfView ? ( - + {t("download-pdf")} diff --git a/src/app/[locale]/t/[bookId]/page.tsx b/src/app/[locale]/t/[bookId]/page.tsx index 1d1bf1b..671c6ea 100644 --- a/src/app/[locale]/t/[bookId]/page.tsx +++ b/src/app/[locale]/t/[bookId]/page.tsx @@ -131,8 +131,12 @@ export default async function SidebarContent({

Coming Soon

); - } else if (view === "pdf" && "pdf" in response.content) { - readerContent = ; + } else if (view === "pdf") { + if (!("pdfUrl" in response.content) || !response.content.pdfUrl) { + notFound(); + } + + readerContent = ; } else { readerContent = (
diff --git a/src/types/api/content/base.ts b/src/types/api/content/base.ts new file mode 100644 index 0000000..0af4fac --- /dev/null +++ b/src/types/api/content/base.ts @@ -0,0 +1,8 @@ +export type BaseContent = { + id: string; + publicationDetails?: PrismaJson.PublicationDetails; +}; + +export type BaseContentWithPdf = BaseContent & { + pdfUrl?: string; +}; diff --git a/src/types/api/content/external.ts b/src/types/api/content/external.ts index f69ab1c..b313a98 100644 --- a/src/types/api/content/external.ts +++ b/src/types/api/content/external.ts @@ -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; }; diff --git a/src/types/api/content/openiti.ts b/src/types/api/content/openiti.ts index 68c438c..6593199 100644 --- a/src/types/api/content/openiti.ts +++ b/src/types/api/content/openiti.ts @@ -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; }; diff --git a/src/types/api/content/pdf.ts b/src/types/api/content/pdf.ts index 07bfb69..8a0f656 100644 --- a/src/types/api/content/pdf.ts +++ b/src/types/api/content/pdf.ts @@ -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; }; diff --git a/src/types/api/content/turath.ts b/src/types/api/content/turath.ts index 9fa8b18..ff15a21 100644 --- a/src/types/api/content/turath.ts +++ b/src/types/api/content/turath.ts @@ -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: | { @@ -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; };