아카이브
지금까지 작성한 {totalPosts}개의 글을 시간순으로 정리했습니다.
'use client'; import { useMemo } from 'react'; import { useQuery } from '@tanstack/react-query'; import { format } from 'date-fns'; import Link from 'next/link'; import { Archive, Calendar, ChevronRight, FileText, Loader2 } from 'lucide-react'; import { getPosts } from '@/api/posts'; import EmptyState from '@/components/ui/EmptyState'; import StatusBadge from '@/components/ui/StatusBadge'; import Surface from '@/components/ui/Surface'; import WindowSurface from '@/components/ui/WindowSurface'; import { Post, PostListResponse } from '@/types'; const getTotalElements = (data?: PostListResponse) => { return data?.page?.totalElements ?? data?.totalElements ?? 0; }; export default function ArchivePage() { const { data, isLoading } = useQuery({ queryKey: ['posts', 'all'], queryFn: () => getPosts({ page: 0, size: 1000 }), staleTime: 1000 * 60 * 5, }); const archiveGroups = useMemo(() => { if (!data?.content) return {}; const groups: { [year: string]: { [month: string]: Post[] } } = {}; data.content.forEach((post) => { const date = new Date(post.createdAt); const year = date.getFullYear().toString(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); if (!groups[year]) groups[year] = {}; if (!groups[year][month]) groups[year][month] = []; groups[year][month].push(post); }); return groups; }, [data]); const sortedYears = useMemo(() => { return Object.keys(archiveGroups).sort((a, b) => Number(b) - Number(a)); }, [archiveGroups]); const totalPosts = getTotalElements(data); if (isLoading) { return (
지금까지 작성한 {totalPosts}개의 글을 시간순으로 정리했습니다.