Skip to content

Commit 71e80f4

Browse files
committed
code restructuring
1 parent c63b75f commit 71e80f4

11 files changed

Lines changed: 315 additions & 144 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/response/response-format.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ResponseFormat as SharedResponseFormat } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/components/starter/input-format'
2+
import type { ActiveSearchTarget } from '@/stores/panel/editor/store'
23

34
interface ResponseFormatProps {
45
blockId: string
@@ -7,6 +8,7 @@ interface ResponseFormatProps {
78
previewValue?: any
89
disabled?: boolean
910
config?: any
11+
activeSearchTarget?: ActiveSearchTarget | null
1012
}
1113

1214
export function ResponseFormat({
@@ -16,6 +18,7 @@ export function ResponseFormat({
1618
previewValue,
1719
disabled = false,
1820
config,
21+
activeSearchTarget,
1922
}: ResponseFormatProps) {
2023
return (
2124
<SharedResponseFormat
@@ -25,6 +28,7 @@ export function ResponseFormat({
2528
previewValue={previewValue}
2629
disabled={disabled}
2730
config={config}
31+
activeSearchTarget={activeSearchTarget}
2832
/>
2933
)
3034
}

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/editor/components/sub-block/hooks/use-depends-on-gate.ts

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { useStoreWithEqualityFn } from 'zustand/traditional'
66
import {
77
buildCanonicalIndex,
88
isNonEmptyValue,
9+
normalizeDependencyValue,
10+
parseDependsOn,
911
resolveDependencyValue,
1012
} from '@/lib/workflows/subblocks/visibility'
1113
import { getBlock } from '@/blocks/registry'
@@ -14,35 +16,6 @@ import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1416
import { useSubBlockStore } from '@/stores/workflows/subblock/store'
1517
import { useWorkflowStore } from '@/stores/workflows/workflow/store'
1618

17-
type DependsOnConfig = string[] | { all?: string[]; any?: string[] }
18-
19-
/**
20-
* Parses dependsOn config and returns normalized all/any arrays
21-
*/
22-
function parseDependsOn(dependsOn: DependsOnConfig | undefined): {
23-
allFields: string[]
24-
anyFields: string[]
25-
allDependsOnFields: string[]
26-
} {
27-
if (!dependsOn) {
28-
return { allFields: [], anyFields: [], allDependsOnFields: [] }
29-
}
30-
31-
if (Array.isArray(dependsOn)) {
32-
// Simple array format: all fields required (AND logic)
33-
return { allFields: dependsOn, anyFields: [], allDependsOnFields: dependsOn }
34-
}
35-
36-
// Object format with all/any
37-
const allFields = dependsOn.all || []
38-
const anyFields = dependsOn.any || []
39-
return {
40-
allFields,
41-
anyFields,
42-
allDependsOnFields: [...allFields, ...anyFields],
43-
}
44-
}
45-
4619
/**
4720
* Centralized dependsOn gating for sub-block components.
4821
* - Computes dependency values from the active workflow/block
@@ -76,29 +49,6 @@ export function useDependsOnGate(
7649
// For backward compatibility, expose flat list of all dependency fields
7750
const dependsOn = allDependsOnFields
7851

79-
const normalizeDependencyValue = (rawValue: unknown): unknown => {
80-
if (rawValue === null || rawValue === undefined) return null
81-
82-
if (typeof rawValue === 'object') {
83-
if (Array.isArray(rawValue)) {
84-
if (rawValue.length === 0) return null
85-
return rawValue.map((item) => normalizeDependencyValue(item))
86-
}
87-
88-
const record = rawValue as Record<string, any>
89-
if ('value' in record) {
90-
return normalizeDependencyValue(record.value)
91-
}
92-
if ('id' in record) {
93-
return record.id
94-
}
95-
96-
return record
97-
}
98-
99-
return rawValue
100-
}
101-
10252
const dependencySelector = useCallback(
10353
(state: ReturnType<typeof useSubBlockStore.getState>) => {
10454
if (allDependsOnFields.length === 0) return {} as Record<string, unknown>

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/search-replace/hooks/use-workflow-search-reference-hydration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
useWorkflowSearchOAuthCredentialDetails,
1111
useWorkflowSearchSelectorDetails,
1212
useWorkflowSearchTableDetails,
13+
type WorkflowSearchResolvedResource,
1314
} from '@/hooks/queries/workflow-search-replace'
1415

1516
export interface HydratedWorkflowSearchMatch extends WorkflowSearchMatch {
@@ -49,7 +50,7 @@ export function useWorkflowSearchReferenceHydration({
4950
{ label: string; resolved: boolean; inaccessible: boolean }
5051
>()
5152

52-
const setResolvedLabel = (query: (typeof oauthDetails)[number]) => {
53+
const setResolvedLabel = (query: { data?: WorkflowSearchResolvedResource }) => {
5354
if (!query.data) return
5455
const value = {
5556
label: query.data.label,

apps/sim/hooks/queries/workflow-search-replace.ts

Lines changed: 110 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { useMemo } from 'react'
2-
import { useQueries } from '@tanstack/react-query'
2+
import { useQueries, useQuery } from '@tanstack/react-query'
33
import { requestJson } from '@/lib/api/client/request'
44
import type { KnowledgeBaseData } from '@/lib/api/contracts/knowledge'
5-
import { discoverMcpToolsContract, listMcpServersContract } from '@/lib/api/contracts/mcp'
6-
import { getTableContract, listTablesContract } from '@/lib/api/contracts/tables'
7-
import { listWorkspaceFilesContract } from '@/lib/api/contracts/workspace-files'
5+
import {
6+
type DiscoverMcpToolsResponse,
7+
discoverMcpToolsContract,
8+
type ListMcpServersResponse,
9+
listMcpServersContract,
10+
} from '@/lib/api/contracts/mcp'
11+
import {
12+
type GetTableResponse,
13+
getTableContract,
14+
type ListTablesResponse,
15+
listTablesContract,
16+
} from '@/lib/api/contracts/tables'
17+
import {
18+
type ListWorkspaceFilesResponse,
19+
listWorkspaceFilesContract,
20+
} from '@/lib/api/contracts/workspace-files'
821
import { createMcpToolId } from '@/lib/mcp/shared'
922
import type { Credential } from '@/lib/oauth'
1023
import { stableStringifyWorkflowSearchValue } from '@/lib/workflows/search-replace/resource-resolvers'
@@ -59,16 +72,22 @@ export const workflowSearchReplaceKeys = {
5972
fileDetails: () => [...workflowSearchReplaceKeys.resourceDetails(), 'file'] as const,
6073
fileDetail: (workspaceId?: string, fileKey?: string) =>
6174
[...workflowSearchReplaceKeys.fileDetails(), workspaceId ?? '', fileKey ?? ''] as const,
75+
fileListDetails: (workspaceId?: string) =>
76+
[...workflowSearchReplaceKeys.fileDetails(), 'list', workspaceId ?? ''] as const,
6277
fileReplacementOptions: (workspaceId?: string) =>
6378
[...workflowSearchReplaceKeys.replacementOptions(), 'file', workspaceId ?? ''] as const,
6479
mcpServerDetails: () => [...workflowSearchReplaceKeys.resourceDetails(), 'mcp-server'] as const,
6580
mcpServerDetail: (workspaceId?: string, serverId?: string) =>
6681
[...workflowSearchReplaceKeys.mcpServerDetails(), workspaceId ?? '', serverId ?? ''] as const,
82+
mcpServerListDetails: (workspaceId?: string) =>
83+
[...workflowSearchReplaceKeys.mcpServerDetails(), 'list', workspaceId ?? ''] as const,
6784
mcpServerReplacementOptions: (workspaceId?: string) =>
6885
[...workflowSearchReplaceKeys.replacementOptions(), 'mcp-server', workspaceId ?? ''] as const,
6986
mcpToolDetails: () => [...workflowSearchReplaceKeys.resourceDetails(), 'mcp-tool'] as const,
7087
mcpToolDetail: (workspaceId?: string, toolId?: string) =>
7188
[...workflowSearchReplaceKeys.mcpToolDetails(), workspaceId ?? '', toolId ?? ''] as const,
89+
mcpToolListDetails: (workspaceId?: string) =>
90+
[...workflowSearchReplaceKeys.mcpToolDetails(), 'list', workspaceId ?? ''] as const,
7291
mcpToolReplacementOptions: (workspaceId?: string) =>
7392
[...workflowSearchReplaceKeys.replacementOptions(), 'mcp-tool', workspaceId ?? ''] as const,
7493
knowledgeReplacementOptions: (workspaceId?: string) =>
@@ -196,7 +215,7 @@ export function useWorkflowSearchTableDetails(
196215
}),
197216
enabled: Boolean(workspaceId && match.rawValue),
198217
staleTime: 60 * 1000,
199-
select: (response): WorkflowSearchResolvedResource => ({
218+
select: (response: GetTableResponse): WorkflowSearchResolvedResource => ({
200219
matchRawValue: match.rawValue,
201220
resourceGroupKey: match.resource?.resourceGroupKey,
202221
label: response.data.table.name,
@@ -217,31 +236,38 @@ export function useWorkflowSearchFileDetails(matches: WorkflowSearchMatch[], wor
217236
[matches]
218237
)
219238

220-
return useQueries({
221-
queries: fileMatches.map((match) => ({
222-
queryKey: workflowSearchReplaceKeys.fileDetail(workspaceId, match.rawValue),
223-
queryFn: ({ signal }: { signal: AbortSignal }) =>
224-
requestJson(listWorkspaceFilesContract, {
225-
params: { id: workspaceId as string },
226-
query: { scope: 'active' },
227-
signal,
228-
}),
229-
enabled: Boolean(workspaceId && match.rawValue),
230-
staleTime: 60 * 1000,
231-
select: (response): WorkflowSearchResolvedResource => {
232-
const file = response.files.find((item) =>
239+
const filesQuery = useQuery({
240+
queryKey: workflowSearchReplaceKeys.fileListDetails(workspaceId),
241+
queryFn: ({ signal }: { signal: AbortSignal }) =>
242+
requestJson(listWorkspaceFilesContract, {
243+
params: { id: workspaceId as string },
244+
query: { scope: 'active' },
245+
signal,
246+
}),
247+
enabled: Boolean(workspaceId && fileMatches.length > 0),
248+
staleTime: 60 * 1000,
249+
})
250+
251+
return useMemo(
252+
() =>
253+
fileMatches.map((match) => {
254+
const file = filesQuery.data?.files.find((item) =>
233255
[item.id, item.key, item.path, item.name].includes(match.rawValue)
234256
)
235257
return {
236-
matchRawValue: match.rawValue,
237-
resourceGroupKey: match.resource?.resourceGroupKey,
238-
label: file?.name ?? match.rawValue,
239-
resolved: Boolean(file),
240-
inaccessible: false,
258+
data: filesQuery.data
259+
? {
260+
matchRawValue: match.rawValue,
261+
resourceGroupKey: match.resource?.resourceGroupKey,
262+
label: file?.name ?? match.rawValue,
263+
resolved: Boolean(file),
264+
inaccessible: false,
265+
}
266+
: undefined,
241267
}
242-
},
243-
})),
244-
})
268+
}),
269+
[fileMatches, filesQuery.data]
270+
)
245271
}
246272

247273
export function useWorkflowSearchMcpServerDetails(
@@ -250,28 +276,35 @@ export function useWorkflowSearchMcpServerDetails(
250276
) {
251277
const serverMatches = useMemo(() => uniqueMatches(matches, 'mcp-server'), [matches])
252278

253-
return useQueries({
254-
queries: serverMatches.map((match) => ({
255-
queryKey: workflowSearchReplaceKeys.mcpServerDetail(workspaceId, match.rawValue),
256-
queryFn: ({ signal }: { signal: AbortSignal }) =>
257-
requestJson(listMcpServersContract, {
258-
query: { workspaceId: workspaceId as string },
259-
signal,
260-
}),
261-
enabled: Boolean(workspaceId && match.rawValue),
262-
staleTime: 60 * 1000,
263-
select: (response): WorkflowSearchResolvedResource => {
264-
const server = response.data.servers.find((item) => item.id === match.rawValue)
279+
const serversQuery = useQuery({
280+
queryKey: workflowSearchReplaceKeys.mcpServerListDetails(workspaceId),
281+
queryFn: ({ signal }: { signal: AbortSignal }) =>
282+
requestJson(listMcpServersContract, {
283+
query: { workspaceId: workspaceId as string },
284+
signal,
285+
}),
286+
enabled: Boolean(workspaceId && serverMatches.length > 0),
287+
staleTime: 60 * 1000,
288+
})
289+
290+
return useMemo(
291+
() =>
292+
serverMatches.map((match) => {
293+
const server = serversQuery.data?.data.servers.find((item) => item.id === match.rawValue)
265294
return {
266-
matchRawValue: match.rawValue,
267-
resourceGroupKey: match.resource?.resourceGroupKey,
268-
label: server?.name ?? match.rawValue,
269-
resolved: Boolean(server),
270-
inaccessible: false,
295+
data: serversQuery.data
296+
? {
297+
matchRawValue: match.rawValue,
298+
resourceGroupKey: match.resource?.resourceGroupKey,
299+
label: server?.name ?? match.rawValue,
300+
resolved: Boolean(server),
301+
inaccessible: false,
302+
}
303+
: undefined,
271304
}
272-
},
273-
})),
274-
})
305+
}),
306+
[serverMatches, serversQuery.data]
307+
)
275308
}
276309

277310
export function useWorkflowSearchMcpToolDetails(
@@ -280,30 +313,37 @@ export function useWorkflowSearchMcpToolDetails(
280313
) {
281314
const toolMatches = useMemo(() => uniqueMatches(matches, 'mcp-tool'), [matches])
282315

283-
return useQueries({
284-
queries: toolMatches.map((match) => ({
285-
queryKey: workflowSearchReplaceKeys.mcpToolDetail(workspaceId, match.rawValue),
286-
queryFn: ({ signal }: { signal: AbortSignal }) =>
287-
requestJson(discoverMcpToolsContract, {
288-
query: { workspaceId: workspaceId as string },
289-
signal,
290-
}),
291-
enabled: Boolean(workspaceId && match.rawValue),
292-
staleTime: 60 * 1000,
293-
select: (response): WorkflowSearchResolvedResource => {
294-
const tool = response.data.tools.find(
316+
const toolsQuery = useQuery({
317+
queryKey: workflowSearchReplaceKeys.mcpToolListDetails(workspaceId),
318+
queryFn: ({ signal }: { signal: AbortSignal }) =>
319+
requestJson(discoverMcpToolsContract, {
320+
query: { workspaceId: workspaceId as string },
321+
signal,
322+
}),
323+
enabled: Boolean(workspaceId && toolMatches.length > 0),
324+
staleTime: 60 * 1000,
325+
})
326+
327+
return useMemo(
328+
() =>
329+
toolMatches.map((match) => {
330+
const tool = toolsQuery.data?.data.tools.find(
295331
(item) => createMcpToolId(item.serverId, item.name) === match.rawValue
296332
)
297333
return {
298-
matchRawValue: match.rawValue,
299-
resourceGroupKey: match.resource?.resourceGroupKey,
300-
label: tool ? `${tool.serverName}: ${tool.name}` : match.rawValue,
301-
resolved: Boolean(tool),
302-
inaccessible: false,
334+
data: toolsQuery.data
335+
? {
336+
matchRawValue: match.rawValue,
337+
resourceGroupKey: match.resource?.resourceGroupKey,
338+
label: tool ? `${tool.serverName}: ${tool.name}` : match.rawValue,
339+
resolved: Boolean(tool),
340+
inaccessible: false,
341+
}
342+
: undefined,
303343
}
304-
},
305-
})),
306-
})
344+
}),
345+
[toolMatches, toolsQuery.data]
346+
)
307347
}
308348

309349
export function useWorkflowSearchSelectorDetails(matches: WorkflowSearchMatch[]) {
@@ -418,7 +458,7 @@ export function useWorkflowSearchTableReplacementOptions(
418458
}),
419459
enabled: Boolean(workspaceId && tableMatch),
420460
staleTime: 60 * 1000,
421-
select: (response): WorkflowSearchReplacementOption[] =>
461+
select: (response: ListTablesResponse): WorkflowSearchReplacementOption[] =>
422462
response.data.tables.map((table) => ({
423463
kind: 'table',
424464
value: table.id,
@@ -451,7 +491,7 @@ export function useWorkflowSearchFileReplacementOptions(
451491
}),
452492
enabled: Boolean(workspaceId && fileMatch),
453493
staleTime: 60 * 1000,
454-
select: (response): WorkflowSearchReplacementOption[] =>
494+
select: (response: ListWorkspaceFilesResponse): WorkflowSearchReplacementOption[] =>
455495
response.files.map((file) => ({
456496
kind: 'file',
457497
value: JSON.stringify({
@@ -486,7 +526,7 @@ export function useWorkflowSearchMcpServerReplacementOptions(
486526
}),
487527
enabled: Boolean(workspaceId && serverMatch),
488528
staleTime: 60 * 1000,
489-
select: (response): WorkflowSearchReplacementOption[] =>
529+
select: (response: ListMcpServersResponse): WorkflowSearchReplacementOption[] =>
490530
response.data.servers.map((server) => ({
491531
kind: 'mcp-server',
492532
value: server.id,
@@ -515,7 +555,7 @@ export function useWorkflowSearchMcpToolReplacementOptions(
515555
}),
516556
enabled: Boolean(workspaceId && toolMatch),
517557
staleTime: 60 * 1000,
518-
select: (response): WorkflowSearchReplacementOption[] =>
558+
select: (response: DiscoverMcpToolsResponse): WorkflowSearchReplacementOption[] =>
519559
response.data.tools.map((tool) => ({
520560
kind: 'mcp-tool',
521561
value: createMcpToolId(tool.serverId, tool.name),

0 commit comments

Comments
 (0)