Line data Source code
1 : import { computed } from 'vue'
2 : import { useI18n } from 'vue-i18n'
3 :
4 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
5 : import type { ServerFact } from '@/platform/serverFacts/serverFact'
6 : import { formatSubscriptionDate } from '@/platform/billing/components/subscriptionPanelWorkspace.logic'
7 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
8 :
9 : type DeleteWorkspaceGate =
10 : | { status: 'hidden' }
11 : | { status: 'enabled' }
12 : | { status: 'disabled'; denial: string | undefined }
13 :
14 : interface DeleteWorkspaceGateInput {
15 : resolved: boolean
16 : canDelete: ServerFact<'can_delete_workspace'>
17 : denial: string | undefined
18 : }
19 :
20 6 : const HIDDEN_DENIALS: ReadonlySet<string> = new Set([
21 : 'not_a_member',
22 : 'not_workspace_owner',
23 : 'personal_workspace',
24 : 'sales_managed_plan',
25 : 'workspace_sso_attached'
26 : ])
27 :
28 6 : const DENIAL_TOOLTIP_KEYS: Readonly<Record<string, string>> = {
29 : plan_not_ended: 'workspacePanel.menu.deleteWorkspacePlanNotEndedTooltip',
30 : billing_operation_parked:
31 : 'workspacePanel.menu.deleteWorkspaceBillingParkedTooltip',
32 : billing_operation_in_progress:
33 : 'workspacePanel.menu.deleteWorkspaceBillingInProgressTooltip'
34 : }
35 :
36 6 : const GENERIC_TOOLTIP_KEY =
37 : 'workspacePanel.menu.deleteWorkspaceUnavailableTooltip'
38 :
39 6 : function resolveDeleteWorkspaceGate({
40 : resolved,
41 : canDelete,
42 : denial
43 : }: DeleteWorkspaceGateInput): DeleteWorkspaceGate {
44 6 : if (!resolved) return { status: 'hidden' }
45 6 : if (canDelete) return { status: 'enabled' }
46 6 : if (denial !== undefined && HIDDEN_DENIALS.has(denial)) {
47 0 : return { status: 'hidden' }
48 : }
49 6 : return { status: 'disabled', denial }
50 : }
51 :
52 6 : export function useDeleteWorkspaceGate() {
53 6 : const { t, locale } = useI18n()
54 6 : const { subscription } = useBillingContext()
55 6 : const { canDeleteWorkspace, deleteWorkspaceDenial, hasResolvedCapabilities } =
56 : useBillingCapabilities()
57 :
58 6 : const gate = computed(() =>
59 : resolveDeleteWorkspaceGate({
60 : resolved: hasResolvedCapabilities.value,
61 : canDelete: canDeleteWorkspace.value,
62 : denial: deleteWorkspaceDenial.value
63 : })
64 : )
65 :
66 : const tooltip = computed(() => {
67 6 : const current = gate.value
68 6 : if (current.status !== 'disabled') return undefined
69 6 : const planEndDate =
70 : current.denial === 'plan_not_ended' && subscription.value?.isCancelled
71 : ? formatSubscriptionDate(subscription.value.endDate, locale.value)
72 : : undefined
73 6 : if (planEndDate) {
74 0 : return t('workspacePanel.menu.deleteWorkspaceAfterPlanEndsTooltip', {
75 : date: planEndDate
76 : })
77 : }
78 6 : const key =
79 : (current.denial && DENIAL_TOOLTIP_KEYS[current.denial]) ||
80 : GENERIC_TOOLTIP_KEY
81 6 : return t(key)
82 : })
83 :
84 6 : return { gate, tooltip }
85 : }
|