Line data Source code
1 : import { computed, watch } from 'vue'
2 : import { createSharedComposable } from '@vueuse/core'
3 :
4 : import { useFeatureFlags } from '@/composables/useFeatureFlags'
5 :
6 : import type {
7 : WorkspaceRole,
8 : WorkspaceType
9 : } from '@/platform/workspace/api/workspaceApi'
10 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
11 :
12 : interface WorkspacePermissions {
13 : canViewOtherMembers: boolean
14 : canViewPendingInvites: boolean
15 : }
16 :
17 : interface WorkspaceUIConfig {
18 : showMembersList: boolean
19 : showPendingTab: boolean
20 : showSearch: boolean
21 : showRoleColumn: boolean
22 : showCreditsColumn: boolean
23 : membersGridCols: string
24 : pendingGridCols: string
25 : headerGridCols: string
26 : workspaceMenuAction: 'delete' | null
27 : workspaceMenuDisabledTooltip: string | null
28 : }
29 :
30 1 : function getUIConfig(
31 : type: WorkspaceType,
32 : role: WorkspaceRole
33 : ): WorkspaceUIConfig {
34 1 : if (role === 'member') {
35 0 : return {
36 : showMembersList: true,
37 : showPendingTab: false,
38 : showSearch: true,
39 : showRoleColumn: true,
40 : showCreditsColumn: false,
41 : membersGridCols: 'grid-cols-[1fr_auto]',
42 : pendingGridCols: 'grid-cols-[50%_20%_20%_10%]',
43 : headerGridCols: 'grid-cols-[1fr_auto]',
44 : workspaceMenuAction: null,
45 : workspaceMenuDisabledTooltip: null
46 : }
47 : }
48 :
49 1 : if (type === 'personal') {
50 1 : return {
51 : showMembersList: false,
52 : showPendingTab: false,
53 : showSearch: false,
54 : showRoleColumn: false,
55 : showCreditsColumn: false,
56 : membersGridCols: 'grid-cols-1',
57 : pendingGridCols: 'grid-cols-[50%_20%_20%_10%]',
58 : headerGridCols: 'grid-cols-1',
59 : workspaceMenuAction: null,
60 : workspaceMenuDisabledTooltip: null
61 : }
62 : }
63 :
64 0 : return {
65 : showMembersList: true,
66 : showPendingTab: true,
67 : showSearch: true,
68 : showRoleColumn: true,
69 : showCreditsColumn: false,
70 : membersGridCols: 'grid-cols-[50%_40%_10%]',
71 : pendingGridCols: 'grid-cols-[50%_20%_20%_10%]',
72 : headerGridCols: 'grid-cols-[50%_40%_10%]',
73 : workspaceMenuAction: 'delete',
74 : workspaceMenuDisabledTooltip:
75 : 'workspacePanel.menu.deleteWorkspaceDisabledTooltip'
76 : }
77 : }
78 :
79 109 : function useWorkspaceUIInternal() {
80 109 : const store = useTeamWorkspaceStore()
81 109 : const { flags } = useFeatureFlags()
82 :
83 109 : const isInPersonalWorkspace = computed(() => store.isInPersonalWorkspace)
84 :
85 109 : const workspaceType = computed<WorkspaceType>(
86 : () => store.activeWorkspace?.type ?? 'personal'
87 : )
88 :
89 109 : const workspaceRole = computed<WorkspaceRole>(
90 : () => store.activeWorkspace?.role ?? 'owner'
91 : )
92 :
93 109 : watch(
94 : [() => store.activeWorkspace?.id, () => store.activeWorkspace?.role],
95 : () => {
96 109 : if (store.activeWorkspace?.role === 'owner') {
97 8 : void store.ensureMembersLoaded()
98 : }
99 : },
100 : { immediate: true }
101 : )
102 :
103 109 : const uiConfig = computed<WorkspaceUIConfig>(() => {
104 1 : const base = getUIConfig(workspaceType.value, workspaceRole.value)
105 : const showCreditsColumn =
106 : flags.memberCreditLimitsEnabled &&
107 : workspaceType.value === 'team' &&
108 : workspaceRole.value === 'owner'
109 1 : if (!showCreditsColumn) return base
110 :
111 0 : return {
112 : ...base,
113 : showCreditsColumn: true,
114 : membersGridCols: 'grid-cols-[38%_18%_30%_14%]',
115 : headerGridCols: 'grid-cols-[38%_18%_30%_14%]'
116 : }
117 : })
118 :
119 : const permissions = computed<WorkspacePermissions>(() => ({
120 : canViewOtherMembers: uiConfig.value.showMembersList,
121 : canViewPendingInvites: uiConfig.value.showPendingTab
122 : }))
123 :
124 : const isWorkspaceMenuInHeader = computed(
125 : () => flags.unifiedBillingSettingsEnabled
126 : )
127 :
128 109 : return {
129 : permissions,
130 : uiConfig,
131 : workspaceType,
132 : workspaceRole,
133 : isInPersonalWorkspace,
134 : isWorkspaceMenuInHeader
135 : }
136 : }
137 :
138 2355 : export const useWorkspaceUI = createSharedComposable(useWorkspaceUIInternal)
|