feat: query portfolio in site home

This commit is contained in:
DIYgod 2023-08-04 22:19:34 +01:00
parent 1d918cba8a
commit cd591ef5a4
No known key found for this signature in database
7 changed files with 68 additions and 20 deletions

View File

@ -7,7 +7,7 @@ export async function GET(req: Request) {
const result = await getPagesBySite({
characterId: +(query.characterId || 0) as number,
type: query.type as "post" | "page",
type: query.type,
visibility: query.visibility as PageVisibilityEnum,
limit: query.limit ? parseInt(query.limit as string) : undefined,
cursor: query.cursor as string,

View File

@ -19,7 +19,7 @@ async function SiteIndexPage({
await prefetchGetPagesBySite(
{
characterId: site?.characterId,
type: "post",
type: ["post", "portfolio"],
visibility: PageVisibilityEnum.Published,
useStat: true,
},

View File

@ -9,7 +9,7 @@ export default function SiteHome({ handle }: { handle: string }) {
const site = useGetSite(handle)
const posts = useGetPagesBySiteLite({
characterId: site.data?.characterId,
type: "post",
type: ["post", "portfolio"],
visibility: PageVisibilityEnum.Published,
useStat: true,
})

15
src/lib/search-params.ts Normal file
View File

@ -0,0 +1,15 @@
export default function createSearchParams(
params: Record<string, string | string[]>,
) {
const searchParams = new URLSearchParams()
Object.entries(params).forEach(([key, values]) => {
if (Array.isArray(values)) {
values.forEach((value) => {
searchParams.append(key, value)
})
} else {
searchParams.append(key, values)
}
})
return searchParams
}

View File

@ -7,7 +7,15 @@ export const getQuery = (req: Request) => {
const obj = {} as Record<string, any>
for (const [key, value] of searchParams.entries()) {
obj[key] = value
if (obj[key]) {
if (Array.isArray(obj[key])) {
obj[key].push(value)
} else {
obj[key] = [obj[key], value]
}
} else {
obj[key] = value
}
}
return obj

View File

@ -157,7 +157,7 @@ const getLocalPages = (input: {
export async function getPagesBySite(input: {
characterId?: number
type: NoteType
type: NoteType | NoteType[]
visibility?: PageVisibilityEnum
limit?: number
cursor?: string
@ -180,7 +180,7 @@ export async function getPagesBySite(input: {
let pinnedNote: NoteEntity | null = null
let pinnedNoteId: number | null = null
if (input.type === "post") {
if (input.type === "post" || input.type.includes("post")) {
if (!input.cursor) {
pinnedNote = await getPinnedPage(input)
pinnedNoteId = pinnedNote?.noteId || null
@ -199,14 +199,18 @@ export async function getPagesBySite(input: {
},
`
: ""
const tagsQuery = [...(input.tags || []), input.type]
.map(
if (typeof input.type === "string") {
input.type = [input.type]
}
const typesQuery = input.type
?.map(
(tag) => `{
content: {
path: "tags",
array_contains: "${tag}"
}
}`,
content: {
path: "tags",
array_contains: ["${tag}"]
}
}`,
)
.join(", ")
const whereQuery = `
@ -223,11 +227,31 @@ export async function getPagesBySite(input: {
equals: false,
},
metadata: {
content: {
path: "sources",
array_contains: "xlog"
},
AND: [${tagsQuery}]
AND: [
{
content: {
path: "sources",
array_contains: "xlog"
},
},
${
input.tags
? `
{
content: {
path: "tags",
array_contains: ${JSON.stringify(input.tags)}
}
},
`
: ""
}
{
OR: [
${typesQuery}
]
}
],
},
}
`

View File

@ -25,6 +25,7 @@ import {
useQueryClient,
} from "@tanstack/react-query"
import createSearchParams from "~/lib/search-params"
import { NoteType } from "~/lib/types"
import * as pageModel from "~/models/page.model"
@ -37,10 +38,10 @@ export const useGetPagesBySiteLite = (
const result: ReturnType<typeof pageModel.getPagesBySite> = await (
await fetch(
"/api/pages?" +
new URLSearchParams({
createSearchParams({
...input,
...(pageParam && { cursor: pageParam }),
} as any),
}),
)
).json()
return result