|
1
|
import React, { useEffect, useState } from "react" |
|
2
|
import { Button, Tooltip, DropdownMenu, usePrompt } from "@medusajs/ui" |
|
3
|
import { |
|
4
|
Eye, |
|
5
|
Plus, |
|
6
|
Trash, |
|
7
|
PencilSquare, |
|
8
|
Star, |
|
9
|
CheckCircleSolid, |
|
10
|
ArrowUturnLeft, |
|
11
|
} from "@medusajs/icons" |
|
12
|
import { |
|
13
|
useViewConfigurations, |
|
14
|
useViewConfiguration, |
|
15
|
} from "../../../hooks/use-view-configurations" |
|
16
|
import type { ViewConfiguration } from "../../../hooks/use-view-configurations" |
|
17
|
import { SaveViewDialog } from "../save-view-dialog" |
|
18
|
|
|
19
|
interface ViewSelectorProps { |
|
20
|
entity: string |
|
21
|
onViewChange?: (view: ViewConfiguration | null) => void |
|
22
|
currentColumns?: { |
|
23
|
visible: string[] |
|
24
|
order: string[] |
|
25
|
} |
|
26
|
} |
|
27
|
|
|
28
|
export const ViewSelector: React.FC<ViewSelectorProps> = ({ |
|
29
|
entity, |
|
30
|
onViewChange, |
|
31
|
currentColumns, |
|
32
|
}) => { |
|
33
|
const { listViews, activeView, setActiveView } = useViewConfigurations(entity) |
|
34
|
|
|
35
|
const [saveDialogOpen, setSaveDialogOpen] = useState(false) |
|
36
|
const [editingView, setEditingView] = useState<ViewConfiguration | null>(null) |
|
37
|
const [deletingViewId, setDeletingViewId] = useState<string | null>(null) |
|
38
|
const prompt = usePrompt() |
|
39
|
|
|
40
|
const views = (listViews as any).data?.view_configurations || [] |
|
41
|
const currentActiveView = (activeView as any).data?.view_configuration || null |
|
42
|
|
|
43
|
// Get delete mutation for the current deleting view |
|
44
|
const { deleteView } = useViewConfiguration(entity, deletingViewId || "") |
|
45
|
|
|
46
|
// Load views and active view |
|
47
|
useEffect(() => { |
|
48
|
if (activeView.isSuccess && onViewChange) { |
|
49
|
onViewChange(currentActiveView) |
|
50
|
} |
|
51
|
}, [activeView.isSuccess, currentActiveView, onViewChange]) |
|
52
|
|
|
53
|
const handleViewSelect = async (viewId: string) => { |
|
54
|
const view = views.find((v: ViewConfiguration) => v.id === viewId) |
|
55
|
if (view) { |
|
56
|
await setActiveView.mutateAsync(viewId) |
|
57
|
if (onViewChange) { |
|
58
|
onViewChange(view) |
|
59
|
} |
|
60
|
} |
|
61
|
} |
|
62
|
|
|
63
|
const handleDeleteView = async (view: ViewConfiguration) => { |
|
64
|
const result = await prompt({ |
|
65
|
title: "Delete view", |
|
66
|
description: `Are you sure you want to delete "${view.name}"? This action cannot be undone.`, |
|
67
|
confirmText: "Delete", |
|
68
|
cancelText: "Cancel", |
|
69
|
}) |
|
70
|
|
|
71
|
if (result) { |
|
72
|
setDeletingViewId(view.id) |
|
73
|
} |
|
74
|
} |
|
75
|
|
|
76
|
// Handle deletion when deletingViewId is set |
|
77
|
useEffect(() => { |
|
78
|
if (deletingViewId && deleteView.mutateAsync) { |
|
79
|
deleteView |
|
80
|
.mutateAsync() |
|
81
|
.then(() => { |
|
82
|
if (currentActiveView?.id === deletingViewId) { |
|
83
|
if (onViewChange) { |
|
84
|
onViewChange(null) |
|
85
|
} |
|
86
|
} |
|
87
|
setDeletingViewId(null) |
|
88
|
}) |
|
89
|
.catch(() => { |
|
90
|
setDeletingViewId(null) |
|
91
|
}) |
|
92
|
} |
|
93
|
}, [ |
|
94
|
deletingViewId, |
|
95
|
deleteView.mutateAsync, |
|
96
|
currentActiveView?.id, |
|
97
|
onViewChange, |
|
98
|
]) |
|
99
|
|
|
100
|
const handleSaveView = () => { |
|
101
|
setSaveDialogOpen(true) |
|
102
|
setEditingView(null) |
|
103
|
} |
|
104
|
|
|
105
|
const handleEditView = (view: ViewConfiguration) => { |
|
106
|
setEditingView(view) |
|
107
|
setSaveDialogOpen(true) |
|
108
|
} |
|
109
|
|
|
110
|
const handleResetSystemDefault = async ( |
|
111
|
systemDefaultView: ViewConfiguration |
|
112
|
) => { |
|
113
|
const result = await prompt({ |
|
114
|
title: "Reset system default", |
|
115
|
description: |
|
116
|
"This will delete the saved system default and revert to the original code-level defaults. All users will be affected. Are you sure?", |
|
117
|
confirmText: "Reset", |
|
118
|
cancelText: "Cancel", |
|
119
|
}) |
|
120
|
|
|
121
|
if (result) { |
|
122
|
setDeletingViewId(systemDefaultView.id) |
|
123
|
} |
|
124
|
} |
|
125
|
|
|
126
|
const systemDefaultView = views.find( |
|
127
|
(v: ViewConfiguration) => v.is_system_default |
|
128
|
) |
|
129
|
const personalViews = views.filter( |
|
130
|
(v: ViewConfiguration) => !v.is_system_default |
|
131
|
) |
|
132
|
|
|
133
|
return ( |
|
134
|
<> |
|
135
|
<div className="flex items-center gap-2"> |
|
136
|
<DropdownMenu> |
|
137
|
<DropdownMenu.Trigger asChild> |
|
138
|
<Button variant="secondary" size="small"> |
|
139
|
<Eye className="h-4 w-4" /> |
|
140
|
{currentActiveView ? currentActiveView.name : "Default View"} |
|
141
|
</Button> |
|
142
|
</DropdownMenu.Trigger> |
|
143
|
<DropdownMenu.Content className="w-[260px]"> |
|
144
|
{systemDefaultView && ( |
|
145
|
<> |
|
146
|
<DropdownMenu.Label>System Default</DropdownMenu.Label> |
|
147
|
<DropdownMenu.Item |
|
148
|
onClick={() => handleViewSelect(systemDefaultView.id)} |
|
149
|
className="group justify-between" |
|
150
|
> |
|
151
|
<span className="flex items-center gap-2"> |
|
152
|
<Star className="h-4 w-4" /> |
|
153
|
{systemDefaultView.name || "System Default"} |
|
154
|
</span> |
|
155
|
<div className="flex items-center gap-1"> |
|
156
|
{currentActiveView?.id === systemDefaultView.id && ( |
|
157
|
<CheckCircleSolid className="text-ui-fg-positive h-4 w-4" /> |
|
158
|
)} |
|
159
|
<div className="opacity-0 group-hover:opacity-100"> |
|
160
|
<Tooltip content="Reset to code defaults"> |
|
161
|
<Button |
|
162
|
variant="transparent" |
|
163
|
size="small" |
|
164
|
onClick={(e) => { |
|
165
|
e.stopPropagation() |
|
166
|
handleResetSystemDefault(systemDefaultView) |
|
167
|
}} |
|
168
|
> |
|
169
|
<ArrowUturnLeft className="h-3 w-3" /> |
|
170
|
</Button> |
|
171
|
</Tooltip> |
|
172
|
</div> |
|
173
|
</div> |
|
174
|
</DropdownMenu.Item> |
|
175
|
{personalViews.length > 0 && <DropdownMenu.Separator />} |
|
176
|
</> |
|
177
|
)} |
|
178
|
|
|
179
|
{personalViews.length > 0 && ( |
|
180
|
<> |
|
181
|
<DropdownMenu.Label>Personal Views</DropdownMenu.Label> |
|
182
|
{personalViews.map((view: ViewConfiguration) => ( |
|
183
|
<DropdownMenu.Item |
|
184
|
key={view.id} |
|
185
|
onClick={() => handleViewSelect(view.id)} |
|
186
|
className="group justify-between" |
|
187
|
> |
|
188
|
<span className="flex-1">{view.name}</span> |
|
189
|
<div className="flex items-center gap-1"> |
|
190
|
{currentActiveView?.id === view.id && ( |
|
191
|
<CheckCircleSolid className="text-ui-fg-positive h-4 w-4" /> |
|
192
|
)} |
|
193
|
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100"> |
|
194
|
<Tooltip content="Edit view"> |
|
195
|
<Button |
|
196
|
variant="transparent" |
|
197
|
size="small" |
|
198
|
onClick={(e) => { |
|
199
|
e.stopPropagation() |
|
200
|
handleEditView(view) |
|
201
|
}} |
|
202
|
> |
|
203
|
<PencilSquare className="h-3 w-3" /> |
|
204
|
</Button> |
|
205
|
</Tooltip> |
|
206
|
<Tooltip content="Delete view"> |
|
207
|
<Button |
|
208
|
variant="transparent" |
|
209
|
size="small" |
|
210
|
onClick={(e) => { |
|
211
|
e.stopPropagation() |
|
212
|
handleDeleteView(view) |
|
213
|
}} |
|
214
|
> |
|
215
|
<Trash className="h-3 w-3" /> |
|
216
|
</Button> |
|
217
|
</Tooltip> |
|
218
|
</div> |
|
219
|
</div> |
|
220
|
</DropdownMenu.Item> |
|
221
|
))} |
|
222
|
</> |
|
223
|
)} |
|
224
|
|
|
225
|
<DropdownMenu.Separator /> |
|
226
|
<DropdownMenu.Item |
|
227
|
onClick={handleSaveView} |
|
228
|
className="text-ui-fg-interactive" |
|
229
|
> |
|
230
|
<Plus className="h-4 w-4" /> |
|
231
|
Save current view |
|
232
|
</DropdownMenu.Item> |
|
233
|
</DropdownMenu.Content> |
|
234
|
</DropdownMenu> |
|
235
|
</div> |
|
236
|
|
|
237
|
{saveDialogOpen && ( |
|
238
|
<SaveViewDialog |
|
239
|
entity={entity} |
|
240
|
currentColumns={currentColumns} |
|
241
|
editingView={editingView} |
|
242
|
onClose={() => { |
|
243
|
setSaveDialogOpen(false) |
|
244
|
setEditingView(null) |
|
245
|
}} |
|
246
|
onSaved={(newView) => { |
|
247
|
setSaveDialogOpen(false) |
|
248
|
setEditingView(null) |
|
249
|
if (onViewChange) { |
|
250
|
onViewChange(newView) |
|
251
|
} |
|
252
|
}} |
|
253
|
/> |
|
254
|
)} |
|
255
|
</> |
|
256
|
) |
|
257
|
} |