|
| 1 | +import type { ComputedRef } from 'vue'; |
| 2 | + |
| 3 | +import { useMutation } from '@tanstack/vue-query'; |
| 4 | + |
| 5 | +import type { ListResponse } from '@/api-clients/_common/schema/api-verbs/list'; |
| 6 | +import { RESOURCE_GROUP } from '@/api-clients/_common/schema/constant'; |
| 7 | +import type { DashboardCreateParams, DashboardModel } from '@/api-clients/dashboard/_types/dashboard-type'; |
| 8 | +import type { WidgetModel } from '@/api-clients/dashboard/_types/widget-type'; |
| 9 | +import { usePrivateDashboardApi } from '@/api-clients/dashboard/private-dashboard/composables/use-private-dashboard-api'; |
| 10 | +import type { PrivateDashboardCreateParameters } from '@/api-clients/dashboard/private-dashboard/schema/api-verbs/create'; |
| 11 | +import { usePrivateWidgetApi } from '@/api-clients/dashboard/private-widget/composables/use-private-widget-api'; |
| 12 | +import { usePublicDashboardApi } from '@/api-clients/dashboard/public-dashboard/composables/use-public-dashboard-api'; |
| 13 | +import type { PublicDashboardCreateParameters } from '@/api-clients/dashboard/public-dashboard/schema/api-verbs/create'; |
| 14 | +import { usePublicWidgetApi } from '@/api-clients/dashboard/public-widget/composables/use-public-widget-api'; |
| 15 | + |
| 16 | +import { useAllReferenceStore } from '@/store/reference/all-reference-store'; |
| 17 | +import { useUserStore } from '@/store/user/user-store'; |
| 18 | + |
| 19 | +import { useDashboardRouteContext } from '@/services/dashboard-shared/core/composables/use-dashboard-route-context'; |
| 20 | +import { getSharedDashboardLayouts } from '@/services/dashboard-shared/core/helpers/dashboard-share-helper'; |
| 21 | + |
| 22 | + |
| 23 | +interface UseDashboardCloneActionOptions { |
| 24 | + dashboardId: ComputedRef<string|undefined>; |
| 25 | + isPrivate?: ComputedRef<boolean>; |
| 26 | + onSuccess?: (data: DashboardModel, variables: DashboardCreateParams) => void|Promise<void>; |
| 27 | + onError?: (error: Error, variables: DashboardCreateParams) => void|Promise<void>; |
| 28 | + onSettled?: (data: DashboardModel|undefined, error: Error|null, variables: DashboardCreateParams) => void|Promise<void>; |
| 29 | +} |
| 30 | + |
| 31 | +export const useDashboardCloneAction = (options: UseDashboardCloneActionOptions) => { |
| 32 | + const { publicDashboardAPI } = usePublicDashboardApi(); |
| 33 | + const { privateDashboardAPI } = usePrivateDashboardApi(); |
| 34 | + const { privateWidgetAPI } = usePrivateWidgetApi(); |
| 35 | + const { publicWidgetAPI } = usePublicWidgetApi(); |
| 36 | + const allReferenceStore = useAllReferenceStore(); |
| 37 | + const userStore = useUserStore(); |
| 38 | + const { |
| 39 | + entryPoint, |
| 40 | + } = useDashboardRouteContext(); |
| 41 | + |
| 42 | + const { |
| 43 | + dashboardId, isPrivate, onSuccess, onError, onSettled, |
| 44 | + } = options; |
| 45 | + |
| 46 | + |
| 47 | + const getDashboardFn = async (): Promise<DashboardModel> => { |
| 48 | + if (!dashboardId.value) throw new Error('Dashboard id not found'); |
| 49 | + try { |
| 50 | + if (dashboardId.value.startsWith('private')) { |
| 51 | + return privateDashboardAPI.get({ dashboard_id: dashboardId.value }); |
| 52 | + } |
| 53 | + return publicDashboardAPI.get({ dashboard_id: dashboardId.value }); |
| 54 | + } catch (error) { |
| 55 | + throw new Error('Dashboard not found'); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + const listWidgetFn = async (): Promise<ListResponse<WidgetModel>> => { |
| 60 | + if (!dashboardId.value) throw new Error('Dashboard id not found'); |
| 61 | + try { |
| 62 | + if (dashboardId.value.startsWith('private')) { |
| 63 | + return privateWidgetAPI.list({ dashboard_id: dashboardId.value }); |
| 64 | + } |
| 65 | + return publicWidgetAPI.list({ dashboard_id: dashboardId.value }); |
| 66 | + } catch (error) { |
| 67 | + throw new Error('Widget list not found'); |
| 68 | + } |
| 69 | + }; |
| 70 | + |
| 71 | + |
| 72 | + const cloneDashboardFn = async (params: DashboardCreateParams): Promise<DashboardModel> => { |
| 73 | + if (!dashboardId.value) throw new Error('Dashboard id not found'); |
| 74 | + |
| 75 | + const dashboard = await getDashboardFn(); |
| 76 | + const widgetList = await listWidgetFn(); |
| 77 | + |
| 78 | + const _sharedLayouts = await getSharedDashboardLayouts(dashboard.layouts, widgetList.results || [], allReferenceStore.getters.costDataSource); |
| 79 | + |
| 80 | + const _sharedDashboard: DashboardCreateParams = { |
| 81 | + name: params.name, |
| 82 | + layouts: _sharedLayouts, |
| 83 | + options: dashboard.options || {}, |
| 84 | + labels: dashboard.labels || [], |
| 85 | + tags: { created_by: userStore.state.userId }, |
| 86 | + vars: dashboard.vars, |
| 87 | + vars_schema: dashboard.vars_schema, |
| 88 | + }; |
| 89 | + if (entryPoint.value === 'ADMIN') { |
| 90 | + (_sharedDashboard as PublicDashboardCreateParameters).resource_group = RESOURCE_GROUP.DOMAIN; |
| 91 | + } else if (entryPoint.value === 'WORKSPACE') { |
| 92 | + if (!isPrivate?.value) { |
| 93 | + (_sharedDashboard as PublicDashboardCreateParameters).resource_group = RESOURCE_GROUP.WORKSPACE; |
| 94 | + } |
| 95 | + } else if (entryPoint.value === 'PROJECT') { |
| 96 | + (_sharedDashboard as PublicDashboardCreateParameters).resource_group = RESOURCE_GROUP.PROJECT; |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + if (isPrivate?.value) { |
| 101 | + return privateDashboardAPI.create(_sharedDashboard as PrivateDashboardCreateParameters); |
| 102 | + } |
| 103 | + return publicDashboardAPI.create(_sharedDashboard as PublicDashboardCreateParameters); |
| 104 | + }; |
| 105 | + |
| 106 | + return useMutation({ |
| 107 | + mutationFn: cloneDashboardFn, |
| 108 | + onSuccess: async (data, variables) => { |
| 109 | + if (onSuccess) await onSuccess(data, variables); |
| 110 | + }, |
| 111 | + onError: (error, variables) => { |
| 112 | + if (onError) onError(error, variables); |
| 113 | + }, |
| 114 | + onSettled: (data, error, variables) => { |
| 115 | + if (onSettled) onSettled(data, error, variables); |
| 116 | + }, |
| 117 | + }); |
| 118 | +}; |
0 commit comments