Skip to content

Commit

Permalink
update cover image & handle new api schema & remove book fetchers
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedriad1 committed Dec 11, 2024
1 parent 0ebfd0b commit 3d9a9c7
Show file tree
Hide file tree
Showing 30 changed files with 187 additions and 499 deletions.
Binary file modified public/cover.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/app/[locale]/t/[bookId]/_components/ai-tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useTranslations } from "next-intl";
import type { TabProps } from "../sidebar/tabs";
import { usePageNavigation } from "../usePageNavigation";
import ChatForm from "./ChatForm";
import { HistoryIcon, InfoIcon, SquarePenIcon } from "lucide-react";
import { InfoIcon, SquarePenIcon } from "lucide-react";
import { config } from "@/lib/seo";
import { VersionAlert } from "../version-alert";
import SidebarContainer from "../sidebar/sidebar-container";
Expand Down Expand Up @@ -53,7 +53,7 @@ export default function AITab({ bookSlug, bookResponse }: TabProps) {
const isLoading = isPending;

const isVersionMismatch =
bookResponse.book.aiVersion !== bookResponse.content.versionId;
bookResponse.book.aiVersion !== bookResponse.content.id;

return (
<div className="pb-2">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import PageNavigator from "./page-navigator";
import { useMobileSidebar } from "../mobile-sidebar-provider";
import React, { useMemo } from "react";
import type { UsePageNavigationReturnType } from "../usePageNavigation";
import type { Openiti, Turath } from "@/types/ApiBookResponse";
import type { OpenitiContent } from "@/types/api/content/openiti";
import type { TurathContent } from "@/types/api/content/turath";

import { type TreeDataItem, TreeView } from "@/components/tree-view";
import { useRouter } from "@/navigation";
import { useParams, useSearchParams } from "next/navigation";
import { navigation } from "@/lib/urls";

type OpenitiChapter = NonNullable<Openiti["headings"]>[number];
type TurathChapter = NonNullable<Turath["headings"]>[number];
type OpenitiChapter = NonNullable<OpenitiContent["headings"]>[number];
type TurathChapter = NonNullable<TurathContent["headings"]>[number];

type BookDataItem = TreeDataItem & {
level: number;
Expand Down Expand Up @@ -78,7 +80,7 @@ export default function ChaptersList({
getVirtuosoScrollProps,
isSinglePage,
}: {
headers: NonNullable<Openiti["headings"] | Turath["headings"]>;
headers: NonNullable<OpenitiContent["headings"] | TurathContent["headings"]>;
pagesRange: UsePageNavigationReturnType["pagesRange"];
getVirtuosoScrollProps: UsePageNavigationReturnType["getVirtuosoScrollProps"];
isSinglePage?: boolean;
Expand Down
9 changes: 4 additions & 5 deletions src/app/[locale]/t/[bookId]/_components/content-tab/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import { Separator } from "@/components/ui/separator";
import SidebarContainer from "../sidebar/sidebar-container";
import PageNavigator from "./page-navigator";
import ChaptersList from "./chapters-section";
Expand All @@ -13,22 +12,22 @@ function ContentTab({ bookResponse, isSinglePage }: TabProps) {
const { pagesRange, getVirtuosoScrollProps } =
usePageNavigation(bookResponse);

const view = (useSearchParams().get("view") ?? "default") as
const _view = (useSearchParams().get("view") ?? "default") as
| "pdf"
| "default";
const view = bookResponse.content.source === "pdf" ? "pdf" : _view;

const bookContent = bookResponse.content;

const isExternal = bookContent.source === "external";

const headings = !isExternal ? bookContent.headings : [];

if (isExternal) return null;

let content;
if (view === "pdf") {
content = <PdfChaptersList />;
} else {
const headings = "headings" in bookContent ? bookContent.headings : [];

content = (
<>
{headings && headings.length > 0 && !isSinglePage ? (
Expand Down
14 changes: 8 additions & 6 deletions src/app/[locale]/t/[bookId]/_components/pdf-view/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"use client";

import type { TurathBookResponse } from "@/server/services/books";
import type {
Core,
WebViewerInstance,
Expand All @@ -14,13 +13,14 @@ 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: TurathBookResponse["turathResponse"]["pdf"];
pdf: TurathContent["pdf"];
}) {
const { resolvedTheme = "light" } = useTheme();
const viewerRef = useRef<HTMLDivElement>(null);
Expand All @@ -33,16 +33,18 @@ export default function PdfView({

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

isInitializedByUrl.set(pdfSource.finalUrl!, true);
if (isInitializedByUrl.get(pdfSource.fullBookUrl)) return;

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

const instance: WebViewerInstance = await WebViewer(
{
path: "/pdf-express", // point to where the files you copied are served from
initialDoc: pdfSource.finalUrl!, // path to your document
initialDoc: pdfSource.fullBookUrl, // path to your document
enableAnnotations: false,
disabledElements: [
// "selectToolButton",
Expand Down Expand Up @@ -89,7 +91,7 @@ export default function PdfView({
if (typeof window !== "undefined") {
initialize();
}
}, [pdfSource.finalUrl]);
}, [pdfSource]);

// when theme changes, update the theme of the viewer
useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Virtualizer } from "virtua";
import React, { forwardRef, memo, useMemo, useRef } from "react";
import { useReaderVirtuoso, useSetReaderScroller } from "../context";
import Footer from "@/app/_components/footer";
import type { ApiBookResponse } from "@/types/ApiBookResponse";
import type { ApiBookResponse } from "@/types/api/book";
import ReaderPage from "./reader-page";
import { READER_OVERSCAN_SIZE, READER_PAGINATION_SIZE } from "@/lib/constants";
import Container from "@/components/ui/container";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import RenderBlock from "@/components/render-markdown";
import { Skeleton } from "@/components/ui/skeleton";
import { getBook } from "@/lib/api";
import type { Openiti, Turath } from "@/types/ApiBookResponse";
import type { OpenitiContent } from "@/types/api/content/openiti";
import type { TurathContent } from "@/types/api/content/turath";
import { useQuery } from "@tanstack/react-query";
import { useTranslations } from "next-intl";
import { useParams } from "next/navigation";
import { type PropsWithChildren, useMemo } from "react";

type DefaultPages = NonNullable<Turath["pages"] | Openiti["pages"]>;
type DefaultPages = NonNullable<
TurathContent["pages"] | OpenitiContent["pages"]
>;

const PageLabel = (props: PropsWithChildren) => (
<p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import { Separator } from "@/components/ui/separator";
import { useDirection } from "@/lib/locale/utils";
import { navigation } from "@/lib/urls";
import { Link } from "@/navigation";
import type { ApiBookResponse } from "@/types/ApiBookResponse";
import { CheckIcon, ChevronDownIcon, XIcon } from "lucide-react";
import type { ApiBookResponse } from "@/types/api/book";
import { ChevronDownIcon } from "lucide-react";
import { useTranslations } from "next-intl";
import { Fragment } from "react";
import * as AccordionPrimitive from "@radix-ui/react-accordion";
import { cn } from "@/lib/utils";
import { useBookDetailsStore } from "../../_stores/book-details";
import Container from "@/components/ui/container";
// import { useNavbarStore } from "@/stores/navbar";

export default function BookInfoHeader({
bookResponse,
Expand Down Expand Up @@ -63,18 +62,17 @@ export default function BookInfoHeader({
);

const bookContent = bookResponse.content;
const isExternal = bookContent.source === "external";
const publicationDetails = !isExternal ? bookContent.publicationDetails : {};
const publicationDetails = bookContent.publicationDetails;

const renderPublicationDetails = () => {
if (!publicationDetails) return null;

const final: { title: string; text: string | React.ReactNode }[] = [];

if (publicationDetails.editor)
if (publicationDetails.investigator)
final.push({
title: t("reader.publication-details.editor"),
text: publicationDetails.editor,
text: publicationDetails.investigator,
});

if (publicationDetails.publisher)
Expand All @@ -83,26 +81,10 @@ export default function BookInfoHeader({
text: publicationDetails.publisher,
});

if (publicationDetails.printVersion)
if (publicationDetails.editionNumber)
final.push({
title: t("reader.publication-details.print-version"),
text: publicationDetails.printVersion,
});

if (publicationDetails.volumes)
final.push({
title: t("reader.publication-details.volumes"),
text: publicationDetails.volumes,
});

if (publicationDetails.pageNumbersMatchPrint !== undefined)
final.push({
title: t("reader.publication-details.page-numbers-match-print"),
text: publicationDetails.pageNumbersMatchPrint ? (
<CheckIcon className="size-4" />
) : (
<XIcon className="size-4" />
),
text: publicationDetails.editionNumber,
});

return final;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { ButtonProps } from "@/components/ui/button";
import type { TurathBookResponse } from "@/server/services/books";
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?: TurathBookResponse["turathResponse"]["pdf"] | null;
pdf?: TurathContent["pdf"];
slug: string;
}) {
const t = useTranslations("reader");
Expand All @@ -19,10 +19,10 @@ export default function DownloadButton({
tooltipProps: { side: "bottom" },
};

if (pdf?.finalUrl) {
if (pdf && "fullBookUrl" in pdf) {
return (
<ReaderNavigationButton {...commonProps} asChild>
<a href={pdf?.finalUrl} download={slug + ".pdf"} target="_blank">
<a href={pdf.fullBookUrl} download={slug + ".pdf"} target="_blank">
<ArrowDownTrayIcon />
</a>
</ReaderNavigationButton>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import Container from "@/components/ui/container";
import VersionSelector from "./version-selector";
import type { ApiBookResponse } from "@/types/ApiBookResponse";
import type { ApiBookResponse } from "@/types/api/book";
import DownloadButton from "./download-button";
import ViewTabs from "./view-tabs";
import BookInfoHeader from "./book-info-header";
Expand Down Expand Up @@ -34,7 +34,7 @@ export default function ReaderNavigation({
(typeof tabs)[number]["id"] | null
>(null);

const versionId = bookResponse.content.versionId;
const versionId = bookResponse.content.id;

const pdf =
bookResponse.content.source === "turath"
Expand All @@ -61,12 +61,12 @@ export default function ReaderNavigation({

<VersionSelector
versions={bookResponse.book.versions}
versionId={bookResponse.content.versionId}
versionId={bookResponse.content.id}
/>
</div>

<div className="flex flex-1 justify-center">
<ViewTabs hasPdf={!!pdf?.finalUrl} />
<ViewTabs hasPdf={!!pdf && "fullBookUrl" in 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 @@ -12,23 +12,23 @@ import { EllipsisIcon, FileText } from "lucide-react";
import ReaderNavigationButton from "./navigation-button";
import { useGetBookUrl, useReaderView } from "./utils";
import { useTranslations } from "next-intl";
import type { TurathBookResponse } from "@/server/services/books";
import { Link } from "@/navigation";
import type { TurathContent } from "@/types/api/content/turath";

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

const hasPdfView = !!pdf?.finalUrl;
const hasPdfView = !!pdf && "fullBookUrl" in pdf;

return (
<DropdownMenu>
Expand Down Expand Up @@ -60,9 +60,13 @@ export default function ReaderNavigationMobileActions({
<DropdownMenuSeparator />

<DropdownMenuGroup>
{pdf?.finalUrl ? (
{hasPdfView ? (
<DropdownMenuItem asChild className="gap-2">
<a href={pdf?.finalUrl} download={slug + ".pdf"} target="_blank">
<a
href={pdf.fullBookUrl}
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
Expand Up @@ -29,12 +29,12 @@ export default function VersionSelector({

const [selectedVersion, setSelectedVersion] = useState(() => {
const version = versionId
? versions.find((v) => v.value === versionId)?.value ?? versions[0]?.value
: versions[0]?.value;
? versions.find((v) => v.id === versionId)?.id ?? versions[0]?.id
: versions[0]?.id;

return version;
});
const selectedVersionObj = versions.find((v) => v.value === selectedVersion);
const selectedVersionObj = versions.find((v) => v.id === selectedVersion);

const handleVersionChange = (newVersion: string) => {
setSelectedVersion(newVersion);
Expand Down Expand Up @@ -71,7 +71,7 @@ export default function VersionSelector({

<SelectContent>
{versions.map((version, idx) => (
<SelectItem key={idx} value={version.value}>
<SelectItem key={idx} value={version.id}>
{versionToName(version)}
</SelectItem>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function SearchTab({ bookSlug, bookResponse }: TabProps) {
const { value, setValue, results, setResults } = useSearchStore();

const isVersionMismatch =
bookResponse.book.aiVersion !== bookResponse.content.versionId;
bookResponse.book.aiVersion !== bookResponse.content.id;

const { mutateAsync, isPending, error } = useMutation<
SemanticSearchBookNode[],
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/t/[bookId]/_components/sidebar/tabs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ApiBookResponse } from "@/types/ApiBookResponse";
import type { ApiBookResponse } from "@/types/api/book";

import {
MagnifyingGlassIcon,
Expand Down
4 changes: 2 additions & 2 deletions src/app/[locale]/t/[bookId]/_components/version-alert.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { versionToName } from "@/lib/version";
import { usePathname, useRouter } from "@/navigation";
import type { ApiBookResponse } from "@/types/ApiBookResponse";
import type { ApiBookResponse } from "@/types/api/book";
import { InfoIcon } from "lucide-react";
import { useTranslations } from "next-intl";
import { useSearchParams } from "next/navigation";
Expand All @@ -22,7 +22,7 @@ export const VersionAlert = ({
const searchParams = useSearchParams();
const router = useRouter();

const versionObj = versions.find((v) => v.value === versionId);
const versionObj = versions.find((v) => v.id === versionId);

if (!versionObj) {
return null;
Expand Down
Loading

0 comments on commit 3d9a9c7

Please sign in to comment.