<script>
	import { postToChannel } from '$lib/apiServer';

	let { channel } = $props();

	let form;
	let value = $state('');
	let pending = false;

	let disabled = $derived(pending);

	async function onSubmit(event) {
		event.preventDefault();
		pending = true;
		await postToChannel(channel, value);
		form.reset();
		pending = false;
	}

	function onKeyDown(event) {
		if (!event.altKey && event.key === 'Enter') {
			onSubmit(event);
		}
	}
</script>

<form bind:this={form} onsubmit={onSubmit} class="flex flex-row flex-nowrap">
	<textarea
		onkeydown={onKeyDown}
		bind:value
		{disabled}
		type="search"
		class="flex-auto h-6 input rounded-r-none"
	></textarea>
	<button
		color="primary variant-filled-secondary"
		type="submit"
		class="flex-none w-6 h-6 btn-icon variant-filled rounded-l-none">&raquo;</button
	>
</form>
