-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecent-topic-list.tsx
More file actions
67 lines (57 loc) · 1.81 KB
/
Copy pathrecent-topic-list.tsx
File metadata and controls
67 lines (57 loc) · 1.81 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
import { useEffect } from "react";
import { usePathname } from "next/navigation";
import { useRouter } from "next/navigation";
import SentinelSpinner from "@/components/common/sentinel-spinner";
import { useGetAllTopics } from "@/lib/tanstack/query/topic";
import { useDashboardStore } from "@/lib/zustand/dashboard";
import type { TopicDTO } from "@/models/topic";
import RecentTopicItem from "./recent-topic-item";
export default function RecentTopicList() {
const pathname = usePathname();
const router = useRouter();
const setTotalTopics = useDashboardStore((state) => state.setTotalTopics);
const {
data: recentTopics,
isLoading,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
isPending,
} = useGetAllTopics();
const onTopicClick = (topic: TopicDTO) => {
router.push(`/topic/${topic.id}`);
close(); // 모바일에서 사이드바 닫기
};
const isSelected = (topicId: number) => {
const topicPath = `/topic/${topicId}`;
return pathname === topicPath || pathname.startsWith(`${topicPath}/`);
};
useEffect(() => {
if (!isPending) {
setTotalTopics(recentTopics.data.length);
}
}, [isPending, recentTopics, setTotalTopics]);
return (
<>
{recentTopics.data.length === 0 ? (
<p className="text-muted-foreground text-center text-sm">토픽이 없어요.</p>
) : (
recentTopics.data.map((topic) => (
<RecentTopicItem
key={topic.id}
isSelected={isSelected(topic.id)}
topic={topic}
onTopicClick={() => onTopicClick(topic)}
/>
))
)}
<SentinelSpinner
className="mx-auto"
fetchNextPage={fetchNextPage}
hasNextPage={hasNextPage}
isLoading={isLoading}
isFetchingNextPage={isFetchingNextPage}
/>
</>
);
}