1+ import { createLogger } from '@sim/logger'
12import { WebflowIcon } from '@/components/icons'
3+ import { useSubBlockStore } from '@/stores/workflows/subblock/store'
24import type { TriggerConfig } from '../types'
35
6+ const logger = createLogger ( 'webflow-collection-item-changed-trigger' )
7+
48export const webflowCollectionItemChangedTrigger : TriggerConfig = {
59 id : 'webflow_collection_item_changed' ,
610 name : 'Collection Item Changed' ,
@@ -38,6 +42,58 @@ export const webflowCollectionItemChangedTrigger: TriggerConfig = {
3842 field : 'selectedTriggerId' ,
3943 value : 'webflow_collection_item_changed' ,
4044 } ,
45+ fetchOptions : async ( blockId : string ) => {
46+ const credentialId = useSubBlockStore . getState ( ) . getValue ( blockId , 'triggerCredentials' ) as
47+ | string
48+ | null
49+ if ( ! credentialId ) {
50+ throw new Error ( 'No Webflow credential selected' )
51+ }
52+ try {
53+ const response = await fetch ( '/api/tools/webflow/sites' , {
54+ method : 'POST' ,
55+ headers : { 'Content-Type' : 'application/json' } ,
56+ body : JSON . stringify ( { credential : credentialId } ) ,
57+ } )
58+ if ( ! response . ok ) {
59+ throw new Error ( 'Failed to fetch Webflow sites' )
60+ }
61+ const data = await response . json ( )
62+ if ( data . sites && Array . isArray ( data . sites ) ) {
63+ return data . sites . map ( ( site : { id : string ; name : string } ) => ( {
64+ id : site . id ,
65+ label : site . name ,
66+ } ) )
67+ }
68+ return [ ]
69+ } catch ( error ) {
70+ logger . error ( 'Error fetching Webflow sites:' , error )
71+ throw error
72+ }
73+ } ,
74+ fetchOptionById : async ( blockId : string , _subBlockId : string , optionId : string ) => {
75+ const credentialId = useSubBlockStore . getState ( ) . getValue ( blockId , 'triggerCredentials' ) as
76+ | string
77+ | null
78+ if ( ! credentialId ) return null
79+ try {
80+ const response = await fetch ( '/api/tools/webflow/sites' , {
81+ method : 'POST' ,
82+ headers : { 'Content-Type' : 'application/json' } ,
83+ body : JSON . stringify ( { credential : credentialId , siteId : optionId } ) ,
84+ } )
85+ if ( ! response . ok ) return null
86+ const data = await response . json ( )
87+ const site = data . sites ?. find ( ( s : { id : string } ) => s . id === optionId )
88+ if ( site ) {
89+ return { id : site . id , label : site . name }
90+ }
91+ return null
92+ } catch {
93+ return null
94+ }
95+ } ,
96+ dependsOn : [ 'triggerCredentials' ] ,
4197 } ,
4298 {
4399 id : 'collectionId' ,
@@ -52,6 +108,60 @@ export const webflowCollectionItemChangedTrigger: TriggerConfig = {
52108 field : 'selectedTriggerId' ,
53109 value : 'webflow_collection_item_changed' ,
54110 } ,
111+ fetchOptions : async ( blockId : string ) => {
112+ const credentialId = useSubBlockStore . getState ( ) . getValue ( blockId , 'triggerCredentials' ) as
113+ | string
114+ | null
115+ const siteId = useSubBlockStore . getState ( ) . getValue ( blockId , 'siteId' ) as string | null
116+ if ( ! credentialId || ! siteId ) {
117+ return [ ]
118+ }
119+ try {
120+ const response = await fetch ( '/api/tools/webflow/collections' , {
121+ method : 'POST' ,
122+ headers : { 'Content-Type' : 'application/json' } ,
123+ body : JSON . stringify ( { credential : credentialId , siteId } ) ,
124+ } )
125+ if ( ! response . ok ) {
126+ throw new Error ( 'Failed to fetch Webflow collections' )
127+ }
128+ const data = await response . json ( )
129+ if ( data . collections && Array . isArray ( data . collections ) ) {
130+ return data . collections . map ( ( collection : { id : string ; name : string } ) => ( {
131+ id : collection . id ,
132+ label : collection . name ,
133+ } ) )
134+ }
135+ return [ ]
136+ } catch ( error ) {
137+ logger . error ( 'Error fetching Webflow collections:' , error )
138+ throw error
139+ }
140+ } ,
141+ fetchOptionById : async ( blockId : string , _subBlockId : string , optionId : string ) => {
142+ const credentialId = useSubBlockStore . getState ( ) . getValue ( blockId , 'triggerCredentials' ) as
143+ | string
144+ | null
145+ const siteId = useSubBlockStore . getState ( ) . getValue ( blockId , 'siteId' ) as string | null
146+ if ( ! credentialId || ! siteId ) return null
147+ try {
148+ const response = await fetch ( '/api/tools/webflow/collections' , {
149+ method : 'POST' ,
150+ headers : { 'Content-Type' : 'application/json' } ,
151+ body : JSON . stringify ( { credential : credentialId , siteId } ) ,
152+ } )
153+ if ( ! response . ok ) return null
154+ const data = await response . json ( )
155+ const collection = data . collections ?. find ( ( c : { id : string } ) => c . id === optionId )
156+ if ( collection ) {
157+ return { id : collection . id , label : collection . name }
158+ }
159+ return null
160+ } catch {
161+ return null
162+ }
163+ } ,
164+ dependsOn : [ 'triggerCredentials' , 'siteId' ] ,
55165 } ,
56166 {
57167 id : 'triggerSave' ,
@@ -72,9 +182,9 @@ export const webflowCollectionItemChangedTrigger: TriggerConfig = {
72182 type : 'text' ,
73183 defaultValue : [
74184 'Connect your Webflow account using the "Select Webflow credential" button above.' ,
75- 'Enter your Webflow Site ID (found in the site URL or site settings) .' ,
76- 'Optionally enter a Collection ID to monitor only specific collections.' ,
77- 'If no Collection ID is provided , the trigger will fire for items changed in any collection on the site.' ,
185+ 'Select your Webflow site from the dropdown .' ,
186+ 'Optionally select a collection to monitor only specific collections.' ,
187+ 'If no collection is selected , the trigger will fire for items changed in any collection on the site.' ,
78188 'The webhook will trigger whenever an existing item is updated in the specified collection(s).' ,
79189 'Make sure your Webflow account has appropriate permissions for the specified site.' ,
80190 ]
0 commit comments