-
Notifications
You must be signed in to change notification settings - Fork 672
Expand file tree
/
Copy pathuseWorkspaceMenuItems.ts
More file actions
132 lines (114 loc) · 3.59 KB
/
Copy pathuseWorkspaceMenuItems.ts
File metadata and controls
132 lines (114 loc) · 3.59 KB
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
import { computed } from 'vue'
import { useI18n } from 'vue-i18n'
import type { MenuItem } from 'primevue/menuitem'
import { useBillingContext } from '@/composables/billing/useBillingContext'
import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
import { useDialogService } from '@/services/dialogService'
/**
* Builds the Plan & Credits overflow-menu model for the workspace subscription
* panel. Visibility and the Delete enable/disable policy are derived from the
* shared useWorkspaceUI state so this menu can't desync with the sibling
* Plan & Credits panel menu.
*/
export function useWorkspaceMenuItems() {
const { t } = useI18n()
const { billingStatus, isFreeTier, subscription } = useBillingContext()
const {
permissions,
uiConfig,
isInPersonalWorkspace,
isActiveSubscription,
isSubscriptionCancelled,
isDeleteDisabled,
deleteDisabledTooltipKey
} = useWorkspaceUI()
const {
showCancelSubscriptionFlow,
showEditWorkspaceDialog,
showDeleteWorkspaceDialog,
showLeaveWorkspaceDialog
} = useDialogService()
function editWorkspace() {
void showEditWorkspaceDialog()
}
function cancelSubscription() {
if (
!permissions.value.canManageSubscriptionLifecycle ||
!canCancelPlan.value
) {
return
}
void showCancelSubscriptionFlow(subscription.value?.endDate ?? undefined)
}
function deleteWorkspace() {
if (
!permissions.value.canManageSubscription ||
isInPersonalWorkspace.value ||
isDeleteDisabled.value
) {
return
}
void showDeleteWorkspaceDialog()
}
function leaveWorkspace() {
if (!permissions.value.canLeaveWorkspace) return
void showLeaveWorkspaceDialog()
}
const canCancelPlan = computed(
() =>
permissions.value.canManageSubscriptionLifecycle &&
(isActiveSubscription.value ||
billingStatus.value === 'payment_failed' ||
billingStatus.value === 'paused') &&
!isSubscriptionCancelled.value &&
!isFreeTier.value
)
const canDeleteWorkspace = computed(
() =>
permissions.value.canManageSubscription && !isInPersonalWorkspace.value
)
const deleteTooltip = computed(() => {
const key = deleteDisabledTooltipKey.value
return key ? t(key) : undefined
})
const menuItems = computed<MenuItem[]>(() => {
const items: MenuItem[] = []
if (uiConfig.value.showEditWorkspaceMenuItem) {
items.push({
label: t('workspacePanel.menu.editWorkspace'),
command: editWorkspace
})
}
if (canCancelPlan.value) {
items.push({
label: t('subscription.cancelPlan'),
command: cancelSubscription
})
}
if (canDeleteWorkspace.value) {
items.push({
label: t('workspacePanel.menu.deleteWorkspace'),
class: isDeleteDisabled.value
? 'data-disabled:cursor-not-allowed data-disabled:text-destructive-background/50 data-disabled:pointer-events-auto'
: 'text-destructive-background',
disabled: isDeleteDisabled.value,
tooltip: deleteTooltip.value,
command: isDeleteDisabled.value ? undefined : deleteWorkspace
})
}
if (permissions.value.canLeaveWorkspace) {
items.push({
label: t('workspacePanel.menu.leaveWorkspace'),
command: leaveWorkspace
})
}
return items
})
// Figma 3343-25140 renders a divider between every menu option.
const menuEntries = computed<MenuItem[]>(() =>
menuItems.value.flatMap((item, index) =>
index === 0 ? [item] : [{ separator: true }, item]
)
)
return { menuItems, menuEntries }
}