Line data Source code
1 : import type {
2 : WorkspaceDeployment,
3 : WorkspaceDeploymentList
4 : } from '@/platform/workspace/api/workspaceApi'
5 :
6 : /**
7 : * Which developer-platform deployment this browser runs its Cloud jobs on, and
8 : * the list it may pick from (FE-2434). A deployment keeps one URL while its
9 : * owner updates it to new Releases, so a pick follows those updates. The pick
10 : * itself is an HttpOnly cookie ingest sets; the editor learns it from the
11 : * listing and never reads it.
12 : */
13 : /** Where the current pick came from (BE-17480). */
14 : export type DeploymentPickSource = 'browser' | 'workspace_default'
15 :
16 : interface DeploymentListing {
17 : deployments: WorkspaceDeployment[]
18 : pickedDeploymentId: string | null
19 : /**
20 : * `browser` when this browser picked for itself (a deployment, or
21 : * Comfy Cloud); `workspace_default` when it follows the workspace's default
22 : * deployment; null when there is neither, so it runs on Comfy Cloud.
23 : */
24 : pickSource: DeploymentPickSource | null
25 : /** The workspace's default deployment an owner set, or null. */
26 : defaultDeploymentId: string | null
27 : /**
28 : * This browser's own pick, when ingest found it gone (deleted, or no
29 : * longer the workspace's) and cleared it. Only the listing that cleared
30 : * it says so; the browser is on Comfy Cloud.
31 : */
32 : gonePickedDeploymentId: string | null
33 : /**
34 : * The workspace's default deployment, when it is gone; then
35 : * `defaultDeploymentId` is null. Said on every listing until an owner
36 : * sets or clears the default.
37 : */
38 : goneDefaultDeploymentId: string | null
39 : buildsVisible: boolean
40 : }
41 :
42 : /**
43 : * A reload of the listing does not change the state until it answers: the
44 : * last listing stays on screen (or the switcher stays hidden) meanwhile, so
45 : * reopening the popover never flashes "Comfy Cloud" or no default. A listing
46 : * that lands while a switch is in flight updates what is shown but does not
47 : * end the switch, so a second pick cannot go out before the first answers.
48 : */
49 : export type DeploymentPickState =
50 : /** Nothing has answered yet; the switcher is not shown. */
51 : | { phase: 'idle' }
52 : /**
53 : * The account is outside the rollout, ingest has no deployment routes,
54 : * the session cannot pick, or the first listing failed.
55 : */
56 : | { phase: 'hidden' }
57 : | ({ phase: 'ready' } & DeploymentListing)
58 : /**
59 : * A pick, a clear or a new default is in flight. A pick or a clear reloads
60 : * the page on success; a new default reloads it only when this browser has
61 : * no pick of its own, since only then does it change what this browser
62 : * runs on.
63 : */
64 : | ({ phase: 'switching'; target: string | null } & DeploymentListing)
65 : | {
66 : phase: 'switching'
67 : target: string | null
68 : previousPhase: 'idle' | 'hidden'
69 : }
70 :
71 : export type DeploymentPickEvent =
72 : | ({ type: 'loaded' } & DeploymentListing)
73 : /**
74 : * A 403 (the account is outside the rollout) or a 404 (ingest without
75 : * these routes): hidden, whatever was shown before.
76 : */
77 : | { type: 'loadRefused' }
78 : /** Any other failure: keep a listing already shown, else stay hidden. */
79 : | { type: 'loadFailed' }
80 : | { type: 'switchStarted'; target: string | null }
81 : /** The server refused the switch. */
82 : | { type: 'switchFailed' }
83 : /**
84 : * The server took a new workspace default (or its clearing) and this
85 : * browser does not reload: show it now, whatever the refresh after it
86 : * answers, and end the switch.
87 : */
88 : | { type: 'defaultChanged'; defaultDeploymentId: string | null }
89 : /** This browser dropped its own pick of the default and now follows it. */
90 : | { type: 'followedInPlace' }
91 : /** Another workspace or account: nothing shown belongs to it. */
92 : | { type: 'workspaceChanged' }
93 :
94 : /** The `loaded` event for a listing from GET /api/workspaces/{id}/deployments. */
95 0 : export function loadedEvent(
96 : listing: WorkspaceDeploymentList
97 : ): DeploymentPickEvent {
98 0 : return {
99 : type: 'loaded',
100 : deployments: listing.items,
101 : pickedDeploymentId: listing.picked_deployment_id ?? null,
102 : pickSource: listing.pick_source ?? null,
103 : defaultDeploymentId: listing.default_deployment_id ?? null,
104 : gonePickedDeploymentId: listing.gone_picked_deployment_id ?? null,
105 : goneDefaultDeploymentId: listing.gone_default_deployment_id ?? null,
106 : buildsVisible: listing.builds_visible
107 : }
108 : }
109 :
110 : /** The listing fields of a state or a `loaded` event, without the rest. */
111 0 : function listingOf(source: DeploymentListing): DeploymentListing {
112 0 : return {
113 : deployments: source.deployments,
114 : pickedDeploymentId: source.pickedDeploymentId,
115 : pickSource: source.pickSource,
116 : defaultDeploymentId: source.defaultDeploymentId,
117 : gonePickedDeploymentId: source.gonePickedDeploymentId,
118 : goneDefaultDeploymentId: source.goneDefaultDeploymentId,
119 : buildsVisible: source.buildsVisible
120 : }
121 : }
122 :
123 9 : export function reduceDeploymentPick(
124 : state: DeploymentPickState,
125 : event: DeploymentPickEvent
126 : ): DeploymentPickState {
127 9 : switch (event.type) {
128 : case 'loaded':
129 0 : return withListing(state, listingOf(event))
130 : case 'loadRefused':
131 0 : return state.phase === 'switching'
132 : ? { phase: 'switching', target: state.target, previousPhase: 'hidden' }
133 : : { phase: 'hidden' }
134 : case 'loadFailed':
135 0 : return state.phase === 'switching' || 'deployments' in state
136 : ? state
137 : : { phase: 'hidden' }
138 : case 'switchStarted':
139 0 : return startSwitch(state, event.target)
140 : case 'switchFailed':
141 0 : return endSwitch(state, (listing) => listing)
142 : case 'defaultChanged':
143 0 : return endSwitch(state, (listing) => ({
144 : ...listing,
145 : defaultDeploymentId: event.defaultDeploymentId,
146 : goneDefaultDeploymentId: null
147 : }))
148 : case 'followedInPlace':
149 0 : return endSwitch(state, (listing) => ({
150 : ...listing,
151 : pickSource: 'workspace_default'
152 : }))
153 : case 'workspaceChanged':
154 9 : return { phase: 'idle' }
155 : }
156 : }
157 :
158 0 : function withListing(
159 : state: DeploymentPickState,
160 : listing: DeploymentListing
161 : ): DeploymentPickState {
162 0 : if (state.phase !== 'switching') return { phase: 'ready', ...listing }
163 0 : return { phase: 'switching', target: state.target, ...listing }
164 : }
165 :
166 0 : function startSwitch(
167 : state: DeploymentPickState,
168 : target: string | null
169 : ): DeploymentPickState {
170 0 : if (state.phase === 'switching') return state
171 0 : if (state.phase === 'ready') {
172 0 : return { phase: 'switching', target, ...listingOf(state) }
173 : }
174 0 : return { phase: 'switching', target, previousPhase: state.phase }
175 : }
176 :
177 0 : function endSwitch(
178 : state: DeploymentPickState,
179 : shown: (listing: DeploymentListing) => DeploymentListing
180 : ): DeploymentPickState {
181 0 : if (state.phase !== 'switching') return state
182 0 : if (!('deployments' in state)) return { phase: state.previousPhase }
183 0 : return { phase: 'ready', ...shown(listingOf(state)) }
184 : }
185 :
186 : /**
187 : * What changed between the deployment a page booted on and the one it runs
188 : * on now (null for Comfy Cloud in both): `release` when the same deployment
189 : * runs another Release, `deployment` when it runs on another deployment or
190 : * Comfy Cloud with other nodes, null when its nodes are the same.
191 : */
192 0 : export function changeBetween(
193 : boot: WorkspaceDeployment | null,
194 : now: WorkspaceDeployment | null
195 : ): 'release' | 'deployment' | null {
196 0 : if (releaseOf(boot) === releaseOf(now)) return null
197 0 : return deploymentOf(boot) === deploymentOf(now) ? 'release' : 'deployment'
198 : }
199 :
200 0 : function releaseOf(deployment: WorkspaceDeployment | null): string | null {
201 0 : return deployment?.release_id ?? null
202 : }
203 :
204 0 : function deploymentOf(deployment: WorkspaceDeployment | null): string | null {
205 0 : return deployment?.deployment_id ?? null
206 : }
|