From b0a92e0dba0e1241fd174ed41747bd06bb95eac1 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Thu, 13 Jul 2023 22:02:21 +0100 Subject: [PATCH] feat: get home feed in server side --- .../(activities)/topic/[topic]/page.tsx | 4 +--- src/app/api/feed/route.ts | 21 +++++++++++++++++++ src/components/home/HomeFeed.tsx | 4 +--- src/models/home.model.ts | 21 +++++++++---------- src/queries/home.ts | 14 +++++++++---- 5 files changed, 43 insertions(+), 21 deletions(-) create mode 100644 src/app/api/feed/route.ts diff --git a/src/app/(home)/(activities)/topic/[topic]/page.tsx b/src/app/(home)/(activities)/topic/[topic]/page.tsx index e8b4aa97..31f11f62 100644 --- a/src/app/(home)/(activities)/topic/[topic]/page.tsx +++ b/src/app/(home)/(activities)/topic/[topic]/page.tsx @@ -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, ) diff --git a/src/app/api/feed/route.ts b/src/app/api/feed/route.ts new file mode 100644 index 00000000..22325927 --- /dev/null +++ b/src/app/api/feed/route.ts @@ -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) +} diff --git a/src/components/home/HomeFeed.tsx b/src/components/home/HomeFeed.tsx index c201ab98..5cde321b 100644 --- a/src/components/home/HomeFeed.tsx +++ b/src/components/home/HomeFeed.tsx @@ -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": diff --git a/src/models/home.model.ts b/src/models/home.model.ts index ea5fc7df..5c897fc5 100644 --- a/src/models/home.model.ts +++ b/src/models/home.model.ts @@ -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: ${ diff --git a/src/queries/home.ts b/src/queries/home.ts index 0b8a5a0a..9fc942a4 100644 --- a/src/queries/home.ts +++ b/src/queries/home.ts @@ -8,10 +8,16 @@ export const useGetFeed = (data?: Parameters[0]) => { return useInfiniteQuery({ queryKey: ["getFeed", data], queryFn: async ({ pageParam }) => { - return homeModel.getFeed({ - ...data, - cursor: pageParam, - }) + const result: ReturnType = await ( + await fetch( + "/api/feed?" + + new URLSearchParams({ + ...data, + ...(pageParam && { cursor: pageParam }), + } as any), + ) + ).json() + return result }, getNextPageParam: (lastPage) => lastPage?.cursor || undefined, refetchOnWindowFocus: false,