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
|
"use client";
import * as React from "react";
import { Table } from "@tanstack/react-table";
import { useSession } from "next-auth/react";
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Bookmark, Save, Trash2 } from "lucide-react";
import {
getUserCustomSettings,
saveUserCustomSetting,
deleteUserCustomSetting,
} from "@/actions/user-custom-data";
import { toast } from "sonner";
interface ClientTableSaveViewProps<TData> {
table: Table<TData>;
tableKey: string;
}
export function ClientTableSaveView<TData>({
table,
tableKey,
}: ClientTableSaveViewProps<TData>) {
const { data: session } = useSession();
const [savedViews, setSavedViews] = React.useState<{ id: string; customSettingName: string; customSetting: Record<string, any> }[]>([]);
const [isSaveDialogOpen, setIsSaveDialogOpen] = React.useState(false);
const [newViewName, setNewViewName] = React.useState("");
const [isLoading, setIsLoading] = React.useState(false);
const fetchSettings = React.useCallback(async () => {
const userIdVal = session?.user?.id;
if (!userIdVal) return;
const userId = Number(userIdVal);
if (isNaN(userId)) return;
const res = await getUserCustomSettings(tableKey, userId);
if (res.success && res.data) {
// @ts-ignore - data from DB might need casting
setSavedViews(res.data);
}
}, [session, tableKey]);
React.useEffect(() => {
if (session) {
fetchSettings();
}
}, [fetchSettings, session]);
const handleSaveView = async () => {
const userIdVal = session?.user?.id;
if (!newViewName.trim() || !userIdVal) return;
const userId = Number(userIdVal);
if (isNaN(userId)) return;
setIsLoading(true);
const state = table.getState();
const settingToSave = {
sorting: state.sorting,
columnFilters: state.columnFilters,
globalFilter: state.globalFilter,
columnVisibility: state.columnVisibility,
columnPinning: state.columnPinning,
columnOrder: state.columnOrder,
grouping: state.grouping,
pagination: { pageSize: state.pagination.pageSize },
};
const res = await saveUserCustomSetting(userId, tableKey, newViewName, settingToSave);
setIsLoading(false);
if (res.success) {
toast.success("View saved successfully");
setIsSaveDialogOpen(false);
setNewViewName("");
fetchSettings();
} else {
toast.error("Failed to save view");
}
};
const handleLoadView = (setting: { customSetting: Record<string, any> | unknown; customSettingName: string }) => {
const s = setting.customSetting as Record<string, any>;
if (!s) return;
if (s.sorting) table.setSorting(s.sorting);
if (s.columnFilters) table.setColumnFilters(s.columnFilters);
if (s.globalFilter !== undefined) table.setGlobalFilter(s.globalFilter);
if (s.columnVisibility) table.setColumnVisibility(s.columnVisibility);
if (s.columnPinning) table.setColumnPinning(s.columnPinning);
if (s.columnOrder) table.setColumnOrder(s.columnOrder);
if (s.grouping) table.setGrouping(s.grouping);
if (s.pagination?.pageSize) table.setPageSize(s.pagination.pageSize);
toast.success(`View "${setting.customSettingName}" loaded`);
};
const handleDeleteView = async (e: React.MouseEvent, id: string) => {
e.stopPropagation();
if (!confirm("Are you sure you want to delete this view?")) return;
const res = await deleteUserCustomSetting(id);
if (res.success) {
toast.success("View deleted");
fetchSettings();
} else {
toast.error("Failed to delete view");
}
};
if (!session) return null;
return (
<>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant="outline" size="sm" className="ml-2 hidden h-8 lg:flex">
<Bookmark className="mr-2 h-4 w-4" />
Views
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align="end" className="w-[200px]">
<DropdownMenuLabel>Saved Views</DropdownMenuLabel>
<DropdownMenuSeparator />
{savedViews.length === 0 ? (
<div className="p-2 text-sm text-muted-foreground text-center">No saved views</div>
) : (
savedViews.map((view) => (
<DropdownMenuItem key={view.id} onClick={() => handleLoadView(view)} className="flex justify-between cursor-pointer">
<span className="truncate flex-1">{view.customSettingName}</span>
<Button variant="ghost" size="icon" className="h-4 w-4" onClick={(e) => handleDeleteView(e, view.id)}>
<Trash2 className="h-3 w-3 text-destructive" />
</Button>
</DropdownMenuItem>
))
)}
<DropdownMenuSeparator />
<DropdownMenuItem onClick={() => setIsSaveDialogOpen(true)} className="cursor-pointer">
<Save className="mr-2 h-4 w-4" />
Save Current View
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
<Dialog open={isSaveDialogOpen} onOpenChange={setIsSaveDialogOpen}>
<DialogContent>
<DialogHeader>
<DialogTitle>Save View</DialogTitle>
<DialogDescription>
Save the current table configuration as a preset.
</DialogDescription>
</DialogHeader>
<div className="grid gap-4 py-4">
<Input
placeholder="View Name"
value={newViewName}
onChange={(e) => setNewViewName(e.target.value)}
/>
</div>
<DialogFooter>
<Button variant="outline" onClick={() => setIsSaveDialogOpen(false)}>Cancel</Button>
<Button onClick={handleSaveView} disabled={isLoading}>Save</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}
|