11'use client'
22
3- import { useEffect , useState } from 'react'
3+ import { useEffect , useRef , useState } from 'react'
4+ import { useParams } from 'next/navigation'
45import { Tooltip , TooltipContent , TooltipProvider , TooltipTrigger } from '@/components/ui/tooltip'
56import {
67 type SlackChannelInfo ,
78 SlackChannelSelector ,
89} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/components/channel-selector/components/slack-channel-selector'
910import { useDependsOnGate } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/hooks/use-depends-on-gate'
11+ import { useForeignCredential } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/hooks/use-foreign-credential'
1012import { useSubBlockValue } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/workflow-block/components/sub-block/hooks/use-sub-block-value'
1113import type { SubBlockConfig } from '@/blocks/types'
1214
@@ -15,7 +17,6 @@ interface ChannelSelectorInputProps {
1517 subBlock : SubBlockConfig
1618 disabled ?: boolean
1719 onChannelSelect ?: ( channelId : string ) => void
18- credential ?: string
1920 isPreview ?: boolean
2021 previewValue ?: any | null
2122}
@@ -25,10 +26,11 @@ export function ChannelSelectorInput({
2526 subBlock,
2627 disabled = false ,
2728 onChannelSelect,
28- credential : providedCredential ,
2929 isPreview = false ,
3030 previewValue,
3131} : ChannelSelectorInputProps ) {
32+ const params = useParams ( )
33+ const workflowIdFromUrl = ( params ?. workflowId as string ) || ''
3234 // Use the proper hook to get the current value and setter (same as file-selector)
3335 const [ storeValue , setStoreValue ] = useSubBlockValue ( blockId , subBlock . id )
3436 // Reactive upstream fields
@@ -42,20 +44,22 @@ export function ChannelSelectorInput({
4244 const provider = subBlock . provider || 'slack'
4345 const isSlack = provider === 'slack'
4446 // Central dependsOn gating
45- const { finalDisabled } = useDependsOnGate ( blockId , subBlock , { disabled, isPreview } )
47+ const { finalDisabled, dependsOn, dependencyValues } = useDependsOnGate ( blockId , subBlock , {
48+ disabled,
49+ isPreview,
50+ } )
4651
47- // Get the credential for the provider - use provided credential or fall back to reactive values
48- let credential : string
49- if ( providedCredential ) {
50- credential = providedCredential
51- } else if ( ( authMethod as string ) === 'bot_token' && ( botToken as string ) ) {
52- credential = botToken as string
53- } else {
54- credential = ( connectedCredential as string ) || ''
55- }
52+ // Choose credential strictly based on auth method
53+ const credential : string =
54+ ( authMethod as string ) === 'bot_token'
55+ ? ( botToken as string ) || ''
56+ : ( connectedCredential as string ) || ''
5657
57- // Use preview value when in preview mode, otherwise use store value
58- const value = isPreview ? previewValue : storeValue
58+ // Determine if connected OAuth credential is foreign (not applicable for bot tokens)
59+ const { isForeignCredential } = useForeignCredential (
60+ 'slack' ,
61+ ( authMethod as string ) === 'bot_token' ? '' : ( connectedCredential as string ) || ''
62+ )
5963
6064 // Get the current value from the store or prop value if in preview mode (same pattern as file-selector)
6165 useEffect ( ( ) => {
@@ -65,6 +69,21 @@ export function ChannelSelectorInput({
6569 }
6670 } , [ isPreview , previewValue , storeValue ] )
6771
72+ // Clear channel when any declared dependency changes (e.g., authMethod/credential)
73+ const prevDepsSigRef = useRef < string > ( '' )
74+ useEffect ( ( ) => {
75+ if ( dependsOn . length === 0 ) return
76+ const currentSig = JSON . stringify ( dependencyValues )
77+ if ( prevDepsSigRef . current && prevDepsSigRef . current !== currentSig ) {
78+ if ( ! isPreview ) {
79+ setSelectedChannelId ( '' )
80+ setChannelInfo ( null )
81+ setStoreValue ( '' )
82+ }
83+ }
84+ prevDepsSigRef . current = currentSig
85+ } , [ dependsOn , dependencyValues , isPreview , setStoreValue ] )
86+
6887 // Handle channel selection (same pattern as file-selector)
6988 const handleChannelChange = ( channelId : string , info ?: SlackChannelInfo ) => {
7089 setSelectedChannelId ( channelId )
@@ -90,6 +109,8 @@ export function ChannelSelectorInput({
90109 credential = { credential }
91110 label = { subBlock . placeholder || 'Select Slack channel' }
92111 disabled = { finalDisabled }
112+ workflowId = { workflowIdFromUrl }
113+ isForeignCredential = { isForeignCredential }
93114 />
94115 </ div >
95116 </ TooltipTrigger >
0 commit comments