11'use client'
22
33import { useMemo } from 'react'
4+ import { generateShortId } from '@sim/utils/id'
45import { useParams } from 'next/navigation'
5- import { SELECTOR_CONTEXT_FIELDS } from '@/lib/workflows/subblocks/context'
66import type { SubBlockConfig } from '@/blocks/types'
7- import { extractEnvVarName , isEnvVarReference , isReference } from '@/executor/constants'
87import { usePersonalEnvironment } from '@/hooks/queries/environment'
8+ import {
9+ applySelectorDependenciesToContext ,
10+ resolveSelectorDependencyValues ,
11+ } from '@/hooks/selectors/context-resolution'
12+ import { getSelectorDefinition } from '@/hooks/selectors/registry'
913import type { SelectorContext , SelectorKey } from '@/hooks/selectors/types'
1014import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
1115import { useDependsOnGate } from './use-depends-on-gate'
@@ -35,6 +39,15 @@ export function useSelectorSetup(
3539
3640 const { data : envVariables = { } } = usePersonalEnvironment ( )
3741
42+ const selectorKey = ( subBlock . selectorKey ?? null ) as SelectorKey | null
43+ const serverResolvedContextFields = useMemo (
44+ ( ) =>
45+ new Set < keyof SelectorContext > (
46+ selectorKey ? ( getSelectorDefinition ( selectorKey ) . serverResolvedContextFields ?? [ ] ) : [ ]
47+ ) ,
48+ [ selectorKey ]
49+ )
50+
3851 const { finalDisabled, dependencyValues, canonicalIndex } = useDependsOnGate (
3952 blockId ,
4053 subBlock ,
@@ -43,42 +56,33 @@ export function useSelectorSetup(
4356
4457 const [ impersonateUserEmail ] = useSubBlockValue < string | null > ( blockId , 'impersonateUserEmail' )
4558
59+ const selectorCacheScope = useMemo (
60+ ( ) => generateShortId ( ) ,
61+ [ blockId , subBlock . id , selectorKey , dependencyValues ]
62+ )
63+
4664 const resolvedDependencyValues = useMemo ( ( ) => {
47- const resolved : Record < string , unknown > = { }
48- for ( const [ key , value ] of Object . entries ( dependencyValues ) ) {
49- if ( value === null || value === undefined ) {
50- resolved [ key ] = value
51- continue
52- }
53- const str = String ( value )
54- if ( isEnvVarReference ( str ) ) {
55- const varName = extractEnvVarName ( str )
56- resolved [ key ] = envVariables [ varName ] ?. value || undefined
57- } else {
58- resolved [ key ] = value
59- }
60- }
61- return resolved
62- } , [ dependencyValues , envVariables ] )
65+ return resolveSelectorDependencyValues ( {
66+ dependencyValues,
67+ personalEnvironment : envVariables ,
68+ canonicalIndex,
69+ serverResolvedContextFields,
70+ } )
71+ } , [ dependencyValues , envVariables , canonicalIndex , serverResolvedContextFields ] )
6372
6473 const selectorContext = useMemo < SelectorContext > ( ( ) => {
6574 const context : SelectorContext = {
6675 workflowId,
6776 workspaceId : workspaceId || undefined ,
77+ selectorCacheScope,
6878 mimeType : subBlock . mimeType ,
6979 }
7080
71- for ( const [ depKey , value ] of Object . entries ( resolvedDependencyValues ) ) {
72- if ( value === null || value === undefined ) continue
73- const strValue = String ( value )
74- if ( ! strValue ) continue
75- if ( isReference ( strValue ) ) continue
76-
77- const canonicalParamId = canonicalIndex . canonicalIdBySubBlockId [ depKey ] ?? depKey
78- if ( SELECTOR_CONTEXT_FIELDS . has ( canonicalParamId as keyof SelectorContext ) ) {
79- context [ canonicalParamId as keyof SelectorContext ] = strValue
80- }
81- }
81+ applySelectorDependenciesToContext ( {
82+ context,
83+ dependencyValues : resolvedDependencyValues ,
84+ canonicalIndex,
85+ } )
8286
8387 if ( context . oauthCredential && impersonateUserEmail ) {
8488 context . impersonateUserEmail = impersonateUserEmail
@@ -90,12 +94,13 @@ export function useSelectorSetup(
9094 canonicalIndex ,
9195 workflowId ,
9296 workspaceId ,
97+ selectorCacheScope ,
9398 subBlock . mimeType ,
9499 impersonateUserEmail ,
95100 ] )
96101
97102 return {
98- selectorKey : ( subBlock . selectorKey ?? null ) as SelectorKey | null ,
103+ selectorKey,
99104 selectorContext,
100105 allowSearch : subBlock . selectorAllowSearch ?? true ,
101106 disabled : finalDisabled || ! subBlock . selectorKey ,
0 commit comments