Unify portfolio and animal studies galleries
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
"use server";
|
||||
|
||||
import { Prisma } from "@/generated/prisma/browser";
|
||||
import type { Prisma } from "@/generated/prisma/browser";
|
||||
import { prisma } from "@/lib/prisma";
|
||||
|
||||
export type Cursor = {
|
||||
@ -11,7 +11,6 @@ export type Cursor = {
|
||||
export type PortfolioArtworkItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
slug: string;
|
||||
altText: string | null;
|
||||
|
||||
sortKey: number | null;
|
||||
@ -63,7 +62,8 @@ export async function getPortfolioArtworksPage(args: {
|
||||
|
||||
const year = coerceYear(filters.year ?? null);
|
||||
const q = normQ(filters.q);
|
||||
const albumId = filters.albumId && filters.albumId !== "all" ? filters.albumId : null;
|
||||
const albumId =
|
||||
filters.albumId && filters.albumId !== "all" ? filters.albumId : null;
|
||||
|
||||
const baseWhere: Prisma.ArtworkWhereInput = {
|
||||
...(onlyPublished ? { published: true } : {}),
|
||||
@ -79,10 +79,9 @@ export async function getPortfolioArtworksPage(args: {
|
||||
{
|
||||
OR: [
|
||||
{ name: { contains: q, mode: "insensitive" } },
|
||||
{ slug: { contains: q, mode: "insensitive" } },
|
||||
{ altText: { contains: q, mode: "insensitive" } },
|
||||
{ tags: { some: { name: { contains: q, mode: "insensitive" } } } },
|
||||
{ albums: { some: { name: { contains: q, mode: "insensitive" } } } },
|
||||
{
|
||||
tags: { some: { name: { contains: q, mode: "insensitive" } } },
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
@ -130,14 +129,15 @@ export async function getPortfolioArtworksPage(args: {
|
||||
},
|
||||
} satisfies Prisma.ArtworkSelect;
|
||||
|
||||
const mapRow = (r: any): PortfolioArtworkItem | null => {
|
||||
type ArtworkRow = Prisma.ArtworkGetPayload<{ select: typeof select }>;
|
||||
|
||||
const mapRow = (r: ArtworkRow): PortfolioArtworkItem | null => {
|
||||
const thumb = pickVariant(r.variants, "thumbnail");
|
||||
if (!thumb?.width || !thumb?.height) return null;
|
||||
|
||||
return {
|
||||
id: r.id,
|
||||
name: r.name,
|
||||
slug: r.slug,
|
||||
altText: r.altText ?? null,
|
||||
sortKey: r.sortKey ?? null,
|
||||
year: r.year ?? null,
|
||||
@ -171,20 +171,27 @@ export async function getPortfolioArtworksPage(args: {
|
||||
select,
|
||||
});
|
||||
|
||||
items = rowsA.map(mapRow).filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
items = rowsA
|
||||
.map(mapRow)
|
||||
.filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
|
||||
if (items.length >= take) {
|
||||
const last = items[items.length - 1]!;
|
||||
nextCursor = { afterSortKey: last.sortKey!, afterId: last.id };
|
||||
const last = items.at(-1);
|
||||
if (!last) {
|
||||
return { items, nextCursor: null, total, years, albums };
|
||||
}
|
||||
// last.sortKey can be null only in null-segment, which we are not in here.
|
||||
if (last.sortKey == null) {
|
||||
return { items, nextCursor: null, total, years, albums };
|
||||
}
|
||||
nextCursor = { afterSortKey: last.sortKey, afterId: last.id };
|
||||
return { items, nextCursor, total, years, albums };
|
||||
}
|
||||
|
||||
const remaining = take - items.length;
|
||||
const lastAId = items.length ? items[items.length - 1]!.id : null;
|
||||
|
||||
const whereB: Prisma.ArtworkWhereInput = {
|
||||
AND: [where, { sortKey: null }],
|
||||
...(lastAId ? { id: { gt: lastAId } } : {}),
|
||||
};
|
||||
|
||||
const rowsB = await prisma.artwork.findMany({
|
||||
@ -194,7 +201,9 @@ export async function getPortfolioArtworksPage(args: {
|
||||
select,
|
||||
});
|
||||
|
||||
const more = rowsB.map(mapRow).filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
const more = rowsB
|
||||
.map(mapRow)
|
||||
.filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
items = items.concat(more);
|
||||
|
||||
const last = items[items.length - 1];
|
||||
@ -218,11 +227,15 @@ export async function getPortfolioArtworksPage(args: {
|
||||
select,
|
||||
});
|
||||
|
||||
items = rowsB.map(mapRow).filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
items = rowsB
|
||||
.map(mapRow)
|
||||
.filter((x): x is PortfolioArtworkItem => x !== null);
|
||||
|
||||
const last = items[items.length - 1];
|
||||
nextCursor =
|
||||
items.length < take || !last ? null : { afterSortKey: null, afterId: last.id };
|
||||
items.length < take || !last
|
||||
? null
|
||||
: { afterSortKey: null, afterId: last.id };
|
||||
|
||||
return { items, nextCursor, total, years, albums };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user