|
14 | 14 | import * as AlertDialog from "$lib/components/ui/alert-dialog" |
15 | 15 | import { invalidate } from "$app/navigation" |
16 | 16 | import { toast } from "svelte-sonner" |
17 | | - import { getDashboard, getIsDashboard } from "$lib/store/dashboard.store" |
| 17 | + import { getDashboard, getDashboardWidgetItemsStore, getIsDashboard } from "$lib/store/dashboard.store" |
18 | 18 | import { cn } from "$lib/utils" |
19 | | - import { GripVerticalIcon, ChevronRightIcon } from "lucide-svelte" |
| 19 | + import { GripVerticalIcon, ChevronRightIcon, CopyIcon } from "lucide-svelte" |
| 20 | + import { COLS } from "$lib/store/widget.store" |
| 21 | + import { DashboardLayouts } from "@undb/dashboard" |
20 | 22 |
|
21 | 23 | export let tableId: string | undefined |
22 | 24 | export let table: TableDo | undefined |
|
61 | 63 | toast.error(error.message) |
62 | 64 | }, |
63 | 65 | }) |
| 66 | +
|
| 67 | + const dashboardWidgets = getDashboardWidgetItemsStore() |
| 68 | +
|
| 69 | + let confirmDuplicate = false |
| 70 | + const duplicateDashboardWidgetMutation = createMutation({ |
| 71 | + mutationFn: trpc.dashboard.widget.duplicate.mutate, |
| 72 | + onSuccess: async () => { |
| 73 | + if ($isDashboard) { |
| 74 | + await invalidate(`dashboard:${$dashboard.id.value}`) |
| 75 | + } |
| 76 | + }, |
| 77 | + onError(error, variables, context) { |
| 78 | + toast.error(error.message) |
| 79 | + }, |
| 80 | + }) |
| 81 | +
|
| 82 | + const duplicateViewWidgetMutation = createMutation({ |
| 83 | + mutationFn: trpc.table.view.widget.duplicate.mutate, |
| 84 | + onSuccess: async () => { |
| 85 | + confirmDuplicate = false |
| 86 | + if (table) { |
| 87 | + await invalidate(`table:${tableId}`) |
| 88 | + } |
| 89 | + }, |
| 90 | + onError(error, variables, context) { |
| 91 | + toast.error(error.message) |
| 92 | + }, |
| 93 | + }) |
64 | 94 | </script> |
65 | 95 |
|
66 | 96 | <div class={cn("group flex h-full w-full flex-col rounded-sm border", $$restProps.class)}> |
|
150 | 180 | <PencilIcon class="mr-2 size-3" /> |
151 | 181 | Edit Name |
152 | 182 | </DropdownMenu.Item> |
| 183 | + <DropdownMenu.Item class="text-xs" on:click={() => (confirmDuplicate = true)}> |
| 184 | + <CopyIcon class="mr-2 size-3" /> |
| 185 | + Duplicate |
| 186 | + </DropdownMenu.Item> |
153 | 187 | <DropdownMenu.Item class="text-xs" on:click={() => (confirmDelete = true)}> |
154 | 188 | <TrashIcon class="mr-2 size-3" /> |
155 | 189 | Delete |
|
202 | 236 | </AlertDialog.Footer> |
203 | 237 | </AlertDialog.Content> |
204 | 238 | </AlertDialog.Root> |
| 239 | + |
| 240 | +<AlertDialog.Root bind:open={confirmDuplicate}> |
| 241 | + <AlertDialog.Content> |
| 242 | + <AlertDialog.Header> |
| 243 | + <AlertDialog.Title>Duplicate Widget {widget.name}?</AlertDialog.Title> |
| 244 | + </AlertDialog.Header> |
| 245 | + <AlertDialog.Footer> |
| 246 | + <AlertDialog.Cancel>Cancel</AlertDialog.Cancel> |
| 247 | + <AlertDialog.Action asChild> |
| 248 | + <Button |
| 249 | + on:click={() => { |
| 250 | + if ($isDashboard) { |
| 251 | + const added = dashboardWidgets.add(widget.item.type) |
| 252 | + const layout = added[COLS] |
| 253 | + const defaultLayout = DashboardLayouts.default() |
| 254 | + $duplicateDashboardWidgetMutation.mutate({ |
| 255 | + dashboardId: $dashboard.id.value, |
| 256 | + widgetId: widget.id, |
| 257 | + layout: { |
| 258 | + x: layout.x ?? defaultLayout.x, |
| 259 | + y: layout.y ?? defaultLayout.y, |
| 260 | + w: layout.w ?? defaultLayout.w, |
| 261 | + h: layout.h ?? defaultLayout.h, |
| 262 | + }, |
| 263 | + }) |
| 264 | + } else if (viewId && tableId) { |
| 265 | + $duplicateViewWidgetMutation.mutate({ tableId, viewId, widgetId: widget.id }) |
| 266 | + } |
| 267 | + }} |
| 268 | + > |
| 269 | + Duplicate |
| 270 | + </Button> |
| 271 | + </AlertDialog.Action> |
| 272 | + </AlertDialog.Footer> |
| 273 | + </AlertDialog.Content> |
| 274 | +</AlertDialog.Root> |
0 commit comments