-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpage.tsx
More file actions
204 lines (188 loc) · 6.68 KB
/
page.tsx
File metadata and controls
204 lines (188 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
'use client';
import Link from 'next/link';
import { useRouter, useSearchParams } from 'next/navigation';
import { Suspense, useEffect, useState } from 'react';
import { formatDate } from '@/app/lib/utils/format';
interface PostItem {
postId: string;
title: string;
slug: string;
date: number;
seriesTitle?: string;
likeCount: number;
totalViews?: number;
todayViews: number;
}
const TABS = [
{ key: 'all', label: '전체 인기 글 통계' },
{ key: 'today', label: '오늘 인기 글 통계' },
] as const;
type TabKey = (typeof TABS)[number]['key'];
function SkeletonList() {
return (
<ul className="animate-pulse space-y-1">
{[...Array(20)].map((_, i) => (
<li
key={i}
className="px-3 py-2.5 border border-gray-100 dark:border-gray-700 rounded-lg flex items-center gap-3"
>
<div className="h-3.5 w-4 bg-gray-200 dark:bg-gray-700 rounded shrink-0" />
<div className="h-3.5 flex-1 bg-gray-200 dark:bg-gray-700 rounded" />
<div className="h-3 w-16 bg-gray-200 dark:bg-gray-700 rounded shrink-0" />
<div className="h-3 w-20 bg-gray-200 dark:bg-gray-700 rounded shrink-0" />
<div className="h-3 w-10 bg-gray-200 dark:bg-gray-700 rounded shrink-0" />
<div className="h-3.5 w-14 bg-gray-200 dark:bg-gray-700 rounded shrink-0" />
</li>
))}
</ul>
);
}
function PostListItem({
post,
rank,
viewsNode,
}: {
post: PostItem;
rank: number;
viewsNode: React.ReactNode;
}) {
return (
<li className="px-3 py-2.5 border border-gray-100 dark:border-gray-700 rounded-lg hover:bg-gray-50 dark:hover:bg-gray-800 transition-colors duration-150 flex items-center gap-3 min-w-0">
<span className="text-xs text-gray-400 dark:text-gray-500 w-4 shrink-0 text-right">
{rank}
</span>
<Link
href={`/posts/${post.slug}`}
className="flex-1 text-sm font-medium truncate dark:text-gray-200 hover:text-brand-primary dark:hover:text-brand-secondary transition-colors min-w-0"
>
{post.title}
</Link>
<div className=" shrink-0 flex justify-center">
{post.seriesTitle ? (
<span className="text-xs px-1.5 py-0.5 rounded bg-brand-primary/10 text-brand-primary dark:bg-brand-secondary/10 dark:text-brand-secondary whitespace-nowrap text-nowrap max-w-full">
{post.seriesTitle}
</span>
) : (
<span className="text-xs text-gray-300 dark:text-gray-600">—</span>
)}
</div>
<span className="text-xs text-gray-400 dark:text-gray-500 w-24 shrink-0 text-center">
{formatDate(post.date)}
</span>
<span className="text-xs text-gray-400 dark:text-gray-500 w-12 shrink-0 text-center">
♥ {post.likeCount.toLocaleString()}
</span>
<span className="text-sm font-semibold shrink-0 w-20 text-right dark:text-gray-200">
{viewsNode}
</span>
</li>
);
}
function AnalyticsContent() {
const router = useRouter();
const searchParams = useSearchParams();
const tab = (searchParams.get('tab') ?? 'all') as TabKey;
const [allPosts, setAllPosts] = useState<PostItem[]>([]);
const [todayPosts, setTodayPosts] = useState<PostItem[]>([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fetchData = async () => {
setLoading(true);
setError(null);
try {
const res = await fetch(`/api/admin/analytics/popular?type=${tab}`);
const data = await res.json();
if (!data.success) throw new Error(data.error);
if (tab === 'all') setAllPosts(data.posts);
else setTodayPosts(data.posts);
} catch {
setError('데이터를 불러오는 중 오류가 발생했습니다.');
} finally {
setLoading(false);
}
};
fetchData();
}, [tab]);
const handleTabChange = (key: TabKey) => {
router.push(`/admin/analytics?tab=${key}`);
};
const posts = tab === 'all' ? allPosts : todayPosts;
const emptyMessage =
tab === 'all' ? '데이터가 없습니다.' : '오늘 조회된 게시글이 없습니다.';
return (
<div className="max-w-6xl mx-auto p-6">
<h1 className="text-3xl font-bold mb-8 dark:text-white">
방문자 및 조회수 분석
</h1>
{/* 탭 */}
<div className="flex border-b border-gray-200 dark:border-gray-700 mb-6">
{TABS.map(({ key, label }) => (
<button
key={key}
onClick={() => handleTabChange(key)}
className={`px-5 py-3 text-sm font-medium transition-colors ${
tab === key
? 'border-b-2 border-brand-primary text-brand-primary dark:border-brand-secondary dark:text-brand-secondary'
: 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-200'
}`}
>
{label}
</button>
))}
</div>
{/* 리스트 */}
{loading ? (
<SkeletonList />
) : error ? (
<p className="text-red-500">{error}</p>
) : posts.length === 0 ? (
<p className="text-gray-500 dark:text-gray-400 text-sm">
{emptyMessage}
</p>
) : (
<>
{/* 테이블 헤더 */}
<div className="flex items-center gap-3 px-3 py-1.5 text-xs font-medium text-gray-400 dark:text-gray-500 border-b border-gray-100 dark:border-gray-700 mb-1">
<span className="w-4 shrink-0" />
<span className="flex-1">제목</span>
<span className="w-20 shrink-0 text-center">시리즈</span>
<span className="w-24 shrink-0 text-center">작성일</span>
<span className="w-12 shrink-0 text-center">좋아요</span>
<span className="w-20 shrink-0 text-right">조회수</span>
</div>
<ul className="space-y-1">
{posts.map((post, i) => (
<PostListItem
key={post.postId}
post={post}
rank={i + 1}
viewsNode={
tab === 'all' ? (
<>
{post.totalViews!.toLocaleString()}
{post.todayViews > 0 && (
<span className="text-brand-secondary ml-1">
(+{post.todayViews})
</span>
)}
</>
) : (
<>{post.todayViews.toLocaleString()}회</>
)
}
/>
))}
</ul>
</>
)}
</div>
);
}
export default function StatsPage() {
return (
<Suspense>
<AnalyticsContent />
</Suspense>
);
}