Line data Source code
1 16 : <template>
2 1 : <form
3 : class="flex min-h-0 flex-1 flex-col overflow-hidden bg-background"
4 : @submit.prevent
5 : >
6 : <header
7 : v-if="showCloseButton"
8 : class="flex h-16 items-center justify-between px-6"
9 : >
10 : <h2 class="text-base font-normal text-foreground">
11 : {{ $t('comfyHubProfile.createProfileTitle') }}
12 : </h2>
13 : <Button size="icon" :aria-label="$t('g.close')" @click="onClose">
14 : <i class="icon-[lucide--x]" />
15 : </Button>
16 : </header>
17 : <h2 v-else class="px-6 pt-6 text-base font-normal text-foreground">
18 : {{ $t('comfyHubProfile.createProfileTitle') }}
19 : </h2>
20 :
21 : <div class="flex min-h-0 flex-1 flex-col gap-6 overflow-y-auto px-6 py-4">
22 : <div class="flex flex-col gap-4">
23 : <label for="profile-picture" class="text-sm text-muted-foreground">
24 : {{ $t('comfyHubProfile.chooseProfilePicture') }}
25 : </label>
26 : <label
27 : class="flex size-13 cursor-pointer items-center justify-center overflow-hidden rounded-full bg-linear-to-b from-green-600/50 to-green-900"
28 : >
29 : <input
30 : id="profile-picture"
31 : type="file"
32 : accept="image/*"
33 : class="hidden"
34 : @change="handleProfileSelect"
35 : />
36 : <template v-if="profilePreviewUrl">
37 : <img
38 : :src="profilePreviewUrl"
39 : :alt="$t('comfyHubProfile.chooseProfilePicture')"
40 : class="size-full rounded-full object-cover"
41 : />
42 : </template>
43 : <template v-else>
44 : <span class="text-base text-white">
45 : {{ profileInitial }}
46 : </span>
47 : </template>
48 : </label>
49 : </div>
50 :
51 : <div class="flex flex-col gap-6">
52 : <div class="flex flex-col gap-4">
53 : <label for="profile-name" class="text-sm text-muted-foreground">
54 : {{ $t('comfyHubProfile.nameLabel') }}
55 : </label>
56 : <Input
57 : id="profile-name"
58 : v-model="name"
59 : :placeholder="$t('comfyHubProfile.namePlaceholder')"
60 : />
61 : </div>
62 :
63 : <div class="flex flex-col gap-2">
64 : <label for="profile-username" class="text-sm text-muted-foreground">
65 : {{ $t('comfyHubProfile.usernameLabel') }}
66 : </label>
67 : <div class="relative">
68 : <span
69 : :class="
70 : cn(
71 : 'pointer-events-none absolute top-1/2 left-4 -translate-y-1/2 text-sm',
72 : username ? 'text-foreground' : 'text-muted-foreground'
73 : )
74 : "
75 : >
76 : @
77 : </span>
78 : <Input
79 : id="profile-username"
80 : v-model="username"
81 : class="pl-7"
82 : :aria-invalid="showUsernameError ? 'true' : 'false'"
83 : :aria-describedby="
84 : showUsernameError ? 'profile-username-error' : undefined
85 : "
86 : />
87 : </div>
88 : <p
89 : v-if="showUsernameError"
90 : id="profile-username-error"
91 : class="text-xs text-destructive"
92 : >
93 : {{ $t('comfyHubProfile.usernameError') }}
94 : </p>
95 : </div>
96 :
97 : <div class="flex flex-col gap-2">
98 : <label
99 : for="profile-description"
100 : class="text-sm text-muted-foreground"
101 : >
102 : {{ $t('comfyHubProfile.descriptionLabel') }}
103 : </label>
104 : <Textarea
105 : id="profile-description"
106 : v-model="description"
107 : :placeholder="$t('comfyHubProfile.descriptionPlaceholder')"
108 : class="h-24 resize-none p-4 shadow-none"
109 : />
110 : </div>
111 : </div>
112 : </div>
113 :
114 : <footer class="flex items-center justify-end gap-4 border-t px-6 py-4">
115 : <Button size="lg" @click="onClose">
116 : {{ $t('g.cancel') }}
117 : </Button>
118 : <Button
119 : variant="primary"
120 : size="lg"
121 : :disabled="!isUsernameValid || isCreating"
122 : @click="handleCreate"
123 : >
124 : {{
125 : isCreating
126 : ? $t('comfyHubProfile.creatingProfile')
127 : : $t('comfyHubProfile.createProfile')
128 : }}
129 : </Button>
130 : </footer>
131 : </form>
132 : </template>
133 :
134 : <script setup lang="ts">
135 : import { computed, ref } from 'vue'
136 : import { useToast } from '@/components/ui/toast/toastStore'
137 : import { useI18n } from 'vue-i18n'
138 : import { useObjectUrl } from '@vueuse/core'
139 :
140 : import { cn } from '@comfyorg/tailwind-utils'
141 : import Button from '@comfyorg/design-system/button/Button'
142 : import Input from '@comfyorg/design-system/input/Input'
143 : import Textarea from '@comfyorg/design-system/textarea/Textarea'
144 : import {
145 : isFileTooLarge,
146 : MAX_IMAGE_SIZE_MB
147 : } from '@/platform/workflow/sharing/utils/validateFileSize'
148 : import { useComfyHubProfileGate } from '@/platform/workflow/sharing/composables/useComfyHubProfileGate'
149 : import type { ComfyHubProfile } from '@/platform/workflow/sharing/schemas/shareSchemas'
150 : import { getErrorMessage } from '@comfyorg/shared-frontend-utils/errorUtil'
151 :
152 : const {
153 : onProfileCreated,
154 : onClose,
155 : showCloseButton = true
156 : } = defineProps<{
157 : onProfileCreated: (profile: ComfyHubProfile) => void
158 : onClose: () => void
159 : showCloseButton?: boolean
160 : }>()
161 :
162 1 : const { createProfile } = useComfyHubProfileGate()
163 1 : const toast = useToast()
164 1 : const { t } = useI18n()
165 :
166 1 : const name = ref('')
167 1 : const username = ref('')
168 1 : const description = ref('')
169 1 : const profilePictureFile = ref<File | null>(null)
170 1 : const profilePreviewUrl = useObjectUrl(profilePictureFile)
171 1 : const isCreating = ref(false)
172 :
173 1 : const VALID_USERNAME_PATTERN = /^[a-z0-9][a-z0-9-]{1,40}[a-z0-9]$/
174 :
175 1 : const isUsernameValid = computed(() =>
176 : VALID_USERNAME_PATTERN.test(username.value)
177 : )
178 :
179 1 : const showUsernameError = computed(
180 : () => username.value.length > 0 && !isUsernameValid.value
181 : )
182 :
183 1 : const profileInitial = computed(() => {
184 1 : const source = name.value.trim() || username.value.trim()
185 1 : return source ? source[0].toUpperCase() : 'C'
186 : })
187 :
188 0 : function handleProfileSelect(event: Event) {
189 0 : if (!(event.target instanceof HTMLInputElement)) return
190 0 : const file = event.target.files?.[0]
191 0 : if (!file || isFileTooLarge(file, MAX_IMAGE_SIZE_MB)) return
192 0 : profilePictureFile.value = file
193 : }
194 :
195 0 : async function handleCreate() {
196 0 : if (isCreating.value) return
197 0 : isCreating.value = true
198 0 : try {
199 0 : const profile = await createProfile({
200 : username: username.value.trim(),
201 : name: name.value.trim() || undefined,
202 : description: description.value.trim() || undefined,
203 : profilePicture: profilePictureFile.value ?? undefined
204 : })
205 0 : onProfileCreated(profile)
206 : } catch (error) {
207 0 : toast.error(t('g.error'), {
208 : description: getErrorMessage(error) ?? t('g.error')
209 : })
210 : } finally {
211 0 : isCreating.value = false
212 : }
213 : }
214 : </script>
|