hezarfen_frontend
Clone
https://git.tahinli.com/archive/hezarfen_frontend.git
0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits
| 1 | import { createEffect, onMount } from "solid-js"; |
| 2 | import { Button } from "@/components/ui/button"; |
| 3 | import { IconSend } from "@/components/ui/icons"; |
| 4 | import { Textarea } from "@/components/ui/textarea"; |
| 5 | import { cn } from "@/lib/cn"; |
| 6 | |
| 7 | const MAX_HEIGHT_PX = 176; |
| 8 | |
| 9 | export function RagComposer(props: { |
| 10 | value: string; |
| 11 | placeholder: string; |
| 12 | sendLabel: string; |
| 13 | hint: string; |
| 14 | disabled: boolean; |
| 15 | /** Take the caret on mount — the landing screen is the composer, so it should |
| 16 | * be ready to type into, and a send that re-places it returns focus. */ |
| 17 | autofocus?: boolean; |
| 18 | class?: string; |
| 19 | onInput: (value: string) => void; |
| 20 | onSubmit: () => void; |
| 21 | }) { |
| 22 | let field: HTMLTextAreaElement | undefined; |
| 23 | |
| 24 | const resize = () => { |
| 25 | if (!field) return; |
| 26 | field.style.height = "auto"; |
| 27 | field.style.height = `${Math.min(field.scrollHeight, MAX_HEIGHT_PX)}px`; |
| 28 | }; |
| 29 | |
| 30 | createEffect(() => { |
| 31 | void props.value; |
| 32 | resize(); |
| 33 | }); |
| 34 | |
| 35 | onMount(() => { |
| 36 | if (props.autofocus) field?.focus(); |
| 37 | }); |
| 38 | |
| 39 | return ( |
| 40 | <form |
| 41 | class={cn("px-4 sm:px-6", props.class)} |
| 42 | onSubmit={(event) => { |
| 43 | event.preventDefault(); |
| 44 | if (!props.disabled) props.onSubmit(); |
| 45 | }} |
| 46 | > |
| 47 | <div class="mx-auto flex w-full max-w-3xl items-end gap-2 rounded-2xl border border-border bg-surface-overlay px-3.5 py-2.5 transition-colors focus-within:border-primary/50 focus-within:ring-2 focus-within:ring-ring"> |
| 48 | <Textarea |
| 49 | ref={field} |
| 50 | rows={1} |
| 51 | maxlength={8000} |
| 52 | value={props.value} |
| 53 | disabled={props.disabled} |
| 54 | placeholder={props.placeholder} |
| 55 | class="max-h-44 min-h-0 flex-1 resize-none overflow-y-auto border-0 bg-transparent px-0 py-1.5 text-base shadow-none hover:border-0 hover:bg-transparent focus:bg-transparent focus-visible:ring-0 focus-visible:ring-offset-0" |
| 56 | onInput={(event) => props.onInput(event.currentTarget.value)} |
| 57 | onKeyDown={(event) => { |
| 58 | if (event.key === "Enter" && !event.shiftKey) { |
| 59 | event.preventDefault(); |
| 60 | if (!props.disabled) props.onSubmit(); |
| 61 | } |
| 62 | }} |
| 63 | /> |
| 64 | <Button |
| 65 | type="submit" |
| 66 | size="sm" |
| 67 | class="h-9 w-9 shrink-0 rounded-full p-0" |
| 68 | aria-label={props.sendLabel} |
| 69 | title={props.sendLabel} |
| 70 | disabled={props.disabled || !props.value.trim()} |
| 71 | > |
| 72 | <IconSend class="h-4 w-4" /> |
| 73 | </Button> |
| 74 | </div> |
| 75 | <p class="mx-auto mt-2 w-full max-w-3xl px-1 text-center text-[11px] leading-4 text-muted-foreground">{props.hint}</p> |
| 76 | </form> |
| 77 | ); |
| 78 | } |