feat: get home feed in server side

This commit is contained in:
DIYgod 2023-07-13 22:02:21 +01:00
parent a2ba8d6a9a
commit b0a92e0dba
No known key found for this signature in database
5 changed files with 43 additions and 21 deletions

View File

@ -38,9 +38,7 @@ export default async function Topic({
await prefetchGetFeed(
{
type: "topic",
topicIncludeKeywords: info?.includeKeywords,
topicIncludeTags: info?.includeTags,
noteIds: info?.notes,
topic: params.topic,
},
queryClient,
)

21
src/app/api/feed/route.ts Normal file
View File

@ -0,0 +1,21 @@
import { NextServerResponse, getQuery } from "~/lib/server-helper"
import { getFeed } from "~/models/home.model"
export async function GET(req: Request) {
const query = getQuery(req)
const result = await getFeed({
type: query.type,
cursor: query.cursor,
limit: +query.limit || undefined,
characterId: +query.characterId || undefined,
daysInterval: +query.daysInterval || undefined,
searchKeyword: query.searchKeyword,
searchType: query.searchType,
tag: query.tag,
useHTML: false,
topic: query.topic,
})
const res = new NextServerResponse()
return res.status(200).json(result)
}

View File

@ -232,9 +232,7 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => {
const info = topics.find((t) => t.name === params.topic)
feedConfig = {
type,
topicIncludeKeywords: info?.includeKeywords,
topicIncludeTags: info?.includeTags,
noteIds: info?.notes,
topic: params.topic,
}
break
case "hottest":

View File

@ -8,6 +8,7 @@ import { ExpandedNote } from "~/lib/types"
import { client } from "~/queries/graphql"
import filter from "../../data/filter.json"
import topics from "../../data/topics.json"
export type FeedType =
| "latest"
@ -24,27 +25,23 @@ export async function getFeed({
cursor,
limit = 30,
characterId,
noteIds,
daysInterval,
searchKeyword,
searchType,
tag,
useHTML,
topicIncludeKeywords,
topicIncludeTags,
topic,
}: {
type?: FeedType
cursor?: string
limit?: number
characterId?: number
noteIds?: string[]
daysInterval?: number
searchKeyword?: string
searchType?: SearchType
tag?: string
useHTML?: boolean
topicIncludeKeywords?: string[]
topicIncludeTags?: string[]
topic?: string
}) {
if (type === "search" && !searchKeyword) {
type = "latest"
@ -265,7 +262,9 @@ export async function getFeed({
}
}
case "topic": {
if (!topicIncludeKeywords && !topicIncludeTags && !noteIds) {
const info = topics.find((t) => t.name === topic)
if (!info) {
return {
list: [],
cursor: "",
@ -274,18 +273,18 @@ export async function getFeed({
}
const includeString = [
...(topicIncludeKeywords?.map(
...(info.includeKeywords?.map(
(topicIncludeKeyword) =>
`{ content: { path: "title", string_contains: "${topicIncludeKeyword}" } }, { content: { path: "content", string_contains: "${topicIncludeKeyword}" } }`,
) || []),
...(topicIncludeTags?.map(
...(info.includeTags?.map(
(topicIncludeTag) =>
`{ content: { path: "tags", array_contains: "${topicIncludeTag}" } },`,
) || []),
].join("\n")
if (noteIds) {
const orString = noteIds
if (info.notes) {
const orString = info.notes
.map(
(note) =>
`{ noteId: { equals: ${

View File

@ -8,10 +8,16 @@ export const useGetFeed = (data?: Parameters<typeof homeModel.getFeed>[0]) => {
return useInfiniteQuery({
queryKey: ["getFeed", data],
queryFn: async ({ pageParam }) => {
return homeModel.getFeed({
...data,
cursor: pageParam,
})
const result: ReturnType<typeof homeModel.getFeed> = await (
await fetch(
"/api/feed?" +
new URLSearchParams({
...data,
...(pageParam && { cursor: pageParam }),
} as any),
)
).json()
return result
},
getNextPageParam: (lastPage) => lastPage?.cursor || undefined,
refetchOnWindowFocus: false,