Line data Source code
1 : import { ref } from 'vue'
2 :
3 : import { useCurrentUser } from '@/composables/auth/useCurrentUser'
4 : import { useErrorHandling } from '@/composables/useErrorHandling'
5 : import { useComfyHubService } from '@/platform/workflow/sharing/services/comfyHubService'
6 : import { WORKSPACE_STORAGE_KEYS } from '@/platform/workspace/workspaceConstants'
7 : import type { ComfyHubProfile } from '@/platform/workflow/sharing/schemas/shareSchemas'
8 :
9 : // TODO: Migrate to a Pinia store for proper singleton state management
10 : // User-scoped, session-cached profile state (module-level singleton)
11 30 : const hasProfile = ref<boolean | null>(null)
12 30 : const isCheckingProfile = ref(false)
13 30 : const isFetchingProfile = ref(false)
14 30 : const profile = ref<ComfyHubProfile | null>(null)
15 30 : const cachedUserId = ref<string | null>(null)
16 30 : let inflightFetch: Promise<ComfyHubProfile | null> | null = null
17 :
18 0 : function getCurrentWorkspaceId(): string {
19 0 : const workspaceJson = sessionStorage.getItem(
20 : WORKSPACE_STORAGE_KEYS.CURRENT_WORKSPACE
21 : )
22 0 : if (!workspaceJson) {
23 0 : throw new Error('Unable to determine current workspace')
24 : }
25 :
26 0 : let workspace: unknown
27 0 : try {
28 0 : workspace = JSON.parse(workspaceJson)
29 : } catch {
30 0 : throw new Error('Unable to determine current workspace')
31 : }
32 :
33 0 : if (
34 : !workspace ||
35 : typeof workspace !== 'object' ||
36 : !('id' in workspace) ||
37 : typeof workspace.id !== 'string' ||
38 : workspace.id.length === 0
39 : ) {
40 0 : throw new Error('Unable to determine current workspace')
41 : }
42 :
43 0 : return workspace.id
44 : }
45 :
46 91 : export function useComfyHubProfileGate() {
47 91 : const { resolvedUserInfo } = useCurrentUser()
48 91 : const { toastErrorHandler } = useErrorHandling()
49 91 : const {
50 : getMyProfile,
51 : requestAssetUploadUrl,
52 : uploadFileToPresignedUrl,
53 : createProfile: createComfyHubProfile
54 : } = useComfyHubService()
55 :
56 30 : function syncCachedProfileWithCurrentUser(): void {
57 30 : const currentUserId = resolvedUserInfo.value?.id ?? null
58 30 : if (cachedUserId.value === currentUserId) {
59 30 : return
60 : }
61 :
62 0 : hasProfile.value = null
63 0 : profile.value = null
64 0 : cachedUserId.value = currentUserId
65 : }
66 :
67 30 : async function performFetch(): Promise<ComfyHubProfile | null> {
68 30 : isFetchingProfile.value = true
69 30 : try {
70 30 : const nextProfile = await getMyProfile()
71 29 : if (!nextProfile) {
72 2 : hasProfile.value = false
73 2 : profile.value = null
74 2 : return null
75 : }
76 27 : hasProfile.value = true
77 27 : profile.value = nextProfile
78 27 : return nextProfile
79 : } catch (error) {
80 0 : hasProfile.value = false
81 0 : toastErrorHandler(error)
82 0 : return null
83 : } finally {
84 29 : isFetchingProfile.value = false
85 29 : inflightFetch = null
86 : }
87 : }
88 :
89 30 : function fetchProfile(options?: {
90 : force?: boolean
91 : }): Promise<ComfyHubProfile | null> {
92 30 : syncCachedProfileWithCurrentUser()
93 :
94 30 : if (!options?.force && profile.value) {
95 0 : return Promise.resolve(profile.value)
96 : }
97 :
98 30 : if (!options?.force && inflightFetch) return inflightFetch
99 :
100 30 : inflightFetch = performFetch()
101 30 : return inflightFetch
102 : }
103 :
104 0 : async function checkProfile(): Promise<boolean> {
105 0 : syncCachedProfileWithCurrentUser()
106 :
107 0 : if (hasProfile.value !== null) return hasProfile.value
108 0 : isCheckingProfile.value = true
109 0 : try {
110 : const fetchedProfile = await fetchProfile()
111 0 : return fetchedProfile !== null
112 : } finally {
113 0 : isCheckingProfile.value = false
114 : }
115 : }
116 :
117 0 : async function createProfile(data: {
118 : username: string
119 : name?: string
120 : description?: string
121 : profilePicture?: File
122 : }): Promise<ComfyHubProfile> {
123 0 : syncCachedProfileWithCurrentUser()
124 :
125 0 : let avatarToken: string | undefined
126 0 : if (data.profilePicture) {
127 0 : const contentType = data.profilePicture.type || 'application/octet-stream'
128 0 : const upload = await requestAssetUploadUrl({
129 : filename: data.profilePicture.name,
130 : contentType
131 : })
132 :
133 0 : await uploadFileToPresignedUrl({
134 : uploadUrl: upload.uploadUrl,
135 : file: data.profilePicture,
136 : contentType
137 : })
138 :
139 0 : avatarToken = upload.token
140 : }
141 :
142 0 : const createdProfile = await createComfyHubProfile({
143 : workspaceId: getCurrentWorkspaceId(),
144 : username: data.username,
145 : displayName: data.name,
146 : description: data.description,
147 : avatarToken
148 : })
149 :
150 0 : hasProfile.value = true
151 0 : profile.value = createdProfile
152 0 : return createdProfile
153 : }
154 :
155 91 : return {
156 : hasProfile,
157 : profile,
158 : isCheckingProfile,
159 : isFetchingProfile,
160 : checkProfile,
161 : fetchProfile,
162 : createProfile
163 : }
164 : }
|