-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSidebar.tsx
More file actions
182 lines (165 loc) · 5.27 KB
/
Sidebar.tsx
File metadata and controls
182 lines (165 loc) · 5.27 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
import { Icon } from '@pinback/design-system/icons';
import SideItem from './SideItem';
import AccordionItem from './AccordionItem';
import CategoryItem from './CategoryItem';
import CreateItem from './CreateItem';
import MyLevelItem from './MyLevelItem';
import { useAnchoredMenu } from '@shared/hooks/useAnchoredMenu';
import { rightOf } from '@shared/utils/anchorPosition';
import { useSidebarNav } from '@shared/hooks/useSidebarNav';
import { useCategoryPopups } from '@shared/hooks/useCategoryPopups';
import OptionsMenuPortal from './OptionsMenuPortal';
import PopupPortal from './PopupPortal';
import {
useGetDashboardCategories,
usePostCategory,
useGetArcons,
usePutCategory,
} from '@shared/apis/queries';
import { useState } from 'react';
import { useQueryClient } from '@tanstack/react-query';
export function Sidebar() {
const [newCategoryName, setNewCategoryName] = useState('');
const queryClient = useQueryClient();
const { data: categories } = useGetDashboardCategories();
const { mutate: patchCategory } = usePutCategory();
const { mutate: createCategory } = usePostCategory();
const { data, isPending, isError } = useGetArcons();
const {
activeTab,
selectedCategoryId,
goRemind,
goBookmarks,
selectCategory,
goLevel,
setSelectedCategoryId,
} = useSidebarNav();
const { popup, openCreate, openEdit, openDelete, close } =
useCategoryPopups();
const {
state: menu,
open: openMenu,
close: closeMenu,
containerRef,
style,
} = useAnchoredMenu((anchor) => rightOf(anchor, 30));
const getCategoryName = (id: number | null) =>
categories?.categories.find((category) => category.id === id)?.name ?? '';
const handleCategoryChange = (name: string) => {
setNewCategoryName(name);
};
const handleCreateCategory = () => {
createCategory(newCategoryName, {
onSuccess: () => {
handleCategoryChange('');
queryClient.invalidateQueries({ queryKey: ['dashboardCategories'] });
close();
},
onError: (error) => {
console.error('카테고리 생성 실패:', error);
},
});
};
const handlePatchCategory = (id: number) => {
patchCategory(
{ id, categoryName: newCategoryName },
{
onSuccess: () => {
setNewCategoryName('');
queryClient.invalidateQueries({ queryKey: ['dashboardCategories'] });
close();
},
onError: (error) => console.error('카테고리 수정 실패:', error),
}
);
};
if (isPending) return <div></div>;
if (isError) return <div></div>;
const acornCount = data.acornCount;
return (
<aside className="bg-white-bg sticky top-0 h-screen w-[24rem] border-r border-gray-300">
<div className="flex h-full flex-col px-[0.8rem]">
<header className="py-[2.8rem]">
<Icon
name="logo"
aria-label="Pinback 로고"
className="h-[2.4rem] w-[8.7rem]"
/>
</header>
<hr className="my-[0.8rem] border-gray-100" />
<div className="flex-1 overflow-y-auto">
<SideItem
icon="clock"
label="리마인드"
active={activeTab === 'remind'}
onClick={() => {
setSelectedCategoryId(null);
closeMenu();
goRemind();
}}
/>
<AccordionItem
icon="bookmark"
label="나의 북마크"
active={activeTab === 'mybookmark'}
defaultOpen
onClick={() => {
setSelectedCategoryId(null);
closeMenu();
goBookmarks();
}}
>
<ul className="bg-none">
{categories?.categories?.map((category) => (
<CategoryItem
key={category.id}
id={category.id}
label={category.name}
active={selectedCategoryId === category.id}
onClick={(id) => {
closeMenu();
selectCategory(id);
}}
onOptionsClick={(id, el) => openMenu(id, el)}
/>
))}
<CreateItem onClick={openCreate} />
</ul>
</AccordionItem>
<OptionsMenuPortal
open={menu.open}
style={style ?? undefined}
categoryId={menu.categoryId}
getCategoryName={getCategoryName}
onEdit={(id, name) => openEdit(id, name)}
onDelete={(id, name) => openDelete(id, name)}
onClose={closeMenu}
containerRef={containerRef}
/>
</div>
<footer className="pb-[2.8rem] pt-[1.2rem]">
<MyLevelItem
acorns={acornCount}
isActive={activeTab === 'level'}
onClick={() => {
setSelectedCategoryId(null);
closeMenu();
goLevel();
}}
/>
</footer>
</div>
<PopupPortal
popup={popup}
onClose={close}
onChange={handleCategoryChange}
onCreateConfirm={handleCreateCategory}
onEditConfirm={(id) => handlePatchCategory(id)}
onDeleteConfirm={() => {
// TODO: 삭제 API
close();
}}
/>
</aside>
);
}