Skip to content

Commit

Permalink
Merge pull request #46 from seemorg/dev
Browse files Browse the repository at this point in the history
Production deployment
  • Loading branch information
ahmedriad1 authored Dec 12, 2024
2 parents b19aff7 + d5c3144 commit 1e13214
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 78 deletions.
2 changes: 1 addition & 1 deletion src/app/[locale]/t/[bookId]/[pageNumber]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const generateMetadata = async ({
title: book.primaryName,
pagePath: navigation.books.reader(bookId),
keywords: [
...book.otherNames,
...(book.otherNames ? book.otherNames : []),
...(book.secondaryName ? [book.secondaryName] : []),
...(book.secondaryOtherNames ?? []),
],
Expand Down
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
43 changes: 30 additions & 13 deletions src/app/[locale]/t/[bookId]/_components/reader-navigation/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@ import { tabs } from "../sidebar/tabs";
import { useState } from "react";
import { Drawer, DrawerContent } from "@/components/ui/drawer";

const getPdfUrl = (bookResponse: ApiBookResponse) => {
if (bookResponse.content.source === "pdf") {
return bookResponse.content.url;
}

if ("pdfUrl" in bookResponse.content) {
return bookResponse.content.pdfUrl;
}

return undefined;
};

export default function ReaderNavigation({
bookResponse,
isSinglePage,
Expand All @@ -35,11 +47,7 @@ export default function ReaderNavigation({
>(null);

const versionId = bookResponse.content.id;

const pdf =
bookResponse.content.source === "turath"
? bookResponse.content.pdf
: undefined;
const pdf = getPdfUrl(bookResponse);

return (
<>
Expand All @@ -66,21 +74,30 @@ export default function ReaderNavigation({
</div>

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

<div className="hidden flex-1 items-center gap-2 md:flex md:justify-end">
<DownloadButton pdf={pdf} slug={bookSlug} />

<ReaderNavigationButton
tooltip={t(isSinglePage ? "all-pages" : "single-page")}
tooltipProps={{ side: "bottom" }}
asChild
>
<Link href={bookUrl}>
{bookResponse.content.source === "pdf" ||
bookResponse.content.source === "external" ? (
<ReaderNavigationButton disabled>
<SinglePageIcon />
</ReaderNavigationButton>
) : (
<Link href={bookUrl}>
<ReaderNavigationButton
tooltip={t(isSinglePage ? "all-pages" : "single-page")}
tooltipProps={{ side: "bottom" }}
>
<SinglePageIcon />
</ReaderNavigationButton>
</Link>
</ReaderNavigationButton>
)}
</div>

<div className="flex flex-row-reverse items-center gap-1 md:hidden">
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { useTranslations } from "next-intl";
import { useReaderView } from "./utils";
import type { ApiBookResponse } from "@/types/api/book";

export default function ViewTabs({ hasPdf }: { hasPdf?: boolean }) {
export default function ViewTabs({
hasPdf,
contentSource,
}: {
hasPdf: boolean;
contentSource: ApiBookResponse["content"]["source"];
}) {
const t = useTranslations("reader");
const { view, setView } = useReaderView();
const isPdfSource = contentSource === "pdf";

return (
<Tabs
defaultValue="ebook"
value={view === "pdf" ? "pdf" : "ebook"}
value={isPdfSource || view === "pdf" ? "pdf" : "ebook"}
onValueChange={setView as any}
className="hidden md:inline-flex"
>
<TabsList>
<TabsTrigger value="ebook">{t("e-book")}</TabsTrigger>
<TabsTrigger value="ebook" disabled={isPdfSource}>
{t("e-book")}
</TabsTrigger>

<TabsTrigger value="pdf" disabled={!hasPdf}>
{t("pdf")}
</TabsTrigger>
Expand Down
5 changes: 3 additions & 2 deletions src/app/[locale]/t/[bookId]/_components/usePageNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { TabProps } from "./sidebar/tabs";

export const usePageNavigation = (bookResponse: TabProps["bookResponse"]) => {
const source = bookResponse.content.source;
const total = bookResponse.pagination.total;

if (source === "external") {
if (source === "external" || source === "pdf") {
return {
pagesRange: {
start: 0,
Expand All @@ -18,6 +17,8 @@ export const usePageNavigation = (bookResponse: TabProps["bookResponse"]) => {
};
}

const total = bookResponse.pagination.total;

const firstPage = 1;
const lastPage = total;

Expand Down
17 changes: 9 additions & 8 deletions src/app/[locale]/t/[bookId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export const generateMetadata = async ({
title: book.primaryName,
pagePath: navigation.books.reader(bookId),
keywords: [
...book.otherNames,
...(book.otherNames ? book.otherNames : []),
...(book.secondaryName ? [book.secondaryName] : []),
...(book.secondaryOtherNames ?? []),
],
Expand Down Expand Up @@ -90,6 +90,7 @@ export default async function SidebarContent({
notFound();
}

// if it's an alternate slug, redirect to the primary slug
if ("type" in response) {
const params = new URLSearchParams(searchParams);
const paramsString = params.size > 0 ? `?${params.toString()}` : "";
Expand Down Expand Up @@ -126,13 +127,13 @@ export default async function SidebarContent({
</div>
);
} else if (response.content.source === "pdf") {
readerContent = (
<article>
<h1>Coming Soon</h1>
</article>
);
} else if (view === "pdf" && "pdf" in response.content) {
readerContent = <PdfView pdf={response.content.pdf} />;
readerContent = <PdfView pdf={response.content.url} />;
} 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 1e13214

Please sign in to comment.