1- import { useEffect , useMemo , useState } from 'react'
1+ import { useEffect , useMemo , useRef , useState } from 'react'
22import { Trash2 } from 'lucide-react'
33import { Badge } from '@/components/ui/badge'
44import { Button } from '@/components/ui/button'
@@ -58,6 +58,7 @@ export function TriggerModal({
5858 const [ dynamicOptions , setDynamicOptions ] = useState <
5959 Record < string , Array < { id : string ; name : string } > >
6060 > ( { } )
61+ const lastCredentialIdRef = useRef < string | null > ( null )
6162
6263 // Reset provider-dependent config fields when credentials change
6364 const resetFieldsForCredentialChange = ( ) => {
@@ -103,7 +104,8 @@ export function TriggerModal({
103104 const credentialValue = subBlockStore . getValue ( blockId , 'triggerCredentials' ) as
104105 | string
105106 | null
106- const hasCredential = Boolean ( credentialValue )
107+ const currentCredentialId = credentialValue || null
108+ const hasCredential = Boolean ( currentCredentialId )
107109 setHasCredentials ( hasCredential )
108110
109111 // If credential was cleared by another user, reset local state and dynamic options
@@ -113,22 +115,44 @@ export function TriggerModal({
113115 }
114116 // Clear provider-specific dynamic options
115117 setDynamicOptions ( { } )
116- // Clear any selected values that depend on the credential
117- resetFieldsForCredentialChange ( )
118+ // Per requirements: only clear dependent selections on actual credential CHANGE,
119+ // not when it becomes empty. So we do NOT reset fields here.
120+ lastCredentialIdRef . current = null
118121 return
119122 }
120123
121124 // If credential changed, clear options immediately and load for new cred
122- if ( credentialValue && credentialValue !== selectedCredentialId ) {
123- setSelectedCredentialId ( credentialValue )
125+ const previousCredentialId = lastCredentialIdRef . current
126+
127+ // First detection (prev null → current non-null): do not clear selections
128+ if ( previousCredentialId === null ) {
129+ setSelectedCredentialId ( currentCredentialId )
130+ lastCredentialIdRef . current = currentCredentialId
131+ if ( typeof currentCredentialId === 'string' ) {
132+ if ( triggerDef . provider === 'gmail' ) {
133+ void loadGmailLabels ( currentCredentialId )
134+ } else if ( triggerDef . provider === 'outlook' ) {
135+ void loadOutlookFolders ( currentCredentialId )
136+ }
137+ }
138+ return
139+ }
140+
141+ // Real change (prev non-null → different non-null): clear dependent selections
142+ if (
143+ typeof currentCredentialId === 'string' &&
144+ currentCredentialId !== previousCredentialId
145+ ) {
146+ setSelectedCredentialId ( currentCredentialId )
147+ lastCredentialIdRef . current = currentCredentialId
124148 // Clear stale options before loading new ones
125149 setDynamicOptions ( { } )
126150 // Clear any selected values that depend on the credential
127151 resetFieldsForCredentialChange ( )
128152 if ( triggerDef . provider === 'gmail' ) {
129- void loadGmailLabels ( credentialValue )
153+ void loadGmailLabels ( currentCredentialId )
130154 } else if ( triggerDef . provider === 'outlook' ) {
131- void loadOutlookFolders ( credentialValue )
155+ void loadOutlookFolders ( currentCredentialId )
132156 }
133157 }
134158 }
0 commit comments