Line data Source code
1 : <!-- A popover that shows current user information and actions -->
2 : <template>
3 0 : <div data-testid="current-user-popover" class="current-user-popover p-2">
4 : <!-- User Info Section -->
5 : <div class="mb-4 flex flex-col items-center px-0 py-3">
6 : <UserAvatar
7 : :label="$t('auth.login.userAvatar')"
8 : class="mb-1"
9 : :photo-url="userPhotoUrl"
10 : icon-class="size-6"
11 : size="large"
12 : />
13 :
14 : <!-- User Details -->
15 : <h3 class="my-0 mb-1 truncate text-base font-bold text-foreground">
16 : {{ userDisplayName || $t('g.user') }}
17 : </h3>
18 : <p v-if="userEmail" class="my-0 truncate text-sm text-muted-foreground">
19 : {{ userEmail }}
20 : </p>
21 : <Badge
22 : v-if="subscriptionTierName"
23 : variant="pill"
24 : class="mt-2 bg-accent text-xs font-bold uppercase"
25 : >
26 : {{ subscriptionTierName }}
27 : </Badge>
28 : </div>
29 :
30 : <PopoverRoot v-if="showWorkspaceSwitcher" v-slot="{ close }">
31 : <PopoverTrigger as-child>
32 : <Button
33 : variant="muted-textonly"
34 : class="flex h-auto w-full justify-between px-4 py-2"
35 : :tooltip="workspaceName"
36 : data-testid="workspace-switcher-trigger"
37 : >
38 : <div class="flex w-0 flex-1 items-center gap-2">
39 : <WorkspaceProfilePic
40 : class="size-6 shrink-0 text-xs"
41 : :workspace-name
42 : :subscription-tier="tier"
43 : />
44 : <span class="truncate text-sm text-foreground">
45 : {{ workspaceName }}
46 : </span>
47 : </div>
48 : <i
49 : class="icon-[lucide--chevron-down] size-3.5 text-muted-foreground"
50 : />
51 : </Button>
52 : </PopoverTrigger>
53 : <PopoverContent
54 : side="left"
55 : align="start"
56 : :side-offset="16"
57 : prioritize-position
58 : dismiss-on-viewport-change
59 : class="w-auto overflow-visible rounded-lg border-border/50 p-2 shadow-sm"
60 : data-testid="workspace-switcher-panel"
61 : >
62 : <WorkspaceSwitcherPopover @select="close" />
63 : <PopoverArrow class="fill-background stroke-border/50" />
64 : </PopoverContent>
65 : </PopoverRoot>
66 :
67 : <!-- Credits Section -->
68 : <div
69 : v-if="canAccessSubscriptionFeatures || showWorkspaceSwitcher"
70 : class="flex items-center gap-2 px-4 py-2"
71 : >
72 : <i class="icon-[lucide--coins] text-sm text-credit" />
73 : <Skeleton v-if="isLoading" class="h-5 w-16" />
74 : <span v-else class="text-base font-semibold text-foreground">{{
75 : formattedBalance
76 : }}</span>
77 : <Button
78 : :tooltip="$t('credits.unified.tooltip')"
79 : variant="muted-textonly"
80 : size="icon-sm"
81 : class="mr-auto"
82 : :aria-label="$t('credits.unified.tooltip')"
83 : data-testid="credits-info-button"
84 : >
85 : <i class="icon-[lucide--circle-help]" />
86 : </Button>
87 : <Button
88 : v-if="canTopUp"
89 : variant="secondary"
90 : size="sm"
91 : data-testid="add-credits-button"
92 : :data-dd-action-name="BILLING_ACTION_NAME.addCreditsPopover"
93 : @click="handleTopUp"
94 : >
95 : {{ $t('subscription.addCredits') }}
96 : </Button>
97 : </div>
98 :
99 : <div class="mx-0 my-2 border-t" />
100 :
101 : <div
102 : v-if="canAccessSubscriptionFeatures"
103 : class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
104 : data-testid="partner-nodes-menu-item"
105 : @click="handleOpenPartnerNodesInfo"
106 : >
107 : <i class="icon-[lucide--tag] text-muted-foreground" />
108 : <span class="flex-1 text-foreground">{{
109 : $t('subscription.partnerNodesCredits')
110 : }}</span>
111 : </div>
112 :
113 : <div
114 : v-if="canAccessSubscriptionFeatures || showWorkspaceSwitcher"
115 : class="flex cursor-pointer items-center gap-2 px-4 py-2 hover:bg-accent"
116 : data-testid="manage-plan-menu-item"
117 : @click="handleOpenPlanAndCreditsSettings"
118 : >
119 : <i class="icon-[lucide--coins] text-muted-foreground" />
120 : <span class="flex-1 text-sm text-foreground">{{
121 : $t('credits.credits')
122 : }}</span>
123 : </div>
124 :
125 : <div
126 : class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
127 : data-testid="user-settings-menu-item"
128 : @click="handleOpenUserSettings"
129 : >
130 : <i class="icon-[lucide--settings-2] text-muted-foreground" />
131 : <span class="flex-1 text-foreground">{{
132 : $t('userSettings.accountSettings')
133 : }}</span>
134 : </div>
135 :
136 : <div class="mx-0 my-2 border-t" />
137 :
138 : <div
139 : class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
140 : data-testid="logout-menu-item"
141 : @click="handleLogout"
142 : >
143 : <i class="icon-[lucide--log-out] text-muted-foreground" />
144 : <span class="flex-1 text-foreground">{{
145 : $t('auth.signOut.signOut')
146 : }}</span>
147 : </div>
148 : </div>
149 : </template>
150 :
151 : <script setup lang="ts">
152 : import { PopoverArrow, PopoverRoot, PopoverTrigger } from 'reka-ui'
153 : import Badge from '@comfyorg/design-system/badge/Badge'
154 : import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames'
155 : import { storeToRefs } from 'pinia'
156 : import { computed, onMounted } from 'vue'
157 : import { useI18n } from 'vue-i18n'
158 :
159 : import { formatCreditsFromCents } from '@/base/credits/comfyCredits'
160 : import UserAvatar from '@comfyorg/design-system/common/UserAvatar'
161 : import Button from '@comfyorg/design-system/button/Button'
162 : import PopoverContent from '@comfyorg/design-system/popover/PopoverContent'
163 : import Skeleton from '@comfyorg/design-system/skeleton/Skeleton'
164 : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
165 : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
166 : import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground'
167 : import { useExternalLink } from '@/composables/useExternalLink'
168 : import { useTelemetry } from '@comfyorg/telemetry'
169 : import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
170 : import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog'
171 : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
172 : import WorkspaceProfilePic from '@/platform/workspace/components/WorkspaceProfilePic.vue'
173 : import WorkspaceSwitcherPopover from '@/platform/account/components/WorkspaceSwitcherPopover.vue'
174 : import { useWorkspaceTierLabel } from '@/platform/billing/composables/useWorkspaceTierLabel'
175 : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
176 : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
177 :
178 0 : const emit = defineEmits<{
179 : close: []
180 : }>()
181 :
182 0 : const { buildDocsUrl, docsPaths } = useExternalLink()
183 :
184 0 : const { userDisplayName, userEmail, userPhotoUrl, handleSignOut } =
185 : useCurrentUser()
186 0 : const settingsDialog = useSettingsDialog()
187 0 : const { showTopUpCreditsDialog } = useBillingDialogs()
188 0 : const {
189 : canAccessSubscriptionFeatures,
190 : tier,
191 : subscription,
192 : balance,
193 : isLoading,
194 : fetchBalance
195 : } = useBillingContext()
196 0 : const { formatTierName } = useWorkspaceTierLabel()
197 0 : const { locale } = useI18n()
198 :
199 0 : const { initState, workspaces, workspaceName } = storeToRefs(
200 : useTeamWorkspaceStore()
201 : )
202 0 : const showWorkspaceSwitcher = computed(
203 : () => initState.value === 'ready' && workspaces.value.length > 0
204 : )
205 :
206 0 : const { canTopUp } = useBillingCapabilities()
207 :
208 0 : const subscriptionTierName = computed(() =>
209 : formatTierName(tier.value, subscription.value?.duration === 'ANNUAL')
210 : )
211 :
212 0 : const formattedBalance = computed(() => {
213 0 : const cents =
214 : balance.value?.effectiveBalanceMicros ?? balance.value?.amountMicros ?? 0
215 0 : return formatCreditsFromCents({
216 : cents,
217 : locale: locale.value,
218 : numberOptions: {
219 : minimumFractionDigits: 0,
220 : maximumFractionDigits: 2
221 : }
222 : })
223 : })
224 :
225 0 : const handleOpenUserSettings = () => {
226 0 : settingsDialog.show('user')
227 0 : emit('close')
228 : }
229 :
230 0 : const handleOpenPlanAndCreditsSettings = () => {
231 0 : settingsDialog.show('workspace')
232 0 : emit('close')
233 : }
234 :
235 0 : const handleTopUp = () => {
236 0 : useTelemetry()?.trackAddApiCreditButtonClicked({ source: 'avatar_menu' })
237 0 : showTopUpCreditsDialog({
238 : source: paymentIntentSourceForAddCreditsClick('avatar_menu')
239 : })
240 0 : emit('close')
241 : }
242 :
243 0 : const handleOpenPartnerNodesInfo = () => {
244 0 : window.open(
245 : buildDocsUrl(docsPaths.partnerNodesPricing, { includeLocale: true }),
246 : '_blank'
247 : )
248 0 : emit('close')
249 : }
250 :
251 0 : const handleLogout = async () => {
252 0 : await handleSignOut()
253 0 : emit('close')
254 : }
255 :
256 0 : onMounted(() => {
257 0 : void refreshBalanceInBackground(fetchBalance)
258 : })
259 : </script>
|