11import { RepeatIcon , SplitIcon } from 'lucide-react'
22import { create } from 'zustand'
3+ import { devtools } from 'zustand/middleware'
34import { getToolOperationsIndex } from '@/lib/search/tool-operations'
45import { getTriggersForSidebar } from '@/lib/workflows/triggers/trigger-utils'
56import { getAllBlocks } from '@/blocks'
@@ -20,132 +21,137 @@ const initialData: SearchData = {
2021 isInitialized : false ,
2122}
2223
23- export const useSearchModalStore = create < SearchModalState > ( ( set , get ) => ( {
24- isOpen : false ,
25- data : initialData ,
26-
27- setOpen : ( open : boolean ) => {
28- set ( { isOpen : open } )
29- } ,
30-
31- open : ( ) => {
32- set ( { isOpen : true } )
33- } ,
34-
35- close : ( ) => {
36- set ( { isOpen : false } )
37- } ,
38-
39- initializeData : ( filterBlocks ) => {
40- if ( get ( ) . data . isInitialized ) return
41-
42- const allBlocks = getAllBlocks ( )
43- const filteredAllBlocks = filterBlocks ( allBlocks ) as typeof allBlocks
44-
45- const regularBlocks : SearchBlockItem [ ] = [ ]
46- const tools : SearchBlockItem [ ] = [ ]
47- const docs : SearchDocItem [ ] = [ ]
48-
49- for ( const block of filteredAllBlocks ) {
50- if ( block . hideFromToolbar ) continue
51-
52- const searchItem : SearchBlockItem = {
53- id : block . type ,
54- name : block . name ,
55- description : block . description || '' ,
56- icon : block . icon ,
57- bgColor : block . bgColor || '#6B7280' ,
58- type : block . type ,
59- }
60-
61- if ( block . category === 'blocks' && block . type !== 'starter' ) {
62- regularBlocks . push ( searchItem )
63- } else if ( block . category === 'tools' ) {
64- tools . push ( searchItem )
65- }
66-
67- if ( block . docsLink ) {
68- docs . push ( {
69- id : `docs-${ block . type } ` ,
70- name : block . name ,
71- icon : block . icon ,
72- href : block . docsLink ,
73- } )
74- }
75- }
76-
77- const specialBlocks : SearchBlockItem [ ] = [
78- {
79- id : 'loop' ,
80- name : 'Loop' ,
81- description : 'Create a Loop' ,
82- icon : RepeatIcon ,
83- bgColor : '#2FB3FF' ,
84- type : 'loop' ,
24+ export const useSearchModalStore = create < SearchModalState > ( ) (
25+ devtools (
26+ ( set , get ) => ( {
27+ isOpen : false ,
28+ data : initialData ,
29+
30+ setOpen : ( open : boolean ) => {
31+ set ( { isOpen : open } )
8532 } ,
86- {
87- id : 'parallel' ,
88- name : 'Parallel' ,
89- description : 'Parallel Execution' ,
90- icon : SplitIcon ,
91- bgColor : '#FEE12B' ,
92- type : 'parallel' ,
33+
34+ open : ( ) => {
35+ set ( { isOpen : true } )
36+ } ,
37+
38+ close : ( ) => {
39+ set ( { isOpen : false } )
9340 } ,
94- ]
95-
96- const blocks = [ ...regularBlocks , ...( filterBlocks ( specialBlocks ) as SearchBlockItem [ ] ) ]
97-
98- const allTriggers = getTriggersForSidebar ( )
99- const filteredTriggers = filterBlocks ( allTriggers ) as typeof allTriggers
100- const priorityOrder = [ 'Start' , 'Schedule' , 'Webhook' ]
101-
102- const sortedTriggers = [ ...filteredTriggers ] . sort ( ( a , b ) => {
103- const aIndex = priorityOrder . indexOf ( a . name )
104- const bIndex = priorityOrder . indexOf ( b . name )
105- const aHasPriority = aIndex !== - 1
106- const bHasPriority = bIndex !== - 1
107-
108- if ( aHasPriority && bHasPriority ) return aIndex - bIndex
109- if ( aHasPriority ) return - 1
110- if ( bHasPriority ) return 1
111- return a . name . localeCompare ( b . name )
112- } )
113-
114- const triggers = sortedTriggers . map (
115- ( block ) : SearchBlockItem => ( {
116- id : block . type ,
117- name : block . name ,
118- description : block . description || '' ,
119- icon : block . icon ,
120- bgColor : block . bgColor || '#6B7280' ,
121- type : block . type ,
122- config : block ,
123- } )
124- )
125-
126- const allowedBlockTypes = new Set ( tools . map ( ( t ) => t . type ) )
127- const toolOperations : SearchToolOperationItem [ ] = getToolOperationsIndex ( )
128- . filter ( ( op ) => allowedBlockTypes . has ( op . blockType ) )
129- . map ( ( op ) => ( {
130- id : op . id ,
131- name : op . operationName ,
132- searchValue : `${ op . serviceName } ${ op . operationName } ` ,
133- icon : op . icon ,
134- bgColor : op . bgColor ,
135- blockType : op . blockType ,
136- operationId : op . operationId ,
137- keywords : op . aliases ,
138- } ) )
139-
140- set ( {
141- data : {
142- blocks,
143- tools,
144- triggers,
145- toolOperations,
146- docs,
147- isInitialized : true ,
41+
42+ initializeData : ( filterBlocks ) => {
43+ if ( get ( ) . data . isInitialized ) return
44+
45+ const allBlocks = getAllBlocks ( )
46+ const filteredAllBlocks = filterBlocks ( allBlocks ) as typeof allBlocks
47+
48+ const regularBlocks : SearchBlockItem [ ] = [ ]
49+ const tools : SearchBlockItem [ ] = [ ]
50+ const docs : SearchDocItem [ ] = [ ]
51+
52+ for ( const block of filteredAllBlocks ) {
53+ if ( block . hideFromToolbar ) continue
54+
55+ const searchItem : SearchBlockItem = {
56+ id : block . type ,
57+ name : block . name ,
58+ description : block . description || '' ,
59+ icon : block . icon ,
60+ bgColor : block . bgColor || '#6B7280' ,
61+ type : block . type ,
62+ }
63+
64+ if ( block . category === 'blocks' && block . type !== 'starter' ) {
65+ regularBlocks . push ( searchItem )
66+ } else if ( block . category === 'tools' ) {
67+ tools . push ( searchItem )
68+ }
69+
70+ if ( block . docsLink ) {
71+ docs . push ( {
72+ id : `docs-${ block . type } ` ,
73+ name : block . name ,
74+ icon : block . icon ,
75+ href : block . docsLink ,
76+ } )
77+ }
78+ }
79+
80+ const specialBlocks : SearchBlockItem [ ] = [
81+ {
82+ id : 'loop' ,
83+ name : 'Loop' ,
84+ description : 'Create a Loop' ,
85+ icon : RepeatIcon ,
86+ bgColor : '#2FB3FF' ,
87+ type : 'loop' ,
88+ } ,
89+ {
90+ id : 'parallel' ,
91+ name : 'Parallel' ,
92+ description : 'Parallel Execution' ,
93+ icon : SplitIcon ,
94+ bgColor : '#FEE12B' ,
95+ type : 'parallel' ,
96+ } ,
97+ ]
98+
99+ const blocks = [ ...regularBlocks , ...( filterBlocks ( specialBlocks ) as SearchBlockItem [ ] ) ]
100+
101+ const allTriggers = getTriggersForSidebar ( )
102+ const filteredTriggers = filterBlocks ( allTriggers ) as typeof allTriggers
103+ const priorityOrder = [ 'Start' , 'Schedule' , 'Webhook' ]
104+
105+ const sortedTriggers = [ ...filteredTriggers ] . sort ( ( a , b ) => {
106+ const aIndex = priorityOrder . indexOf ( a . name )
107+ const bIndex = priorityOrder . indexOf ( b . name )
108+ const aHasPriority = aIndex !== - 1
109+ const bHasPriority = bIndex !== - 1
110+
111+ if ( aHasPriority && bHasPriority ) return aIndex - bIndex
112+ if ( aHasPriority ) return - 1
113+ if ( bHasPriority ) return 1
114+ return a . name . localeCompare ( b . name )
115+ } )
116+
117+ const triggers = sortedTriggers . map (
118+ ( block ) : SearchBlockItem => ( {
119+ id : block . type ,
120+ name : block . name ,
121+ description : block . description || '' ,
122+ icon : block . icon ,
123+ bgColor : block . bgColor || '#6B7280' ,
124+ type : block . type ,
125+ config : block ,
126+ } )
127+ )
128+
129+ const allowedBlockTypes = new Set ( tools . map ( ( t ) => t . type ) )
130+ const toolOperations : SearchToolOperationItem [ ] = getToolOperationsIndex ( )
131+ . filter ( ( op ) => allowedBlockTypes . has ( op . blockType ) )
132+ . map ( ( op ) => ( {
133+ id : op . id ,
134+ name : op . operationName ,
135+ searchValue : `${ op . serviceName } ${ op . operationName } ` ,
136+ icon : op . icon ,
137+ bgColor : op . bgColor ,
138+ blockType : op . blockType ,
139+ operationId : op . operationId ,
140+ keywords : op . aliases ,
141+ } ) )
142+
143+ set ( {
144+ data : {
145+ blocks,
146+ tools,
147+ triggers,
148+ toolOperations,
149+ docs,
150+ isInitialized : true ,
151+ } ,
152+ } )
148153 } ,
149- } )
150- } ,
151- } ) )
154+ } ) ,
155+ { name : 'search-modal-store' }
156+ )
157+ )
0 commit comments