54 lines
2.0 KiB
TypeScript
54 lines
2.0 KiB
TypeScript
import { useInfiniteQuery, useQuery } from '@tanstack/react-query'
|
|
import { listMyTalksInfiniteOptions, listTalksInfiniteOptions, talkGroupsOptions } from '../api/client/@tanstack/react-query.gen'
|
|
import { TalkTopic, type TalkData } from '../api/client/types.gen'
|
|
import { useRegion } from './regionContext'
|
|
|
|
// 글 주제와 피드 묶음 이름
|
|
export const TOPICS = [
|
|
[TalkTopic.LOCAL, '동네 이야기'],
|
|
[TalkTopic.SKY, '하늘·노을'],
|
|
[TalkTopic.RAIN, '비·눈'],
|
|
[TalkTopic.FOLIAGE, '단풍·꽃'],
|
|
[TalkTopic.OUTFIT, '오늘 옷차림'],
|
|
] as const
|
|
export const topicName = (t?: number) => TOPICS.find(([k]) => k === t)?.[1] ?? '동네 이야기'
|
|
|
|
/** 오늘 피드 묶음: 0 은 '동네 사람들', 나머지는 주제별(3장 이상일 때만 옴) */
|
|
export function useTalkGroups() {
|
|
const { region } = useRegion()
|
|
const q = useQuery(talkGroupsOptions({ query: { region: region.code } }))
|
|
const groups = new Map<number, TalkData[]>((q.data?.groups ?? []).map((g) => [g.topic, g.talks ?? []]))
|
|
return { groups, loaded: q.isSuccess }
|
|
}
|
|
|
|
/** 주제별 글 페이지 목록 (topic 없으면 전체) */
|
|
export function useTopicTalks(topic?: TalkTopic) {
|
|
const { region } = useRegion()
|
|
const list = useInfiniteQuery({
|
|
...listTalksInfiniteOptions({ query: { region: region.code, ...(topic ? { topic } : {}) } }),
|
|
initialPageParam: {},
|
|
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
|
})
|
|
return {
|
|
talks: list.data?.pages.flatMap((p) => p.talks ?? []) ?? [],
|
|
loaded: list.isSuccess,
|
|
more: list.hasNextPage && !list.isFetchingNextPage,
|
|
loadMore: list.fetchNextPage,
|
|
}
|
|
}
|
|
|
|
/** 내가 쓴 글 (최신순) */
|
|
export function useMyTalks() {
|
|
const list = useInfiniteQuery({
|
|
...listMyTalksInfiniteOptions(),
|
|
initialPageParam: {},
|
|
getNextPageParam: (last) => last.next_cursor ?? undefined,
|
|
})
|
|
return {
|
|
talks: list.data?.pages.flatMap((p) => p.talks ?? []) ?? [],
|
|
loaded: list.isSuccess,
|
|
more: list.hasNextPage && !list.isFetchingNextPage,
|
|
loadMore: list.fetchNextPage,
|
|
}
|
|
}
|