|
| 1 | +'use client' |
| 2 | + |
| 3 | +import { useInfiniteQuery, useQuery } from '@tanstack/react-query' |
| 4 | +import Link from 'next/link' |
| 5 | +import { useParams } from 'next/navigation' |
| 6 | + |
| 7 | +import { TimelineList } from '~/components/ui/list/TimelineList' |
| 8 | +import { Loading } from '~/components/ui/loading' |
| 9 | +import { BottomToUpSoftScaleTransitionView } from '~/components/ui/transition/BottomToUpSoftScaleTransitionView' |
| 10 | +import { BottomToUpTransitionView } from '~/components/ui/transition/BottomToUpTransitionView' |
| 11 | +import { routeBuilder, Routes } from '~/lib/route-builder' |
| 12 | +import { apiClient } from '~/utils/request' |
| 13 | + |
| 14 | +import { getTopicQuery } from './query' |
| 15 | + |
| 16 | +export default function Page() { |
| 17 | + const { slug } = useParams() |
| 18 | + const { data } = useQuery({ |
| 19 | + ...getTopicQuery(slug), |
| 20 | + enabled: false, |
| 21 | + }) |
| 22 | + |
| 23 | + const { |
| 24 | + data: notes, |
| 25 | + isLoading, |
| 26 | + fetchNextPage, |
| 27 | + } = useInfiniteQuery({ |
| 28 | + queryKey: ['topicId', data?.id], |
| 29 | + |
| 30 | + enabled: !!data, |
| 31 | + queryFn: async ({ queryKey, pageParam }) => { |
| 32 | + const [, topicId] = queryKey |
| 33 | + if (!topicId) throw new Error('topicId is not ready :(') |
| 34 | + return await apiClient.note.getNoteByTopicId(topicId, pageParam) |
| 35 | + }, |
| 36 | + |
| 37 | + getNextPageParam: (lastPage) => |
| 38 | + lastPage.pagination.hasNextPage |
| 39 | + ? lastPage.pagination.currentPage + 1 |
| 40 | + : undefined, |
| 41 | + }) |
| 42 | + if (!data) throw new Error('topic data is lost :(') |
| 43 | + const { name } = data |
| 44 | + |
| 45 | + if (isLoading) return <Loading useDefaultLoadingText /> |
| 46 | + return ( |
| 47 | + <BottomToUpSoftScaleTransitionView> |
| 48 | + <header className="prose"> |
| 49 | + <h1>专栏 - {name}</h1> |
| 50 | + </header> |
| 51 | + |
| 52 | + <main className="mt-10 text-zinc-950/80 dark:text-zinc-50/80"> |
| 53 | + <TimelineList> |
| 54 | + {notes?.pages.map((page) => |
| 55 | + page.data.map((child, i) => { |
| 56 | + const date = new Date(child.created) |
| 57 | + |
| 58 | + return ( |
| 59 | + <BottomToUpTransitionView |
| 60 | + key={child.id} |
| 61 | + delay={700 + 50 * i} |
| 62 | + as="li" |
| 63 | + className="flex min-w-0 items-center justify-between leading-loose" |
| 64 | + > |
| 65 | + <Link |
| 66 | + prefetch={false} |
| 67 | + target="_blank" |
| 68 | + href={routeBuilder(Routes.Note, { |
| 69 | + id: child.nid, |
| 70 | + })} |
| 71 | + className="min-w-0 truncate" |
| 72 | + > |
| 73 | + {child.title} |
| 74 | + </Link> |
| 75 | + <span className="meta"> |
| 76 | + {(date.getMonth() + 1).toString().padStart(2, '0')}/ |
| 77 | + {date.getDate().toString().padStart(2, '0')}/ |
| 78 | + {date.getFullYear()} |
| 79 | + </span> |
| 80 | + </BottomToUpTransitionView> |
| 81 | + ) |
| 82 | + }), |
| 83 | + )} |
| 84 | + </TimelineList> |
| 85 | + </main> |
| 86 | + </BottomToUpSoftScaleTransitionView> |
| 87 | + ) |
| 88 | +} |
0 commit comments