/* eslint-disable @typescript-eslint/no-explicit-any */
import { MoreInfoTooltip } from '@/components/MoreInfoTooltip'
import { Select } from '@/components/inputs/Select'
import { VariablesButton } from '@/features/variables/components/VariablesButton'
import { useWorkspace } from '@/features/workspace/WorkspaceProvider'
import { useToast } from '@/hooks/useToast'
import { trpc } from '@/lib/trpc'
import {
FormControl,
FormHelperText,
FormLabel,
HStack,
Stack,
} from '@chakra-ui/react'
import {
ForgedBlockDefinition,
ForgedBlock,
} from '@typebot.io/forge-repository/types'
import { ReactNode, useMemo } from 'react'
import { findFetcher } from '../helpers/findFetcher'
type Props = {
blockDef: ForgedBlockDefinition
defaultValue?: string
fetcherId: string
options: ForgedBlock['options']
placeholder?: string
label?: string
helperText?: ReactNode
moreInfoTooltip?: string
direction?: 'row' | 'column'
isRequired?: boolean
width?: 'full'
withVariableButton?: boolean
onChange: (value: string | undefined) => void
}
export const ForgeSelectInput = ({
defaultValue,
fetcherId,
options,
blockDef,
placeholder,
label,
helperText,
moreInfoTooltip,
isRequired,
direction = 'column',
width,
withVariableButton = false,
onChange,
}: Props) => {
const { workspace } = useWorkspace()
const { showToast } = useToast()
const fetcher = useMemo(
() => findFetcher(blockDef, fetcherId),
[blockDef, fetcherId]
)
const { data } = trpc.forge.fetchSelectItems.useQuery(
{
integrationId: blockDef.id,
options: pick(
options,
(blockDef.auth ? ['credentialsId'] : []).concat(
fetcher?.dependencies ?? []
)
),
workspaceId: workspace?.id as string,
fetcherId,
},
{
enabled: !!workspace?.id && !!fetcher,
onError: (error) => {
showToast({
description: error.message,
status: 'error',
})
},
}
)
return (
{label && (
{label}{' '}
{moreInfoTooltip && (
{moreInfoTooltip}
)}
)}
{withVariableButton ? (
{
onChange(`{{${variable.name}}}`)
}}
/>
) : null}
{helperText && {helperText}}
)
}
function pick(obj: T, keys: K[]): Pick {
if (!obj) return {} as Pick
const ret: any = {}
keys.forEach((key) => {
ret[key] = obj[key]
})
return ret
}