Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(input-area): send message by enter #8

Merged
merged 3 commits into from
Feb 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/components/input-area.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Message } from '@xsai/shared-chat'
import type { KeyboardEventHandler } from 'react'

import { Icon } from '@iconify/react'
import { Button, Flex, IconButton, Text, TextArea } from '@radix-ui/themes'
import { generateText } from '@xsai/generate-text'
import { useState } from 'react'
import { useCallback, useRef, useState } from 'react'
import { type SubmitHandler, useForm } from 'react-hook-form'

import type { charactersTable } from '../db/schema'
Expand Down Expand Up @@ -31,6 +32,8 @@ export const InputArea = ({ character }: { character?: typeof charactersTable.$i
// watch,
} = useForm<Inputs>()

const formRef = useRef<HTMLFormElement>(null)

const onSubmit: SubmitHandler<Inputs> = async ({ content }) => {
const msg: Message[] = [...messages, { content, role: 'user' }]

Expand All @@ -48,9 +51,16 @@ export const InputArea = ({ character }: { character?: typeof charactersTable.$i
setIsTyping(false)
}

const handleKeyDown = useCallback<KeyboardEventHandler<HTMLTextAreaElement>>((e) => {
if (e.key !== 'Enter' || e.altKey || e.metaKey || e.shiftKey || e.ctrlKey)
return
e.preventDefault()
formRef.current?.dispatchEvent(new Event('submit', { bubbles: true }))
}, [])

return (
// eslint-disable-next-line ts/no-misused-promises
<form data-test-id="input-area" onSubmit={handleSubmit(onSubmit)}>
<form data-test-id="input-area" onSubmit={handleSubmit(onSubmit)} ref={formRef}>
<Flex direction="column" gap="2" style={{ alignSelf: 'flex-end', marginTop: 'auto' }} width="100%">
{isTyping && (
<Flex align="center" gap="2">
Expand All @@ -64,12 +74,14 @@ export const InputArea = ({ character }: { character?: typeof charactersTable.$i
)}
<TextArea
color="gray"
data-test-id="input-area-textarea"
placeholder="Write a Message..."
resize="vertical"
size="3"
style={{ maxHeight: '50vh' }}
variant="soft"
{...register('content', { minLength: 1, required: true })}
onKeyDown={handleKeyDown}
/>
<Flex gap="2">
<IconButton color="gray" disabled variant="soft">
Expand Down
Loading