Line data Source code
1 : import type { WorkspaceDeployment } from '@/platform/workspace/api/workspaceApi'
2 : import { storeToRefs } from 'pinia'
3 : import { computed } from 'vue'
4 : import { useI18n } from 'vue-i18n'
5 :
6 : import { t as translate } from '@/i18n'
7 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
8 :
9 : type Translate = (
10 : key: string,
11 : named: Record<string, string | number>,
12 : options: { escapeParameter: boolean }
13 : ) => string
14 :
15 : /** The switcher's radio value for Comfy Cloud; deployment rows use their id. */
16 2355 : export const COMFY_CLOUD_ROW = 'comfy-cloud'
17 :
18 : /** How a deployment's status reads at a glance. */
19 : export type DeploymentStatusTone = 'ready' | 'pending' | 'failed' | 'stopped'
20 :
21 : /** What a switcher row shows for a deployment, or for Comfy Cloud. */
22 : export interface DeploymentRowLabels {
23 : value: string
24 : deploymentId: string | null
25 : title: string
26 : name: string | null
27 : detail: string | null
28 : tooltip: string
29 : tone?: DeploymentStatusTone
30 : }
31 :
32 2355 : const STATUS_LABEL_KEYS = new Map([
33 : ['stopped', 'deploymentSwitcher.status.stopped'],
34 : ['stopping', 'deploymentSwitcher.status.stopping'],
35 : ['failed', 'deploymentSwitcher.status.failed'],
36 : ['queued', 'deploymentSwitcher.status.starting'],
37 : ['starting', 'deploymentSwitcher.status.starting'],
38 : ['provisioning', 'deploymentSwitcher.status.provisioning']
39 : ])
40 :
41 : /** `dep-f24d36bb` out of `dep-f24d36bb-fd1f-...`: enough to tell two apart. */
42 0 : function shortId(deploymentId: string): string {
43 0 : return deploymentId.slice(0, 12)
44 : }
45 :
46 0 : function labelMessage(
47 : deployment: WorkspaceDeployment
48 : ): [key: string, values: Record<string, string | number>] {
49 0 : if (deployment.deployment_name) {
50 0 : return [
51 : 'deploymentSwitcher.namedDeployment',
52 : { name: deployment.deployment_name }
53 : ]
54 : }
55 0 : if (
56 : deployment.build_name !== undefined &&
57 : deployment.release_version !== undefined
58 : ) {
59 0 : return [
60 : 'deploymentSwitcher.deployment',
61 : { build: deployment.build_name, version: deployment.release_version }
62 : ]
63 : }
64 0 : return [
65 : 'deploymentSwitcher.unnamedDeployment',
66 : { id: shortId(deployment.deployment_id) }
67 : ]
68 : }
69 :
70 2355 : const STARTING_STATUSES = new Set(['queued', 'starting', 'provisioning'])
71 :
72 0 : export function deploymentStatusTone(status: string): DeploymentStatusTone {
73 0 : if (status === 'ready') return 'ready'
74 0 : if (status === 'failed') return 'failed'
75 0 : return STARTING_STATUSES.has(status) ? 'pending' : 'stopped'
76 : }
77 :
78 6027 : function deploymentNames(t: Translate) {
79 6027 : const text = (key: string, values: Record<string, string | number> = {}) =>
80 : t(key, values, { escapeParameter: false })
81 :
82 0 : function title(deployment: WorkspaceDeployment): string {
83 0 : if (deployment.build_name) return deployment.build_name
84 0 : const [key, values] = labelMessage(deployment)
85 0 : return text(key, values)
86 : }
87 :
88 0 : function name(deployment: WorkspaceDeployment): string | null {
89 0 : if (deployment.build_name) {
90 0 : return deployment.deployment_name || shortId(deployment.deployment_id)
91 : }
92 0 : return deployment.deployment_name ? shortId(deployment.deployment_id) : null
93 : }
94 :
95 0 : function fullLabel(deployment: WorkspaceDeployment | null): string {
96 0 : if (!deployment) return text('deploymentSwitcher.comfyCloud')
97 0 : const deploymentName = name(deployment)
98 0 : if (deploymentName === null) return title(deployment)
99 0 : return text('deploymentSwitcher.fullLabel', {
100 : build: title(deployment),
101 : deployment: deploymentName
102 : })
103 : }
104 :
105 0 : function statusText(status: string): string {
106 0 : return text(
107 : STATUS_LABEL_KEYS.get(status) ?? 'deploymentSwitcher.status.notReady'
108 : )
109 : }
110 :
111 0 : function detail(deployment: WorkspaceDeployment): string | null {
112 0 : if (deployment.status !== 'ready') return statusText(deployment.status)
113 0 : if (deployment.release_version === undefined) return null
114 0 : return text('deploymentSwitcher.version', {
115 : version: deployment.release_version
116 : })
117 : }
118 :
119 0 : function rowTooltip(deployment: WorkspaceDeployment): string {
120 0 : const label = fullLabel(deployment)
121 0 : const rowDetail = detail(deployment)
122 0 : if (rowDetail === null) return label
123 0 : return text('deploymentSwitcher.labelWithDetail', {
124 : label,
125 : detail: rowDetail
126 : })
127 : }
128 :
129 0 : function rowLabels(
130 : deployment: WorkspaceDeployment | null
131 : ): DeploymentRowLabels {
132 0 : if (!deployment) {
133 0 : const cloud = text('deploymentSwitcher.comfyCloud')
134 0 : return {
135 : value: COMFY_CLOUD_ROW,
136 : deploymentId: null,
137 : title: cloud,
138 : name: null,
139 : detail: null,
140 : tooltip: cloud
141 : }
142 : }
143 0 : return {
144 : value: deployment.deployment_id,
145 : deploymentId: deployment.deployment_id,
146 : title: title(deployment),
147 : name: name(deployment),
148 : detail: detail(deployment),
149 : tooltip: rowTooltip(deployment),
150 : tone: deploymentStatusTone(deployment.status)
151 : }
152 : }
153 :
154 6027 : return { title, fullLabel, rowLabels }
155 : }
156 :
157 : /**
158 : * The label of the deployment this page booted on, for text outside the
159 : * switcher that names where the editor runs (FE-2434); undefined on Comfy
160 : * Cloud and until the first listing answers. Works outside a component.
161 : */
162 89 : export function useBootDeploymentLabel() {
163 89 : const { bootDeployment } = storeToRefs(useDeploymentPickStore())
164 89 : return computed(() => {
165 0 : if (!bootDeployment.value) return undefined
166 0 : const [key, values] = labelMessage(bootDeployment.value)
167 0 : return translate(key, values, { escapeParameter: false })
168 : })
169 : }
170 :
171 : /** Where this page booted, or Comfy Cloud; undefined until known. */
172 0 : export function useBootDeploymentFullLabel() {
173 0 : const { bootDeployment, listingRefused } = storeToRefs(
174 : useDeploymentPickStore()
175 : )
176 0 : const { fullLabel } = deploymentNames(translate)
177 0 : return computed(() => {
178 0 : if (bootDeployment.value !== undefined) {
179 0 : return fullLabel(bootDeployment.value)
180 : }
181 0 : return listingRefused.value
182 : ? translate('deploymentSwitcher.comfyCloud')
183 : : undefined
184 : })
185 : }
186 :
187 6027 : export function useDeploymentLabels() {
188 6027 : const { t } = useI18n()
189 6027 : const { pickedDeployment, defaultDeployment, changeSinceBoot } = storeToRefs(
190 : useDeploymentPickStore()
191 : )
192 6027 : const names = deploymentNames(t)
193 :
194 6027 : const currentLabel = computed(() => names.fullLabel(pickedDeployment.value))
195 6027 : const defaultTitle = computed(() =>
196 : defaultDeployment.value ? names.title(defaultDeployment.value) : null
197 : )
198 6027 : const changeSinceBootMessage = computed(() => {
199 0 : if (changeSinceBoot.value === null) return null
200 0 : return changeSinceBoot.value === 'release'
201 : ? t('deploymentSwitcher.releaseChanged')
202 : : t(
203 : 'deploymentSwitcher.deploymentChanged',
204 : { deployment: currentLabel.value },
205 : { escapeParameter: false }
206 : )
207 : })
208 :
209 6027 : return {
210 : deploymentRowLabels: names.rowLabels,
211 : deploymentFullLabel: names.fullLabel,
212 : currentLabel,
213 : defaultTitle,
214 : changeSinceBootMessage
215 : }
216 : }
|