LCOV - code coverage report
Current view: top level - src/platform/account/components - CurrentUserPopoverWorkspace.vue Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 67.7 % 65 44
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : <!-- A popover that shows current user information and actions -->
       2              : <template>
       3            1 :   <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              :     </div>
      22              : 
      23              :     <!-- Workspace Selector -->
      24              :     <div v-if="!accountActionsOnly" class="relative">
      25              :       <!-- An API-key session is bound to one server-resolved workspace, and a
      26              :            Desktop build without workspace switching fixes the host session's;
      27              :            neither exposes discovery or switching -->
      28              :       <div
      29              :         v-if="isApiKeyLogin || isDesktopHostWorkspaceFixed"
      30              :         class="flex w-full items-center gap-2 rounded-lg px-4 py-2"
      31              :         data-testid="workspace-context-row"
      32              :       >
      33              :         <WorkspaceProfilePic class="size-6 shrink-0 text-xs" :workspace-name />
      34              :         <span class="truncate text-sm text-foreground">
      35              :           {{ workspaceName }}
      36              :         </span>
      37              :       </div>
      38              :       <PopoverRoot v-else v-slot="{ close }">
      39              :         <PopoverTrigger as-child>
      40              :           <Button
      41              :             variant="muted-textonly"
      42              :             class="flex h-auto w-full justify-between px-4 py-2"
      43              :             :tooltip="workspaceName ?? ''"
      44              :             data-testid="workspace-switcher-trigger"
      45              :           >
      46              :             <div class="flex w-0 flex-1 items-center gap-2">
      47              :               <WorkspaceProfilePic
      48              :                 class="size-6 shrink-0 text-xs"
      49              :                 :workspace-name
      50              :                 :subscription-tier="activeWorkspaceSummary?.subscriptionTier"
      51              :               />
      52              :               <span class="truncate text-sm text-foreground">
      53              :                 {{ workspaceName }}
      54              :               </span>
      55              :             </div>
      56              :             <i
      57              :               class="icon-[lucide--chevron-down] size-3.5 text-muted-foreground"
      58              :             />
      59              :           </Button>
      60              :         </PopoverTrigger>
      61              :         <PopoverContent
      62              :           side="left"
      63              :           align="start"
      64              :           :side-offset="16"
      65              :           prioritize-position
      66              :           dismiss-on-viewport-change
      67              :           class="w-auto overflow-visible rounded-lg border-border/50 p-2 shadow-sm"
      68              :           data-testid="workspace-switcher-panel"
      69              :         >
      70              :           <WorkspaceSwitcherPopover
      71              :             @select="close"
      72              :             @create="(close(), handleCreateWorkspace())"
      73              :           />
      74              :           <PopoverArrow class="fill-background stroke-border/50" />
      75              :         </PopoverContent>
      76              :       </PopoverRoot>
      77              :     </div>
      78              : 
      79              :     <!-- Credits Section -->
      80              : 
      81              :     <div v-if="!accountActionsOnly" class="flex items-center gap-2 px-4 py-2">
      82              :       <i class="icon-[lucide--coins] text-sm text-credit" />
      83              :       <Skeleton v-if="isLoadingBalance" class="h-5 w-16" />
      84              :       <span v-else class="text-base font-semibold text-foreground">{{
      85              :         displayedCredits
      86              :       }}</span>
      87              :       <Button
      88              :         :tooltip="$t('credits.unified.tooltip')"
      89              :         variant="muted-textonly"
      90              :         size="icon-sm"
      91              :         class="mr-auto"
      92              :         :aria-label="$t('credits.unified.tooltip')"
      93              :         data-testid="credits-info-button"
      94              :       >
      95              :         <i class="icon-[lucide--circle-help]" />
      96              :       </Button>
      97              :       <AddCreditsButton
      98              :         :availability="addCredits"
      99              :         :reason-id="refusalReasonId"
     100              :         variant="secondary"
     101              :         size="sm"
     102              :         data-testid="add-credits-button"
     103              :         :data-dd-action-name="BILLING_ACTION_NAME.addCreditsPopover"
     104              :         @click="handleTopUp"
     105              :       >
     106              :         {{ $t('subscription.addCredits') }}
     107              :       </AddCreditsButton>
     108              :       <Button
     109              :         v-if="paymentRecoveryAvailable"
     110              :         variant="secondary"
     111              :         size="sm"
     112              :         :loading="isUpdatingPayment"
     113              :         @click="updatePayment"
     114              :       >
     115              :         {{ $t('workspacePanel.billingStatus.updatePayment') }}
     116              :       </Button>
     117              :       <SubscribeButton
     118              :         v-if="showSubscribeAction && isPersonalWorkspace"
     119              :         :fluid="false"
     120              :         :label="
     121              :           isCancelled
     122              :             ? $t('subscription.resubscribe')
     123              :             : $t('workspaceSwitcher.subscribe')
     124              :         "
     125              :         size="sm"
     126              :         button-variant="subscribe"
     127              :       />
     128              :       <Button
     129              :         v-if="showSubscribeAction && !isPersonalWorkspace"
     130              :         variant="primary"
     131              :         size="sm"
     132              :         @click="handleOpenSubscriptionAction"
     133              :       >
     134              :         {{
     135              :           isCancelled
     136              :             ? $t('subscription.resubscribe')
     137              :             : $t('workspaceSwitcher.subscribe')
     138              :         }}
     139              :       </Button>
     140              :     </div>
     141              : 
     142              :     <AddCreditsRefusalReason
     143              :       v-if="!accountActionsOnly"
     144              :       :availability="addCredits"
     145              :       :reason-id="refusalReasonId"
     146              :       class="m-0 px-4 pb-2 text-xs text-muted-foreground"
     147              :     />
     148              : 
     149              :     <p
     150              :       v-if="processingPayment"
     151              :       role="status"
     152              :       class="m-0 px-4 pb-2 text-sm text-muted-foreground"
     153              :       data-testid="topup-payment-processing-line"
     154              :     >
     155              :       {{ $t('credits.topUp.paymentProcessingFlyout') }}
     156              :     </p>
     157              : 
     158              :     <div v-if="!accountActionsOnly" class="mx-0 my-2 border-t" />
     159              : 
     160              :     <div
     161              :       v-if="!accountActionsOnly && isCloud && showPlansAndPricing"
     162              :       class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
     163              :       data-testid="plans-pricing-menu-item"
     164              :       @click="handleOpenPlansAndPricing"
     165              :     >
     166              :       <i class="icon-[lucide--receipt-text] text-muted-foreground" />
     167              :       <span class="flex-1 text-foreground">{{
     168              :         $t('subscription.plansAndPricing')
     169              :       }}</span>
     170              :     </div>
     171              : 
     172              :     <button
     173              :       v-if="!accountActionsOnly && isCloud && showManagePlan"
     174              :       type="button"
     175              :       class="flex w-full cursor-pointer appearance-none items-center gap-2 border-0 bg-transparent px-4 py-2 text-left hover:bg-accent focus-visible:bg-accent focus-visible:outline-none"
     176              :       data-testid="manage-plan-menu-item"
     177              :       :data-dd-action-name="BILLING_ACTION_NAME.managePlanPopover"
     178              :       @click="handleOpenManagePlanSettings"
     179              :     >
     180              :       <i class="icon-[lucide--credit-card] text-muted-foreground" />
     181              :       <span class="flex-1 text-sm text-foreground">{{
     182              :         $t('subscription.managePlan')
     183              :       }}</span>
     184              :     </button>
     185              : 
     186              :     <button
     187              :       v-if="!accountActionsOnly && showLocalPlansAndCredits"
     188              :       type="button"
     189              :       class="flex w-full cursor-pointer appearance-none items-center gap-2 border-0 bg-transparent px-4 py-2 text-left hover:bg-accent focus-visible:bg-accent focus-visible:outline-none"
     190              :       data-testid="plans-credits-menu-item"
     191              :       @click="handleOpenPlanCreditsSettings"
     192              :     >
     193              :       <i class="icon-[lucide--coins] text-muted-foreground" />
     194              :       <span class="flex-1 text-sm text-foreground">{{
     195              :         $t('subscription.plansAndCredits')
     196              :       }}</span>
     197              :     </button>
     198              : 
     199              :     <!-- Partner Nodes Pricing (always shown) -->
     200              :     <div
     201              :       v-if="!accountActionsOnly"
     202              :       class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
     203              :       data-testid="partner-nodes-menu-item"
     204              :       @click="handleOpenPartnerNodesInfo"
     205              :     >
     206              :       <i class="icon-[lucide--tag] text-muted-foreground" />
     207              :       <span class="flex-1 text-foreground">{{
     208              :         $t('subscription.partnerNodesCredits')
     209              :       }}</span>
     210              :     </div>
     211              : 
     212              :     <div v-if="!accountActionsOnly" class="mx-0 my-2 border-t" />
     213              : 
     214              :     <!-- Workspace Settings (always shown) -->
     215              :     <div
     216              :       v-if="!accountActionsOnly"
     217              :       class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
     218              :       data-testid="workspace-settings-menu-item"
     219              :       @click="handleOpenWorkspaceSettings"
     220              :     >
     221              :       <i class="icon-[lucide--users] text-muted-foreground" />
     222              :       <span class="flex-1 text-foreground">{{
     223              :         $t('userSettings.workspaceSettings')
     224              :       }}</span>
     225              :     </div>
     226              : 
     227              :     <!-- Account Settings (always shown) -->
     228              :     <div
     229              :       class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
     230              :       data-testid="user-settings-menu-item"
     231              :       @click="handleOpenUserSettings"
     232              :     >
     233              :       <i class="icon-[lucide--settings-2] text-muted-foreground" />
     234              :       <span class="flex-1 text-foreground">{{
     235              :         $t('userSettings.accountSettings')
     236              :       }}</span>
     237              :     </div>
     238              : 
     239              :     <div class="mx-0 my-2 border-t" />
     240              : 
     241              :     <!-- Logout (always shown) -->
     242              :     <div
     243              :       class="flex cursor-pointer items-center gap-2 px-4 py-2 text-sm hover:bg-accent"
     244              :       data-testid="logout-menu-item"
     245              :       @click="handleLogout"
     246              :     >
     247              :       <i class="icon-[lucide--log-out] text-muted-foreground" />
     248              :       <span class="flex-1 text-foreground">{{
     249              :         $t('auth.signOut.signOut')
     250              :       }}</span>
     251              :     </div>
     252              :   </div>
     253              : </template>
     254              : 
     255              : <script setup lang="ts">
     256              : import { PopoverArrow, PopoverRoot, PopoverTrigger } from 'reka-ui'
     257              : import { BILLING_ACTION_NAME } from '@/platform/telemetry/billingActionNames'
     258              : import { storeToRefs } from 'pinia'
     259              : import { computed, onMounted, useId } from 'vue'
     260              : import { useI18n } from 'vue-i18n'
     261              : 
     262              : import { formatCreditsFromCents } from '@/base/credits/comfyCredits'
     263              : import UserAvatar from '@comfyorg/design-system/common/UserAvatar'
     264              : import WorkspaceProfilePic from '@/platform/workspace/components/WorkspaceProfilePic.vue'
     265              : import WorkspaceSwitcherPopover from '@/platform/account/components/WorkspaceSwitcherPopover.vue'
     266              : import Button from '@comfyorg/design-system/button/Button'
     267              : import PopoverContent from '@comfyorg/design-system/popover/PopoverContent'
     268              : import Skeleton from '@comfyorg/design-system/skeleton/Skeleton'
     269              : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
     270              : import {
     271              :   canDesktopHostSwitchWorkspace,
     272              :   isDesktopHostSignedIn
     273              : } from '@/platform/auth/desktopHost/desktopHostSession'
     274              : 
     275              : import { useExternalLink } from '@/composables/useExternalLink'
     276              : import { useBillingContext } from '@/platform/billing/composables/useBillingContext'
     277              : import { refreshBalanceInBackground } from '@/platform/billing/refreshBalanceInBackground'
     278              : import SubscribeButton from '@/platform/billing/subscription/components/SubscribeButton.vue'
     279              : import { useSubscriptionDialog } from '@/platform/billing/subscription/composables/useSubscriptionDialog'
     280              : import { isCloud } from '@/platform/distribution/types'
     281              : import { useTelemetry } from '@comfyorg/telemetry'
     282              : import { paymentIntentSourceForAddCreditsClick } from '@/platform/telemetry/utils/paymentIntentSource'
     283              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
     284              : import { usePaymentRecoveryDialog } from '@/platform/billing/composables/usePaymentRecoveryDialog'
     285              : import { addCreditsAvailability } from '@/platform/billing/legacy160/platform/billing/addCreditsAvailability'
     286              : import AddCreditsButton from '@/platform/billing/legacy160/platform/billing/components/AddCreditsButton.vue'
     287              : import AddCreditsRefusalReason from '@/platform/billing/legacy160/platform/billing/components/AddCreditsRefusalReason.vue'
     288              : import { useAccountUI } from '@/platform/account/composables/useAccountUI'
     289              : import { useTeamWorkspaceStore } from '@/platform/workspace/stores/teamWorkspaceStore'
     290              : import { useSettingsDialog } from '@/platform/settings/composables/useSettingsDialog'
     291              : import { useBillingDialogs } from '@/platform/billing/composables/useBillingDialogs'
     292              : import { useProcessingTopupPayment } from '@/platform/billing/composables/useProcessingTopupPayment'
     293              : import { useWorkspaceDialogs } from '@/platform/account/composables/useWorkspaceDialogs'
     294              : import { useWorkspaceBillingStateStore } from '@/platform/billing/stores/workspaceBillingStateStore'
     295              : 
     296            1 : const workspaceStore = useTeamWorkspaceStore()
     297            1 : const billingState = useWorkspaceBillingStateStore()
     298            1 : const {
     299              :   initState,
     300              :   workspaceName,
     301              :   isInPersonalWorkspace: isPersonalWorkspace,
     302              :   activeWorkspace
     303              : } = storeToRefs(workspaceStore)
     304            1 : const activeWorkspaceSummary = computed(() =>
     305              :   activeWorkspace.value ? billingState.getSummary(activeWorkspace.value) : null
     306              : )
     307            1 : const { permissions, canReactivatePlan, canOpenPricingSurface } = useAccountUI()
     308            1 : const {
     309              :   canTopUp,
     310              :   topUpDenial,
     311              :   hasResolvedCapabilities,
     312              :   canSubscribeSelfServe,
     313              :   paymentRecoveryAvailable
     314              : } = useBillingCapabilities()
     315            1 : const { updatePayment, isUpdatingPayment } = usePaymentRecoveryDialog()
     316            1 : const addCredits = computed(() =>
     317              :   addCreditsAvailability({
     318              :     resolved: hasResolvedCapabilities.value,
     319              :     canTopUp: canTopUp.value,
     320              :     denial: topUpDenial.value
     321              :   })
     322              : )
     323            1 : const refusalReasonId = useId()
     324              : 
     325            1 : const emit = defineEmits<{
     326              :   close: []
     327              : }>()
     328              : 
     329              : const { accountActionsOnly = false } = defineProps<{
     330              :   accountActionsOnly?: boolean
     331              : }>()
     332              : 
     333            1 : const { payment: processingPayment } = useProcessingTopupPayment(
     334              :   () => !accountActionsOnly
     335              : )
     336              : 
     337            1 : const { buildDocsUrl, docsPaths } = useExternalLink()
     338              : 
     339            1 : const {
     340              :   userDisplayName,
     341              :   userEmail,
     342              :   userPhotoUrl,
     343              :   handleSignOut,
     344              :   isApiKeyLogin
     345              : } = useCurrentUser()
     346            1 : const isDesktopHostWorkspaceFixed = computed(
     347              :   () => isDesktopHostSignedIn() && !canDesktopHostSwitchWorkspace()
     348              : )
     349            1 : const settingsDialog = useSettingsDialog()
     350            1 : const { showTopUpCreditsDialog } = useBillingDialogs()
     351            1 : const { showCreateWorkspaceDialog } = useWorkspaceDialogs()
     352            1 : const {
     353              :   billingStatus,
     354              :   canAccessSubscriptionFeatures,
     355              :   subscription,
     356              :   balance,
     357              :   isLoading,
     358              :   fetchBalance
     359              : } = useBillingContext()
     360              : 
     361            1 : const isCancelled = computed(() => subscription.value?.isCancelled ?? false)
     362            1 : const subscriptionDialog = useSubscriptionDialog()
     363              : 
     364            1 : const { locale } = useI18n()
     365            1 : const isLoadingBalance = isLoading
     366              : 
     367            1 : const displayedCredits = computed(() => {
     368            2 :   if (initState.value !== 'ready') return ''
     369              : 
     370              :   // API field is named _micros but contains cents (naming inconsistency)
     371            2 :   const cents =
     372              :     balance.value?.effectiveBalanceMicros ?? balance.value?.amountMicros ?? 0
     373            2 :   return formatCreditsFromCents({
     374              :     cents,
     375              :     locale: locale.value,
     376              :     numberOptions: {
     377              :       minimumFractionDigits: 0,
     378              :       maximumFractionDigits: 2
     379              :     }
     380              :   })
     381              : })
     382              : 
     383            1 : const showPlansAndPricing = canOpenPricingSurface
     384              : // Subscribing is a Cloud-only concept: Local users manage plan/credits
     385              : // through settings instead (see showLocalPlansAndCredits below), regardless
     386              : // of subscription status.
     387            1 : const showLocalPlansAndCredits = computed(
     388              :   () => !isCloud && permissions.value.canManageSubscription
     389              : )
     390            1 : const hasDelinquentSubscription = computed(
     391              :   () =>
     392              :     (billingStatus.value === 'payment_failed' ||
     393              :       billingStatus.value === 'paused') &&
     394              :     Boolean(subscription.value?.planSlug)
     395              : )
     396            1 : const showManagePlan = computed(
     397              :   () =>
     398              :     permissions.value.canManageSubscription &&
     399              :     (canAccessSubscriptionFeatures.value || hasDelinquentSubscription.value)
     400              : )
     401              : 
     402            1 : const showSubscribeAction = computed(
     403              :   () =>
     404              :     // Subscribing is Cloud-only, so the whole action stays gated on isCloud.
     405              :     // Self-serve subscribe is server-authoritative; reactivation is not.
     406              :     isCloud &&
     407              :     ((isCancelled.value && canReactivatePlan.value) ||
     408              :       (!canAccessSubscriptionFeatures.value &&
     409              :         !hasDelinquentSubscription.value &&
     410              :         canSubscribeSelfServe.value &&
     411              :         canTopUp.value))
     412              : )
     413              : 
     414            1 : const handleOpenUserSettings = () => {
     415            0 :   settingsDialog.show('user')
     416            0 :   emit('close')
     417              : }
     418              : 
     419            1 : const handleOpenWorkspaceSettings = () => {
     420            0 :   settingsDialog.show('workspace')
     421            0 :   emit('close')
     422              : }
     423              : 
     424              : /**
     425              :  * Plan selection stays in the app: billing-web's `/v1/pricing` has no
     426              :  * personal/team tabs, cycle toggle, or credit slider (G7), and a per-credit
     427              :  * Team plan 400s there (FE-2642). Only checkout hands off to billing-web,
     428              :  * from inside the table (`useSubscriptionCheckout`'s `handleSubscribeClick`
     429              :  * / `handleSubscribeTeamClick`).
     430              :  */
     431            1 : const handleOpenPlansAndPricing = () => {
     432            0 :   subscriptionDialog.showPricingTable({ reason: 'avatar_menu_plans' })
     433            0 :   emit('close')
     434              : }
     435              : 
     436            1 : const handleOpenSubscriptionAction = () => {
     437            0 :   subscriptionDialog.showPricingTable({ reason: 'avatar_menu_plans' })
     438            0 :   emit('close')
     439              : }
     440              : 
     441            1 : const handleOpenManagePlanSettings = () => {
     442            0 :   settingsDialog.show('workspace')
     443            0 :   emit('close')
     444              : }
     445              : 
     446            1 : const handleOpenPlanCreditsSettings = () => {
     447            0 :   settingsDialog.show('workspace')
     448            0 :   emit('close')
     449              : }
     450              : 
     451            1 : const handleTopUp = () => {
     452            0 :   useTelemetry()?.trackAddApiCreditButtonClicked({ source: 'avatar_menu' })
     453            0 :   showTopUpCreditsDialog({
     454              :     source: paymentIntentSourceForAddCreditsClick('avatar_menu')
     455              :   })
     456            0 :   emit('close')
     457              : }
     458              : 
     459            1 : const handleOpenPartnerNodesInfo = () => {
     460            0 :   window.open(
     461              :     buildDocsUrl(docsPaths.partnerNodesPricing, { includeLocale: true }),
     462              :     '_blank'
     463              :   )
     464            0 :   emit('close')
     465              : }
     466              : 
     467            1 : const handleLogout = async () => {
     468            0 :   await handleSignOut()
     469            0 :   emit('close')
     470              : }
     471              : 
     472            1 : const handleCreateWorkspace = () => {
     473            0 :   showCreateWorkspaceDialog()
     474            0 :   emit('close')
     475              : }
     476              : 
     477            1 : onMounted(() => {
     478            1 :   if (!accountActionsOnly) void refreshBalanceInBackground(fetchBalance)
     479              : })
     480              : </script>
        

Generated by: LCOV version 2.0-1