import { ShortTextInput } from '@/components' import { SendButton } from '@/components/SendButton' import { CommandData } from '@/features/commands/types' import { InputSubmitContent } from '@/types' import { isMobile } from '@/utils/isMobileSignal' import type { UrlInputBlock } from '@indite.io/schemas' import { createSignal, onCleanup, onMount } from 'solid-js' import { defaultUrlInputOptions } from '@indite.io/schemas/features/blocks/inputs/url/constants' type Props = { block: UrlInputBlock defaultValue?: string onSubmit: (value: InputSubmitContent) => void } export const UrlInput = (props: Props) => { const [inputValue, setInputValue] = createSignal(props.defaultValue ?? '') let inputRef: HTMLInputElement | HTMLTextAreaElement | undefined const handleInput = (inputValue: string) => { setInputValue(inputValue) } const checkIfInputIsValid = () => inputRef?.value !== '' && inputRef?.reportValidity() const submit = () => { if (inputRef && !inputRef?.value.startsWith('http')) inputRef.value = `https://${inputRef.value}` if (checkIfInputIsValid()) props.onSubmit({ type: 'text', value: inputRef?.value ?? inputValue() }) else inputRef?.focus() setInputValue('') } const submitWhenEnter = (e: KeyboardEvent) => { if (e.key === 'Enter') submit() } onMount(() => { if (!isMobile() && inputRef) inputRef.focus({ preventScroll: true, }) window.addEventListener('message', processIncomingEvent) }) onCleanup(() => { window.removeEventListener('message', processIncomingEvent) }) const processIncomingEvent = (event: MessageEvent) => { const { data } = event if (!data.isFromBot) return if (data.command === 'setInputValue') setInputValue(data.value) } return (
{props.block.options?.labels?.button}
) }