Line data Source code
1 : import { storeToRefs } from 'pinia'
2 : import { computed } from 'vue'
3 : import { useI18n } from 'vue-i18n'
4 :
5 : import { useToast } from '@/components/ui/toast/toastStore'
6 : import { useDeploymentCompatibility } from '@/platform/workspace/composables/useDeploymentCompatibility'
7 : import { useDeploymentLabels } from '@/platform/workspace/composables/useDeploymentLabels'
8 : import { useDeploymentSwitchGuard } from '@/platform/workspace/composables/useDeploymentSwitchGuard'
9 : import { useDeploymentSwitcherOffered } from '@/platform/workspace/composables/useDeploymentSwitcherOffered'
10 : import { confirmUnsavedWorkBeforeSwitch } from '@/platform/workspace/confirmUnsavedWorkBeforeSwitch'
11 : import { useDeploymentPickStore } from '@/platform/workspace/stores/deploymentPickStore'
12 :
13 : interface DeploymentChoice {
14 : deploymentId: string | null
15 : label: string
16 : }
17 :
18 : type MissingNodeDeploymentOffer =
19 : | { kind: 'none' }
20 : | { kind: 'choices'; choices: DeploymentChoice[] }
21 :
22 : /** The deployments to offer where the editor reports missing nodes. */
23 0 : export function useMissingNodeDeploymentOffer() {
24 0 : const { t } = useI18n()
25 0 : const pickStore = useDeploymentPickStore()
26 0 : const { deployments, pickedDeploymentId, isSwitching } =
27 : storeToRefs(pickStore)
28 0 : const { deploymentsThatRunIt, markFor } = useDeploymentCompatibility()
29 0 : const { deploymentFullLabel } = useDeploymentLabels()
30 0 : const switcherOffered = useDeploymentSwitcherOffered()
31 0 : const { targetLabel, withoutLeavePrompt } = useDeploymentSwitchGuard()
32 :
33 0 : function eachCheckedAndLacking() {
34 0 : return [null, ...deployments.value.map((d) => d.deployment_id)].every(
35 : (id) => markFor(id)?.kind === 'missing'
36 : )
37 : }
38 :
39 0 : const offer = computed<MissingNodeDeploymentOffer | null>(() => {
40 0 : const runIt = deploymentsThatRunIt.value
41 0 : if (!switcherOffered.value || runIt === null) return null
42 0 : if (runIt.length === 0) {
43 0 : return eachCheckedAndLacking() ? { kind: 'none' } : null
44 : }
45 0 : const choices = [
46 : { deploymentId: null, label: t('deploymentSwitcher.comfyCloud') },
47 : ...deployments.value
48 : .filter((deployment) => deployment.status === 'ready')
49 : .map((deployment) => ({
50 : deploymentId: deployment.deployment_id,
51 : label: deploymentFullLabel(deployment)
52 : }))
53 : ].filter(
54 : (choice) =>
55 : runIt.includes(choice.deploymentId) &&
56 : choice.deploymentId !== pickedDeploymentId.value
57 : )
58 0 : return choices.length > 0 ? { kind: 'choices', choices } : null
59 : })
60 :
61 0 : async function choose(deploymentId: string | null) {
62 0 : if (!(await confirmUnsavedWorkBeforeSwitch(targetLabel(deploymentId))))
63 0 : return
64 0 : const refusal = await withoutLeavePrompt(() => pickStore.pick(deploymentId))
65 0 : if (refusal === null) return
66 0 : useToast().error(t('deploymentSwitcher.failedToSwitch'), {
67 : description: refusal,
68 : duration: 8000
69 : })
70 : }
71 :
72 0 : return {
73 : offer,
74 : choose,
75 : isSwitching,
76 : openSwitcher: pickStore.requestSwitcherOpen
77 : }
78 : }
|