@@ -53,7 +53,6 @@ import { cn } from '@/lib/utils'
5353import { useNotificationStore } from '@/stores/notifications/store'
5454import { getWorkflowWithValues } from '@/stores/workflows'
5555import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
56- import { useWorkflowStore } from '@/stores/workflows/workflow/store'
5756import {
5857 CATEGORIES ,
5958 getCategoryColor ,
@@ -144,8 +143,24 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
144143 const [ marketplaceInfo , setMarketplaceInfo ] = useState < MarketplaceInfo | null > ( null )
145144 const [ isLoading , setIsLoading ] = useState ( false )
146145 const { addNotification } = useNotificationStore ( )
147- const { activeWorkflowId, workflows } = useWorkflowRegistry ( )
148- const { isPublished, setPublishStatus } = useWorkflowStore ( )
146+ const { activeWorkflowId, workflows, updateWorkflow } = useWorkflowRegistry ( )
147+
148+ // Get marketplace data from the registry
149+ const getMarketplaceData = ( ) => {
150+ if ( ! activeWorkflowId || ! workflows [ activeWorkflowId ] ) return null
151+ return workflows [ activeWorkflowId ] . marketplaceData
152+ }
153+
154+ // Check if workflow is published to marketplace
155+ const isPublished = ( ) => {
156+ return ! ! getMarketplaceData ( )
157+ }
158+
159+ // Check if the current user is the owner of the published workflow
160+ const isOwner = ( ) => {
161+ const marketplaceData = getMarketplaceData ( )
162+ return marketplaceData ?. status === 'owner'
163+ }
149164
150165 // Initialize form with react-hook-form
151166 const form = useForm < MarketplaceFormValues > ( {
@@ -161,7 +176,7 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
161176 // Fetch marketplace information when the modal opens and the workflow is published
162177 useEffect ( ( ) => {
163178 async function fetchMarketplaceInfo ( ) {
164- if ( ! open || ! activeWorkflowId || ! isPublished ) {
179+ if ( ! open || ! activeWorkflowId || ! isPublished ( ) ) {
165180 setMarketplaceInfo ( null )
166181 return
167182 }
@@ -185,16 +200,16 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
185200 }
186201
187202 fetchMarketplaceInfo ( )
188- } , [ open , activeWorkflowId , isPublished , addNotification ] )
203+ } , [ open , activeWorkflowId , addNotification ] )
189204
190205 // Update form values when the active workflow changes or modal opens
191206 useEffect ( ( ) => {
192- if ( open && activeWorkflowId && workflows [ activeWorkflowId ] && ! isPublished ) {
207+ if ( open && activeWorkflowId && workflows [ activeWorkflowId ] && ! isPublished ( ) ) {
193208 const workflow = workflows [ activeWorkflowId ]
194209 form . setValue ( 'name' , workflow . name )
195210 form . setValue ( 'description' , workflow . description || '' )
196211 }
197- } , [ open , activeWorkflowId , workflows , form , isPublished ] )
212+ } , [ open , activeWorkflowId , workflows , form ] )
198213
199214 // Listen for the custom event to open the marketplace modal
200215 useEffect ( ( ) => {
@@ -255,8 +270,10 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
255270 throw new Error ( errorData . error || 'Failed to publish workflow' )
256271 }
257272
258- // Update the publish status with the current date
259- setPublishStatus ( true )
273+ // Update the marketplace data in the workflow registry
274+ updateWorkflow ( activeWorkflowId , {
275+ marketplaceData : { id : activeWorkflowId , status : 'owner' } ,
276+ } )
260277
261278 // Add a marketplace notification with detailed information
262279 addNotification (
@@ -293,8 +310,10 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
293310 throw new Error ( errorData . error || 'Failed to unpublish workflow' )
294311 }
295312
296- // Update the publish status
297- setPublishStatus ( false )
313+ // Remove the marketplace data from the workflow registry
314+ updateWorkflow ( activeWorkflowId , {
315+ marketplaceData : null ,
316+ } )
298317
299318 // Add a notification
300319 addNotification (
@@ -402,26 +421,20 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
402421 </ div >
403422 </ div >
404423
405- { /* Action buttons */ }
406- < div className = "flex justify-end gap-2 pt-2" >
407- < Button
408- type = "button"
409- variant = "destructive"
410- onClick = { handleUnpublish }
411- disabled = { isUnpublishing }
412- className = "gap-2"
413- >
414- < Trash className = "h-4 w-4" />
415- { isUnpublishing ? (
416- < span className = "flex items-center gap-1.5" >
417- < span className = "h-2 w-2 animate-pulse rounded-full bg-background" > </ span >
418- Unpublishing...
419- </ span >
420- ) : (
421- 'Unpublish'
422- ) }
423- </ Button >
424- </ div >
424+ { /* Action buttons - Only show unpublish if owner */ }
425+ { isOwner ( ) && (
426+ < div className = "flex justify-end gap-2 pt-2" >
427+ < Button
428+ type = "button"
429+ variant = "destructive"
430+ onClick = { handleUnpublish }
431+ disabled = { isUnpublishing }
432+ className = "gap-2"
433+ >
434+ { isUnpublishing ? 'Unpublishing...' : 'Unpublish' }
435+ </ Button >
436+ </ div >
437+ ) }
425438 </ div >
426439 )
427440 }
@@ -551,7 +564,7 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
551564 < DialogHeader className = "px-6 py-4 border-b" >
552565 < div className = "flex items-center justify-between" >
553566 < DialogTitle className = "text-lg font-medium" >
554- { isPublished ? 'Marketplace Information' : 'Publish to Marketplace' }
567+ { isPublished ( ) ? 'Marketplace Information' : 'Publish to Marketplace' }
555568 </ DialogTitle >
556569 < Button
557570 variant = "ghost"
@@ -566,7 +579,7 @@ export function MarketplaceModal({ open, onOpenChange }: MarketplaceModalProps)
566579 </ DialogHeader >
567580
568581 < div className = "pt-4 px-6 pb-6 overflow-y-auto" >
569- { isPublished ? renderMarketplaceInfo ( ) : renderPublishForm ( ) }
582+ { isPublished ( ) ? renderMarketplaceInfo ( ) : renderPublishForm ( ) }
570583 </ div >
571584 </ DialogContent >
572585 </ Dialog >
0 commit comments