Raw
1 import React, { useState } from "react"
2 import { Button, Input, Label, Drawer, Heading, Text } from "@medusajs/ui"
3 import { useForm } from "react-hook-form"
4 import {
5 useViewConfigurations,
6 useViewConfiguration,
7 } from "../../../hooks/use-view-configurations"
8 import type { ViewConfiguration } from "../../../hooks/use-view-configurations"
9
10 type SaveViewFormData = {
11 name: string
12 }
13
14 interface SaveViewDialogProps {
15 entity: string
16 currentColumns?: {
17 visible: string[]
18 order: string[]
19 }
20 currentConfiguration?: {
21 filters?: Record<string, unknown>
22 sorting?: { id: string; desc: boolean } | null
23 search?: string
24 }
25 editingView?: ViewConfiguration | null
26 onClose: () => void
27 onSaved: (view: ViewConfiguration) => void
28 }
29
30 export const SaveViewDialog: React.FC<SaveViewDialogProps> = ({
31 entity,
32 currentColumns,
33 currentConfiguration,
34 editingView,
35 onClose,
36 onSaved,
37 }) => {
38 const { createView } = useViewConfigurations(entity)
39 const { updateView } = useViewConfiguration(entity, editingView?.id || "")
40 const [isLoading, setIsLoading] = useState(false)
41
42 const {
43 register,
44 handleSubmit,
45 formState: { errors },
46 } = useForm<SaveViewFormData>({
47 defaultValues: {
48 name: editingView?.name || "",
49 },
50 })
51
52 const onSubmit = async (data: SaveViewFormData) => {
53 if (!data.name.trim()) {
54 return
55 }
56
57 setIsLoading(true)
58 try {
59 if (editingView) {
60 // Update existing view
61 const result = await updateView.mutateAsync({
62 name: data.name.trim(),
63 configuration: {
64 visible_columns:
65 currentColumns?.visible ||
66 editingView.configuration.visible_columns,
67 column_order:
68 currentColumns?.order || editingView.configuration.column_order,
69 filters:
70 currentConfiguration?.filters ||
71 editingView.configuration.filters ||
72 {},
73 sorting:
74 currentConfiguration?.sorting ||
75 editingView.configuration.sorting ||
76 null,
77 search:
78 currentConfiguration?.search ||
79 editingView.configuration.search ||
80 "",
81 },
82 })
83 if (result.view_configuration) {
84 onSaved(result.view_configuration)
85 }
86 } else {
87 // Create new view
88 const result = await createView.mutateAsync({
89 name: data.name.trim(),
90 set_active: true,
91 configuration: {
92 visible_columns: currentColumns?.visible || [],
93 column_order: currentColumns?.order || [],
94 filters: currentConfiguration?.filters || {},
95 sorting: currentConfiguration?.sorting || null,
96 search: currentConfiguration?.search || "",
97 },
98 })
99 if (result.view_configuration) {
100 onSaved(result.view_configuration)
101 }
102 }
103 } catch (error) {
104 // Error is handled by the hook
105 } finally {
106 setIsLoading(false)
107 }
108 }
109
110 return (
111 <Drawer open onOpenChange={onClose}>
112 <Drawer.Content className="flex flex-col">
113 <Drawer.Header>
114 <Drawer.Title asChild>
115 <Heading>
116 {editingView ? "Edit View Name" : "Save as New View"}
117 </Heading>
118 </Drawer.Title>
119 <Drawer.Description asChild>
120 <Text>
121 {editingView
122 ? "Change the name of your saved view"
123 : "Save your current configuration as a new view"}
124 </Text>
125 </Drawer.Description>
126 </Drawer.Header>
127
128 <form
129 onSubmit={handleSubmit(onSubmit)}
130 className="flex flex-1 flex-col"
131 >
132 <Drawer.Body className="flex-1">
133 <div className="flex flex-col gap-y-2">
134 <Label htmlFor="name" weight="plus">
135 View Name
136 </Label>
137 <Input
138 {...register("name", {
139 required: "Name is required",
140 validate: (value) =>
141 value.trim().length > 0 || "Name cannot be empty",
142 })}
143 type="text"
144 placeholder="Enter view name"
145 autoFocus
146 />
147 {errors.name && (
148 <span className="text-ui-fg-error text-sm">
149 {errors.name.message}
150 </span>
151 )}
152 </div>
153 </Drawer.Body>
154
155 <Drawer.Footer>
156 <Drawer.Close asChild>
157 <Button variant="secondary" size="small" type="button">
158 Cancel
159 </Button>
160 </Drawer.Close>
161 <Button
162 variant="primary"
163 size="small"
164 type="submit"
165 isLoading={isLoading}
166 >
167 {editingView ? "Update" : "Save"}
168 </Button>
169 </Drawer.Footer>
170 </form>
171 </Drawer.Content>
172 </Drawer>
173 )
174 }