Skip to content

Commit

Permalink
Merge pull request #22 from seemorg/dev
Browse files Browse the repository at this point in the history
Production deployment
  • Loading branch information
ahmedriad1 authored Sep 6, 2024
2 parents 1abad73 + d532fa1 commit ba06c20
Show file tree
Hide file tree
Showing 22 changed files with 189 additions and 116 deletions.
13 changes: 13 additions & 0 deletions src/app/[locale]/(rootEntityPages)/regions/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RootEntityPage from "../root-entity-page";
import { getTranslations } from "next-intl/server";
import { getMetadata } from "@/lib/seo";
import { navigation } from "@/lib/urls";
import { InfoIcon } from "lucide-react";

type PageProps = InferPagePropsType<RouteType>;

Expand Down Expand Up @@ -41,6 +42,18 @@ async function RegionsPage({ searchParams }: PageProps) {
entity: t("regions"),
})}
>
<p className="-mt-14 mb-12 flex items-center">
<InfoIcon className="mr-1 size-4 rtl:ml-1" />
Regions on Usul are based on the&nbsp;
<a
href="https://althurayya.github.io/"
target="_blank"
className="underline"
>
Turayya project
</a>
</p>

<SearchResults
response={results.results}
pagination={results.pagination}
Expand Down
2 changes: 1 addition & 1 deletion src/app/[locale]/(rootEntityPages)/root-entity-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function RootEntityPage({
}: {
children: React.ReactNode;
title: string;
description?: string;
description?: React.ReactNode;
}) {
return (
<>
Expand Down
18 changes: 5 additions & 13 deletions src/components/author-search-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,17 @@ import {
const AuthorSearchResult = async ({
result,
}: {
result: NonNullable<
Awaited<ReturnType<typeof searchAuthors>>["results"]["hits"]
>[number];
result: Awaited<ReturnType<typeof searchAuthors>>["results"]["hits"][number];
}) => {
const t = await getTranslations();
const pathLocale = await getPathLocale();

const { document } = result;

// const primaryArabicName = highlight?.primaryArabicName?.snippet
// ? highlight.primaryArabicName.snippet
// : document.primaryArabicName;
// const primaryLatinName = highlight?.primaryLatinName?.snippet
// ? highlight.primaryLatinName.snippet
// : document.primaryLatinName;
const primaryName = getPrimaryLocalizedText(
document.primaryNames,
pathLocale,
);
const primaryName =
document.transliteration && pathLocale === "en"
? document.transliteration
: getPrimaryLocalizedText(document.primaryNames, pathLocale);
const secondaryName = getSecondaryLocalizedText(
document.primaryNames,
pathLocale,
Expand Down
4 changes: 2 additions & 2 deletions src/components/authors-filter/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import { useSearchParams } from "next/navigation";
import { useEffect, useMemo, useRef, useState, useTransition } from "react";
import { Button } from "@/components/ui/button";
import type { SearchResponse } from "typesense/lib/Typesense/Documents";
import type { AuthorDocument } from "@/types/author";
import FilterContainer from "../search-results/filter-container";
import type { findAllAuthorIdsWithBooksCount } from "@/server/services/authors";
Expand All @@ -12,6 +11,7 @@ import { useFormatter, useTranslations } from "next-intl";
import { searchAuthors } from "@/server/typesense/author";
import { usePathLocale } from "@/lib/locale/utils";
import { getPrimaryLocalizedText } from "@/server/db/localization";
import type { TypesenseResponse } from "@/server/typesense/utils";

const getAuthorsFilterUrlParams = (
authors: string[],
Expand Down Expand Up @@ -39,7 +39,7 @@ interface AuthorsFilterProps {
initialAuthorsResponse: Awaited<ReturnType<typeof searchAuthors>>;
booksCount: Awaited<ReturnType<typeof findAllAuthorIdsWithBooksCount>>;
currentAuthors: string[];
selectedAuthorsResponse: SearchResponse<AuthorDocument> | null;
selectedAuthorsResponse: TypesenseResponse<AuthorDocument> | null;
filters?: Record<string, any>;
}

Expand Down
37 changes: 21 additions & 16 deletions src/components/book-search-result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,29 @@ const BookSearchResult = ({
view,
}: {
view?: View;
result: NonNullable<
Awaited<ReturnType<typeof searchBooks>>["results"]["hits"]
>[number];
result: Awaited<ReturnType<typeof searchBooks>>["results"]["hits"][number];
}) => {
const t = useTranslations();
const pathLocale = usePathLocale();
const { document } = result;

const { primaryNames, author } = document;

const title = getPrimaryLocalizedText(primaryNames, pathLocale) ?? "";
const title =
document.transliteration && pathLocale === "en"
? document.transliteration
: getPrimaryLocalizedText(primaryNames, pathLocale) ?? "";
const secondaryTitle = getSecondaryLocalizedText(primaryNames, pathLocale);

const authorPrimaryNames =
author?.primaryNames ?? (author as any)?.primaryNameTranslations;
const authorName = authorPrimaryNames
? getPrimaryLocalizedText(authorPrimaryNames, pathLocale)
: undefined;

const authorName =
author.transliteration && pathLocale === "en"
? author.transliteration
: authorPrimaryNames
? getPrimaryLocalizedText(authorPrimaryNames, pathLocale)
: undefined;
const authorSecondaryName = author?.primaryNames
? getSecondaryLocalizedText(author.primaryNames, pathLocale)
: undefined;
Expand Down Expand Up @@ -68,22 +73,22 @@ const BookSearchResult = ({
title={title}
/>

{secondaryTitle && (
<p
className="mt-1 text-wrap text-right"
dangerouslySetInnerHTML={{ __html: secondaryTitle }}
title={secondaryTitle}
/>
)}

{authorName && (
<p
className="mt-2 text-wrap text-right text-sm text-muted-foreground"
className="mt-1 line-clamp-2 text-wrap text-right text-sm text-muted-foreground"
title={authorName}
>
{authorName}
</p>
)}

{secondaryTitle && (
<p
className="mt-1 text-wrap text-right"
dangerouslySetInnerHTML={{ __html: secondaryTitle }}
title={secondaryTitle}
/>
)}
</div>
</Link>
</div>
Expand Down
4 changes: 1 addition & 3 deletions src/components/region-search-result.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import {
export default async function RegionSearchResult({
result,
}: {
result: NonNullable<
Awaited<ReturnType<typeof searchRegions>>["results"]["hits"]
>[number];
result: Awaited<ReturnType<typeof searchRegions>>["results"]["hits"][number];
}) {
const t = await getTranslations();
const pathLocale = await getPathLocale();
Expand Down
8 changes: 3 additions & 5 deletions src/components/search-results/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { Suspense } from "react";
import Paginator from "@/components/ui/pagination";
import type { Pagination } from "@/types/pagination";
import type { SearchResponse } from "typesense/lib/Typesense/Documents";
import type { TypesenseResponse } from "@/server/typesense/utils";
import SearchSort from "./sort";
import {
Drawer,
Expand All @@ -21,10 +21,8 @@ import type { View } from "@/validation/view";
import type { Sort } from "@/types/sort";

interface SearchResultsProps<T extends object & { id: string }> {
response: SearchResponse<T>;
renderResult: (
result: NonNullable<SearchResponse<T>["hits"]>[number],
) => JSX.Element;
response: TypesenseResponse<T>;
renderResult: (result: TypesenseResponse<T>["hits"][number]) => JSX.Element;
pagination?: Pagination;
emptyMessage?: string;
sorts: Sort[];
Expand Down
13 changes: 10 additions & 3 deletions src/server/services/authors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { PathLocale } from "@/lib/locale/utils";
import { db } from "../db";
import { cache } from "react";
import { getLocaleWhereClause } from "../db/localization";
import { unstable_cache } from "next/cache";

export const findAuthorBySlug = cache(
async (slug: string, locale: PathLocale = "en") => {
Expand Down Expand Up @@ -55,6 +56,12 @@ export const findAllAuthorIdsWithBooksCount = cache(async () => {
});
});

export const countAllAuthors = cache(async () => {
return await db.author.count();
});
export const countAllAuthors = cache(
unstable_cache(
async () => {
return await db.author.count();
},
["authors-count"],
{ revalidate: false },
),
);
13 changes: 10 additions & 3 deletions src/server/services/books.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getLocaleWhereClause } from "../db/localization";
import { fetchTurathBook } from "./book-fetchers/turath";
import { fetchOpenitiBook } from "./book-fetchers/openiti";
import { log } from "next-axiom";
import { unstable_cache } from "next/cache";

export type TurathBookResponse = {
source: "turath";
Expand Down Expand Up @@ -131,9 +132,15 @@ export const fetchBook = cache(
},
);

export const countAllBooks = cache(async () => {
return await db.book.count();
});
export const countAllBooks = cache(
unstable_cache(
async () => {
return await db.book.count();
},
["books-count"],
{ revalidate: false },
),
);

export const getPopularBooks = cache(async () => {
const result = await db.book.findMany({
Expand Down
15 changes: 11 additions & 4 deletions src/server/services/genres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { cache } from "react";
import { db } from "../db";
import { unstable_cache } from "next/cache";

export const findAllGenres = cache(async () => {
return await db.genre.findMany();
Expand Down Expand Up @@ -72,7 +73,13 @@ export const findAllGenresWithBooksCount = cache(
},
);

export const countAllGenres = cache(async () => {
const result = await db.genre.count();
return result;
});
export const countAllGenres = cache(
unstable_cache(
async () => {
const result = await db.genre.count();
return result;
},
["genres-count"],
{ revalidate: false },
),
);
13 changes: 10 additions & 3 deletions src/server/services/regions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { cache } from "react";
import { db } from "../db";
import { getLocaleWhereClause } from "../db/localization";
import type { PathLocale } from "@/lib/locale/utils";
import { unstable_cache } from "next/cache";

export const findAllRegionsWithCounts = cache(
async (locale: PathLocale = "en") => {
Expand Down Expand Up @@ -139,6 +140,12 @@ export const findAllRegionsWithBooksCount = cache(
},
);

export const countAllRegions = cache(async () => {
return await db.region.count();
});
export const countAllRegions = cache(
unstable_cache(
async () => {
return await db.region.count();
},
["regions-count"],
{ revalidate: false },
),
);
15 changes: 11 additions & 4 deletions src/server/services/years.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { cache } from "react";
import descriptions from "~/data/centuries.json";
import { db } from "../db";
import { unstable_cache } from "next/cache";

export const findAllYearRanges = cache(async () => {
const counts = await countAllBooksByCentury();
Expand Down Expand Up @@ -39,8 +40,10 @@ export const findYearRangeBySlug = cache(async (slug: string | number) => {
return allRanges.find((r) => r.centuryNumber === Number(slug));
});

export const countAllBooksByCentury = cache(async () => {
const result = await db.$queryRaw<{ century: number; count: number }[]>`
export const countAllBooksByCentury = cache(
unstable_cache(
async () => {
const result = await db.$queryRaw<{ century: number; count: number }[]>`
SELECT
FLOOR("Author"."year" / 100) + 1 AS "century",
COUNT("Book"."id")::int as count
Expand All @@ -49,5 +52,9 @@ LEFT JOIN "Author" ON "Book"."authorId" = "Author"."id"
GROUP BY "century"
`;

return result;
});
return result;
},
["years-count"],
{ revalidate: false },
),
);
35 changes: 16 additions & 19 deletions src/server/typesense/author.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
"use server";

import type { SearchResponse } from "typesense/lib/Typesense/Documents";
import { type SearchOptions, makePagination, prepareQuery } from "./utils";
import {
type SearchOptions,
makePagination,
prepareQuery,
prepareResults,
} from "./utils";
import type { AuthorDocument } from "@/types/author";
import { makeSearchRequest } from "@/lib/typesense";
import { AUTHORS_COLLECTION } from "./config";
Expand All @@ -11,30 +14,24 @@ export const searchAuthors = async (q: string, options?: SearchOptions) => {
options ?? {};

const yearRange = (options?.filters?.yearRange ?? null) as number[] | null;
const geographies = (options?.filters?.geographies ?? null) as
| string[]
| null;
// const geographies = (options?.filters?.geographies ?? null) as
// | string[]
// | null;
const regions = (options?.filters?.regions ?? null) as string[] | null;
const ids = (options?.filters?.ids ?? null) as string[] | null;

const filters: string[] = [];
if (yearRange) filters.push(`year:[${yearRange[0]}..${yearRange[1]}]`);

if (geographies && geographies.length > 0) {
filters.push(
`geographies:[${geographies.map((geo) => `\`${geo}\``).join(", ")}]`,
);
}
// if (geographies && geographies.length > 0) {
// filters.push(
// `geographies:[${geographies.map((geo) => `\`${geo}\``).join(", ")}]`,
// );
// }

if (regions && regions.length > 0) {
filters.push(
`regions:[${regions
.flatMap((region) => {
return ["born", "died", "visited", "resided"].map(
(type) => `\`${type}@${region}\``,
);
})
.join(", ")}]`,
`regions:[${regions.map((region) => `\`${region}\``).join(", ")}]`,
);
}

Expand All @@ -55,7 +52,7 @@ export const searchAuthors = async (q: string, options?: SearchOptions) => {
})) as SearchResponse<AuthorDocument>;

return {
results,
results: prepareResults(results),
pagination: makePagination(results.found, results.page, limit),
};
};
Loading

0 comments on commit ba06c20

Please sign in to comment.