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

  let { channel } = $props();

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

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

  function onKeyDown(event) {
    let modifier = event.shiftKey || event.altKey || event.ctrlKey || event.metaKey;
    if (!modifier && event.key === 'Enter') {
      onSubmit(event);
    }
  }
</script>

<form bind:this={form} onsubmit={onSubmit}>
  <textarea onkeydown={onKeyDown} bind:value {disabled} type="search"></textarea>
  <button type="submit">&raquo;</button>
</form>
