refactor(playground): extract input submit text helper
- move prompt submit text validation into input control utilities - let the input component submit only when a concrete text value is available
This commit is contained in:
@@ -24,6 +24,7 @@ import {
|
||||
PromptInputTextarea,
|
||||
type PromptInputMessage,
|
||||
} from '@/components/ai-elements/prompt-input'
|
||||
import { getSubmittableInputText } from '../lib'
|
||||
import type { ModelOption, GroupOption } from '../types'
|
||||
import { PlaygroundInputControls } from './playground-input-controls'
|
||||
import { PlaygroundInputTools } from './playground-input-tools'
|
||||
@@ -60,8 +61,10 @@ export function PlaygroundInput({
|
||||
const [text, setText] = useState('')
|
||||
|
||||
const handleSubmit = (message: PromptInputMessage) => {
|
||||
if (!message.text?.trim() || disabled) return
|
||||
onSubmit(message.text)
|
||||
const submittableText = getSubmittableInputText(message, disabled)
|
||||
|
||||
if (!submittableText) return
|
||||
onSubmit(submittableText)
|
||||
setText('')
|
||||
}
|
||||
|
||||
|
||||
@@ -34,6 +34,21 @@ type InputControlState = {
|
||||
shouldShowStop: boolean
|
||||
}
|
||||
|
||||
type SubmittableInputMessage = {
|
||||
text?: string | null
|
||||
}
|
||||
|
||||
export function getSubmittableInputText(
|
||||
message: SubmittableInputMessage,
|
||||
disabled?: boolean
|
||||
): string | null {
|
||||
if (disabled || !message.text?.trim()) {
|
||||
return null
|
||||
}
|
||||
|
||||
return message.text
|
||||
}
|
||||
|
||||
export function getInputControlState({
|
||||
disabled,
|
||||
groups,
|
||||
|
||||
Reference in New Issue
Block a user