LCOV - code coverage report
Current view: top level - src/platform/settings/composables - useSettingUI.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 86.1 % 79 68
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import {
       2              :   computed,
       3              :   defineAsyncComponent,
       4              :   defineComponent,
       5              :   h,
       6              :   onMounted,
       7              :   ref
       8              : } from 'vue'
       9              : import type { Component } from 'vue'
      10              : import { useI18n } from 'vue-i18n'
      11              : 
      12              : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
      13              : import { useFeatureFlags } from '@/composables/useFeatureFlags'
      14              : import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
      15              : import { useBillingCapabilities } from '@/platform/billing/composables/useBillingCapabilities'
      16              : import { pendingServerFact } from '@/platform/serverFacts/pendingServerFact'
      17              : import { isCloud, isDesktop } from '@/platform/distribution/types'
      18              : import { getSettingInfo } from '@comfyorg/settings-core/settingStore'
      19              : import { useSettingStore } from '@/platform/settings/settingStore'
      20              : import type { SettingTreeNode } from '@/platform/settings/composables/useSettingSearch'
      21              : import type {
      22              :   SettingPanelType,
      23              :   SettingParams
      24              : } from '@comfyorg/settings-core/types'
      25              : import { useWorkspaceUI } from '@/platform/workspace/composables/useWorkspaceUI'
      26              : import { usePartnerNodeGovernanceStore } from '@/platform/workspace/stores/partnerNodeGovernanceStore'
      27              : import { useSkillPacksStore } from '@/platform/skills/stores/skillPacksStore'
      28              : import type { NavGroupData } from '@comfyorg/design-system/lib/navigation/types'
      29              : import { normalizeI18nKey } from '@/utils/formatUtil'
      30              : import { buildTree } from '@/utils/treeUtil'
      31              : 
      32         2355 : const CATEGORY_ICONS: Record<string, string> = {
      33              :   '3D': 'icon-[lucide--box]',
      34              :   about: 'icon-[lucide--info]',
      35              :   Appearance: 'icon-[lucide--palette]',
      36              :   Comfy: 'icon-[lucide--settings]',
      37              :   credits: 'icon-[lucide--coins]',
      38              :   extension: 'icon-[lucide--puzzle]',
      39              :   keybinding: 'icon-[lucide--keyboard]',
      40              :   LiteGraph: 'icon-[lucide--workflow]',
      41              :   'Mask Editor': 'icon-[lucide--pen-tool]',
      42              :   Members: 'icon-[lucide--users]',
      43              :   Other: 'icon-[lucide--ellipsis]',
      44              :   PlanCredits: 'icon-[lucide--receipt-text]',
      45              :   secrets: 'icon-[lucide--key-round]',
      46              :   'skill-packs': 'icon-[lucide--book-open-text]',
      47              :   'server-config': 'icon-[lucide--server]',
      48              :   user: 'icon-[lucide--user]',
      49              :   workspace: 'icon-[lucide--building-2]',
      50              :   'workspace-allowlist': 'icon-[comfy--ai-model]',
      51              :   'workspace-members': 'icon-[lucide--users]'
      52              : }
      53              : 
      54              : interface SettingPanelItem {
      55              :   node: SettingTreeNode
      56              :   component: Component
      57              :   props?: Record<string, unknown>
      58              : }
      59              : 
      60          107 : export function useSettingUI(
      61              :   defaultPanel?: SettingPanelType,
      62              :   scrollToSettingId?: string
      63              : ) {
      64          107 :   const { t } = useI18n()
      65          107 :   const { isLoggedIn } = useCurrentUser()
      66          107 :   const { canManageMembers } = useBillingCapabilities()
      67          107 :   const settingStore = useSettingStore()
      68          107 :   const activeCategory = ref<SettingTreeNode | null>(null)
      69              : 
      70          107 :   const { flags } = useFeatureFlags()
      71          107 :   const { shouldRenderVueNodes } = useVueFeatureFlags()
      72          107 :   const { workspaceRole } = useWorkspaceUI()
      73          107 :   const governanceStore = usePartnerNodeGovernanceStore()
      74          107 :   const skillPacksStore = useSkillPacksStore()
      75              : 
      76          107 :   const settingRoot = computed<SettingTreeNode>(() => {
      77          107 :     const root = buildTree(
      78              :       Object.values(settingStore.settingsById).filter(
      79              :         (setting: SettingParams) =>
      80              :           setting.type !== 'hidden' &&
      81              :           !(shouldRenderVueNodes.value && setting.hideInVueNodes)
      82              :       ),
      83              :       (setting: SettingParams) => setting.category || setting.id.split('.')
      84              :     )
      85              : 
      86          107 :     const floatingSettings = (root.children ?? []).filter((node) => node.leaf)
      87          107 :     if (floatingSettings.length) {
      88            0 :       root.children = (root.children ?? []).filter((node) => !node.leaf)
      89            0 :       root.children.push({
      90              :         key: 'Other',
      91              :         label: 'Other',
      92              :         leaf: false,
      93              :         children: floatingSettings
      94              :       })
      95              :     }
      96              : 
      97          107 :     return root
      98              :   })
      99              : 
     100          107 :   const settingCategories = computed<SettingTreeNode[]>(
     101              :     () => settingRoot.value.children ?? []
     102              :   )
     103              : 
     104              :   // Core setting categories (built-in to ComfyUI) in display order
     105              :   // 'Other' includes floating settings that don't have a specific category
     106          107 :   const CORE_CATEGORIES_ORDER = [
     107              :     'Comfy',
     108              :     'LiteGraph',
     109              :     'Appearance',
     110              :     '3D',
     111              :     'Mask Editor',
     112              :     'Other'
     113              :   ]
     114          107 :   const CORE_CATEGORIES = new Set(CORE_CATEGORIES_ORDER)
     115              : 
     116          107 :   const coreSettingCategories = computed<SettingTreeNode[]>(() => {
     117              :     const categories = settingCategories.value.filter((node) =>
     118              :       CORE_CATEGORIES.has(node.label)
     119              :     )
     120          107 :     return categories.sort(
     121              :       (a, b) =>
     122              :         CORE_CATEGORIES_ORDER.indexOf(a.label) -
     123              :         CORE_CATEGORIES_ORDER.indexOf(b.label)
     124              :     )
     125              :   })
     126              : 
     127          107 :   const customNodeSettingCategories = computed<SettingTreeNode[]>(() =>
     128              :     settingCategories.value.filter((node) => !CORE_CATEGORIES.has(node.label))
     129              :   )
     130              : 
     131              :   // Define panel items
     132          107 :   const aboutPanel: SettingPanelItem = {
     133              :     node: {
     134              :       key: 'about',
     135              :       label: 'About',
     136              :       children: []
     137              :     },
     138              :     component: defineAsyncComponent(
     139              :       () => import('@/components/dialog/content/setting/AboutPanel.vue')
     140              :     )
     141              :   }
     142              : 
     143          107 :   const creditsPanel: SettingPanelItem = {
     144              :     node: {
     145              :       key: 'credits',
     146              :       label: 'Credits',
     147              :       children: []
     148              :     },
     149              :     component: defineAsyncComponent(
     150              :       () => import('@/platform/billing/components/settings/CreditsPanel.vue')
     151              :     )
     152              :   }
     153              : 
     154          107 :   const userPanel: SettingPanelItem = {
     155              :     node: {
     156              :       key: 'user',
     157              :       label: 'User',
     158              :       children: []
     159              :     },
     160              :     component: defineAsyncComponent(
     161              :       () => import('@/components/dialog/content/setting/UserPanel.vue')
     162              :     )
     163              :   }
     164              : 
     165          107 :   const workspaceSettingsPanelComponent = defineAsyncComponent(
     166              :     () =>
     167              :       import('@/platform/account/components/dialogs/settings/WorkspaceSettingsPanelContent.vue')
     168              :   )
     169              : 
     170          107 :   const planCreditsPanel: SettingPanelItem = {
     171              :     node: {
     172              :       key: 'workspace',
     173              :       label: 'PlanCredits',
     174              :       children: []
     175              :     },
     176              :     component: workspaceSettingsPanelComponent,
     177              :     props: { section: 'planCredits' }
     178              :   }
     179              : 
     180          107 :   const BillingTabCurrent = defineAsyncComponent(
     181              :     () =>
     182              :       import('@/platform/account/components/dialogs/settings/BillingTab.vue')
     183              :   )
     184              :   // shortcut: the 1.60 tab, kept whole for the billing_new_ux_enabled rollback; delete with the flag.
     185          107 :   const BillingTabLegacy = defineAsyncComponent(
     186              :     () =>
     187              :       import('@/platform/account/components/dialogs/settings/BillingTabLegacy.vue')
     188              :   )
     189              : 
     190          107 :   const billingPanel: SettingPanelItem = {
     191              :     node: {
     192              :       key: 'workspace',
     193              :       label: 'Billing',
     194              :       children: []
     195              :     },
     196              :     component: defineComponent(
     197              :       () => () =>
     198              :         h(flags.billingNewUxEnabled ? BillingTabCurrent : BillingTabLegacy)
     199              :     )
     200              :   }
     201              : 
     202          107 :   const membersPanel: SettingPanelItem = {
     203              :     node: {
     204              :       key: 'workspace-members',
     205              :       label: 'Members',
     206              :       children: []
     207              :     },
     208              :     component: workspaceSettingsPanelComponent,
     209              :     props: { section: 'members' }
     210              :   }
     211              : 
     212          107 :   const allowlistPanel: SettingPanelItem = {
     213              :     node: {
     214              :       key: 'workspace-allowlist',
     215              :       label: 'Allowlist',
     216              :       children: []
     217              :     },
     218              :     component: workspaceSettingsPanelComponent,
     219              :     props: { section: 'allowlist' }
     220              :   }
     221              : 
     222          107 :   const shouldShowWorkspacePanel = computed(() => isLoggedIn.value)
     223          107 :   const shouldShowWorkspaceAllowlist = computed(
     224              :     () =>
     225              :       isCloud &&
     226              :       shouldShowWorkspacePanel.value &&
     227              :       flags.partnerNodeGovernanceEnabled &&
     228              :       workspaceRole.value === 'owner'
     229              :   )
     230              : 
     231          107 :   const visibleWorkspacePanels = computed<SettingPanelItem[]>(() => {
     232          107 :     if (!shouldShowWorkspacePanel.value) return []
     233          107 :     const planPanel = flags.unifiedBillingSettingsEnabled
     234              :       ? billingPanel
     235              :       : planCreditsPanel
     236              :     const canViewMembers = pendingServerFact(
     237              :       'BE-24066',
     238              :       isCloud ||
     239              :         (canManageMembers.value && !flags.unifiedBillingSettingsEnabled)
     240              :     )
     241          107 :     const workspacePanels = canViewMembers
     242              :       ? [planPanel, membersPanel]
     243              :       : [planPanel]
     244          107 :     return shouldShowWorkspaceAllowlist.value
     245              :       ? [...workspacePanels, allowlistPanel]
     246              :       : workspacePanels
     247              :   })
     248              : 
     249          107 :   const secretsPanel: SettingPanelItem = {
     250              :     node: {
     251              :       key: 'secrets',
     252              :       label: 'Secrets',
     253              :       children: []
     254              :     },
     255              :     component: defineAsyncComponent(
     256              :       () => import('@/platform/secrets/components/SecretsPanel.vue')
     257              :     )
     258              :   }
     259              : 
     260          107 :   const shouldShowSecretsPanel = computed(
     261              :     () => flags.userSecretsEnabled && isLoggedIn.value
     262              :   )
     263              : 
     264          107 :   const skillPacksPanel: SettingPanelItem = {
     265              :     node: {
     266              :       key: 'skill-packs',
     267              :       label: 'SkillPacks',
     268              :       children: []
     269              :     },
     270              :     component: defineAsyncComponent(
     271              :       () => import('@/platform/skills/components/SkillPacksPanel.vue')
     272              :     )
     273              :   }
     274              : 
     275              :   // Both cohort flags plus a runtime check that the routes have not 404'd.
     276          107 :   const shouldShowSkillPacksPanel = computed(
     277              :     () => skillPacksStore.enabled && isLoggedIn.value
     278              :   )
     279              : 
     280          107 :   const keybindingPanel: SettingPanelItem = {
     281              :     node: {
     282              :       key: 'keybinding',
     283              :       label: 'Keybinding',
     284              :       children: []
     285              :     },
     286              :     component: defineAsyncComponent(
     287              :       () => import('@/platform/keybindings/components/KeybindingPanel.vue')
     288              :     )
     289              :   }
     290              : 
     291          107 :   const extensionPanel: SettingPanelItem = {
     292              :     node: {
     293              :       key: 'extension',
     294              :       label: 'Extension',
     295              :       children: []
     296              :     },
     297              :     component: defineAsyncComponent(
     298              :       () => import('@/platform/extensions/components/ExtensionPanel.vue')
     299              :     )
     300              :   }
     301              : 
     302              :   const serverConfigPanel: SettingPanelItem = {
     303              :     node: {
     304              :       key: 'server-config',
     305              :       label: 'Server-Config',
     306              :       children: []
     307              :     },
     308          107 :     component: defineAsyncComponent(
     309              :       () => import('@/platform/settings/components/ServerConfigPanel.vue')
     310              :     )
     311              :   }
     312              : 
     313          107 :   const panels = computed<SettingPanelItem[]>(() => [
     314              :     aboutPanel,
     315              :     creditsPanel,
     316              :     userPanel,
     317              :     ...visibleWorkspacePanels.value,
     318              :     keybindingPanel,
     319              :     extensionPanel,
     320              :     ...(isDesktop ? [serverConfigPanel] : []),
     321              :     ...(shouldShowSecretsPanel.value ? [secretsPanel] : []),
     322              :     ...(shouldShowSkillPacksPanel.value ? [skillPacksPanel] : [])
     323              :   ])
     324              : 
     325              :   /**
     326              :    * The default category to show when the dialog is opened.
     327              :    */
     328          107 :   const defaultCategory = computed<SettingTreeNode>(() => {
     329          107 :     if (defaultPanel) {
     330            3 :       for (const group of groupedMenuTreeNodes.value) {
     331            6 :         const found = group.children?.find((node) => node.key === defaultPanel)
     332            6 :         if (found) return found
     333              :       }
     334            0 :       const hiddenPanel = panels.value.find(
     335              :         (panel) => panel.node.key === defaultPanel
     336              :       )
     337            0 :       if (hiddenPanel) return translateCategory(hiddenPanel.node)
     338            0 :       return settingCategories.value[0]
     339              :     }
     340              : 
     341          104 :     if (scrollToSettingId) {
     342            0 :       if (Object.hasOwn(settingStore.settingsById, scrollToSettingId)) {
     343            0 :         const setting = settingStore.settingsById[scrollToSettingId]
     344            0 :         const { category } = getSettingInfo(setting)
     345            0 :         const found = settingCategories.value.find((c) => c.label === category)
     346            0 :         if (found) return found
     347              :       }
     348              :     }
     349              : 
     350          104 :     return settingCategories.value[0]
     351              :   })
     352              : 
     353          107 :   const translateCategory = (node: SettingTreeNode) => ({
     354              :     ...node,
     355              :     translatedLabel: t(
     356              :       `settingsCategories.${normalizeI18nKey(node.label)}`,
     357              :       node.label
     358              :     )
     359              :   })
     360              : 
     361              :   const workspaceMenuTreeNodes = computed<SettingTreeNode[]>(() => [
     362              :     translateCategory({
     363              :       key: 'workspace',
     364              :       label: 'Workspace',
     365              :       children: visibleWorkspacePanels.value
     366              :         .map((panel) => panel.node)
     367              :         .map(translateCategory)
     368              :     }),
     369              :     translateCategory({
     370              :       key: 'general',
     371              :       label: 'General',
     372              :       children: [
     373              :         translateCategory(userPanel.node),
     374              :         ...coreSettingCategories.value.slice(0, 1).map(translateCategory),
     375              :         ...(shouldShowSecretsPanel.value
     376              :           ? [translateCategory(secretsPanel.node)]
     377              :           : []),
     378              :         ...(shouldShowSkillPacksPanel.value
     379              :           ? [translateCategory(skillPacksPanel.node)]
     380              :           : []),
     381              :         ...coreSettingCategories.value.slice(1).map(translateCategory),
     382              :         translateCategory(keybindingPanel.node),
     383              :         translateCategory(extensionPanel.node),
     384              :         translateCategory(aboutPanel.node),
     385              :         ...(isDesktop ? [translateCategory(serverConfigPanel.node)] : [])
     386              :       ]
     387              :     }),
     388              :     ...(customNodeSettingCategories.value.length > 0
     389              :       ? [
     390              :           translateCategory({
     391              :             key: 'other',
     392              :             label: 'Other',
     393              :             children: customNodeSettingCategories.value.map(translateCategory)
     394              :           })
     395              :         ]
     396              :       : [])
     397              :   ])
     398              : 
     399          107 :   const groupedMenuTreeNodes = workspaceMenuTreeNodes
     400              : 
     401          107 :   const navGroups = computed<NavGroupData[]>(() =>
     402              :     groupedMenuTreeNodes.value
     403              :       .filter((group) => group.children?.length)
     404              :       .map((group) => ({
     405              :         title:
     406              :           (group as SettingTreeNode & { translatedLabel?: string })
     407              :             .translatedLabel ?? group.label,
     408              :         items: (group.children ?? []).map((child) => ({
     409              :           id: child.key,
     410              :           label:
     411              :             (child as SettingTreeNode & { translatedLabel?: string })
     412              :               .translatedLabel ?? child.label,
     413              :           icon:
     414              :             child.key === 'workspace'
     415              :               ? CATEGORY_ICONS.PlanCredits
     416              :               : (CATEGORY_ICONS[child.key] ?? CATEGORY_ICONS[child.label]),
     417              :           ...(child.key === 'workspace-allowlist' &&
     418              :           governanceStore.status === 'ineligible' &&
     419              :           governanceStore.providers.length > 0
     420              :             ? { suffixIcon: 'icon-[lucide--crown]' }
     421              :             : {})
     422              :         }))
     423              :       }))
     424              :   )
     425              : 
     426           54 :   function findCategoryByKey(key: string): SettingTreeNode | null {
     427           54 :     for (const group of groupedMenuTreeNodes.value) {
     428          109 :       const found = group.children?.find((node) => node.key === key)
     429          109 :       if (found) return found
     430              :     }
     431            0 :     return null
     432              :   }
     433              : 
     434          182 :   function findPanelByKey(key: string): SettingPanelItem | null {
     435          182 :     return panels.value.find((p) => p.node.key === key) ?? null
     436              :   }
     437              : 
     438          107 :   onMounted(() => {
     439          107 :     activeCategory.value = defaultCategory.value
     440          107 :     void skillPacksStore.startFlagGate()
     441              :   })
     442              : 
     443              :   const unifiedBillingSettingsEnabled = computed(
     444              :     () => flags.unifiedBillingSettingsEnabled
     445              :   )
     446              : 
     447          107 :   return {
     448              :     panels,
     449              :     activeCategory,
     450              :     defaultCategory,
     451              :     groupedMenuTreeNodes,
     452              :     settingCategories,
     453              :     navGroups,
     454              :     unifiedBillingSettingsEnabled,
     455              :     findCategoryByKey,
     456              :     findPanelByKey
     457              :   }
     458              : }
        

Generated by: LCOV version 2.0-1