Line data Source code
1 16 : <template>
2 : <BaseModalLayout
3 : :labels="modalLayoutLabels"
4 : :content-title="$t('comfyHubPublish.title')"
5 : content-padding="none"
6 : left-panel-width="16.5rem"
7 : size="md"
8 : @close="onClose"
9 : >
10 : <template #leftPanelHeaderTitle>
11 : <h2 class="flex-1 text-base font-semibold select-none">
12 : {{ $t('comfyHubPublish.title') }}
13 : </h2>
14 : </template>
15 :
16 : <template #leftPanel>
17 : <ComfyHubPublishNav
18 : v-if="!needsSave"
19 : :current-step
20 : @step-click="goToStep"
21 : />
22 : </template>
23 :
24 : <template #header />
25 : <template #content>
26 : <div
27 : v-if="needsSave"
28 : data-testid="publish-save-prompt"
29 : class="flex flex-col gap-4 p-6"
30 : >
31 : <p class="m-0 text-sm text-muted-foreground">
32 : {{ $t('comfyHubPublish.unsavedDescription') }}
33 : </p>
34 : <label v-if="isTemporary" class="flex flex-col gap-1">
35 : <span class="text-sm font-medium text-muted-foreground">
36 : {{ $t('shareWorkflow.workflowNameLabel') }}
37 : </span>
38 : <Input
39 : ref="nameInputRef"
40 : v-model="workflowName"
41 : :disabled="isSaving"
42 : @keydown.enter="() => handleSave()"
43 : />
44 : </label>
45 : <Button
46 : variant="primary"
47 : size="lg"
48 : :loading="isSaving"
49 : @click="() => handleSave()"
50 : >
51 : {{
52 : isSaving
53 : ? $t('shareWorkflow.saving')
54 : : $t('shareWorkflow.saveButton')
55 : }}
56 : </Button>
57 : </div>
58 : <ComfyHubPublishWizardContent
59 : v-else
60 : :current-step
61 : :form-data
62 : :is-first-step
63 : :is-last-step
64 : :is-publishing
65 : :is-update="isAlreadyPublished"
66 : :on-update-form-data="updateFormData"
67 : :on-go-next="goNext"
68 : :on-go-back="goBack"
69 : :on-require-profile="handleRequireProfile"
70 : :on-gate-complete="handlePublishGateComplete"
71 : :on-gate-close="handlePublishGateClose"
72 : :on-publish="handlePublish"
73 : />
74 : </template>
75 : </BaseModalLayout>
76 : </template>
77 :
78 : <script setup lang="ts">
79 : import { useAsyncState } from '@vueuse/core'
80 : import { useToast } from '@/components/ui/toast/toastStore'
81 : import { computed, nextTick, onBeforeUnmount, onMounted, ref, watch } from 'vue'
82 : import { useI18n } from 'vue-i18n'
83 :
84 : import BaseModalLayout from '@comfyorg/design-system/layout/BaseModalLayout'
85 : import Button from '@comfyorg/design-system/button/Button'
86 : import Input from '@comfyorg/design-system/input/Input'
87 : import ComfyHubPublishNav from '@/platform/workflow/sharing/components/publish/ComfyHubPublishNav.vue'
88 : import ComfyHubPublishWizardContent from '@/platform/workflow/sharing/components/publish/ComfyHubPublishWizardContent.vue'
89 : import { useComfyHubPublishSubmission } from '@/platform/workflow/sharing/composables/useComfyHubPublishSubmission'
90 : import {
91 : cachePublishPrefill,
92 : getCachedPrefill,
93 : useComfyHubPublishWizard
94 : } from '@/platform/workflow/sharing/composables/useComfyHubPublishWizard'
95 : import { useComfyHubProfileGate } from '@/platform/workflow/sharing/composables/useComfyHubProfileGate'
96 : import { useWorkflowShareService } from '@/platform/workflow/sharing/services/workflowShareService'
97 : import { useWorkflowService } from '@/platform/workflow/core/services/workflowService'
98 : import { useWorkflowStore } from '@/platform/workflow/management/stores/workflowStore'
99 : import type { ComfyHubPublishFormData } from '@/platform/workflow/sharing/types/comfyHubTypes'
100 : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
101 : import { appendJsonExt } from '@/utils/formatUtil'
102 : import { useModalLayoutLabels } from '@/components/dialog/useModalLayoutLabels'
103 :
104 : const { onClose } = defineProps<{
105 : onClose: () => void
106 : }>()
107 :
108 30 : const { t } = useI18n()
109 30 : const toast = useToast()
110 30 : const { fetchProfile } = useComfyHubProfileGate()
111 30 : const { submitToComfyHub } = useComfyHubPublishSubmission()
112 30 : const shareService = useWorkflowShareService()
113 30 : const workflowService = useWorkflowService()
114 30 : const workflowStore = useWorkflowStore()
115 30 : const {
116 : currentStep,
117 : formData,
118 : isFirstStep,
119 : isLastStep,
120 : goToStep,
121 : goNext,
122 : goBack,
123 : openProfileCreationStep,
124 : closeProfileCreationStep,
125 : applyPrefill
126 : } = useComfyHubPublishWizard()
127 30 : const isPublishing = ref(false)
128 30 : const isAlreadyPublished = ref(false)
129 30 : const needsSave = ref(false)
130 30 : const workflowName = ref('')
131 30 : const nameInputRef = ref<InstanceType<typeof Input> | null>(null)
132 :
133 30 : const isTemporary = computed(
134 : () => workflowStore.activeWorkflow?.isTemporary ?? false
135 : )
136 :
137 30 : function checkNeedsSave() {
138 30 : const workflow = workflowStore.activeWorkflow
139 30 : needsSave.value = !workflow || workflow.isTemporary || workflow.isModified
140 30 : if (workflow) {
141 30 : workflowName.value = workflow.filename.replace(/\.json$/i, '')
142 : }
143 : }
144 :
145 30 : watch(needsSave, async (needs) => {
146 1 : if (needs && isTemporary.value) {
147 1 : await nextTick()
148 1 : nameInputRef.value?.focus()
149 1 : nameInputRef.value?.select()
150 : }
151 : })
152 :
153 0 : function buildWorkflowPath(directory: string, filename: string): string {
154 0 : const normalizedDirectory = directory.replace(/\/+$/, '')
155 0 : const normalizedFilename = appendJsonExt(filename.replace(/\.json$/i, ''))
156 0 : return normalizedDirectory
157 : ? `${normalizedDirectory}/${normalizedFilename}`
158 : : normalizedFilename
159 : }
160 :
161 30 : const { isLoading: isSaving, execute: handleSave } = useAsyncState(
162 : async () => {
163 0 : const workflow = workflowStore.activeWorkflow
164 0 : if (!workflow) return
165 :
166 0 : if (workflow.isTemporary) {
167 0 : const name = workflowName.value.trim()
168 0 : if (!name) return
169 0 : const newPath = buildWorkflowPath(workflow.directory, name)
170 0 : await workflowService.renameWorkflow(workflow, newPath)
171 0 : await workflowStore.saveWorkflow(workflow)
172 : } else {
173 0 : await workflowService.saveWorkflow(workflow)
174 : }
175 :
176 0 : checkNeedsSave()
177 : },
178 : undefined,
179 : {
180 : immediate: false,
181 : onError: (error) => {
182 0 : console.error('Failed to save workflow:', error)
183 0 : toast.error(t('shareWorkflow.saveFailedTitle'), {
184 : description: t('shareWorkflow.saveFailedDescription')
185 : })
186 : }
187 : }
188 : )
189 :
190 0 : function handlePublishGateComplete() {
191 0 : closeProfileCreationStep()
192 0 : void fetchProfile({ force: true })
193 : }
194 :
195 0 : function handlePublishGateClose() {
196 0 : closeProfileCreationStep()
197 : }
198 :
199 1 : function handleRequireProfile() {
200 1 : openProfileCreationStep()
201 : }
202 :
203 3 : async function handlePublish(): Promise<void> {
204 3 : if (isPublishing.value) {
205 0 : return
206 : }
207 :
208 3 : isPublishing.value = true
209 3 : try {
210 3 : await submitToComfyHub(formData.value)
211 3 : const path = workflowStore.activeWorkflow?.path
212 3 : if (path) {
213 2 : cachePublishPrefill(path, formData.value)
214 : }
215 2 : toast.success(t('comfyHubPublish.publishSuccessTitle'), {
216 : description: t('comfyHubPublish.publishSuccessDescription'),
217 : duration: 5000
218 : })
219 2 : onClose()
220 : } catch (error) {
221 1 : console.error('Failed to publish workflow:', error)
222 1 : const reason = getErrorMessage(error)
223 1 : toast.error(t('comfyHubPublish.publishFailedTitle'), {
224 : description: reason
225 : ? t('comfyHubPublish.publishFailedDescriptionWithReason', { reason })
226 : : t('comfyHubPublish.publishFailedDescription')
227 : })
228 : } finally {
229 3 : isPublishing.value = false
230 : }
231 : }
232 :
233 6 : function updateFormData(patch: Partial<ComfyHubPublishFormData>) {
234 6 : formData.value = { ...formData.value, ...patch }
235 : }
236 :
237 30 : async function fetchPublishPrefill() {
238 30 : const path = workflowStore.activeWorkflow?.path
239 30 : if (!path) {
240 0 : isAlreadyPublished.value = false
241 0 : return
242 : }
243 :
244 30 : try {
245 30 : const status = await shareService.getPublishStatus(path)
246 30 : if (workflowStore.activeWorkflow?.path !== path) return
247 30 : isAlreadyPublished.value = status.isPublished
248 30 : const prefill = status.isPublished
249 : ? (status.prefill ?? getCachedPrefill(path))
250 : : getCachedPrefill(path)
251 30 : if (prefill) {
252 0 : applyPrefill(prefill)
253 : }
254 : } catch (error) {
255 0 : if (workflowStore.activeWorkflow?.path !== path) return
256 0 : isAlreadyPublished.value = false
257 0 : console.warn('Failed to fetch publish prefill:', error)
258 0 : const cached = getCachedPrefill(path)
259 0 : if (cached) {
260 0 : applyPrefill(cached)
261 : }
262 : }
263 : }
264 :
265 30 : onMounted(() => {
266 30 : checkNeedsSave()
267 30 : void fetchProfile()
268 30 : void fetchPublishPrefill()
269 : })
270 :
271 30 : watch(
272 : () => workflowStore.activeWorkflow?.path,
273 : (newPath, oldPath) => {
274 0 : if (isPublishing.value) return
275 0 : if (!newPath || newPath === oldPath) return
276 0 : void fetchPublishPrefill()
277 : }
278 : )
279 :
280 30 : onBeforeUnmount(() => {
281 3 : for (const image of formData.value.exampleImages) {
282 0 : if (image.file) {
283 0 : URL.revokeObjectURL(image.url)
284 : }
285 : }
286 : })
287 :
288 30 : const modalLayoutLabels = useModalLayoutLabels()
289 : </script>
|