feat: query portfolio in site home
This commit is contained in:
parent
1d918cba8a
commit
cd591ef5a4
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ async function SiteIndexPage({
|
|||
await prefetchGetPagesBySite(
|
||||
{
|
||||
characterId: site?.characterId,
|
||||
type: "post",
|
||||
type: ["post", "portfolio"],
|
||||
visibility: PageVisibilityEnum.Published,
|
||||
useStat: true,
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
})
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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}
|
||||
]
|
||||
}
|
||||
],
|
||||
},
|
||||
}
|
||||
`
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue