Commit 2d6441dd by liimin

refactor(base): 重构聊天组件的输入区域

- 移除了 ChatInputArea 组件的复杂逻辑和渲染
- 新增了 InputWrapper 组件来替代原有的输入区域
- 删除了未使用的 Operation 组件
- 移除了多余的 console.log 语句
- 优化了组件结构,提高了代码的可维护性和可读性
1 parent 7f0507bb
...@@ -16,7 +16,6 @@ type ChatWithHistoryProps = { ...@@ -16,7 +16,6 @@ type ChatWithHistoryProps = {
const ChatWithHistory: FC<ChatWithHistoryProps> = React.memo(() => { const ChatWithHistory: FC<ChatWithHistoryProps> = React.memo(() => {
const isMobile = useBreakpoints() === MediaType.mobile const isMobile = useBreakpoints() === MediaType.mobile
const { presuppositionProblem, isShowPracticeButton, inputs } = useUrlParams() const { presuppositionProblem, isShowPracticeButton, inputs } = useUrlParams()
console.log('inputs', inputs)
return ( return (
<div className={cn('flex h-full bg-background-default-burn', isMobile && 'flex-col')}> <div className={cn('flex h-full bg-background-default-burn', isMobile && 'flex-col')}>
<div className={cn('relative grow', isMobile && 'h-[calc(100%_-_56px)] p-0')}> <div className={cn('relative grow', isMobile && 'h-[calc(100%_-_56px)] p-0')}>
......
...@@ -3,7 +3,6 @@ import Answer from '../answer' ...@@ -3,7 +3,6 @@ import Answer from '../answer'
import Question from '../question' import Question from '../question'
import { useChatStore } from '@/app/components/base/chat/chat-with-history/store' import { useChatStore } from '@/app/components/base/chat/chat-with-history/store'
import { useChatContext } from '../context' import { useChatContext } from '../context'
import { state } from 'mermaid/dist/rendering-util/rendering-elements/shapes/state'
type ChatBodyProps = { type ChatBodyProps = {
answerIcon?: ReactNode answerIcon?: ReactNode
......
import { createContext, useContext, useState, useCallback, useMemo } from 'react'
import type { ReactNode } from 'react'
import { useChatContext } from '../context'
import { useToastContext } from '@/app/components/base/toast'
import { useFileStore } from '@/app/components/base/file-uploader/store'
import { TransferMethod } from '@/types/app'
import { useTranslation } from 'react-i18next'
import { InputMessageType } from '../../types'
// 定义消息上下文类型
interface MessageContextType {
query: string
setQuery: (value: string) => void
handleSend: () => void
showVoiceInput: boolean
setShowVoiceInput: (value: boolean) => void
isMultipleLine: boolean
setIsMultipleLine: (value: boolean) => void
}
// 创建上下文并设置默认值
const MessageContext = createContext<MessageContextType | undefined>(undefined)
// 自定义hook用于访问消息上下文
export const useMessageContext = (): MessageContextType => {
const context = useContext(MessageContext)
if (!context) {
throw new Error('useMessageContext must be used within a MessageProvider')
}
return context
}
// 消息提供者组件
export const MessageProvider = ({ children }: { children: ReactNode }) => {
const { t } = useTranslation()
const [query, setQuery] = useState('')
const [showVoiceInput, setShowVoiceInput] = useState(false)
const { isResponding, onSend: sendMessage } = useChatContext()
const { notify } = useToastContext()
const filesStore = useFileStore()
const [isMultipleLine, setIsMultipleLine] = useState(false)
const handleSend = useCallback(() => {
if (isResponding) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForResponse'),
})
return
}
if (sendMessage) {
const { files, setFiles } = filesStore.getState()
if (files.find(item => item.transferMethod === TransferMethod.local_file && !item.uploadedId)) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForFileUpload'),
})
return
}
if (!query || !query.trim()) {
notify({
type: 'info',
message: t('appAnnotation.errorMessage.queryRequired'),
})
return
}
sendMessage(query, InputMessageType.BY_HAND, files)
setQuery('')
setFiles([])
}
}, [query, isResponding, sendMessage, filesStore, notify, t])
// 提供上下文值
const value = useMemo(() => ({
query,
setQuery,
handleSend,
showVoiceInput,
setShowVoiceInput,
isMultipleLine,
setIsMultipleLine
}), [
query,
showVoiceInput,
handleSend,
setShowVoiceInput,
isMultipleLine,
setIsMultipleLine
])
return (
<MessageContext.Provider value={value}>
{children}
</MessageContext.Provider>
)
}
\ No newline at end of file \ No newline at end of file
// export {default as useTextAreaHeight} from './useTextAreaHeight'
export {default as useFileOperation} from './useFileOperation'
\ No newline at end of file \ No newline at end of file
import { useFile } from '@/app/components/base/file-uploader/hooks'
import { useChatContext } from '@/app/components/base/chat/chat/context'
const useFileOperation = () => {
const { config } = useChatContext()
const visionConfig = config.file_upload
const {
handleDragFileEnter,
handleDragFileLeave,
handleDragFileOver,
handleDropFile,
handleClipboardPasteFile,
isDragActive,
} = useFile(visionConfig!)
return {
handleDragFileEnter,
handleDragFileLeave,
handleDragFileOver,
handleDropFile,
handleClipboardPasteFile,
isDragActive,
}
}
export default useFileOperation
...@@ -5,7 +5,8 @@ import type { ThemeBuilder } from '@/app/components/base/chat/embedded-chatbot/t ...@@ -5,7 +5,8 @@ import type { ThemeBuilder } from '@/app/components/base/chat/embedded-chatbot/t
import type { InputForm } from '@/app/components/base/chat/chat/type' import type { InputForm } from '@/app/components/base/chat/chat/type'
import TryToAsk from './try-to-ask' import TryToAsk from './try-to-ask'
import Practice from './practice' import Practice from './practice'
import ChatInputArea from '../chat-input-area' // import ChatInputArea from '../chat-input-area'
import ChatInput from './input'
import StopResponding from './stopResponding' import StopResponding from './stopResponding'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { useChatContext } from '../context' import { useChatContext } from '../context'
...@@ -40,7 +41,7 @@ const Footer: FC<FooterProps> = ({ ...@@ -40,7 +41,7 @@ const Footer: FC<FooterProps> = ({
{ <StopResponding />} { <StopResponding />}
</div> </div>
{!!suggestedQuestions?.length && <TryToAsk suggestedQuestions={suggestedQuestions} isMobile={isMobile} />} {!!suggestedQuestions?.length && <TryToAsk suggestedQuestions={suggestedQuestions} isMobile={isMobile} />}
{<ChatInputArea />} {<ChatInput />}
</div> </div>
</div> </div>
) )
......
import { import { createContext, useContext, useState, useCallback, useRef, useMemo, Dispatch, SetStateAction, MutableRefObject } from 'react'
useCallback,
useRef,
useState,
} from 'react'
export const useTextAreaHeight = () => { // 定义上下文类型
interface TextAreaHeightContextType {
wrapperRef: React.RefObject<HTMLDivElement>
textareaRef: MutableRefObject<HTMLTextAreaElement>
textValueRef: React.RefObject<HTMLDivElement>
holdSpaceRef: React.RefObject<HTMLDivElement>
handleTextareaResize: () => void
isMultipleLine: boolean
setIsMultipleLine: Dispatch<SetStateAction<boolean>>
}
// 创建上下文
const TextAreaHeightContext = createContext<TextAreaHeightContextType | undefined>(undefined)
// 自定义Hook访问上下文
export const useTextAreaHeightContext = (): TextAreaHeightContextType => {
const context = useContext(TextAreaHeightContext)
if (!context) {
throw new Error('useTextAreaHeightContext must be used within a TextAreaHeightProvider')
}
return context
}
// Provider组件
export const TextAreaHeightProvider = ({ children }: { children: React.ReactNode }) => {
const wrapperRef = useRef<HTMLDivElement>(null) const wrapperRef = useRef<HTMLDivElement>(null)
const textareaRef = useRef<HTMLTextAreaElement | undefined>(undefined) const textareaRef = useRef<HTMLTextAreaElement | undefined>(undefined)
const textValueRef = useRef<HTMLDivElement>(null) const textValueRef = useRef<HTMLDivElement>(null)
...@@ -35,13 +55,18 @@ export const useTextAreaHeight = () => { ...@@ -35,13 +55,18 @@ export const useTextAreaHeight = () => {
handleComputeHeight() handleComputeHeight()
}, [handleComputeHeight]) }, [handleComputeHeight])
return { const value = useMemo(
() => ({
wrapperRef, wrapperRef,
textareaRef, textareaRef: textareaRef as any,
textValueRef, textValueRef,
holdSpaceRef, holdSpaceRef,
handleTextareaResize, handleTextareaResize,
isMultipleLine, isMultipleLine,
setIsMultipleLine setIsMultipleLine,
} }),
[handleTextareaResize, isMultipleLine],
)
return <TextAreaHeightContext.Provider value={value}>{children}</TextAreaHeightContext.Provider>
} }
import { FC, memo } from 'react'
import { FileListInChatInput } from '@/app/components/base/file-uploader'
import { useChatContext } from '@/app/components/base/chat/chat/context'
const FileList: FC = () => {
const { config } = useChatContext()
const visionConfig = config.file_upload
return <FileListInChatInput fileConfig={visionConfig!} />
}
export default memo(FileList)
import { useCallback, useEffect, useMemo, useRef, useState } from 'react' import { FileContextProvider } from '@/app/components/base/file-uploader/store'
import Textarea from 'react-textarea-autosize' import InputWrapper from './wrapper'
import { useTranslation } from 'react-i18next' import { MessageProvider } from '../context'
import Recorder from 'js-audio-recorder' import { TextAreaHeightProvider } from './context'
import { useCheckInputsForms } from '../../check-input-forms-hooks' const ChatInputAreaWrapper = () => {
import { useTextAreaHeight } from './hooks'
import Operation from './operation'
import cn from '@/utils/classnames'
import { FileListInChatInput } from '@/app/components/base/file-uploader'
import { useFile } from '@/app/components/base/file-uploader/hooks'
import { FileContextProvider, useFileStore } from '@/app/components/base/file-uploader/store'
import VoiceInput from '@/app/components/base/voice-input'
import { useToastContext } from '@/app/components/base/toast'
import FeatureBar from '@/app/components/base/features/new-feature-panel/feature-bar'
import type { FileUpload } from '@/app/components/base/features/types'
import { TransferMethod } from '@/types/app'
import { InputMessageType } from '../../../types'
import { useChatContext } from '../../context'
import Confirm from '@/app/components/base/confirm'
type ChatInputAreaProps = {
}
const ChatInputArea = ({
}: ChatInputAreaProps) => {
const { t } = useTranslation()
const { notify } = useToastContext()
const {isResponding, onSend:sendMessage, onRestart:restartChat,config} = useChatContext()
console.log('visionConfig', isResponding)
const visionConfig = config.file_upload
const speechToTextConfig = config.speech_to_text
// const sendMessage = useChatStore(state => state.sendMessage)
// const isResponding = useChatStore(state => state.isResponding)
// const restartChat = useChatStore(state => state.restartChat)
const {
wrapperRef,
textareaRef,
textValueRef,
holdSpaceRef,
handleTextareaResize,
isMultipleLine,
setIsMultipleLine,
} = useTextAreaHeight()
const [query, setQuery] = useState('')
const [showVoiceInput, setShowVoiceInput] = useState(false)
const filesStore = useFileStore()
const {
handleDragFileEnter,
handleDragFileLeave,
handleDragFileOver,
handleDropFile,
handleClipboardPasteFile,
isDragActive,
} = useFile(visionConfig!)
const { checkInputsForm } = useCheckInputsForms()
const historyRef = useRef([''])
const [currentIndex, setCurrentIndex] = useState(-1)
const isComposingRef = useRef(false)
// const { onSend } = useChatContext()
const [showConfirm, setShowConfirm] = useState<boolean>(false)
const featureBarDisabled = isResponding
const handleSend = () => {
if (isResponding) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForResponse'),
})
return
}
if (sendMessage) {
const { files, setFiles } = filesStore.getState()
if (files.find(item => item.transferMethod === TransferMethod.local_file && !item.uploadedId)) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForFileUpload'),
})
return
}
if (!query || !query.trim()) {
notify({
type: 'info',
message: t('appAnnotation.errorMessage.queryRequired'),
})
return
}
// if (checkInputsForm(inputs, inputsForm)) {
sendMessage(query, InputMessageType.BY_HAND, files)
setQuery('')
setFiles([])
// }
}
}
const handleCompositionStart = () => {
// e: React.CompositionEvent<HTMLTextAreaElement>
isComposingRef.current = true
}
const handleCompositionEnd = () => {
// safari or some browsers will trigger compositionend before keydown.
// delay 50ms for safari.
setTimeout(() => {
isComposingRef.current = false
}, 50)
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
// if isComposing, exit
if (isComposingRef.current) return
e.preventDefault()
setQuery(query.replace(/\n$/, ''))
historyRef.current.push(query)
setCurrentIndex(historyRef.current.length)
handleSend()
} else if (e.key === 'ArrowUp' && !e.shiftKey && !e.nativeEvent.isComposing && e.metaKey) {
// When the cmd + up key is pressed, output the previous element
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1)
setQuery(historyRef.current[currentIndex - 1])
}
} else if (e.key === 'ArrowDown' && !e.shiftKey && !e.nativeEvent.isComposing && e.metaKey) {
// When the cmd + down key is pressed, output the next element
if (currentIndex < historyRef.current.length - 1) {
setCurrentIndex(currentIndex + 1)
setQuery(historyRef.current[currentIndex + 1])
} else if (currentIndex === historyRef.current.length - 1) {
// If it is the last element, clear the input box
setCurrentIndex(historyRef.current.length)
setQuery('')
}
}
}
const handleShowVoiceInput = useCallback(() => {
;(Recorder as any).getPermission().then(
() => {
setShowVoiceInput(true)
},
() => {
notify({ type: 'error', message: t('common.voiceInput.notAllow') })
},
)
}, [t, notify])
const handleRestart = useCallback(() => {
if (isResponding) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForResponse'),
})
return
}
setShowConfirm(true)
}, [isResponding])
const doRestart = useCallback(() => {
restartChat()
setShowConfirm(false)
}, [])
const operation = (
<Operation
ref={holdSpaceRef}
fileConfig={visionConfig}
speechToTextConfig={speechToTextConfig}
onShowVoiceInput={handleShowVoiceInput}
onRestart={handleRestart}
onSend={handleSend}
// theme={theme}
/>
)
// const clear = <Operation ref={holdSpaceRef} onSend={handleClear} imgUrl={CLEAR_BUTTON} />
const hasQuery = useMemo(() => query.length > 0, [query])
useEffect(() => {
if (!hasQuery) {
setIsMultipleLine(false)
}
}, [hasQuery, setIsMultipleLine])
const maxRow = useMemo(() => {
return hasQuery ? undefined : 1
}, [hasQuery])
return (
<>
{showConfirm && (
<Confirm
title="重置对话"
confirmText="确认"
cancelText="取消"
content="您确定要重置此对话吗?"
isShow
onCancel={() => setShowConfirm(false)}
onConfirm={doRestart}
/>
)}
<div
className={cn(
'relative z-10 rounded-xl border border-components-chat-input-border bg-[#fff] pb-[9px] shadow-md',
isDragActive && 'border border-dashed border-components-option-card-option-selected-border',
)}>
<div className="relative max-h-[158px] overflow-y-auto overflow-x-hidden px-[9px] pt-[9px]">
<FileListInChatInput fileConfig={visionConfig!} />
<div ref={wrapperRef} className="flex items-center justify-between">
<div className="relative flex w-full grow items-center ml-8">
<div
ref={textValueRef}
className="body-lg-regular pointer-events-none invisible absolute h-auto w-auto whitespace-pre p-1 leading-6">
{query}
</div>
<Textarea
ref={ref => (textareaRef.current = ref as any)}
className={cn(
'body-lg-regular w-full resize-none bg-transparent p-1 leading-6 text-text-tertiary outline-none',
)}
placeholder={t('common.chat.inputPlaceholder') || ''}
autoFocus
maxRows={maxRow}
onResize={handleTextareaResize}
value={query}
onChange={e => {
setQuery(e.target.value)
setTimeout(handleTextareaResize, 0)
}}
onKeyDown={handleKeyDown}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
onPaste={handleClipboardPasteFile}
onDragEnter={handleDragFileEnter}
onDragLeave={handleDragFileLeave}
onDragOver={handleDragFileOver}
onDrop={handleDropFile}
/>
</div>
{!isMultipleLine && operation}
</div>
{showVoiceInput && (
<VoiceInput onCancel={() => setShowVoiceInput(false)} onConverted={text => setQuery(text)} />
)}
</div>
{isMultipleLine && <div className="px-[9px]">{operation}</div>}
</div>
</>
)
}
const ChatInputAreaWrapper = (props: ChatInputAreaProps) => {
return ( return (
<FileContextProvider> <FileContextProvider>
<ChatInputArea {...props} /> <MessageProvider>
<TextAreaHeightProvider>
<InputWrapper />
</TextAreaHeightProvider>
</MessageProvider>
</FileContextProvider> </FileContextProvider>
) )
} }
......
import { FC, memo } from 'react'
import Opration from '../operation'
import { useTextAreaHeightContext } from './context'
const MultipleLineOperation: FC = () => {
const { isMultipleLine } = useTextAreaHeightContext()
return (
<>
{isMultipleLine && (
<div className="px-[9px]">
<Opration />
</div>
)}
</>
)
}
export default memo(MultipleLineOperation)
import { Dispatch, memo, SetStateAction, use, useMemo } from 'react'
import { RiMicLine, RiSendPlane2Fill } from '@remixicon/react'
import type { EnableType } from '../../types'
import type { Theme } from '../../embedded-chatbot/theme/theme-context'
import Button from '@/app/components/base/button'
import ActionButton from '@/app/components/base/action-button'
import { FileUploaderInChatInput } from '@/app/components/base/file-uploader'
import type { FileUpload } from '@/app/components/base/features/types'
import cn from '@/utils/classnames'
import { useChatStore } from '@/app/components/base/chat/chat-with-history/store'
import {
// SELF_INPUT,
SEND_BUTTON,
CLEAR_BUTTON,
} from '@/app/components/base/chat/constants'
type OperationProps = {
fileConfig?: FileUpload
speechToTextConfig?: EnableType
onShowVoiceInput?: () => void
onSend: () => void
imgUrl?: string
theme?: Theme | null
onRestart: () => void
}
const Operation = ({
ref,
onSend,
speechToTextConfig,
onShowVoiceInput,
onRestart,
}: OperationProps & {
ref: React.RefObject<HTMLDivElement>
}) => {
return (
<div className={cn('flex shrink-0 items-center justify-end')}>
<div className="absolute left-3">
<img onClick={onRestart} src={CLEAR_BUTTON} className="size-6 hover:cursor-pointer" alt="icon" />
</div>
<div className="flex items-center pl-1" ref={ref}>
<div className="flex items-center space-x-1 pr-2">
{/* {fileConfig?.enabled && <FileUploaderInChatInput fileConfig={fileConfig} />} */}
{speechToTextConfig?.enabled && (
<ActionButton size="l" onClick={onShowVoiceInput}>
<RiMicLine className="h-5 w-5" />
</ActionButton>
)}
</div>
<img onClick={onSend} src={SEND_BUTTON} className="size-6 hover:cursor-pointer" alt="icon" />
</div>
</div>
)
}
Operation.displayName = 'Operation'
export default memo(Operation)
import React, { Children, useCallback, useEffect, useMemo, useRef, useState } from 'react'
import Textarea from 'react-textarea-autosize'
import cn from '@/utils/classnames'
import { useTranslation } from 'react-i18next'
import { useFileOperation } from '../hooks'
import { useMessageContext } from '../context'
import { useTextAreaHeightContext } from './context'
const TextInput: React.FC<{
children: React.ReactNode
}> = React.memo(({ children }) => {
const { t } = useTranslation()
const { query, setQuery, handleSend } = useMessageContext()
const historyRef = useRef([''])
const isComposingRef = useRef(false)
const [currentIndex, setCurrentIndex] = useState(-1)
const { wrapperRef, textareaRef, textValueRef, handleTextareaResize, setIsMultipleLine,isMultipleLine } = useTextAreaHeightContext()
const { handleDragFileEnter, handleDragFileLeave, handleDragFileOver, handleDropFile, handleClipboardPasteFile } =
useFileOperation()
const handleCompositionStart = () => {
// e: React.CompositionEvent<HTMLTextAreaElement>
isComposingRef.current = true
}
const handleCompositionEnd = () => {
// safari or some browsers will trigger compositionend before keydown.
// delay 50ms for safari.
setTimeout(() => {
isComposingRef.current = false
}, 50)
}
const handleKeyDown = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey && !e.nativeEvent.isComposing) {
// if isComposing, exit
if (isComposingRef.current) return
e.preventDefault()
setQuery(query.replace(/\n$/, ''))
historyRef.current.push(query)
setCurrentIndex(historyRef.current.length)
handleSend()
} else if (e.key === 'ArrowUp' && !e.shiftKey && !e.nativeEvent.isComposing && e.metaKey) {
// When the cmd + up key is pressed, output the previous element
if (currentIndex > 0) {
setCurrentIndex(currentIndex - 1)
setQuery(historyRef.current[currentIndex - 1])
}
} else if (e.key === 'ArrowDown' && !e.shiftKey && !e.nativeEvent.isComposing && e.metaKey) {
// When the cmd + down key is pressed, output the next element
if (currentIndex < historyRef.current.length - 1) {
setCurrentIndex(currentIndex + 1)
setQuery(historyRef.current[currentIndex + 1])
} else if (currentIndex === historyRef.current.length - 1) {
// If it is the last element, clear the input box
setCurrentIndex(historyRef.current.length)
setQuery('')
}
}
}
const handleInputChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
setQuery(e.target.value)
setTimeout(handleTextareaResize, 0)
}
const hasQuery = useMemo(() => query.length > 0, [query])
useEffect(() => {
if (!hasQuery) {
setIsMultipleLine(false)
}
}, [hasQuery, setIsMultipleLine])
const maxRow = useMemo(() => {
return hasQuery ? undefined : 1
}, [hasQuery])
return (
<div ref={wrapperRef} className="flex items-center justify-between">
<div className="relative flex w-full grow items-center ml-8">
<div
ref={textValueRef}
className="body-lg-regular pointer-events-none invisible absolute h-auto w-auto whitespace-pre p-1 leading-6">
{query}
</div>
<Textarea
ref={ref => (textareaRef.current = ref as any)}
className={cn(
'body-lg-regular w-full resize-none bg-transparent p-1 leading-6 text-text-tertiary outline-none',
)}
placeholder={t('common.chat.inputPlaceholder') || ''}
autoFocus
maxRows={maxRow}
onResize={handleTextareaResize}
value={query}
onChange={handleInputChange}
onKeyDown={handleKeyDown}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
onPaste={handleClipboardPasteFile}
onDragEnter={handleDragFileEnter}
onDragLeave={handleDragFileLeave}
onDragOver={handleDragFileOver}
onDrop={handleDropFile}
/>
</div>
{!isMultipleLine && children}
</div>
)
})
export default TextInput
import { FC, memo } from 'react'
import cn from '@/utils/classnames'
import { useFileOperation } from '../hooks'
import FileList from './file-list'
import TextInput from './text-input'
import VoiceInput from '../voice-input'
import Opration from '../operation'
import MultipleLineOperation from './multiple-line-operation'
const InputWrapper: FC = () => {
const { isDragActive } = useFileOperation()
return (
<div
className={cn(
'relative z-10 rounded-xl border border-components-chat-input-border bg-[#fff] pb-[9px] shadow-md',
isDragActive && 'border border-dashed border-components-option-card-option-selected-border',
)}>
<div className="relative max-h-[158px] overflow-y-auto overflow-x-hidden px-[9px] pt-[9px]">
<FileList />
<TextInput>
<Opration />
</TextInput>
<VoiceInput />
</div>
<MultipleLineOperation />
</div>
)
}
export default memo(InputWrapper)
import { FC, memo } from 'react'
import cn from '@/utils/classnames'
// import { useTextAreaHeight } from '../hooks'
import ReStart from './restart'
import SpeechToText from './speech-to-text'
import Send from './send'
import { useTextAreaHeightContext } from '../input/context'
const Operation: FC = () => {
const { holdSpaceRef } = useTextAreaHeightContext()
return (
<div className={cn('flex shrink-0 items-center justify-end')}>
<ReStart />
<div className="flex items-center pl-1" ref={holdSpaceRef}>
<div className="flex items-center space-x-1 pr-2">
{/* {fileConfig?.enabled && <FileUploaderInChatInput fileConfig={fileConfig} />} */}
<SpeechToText />
</div>
<Send />
</div>
</div>
)
}
Operation.displayName = 'Operation'
export default memo(Operation)
import Confirm from '@/app/components/base/confirm'
import { useToastContext } from '@/app/components/base/toast'
import { FC, memo, useCallback, useState } from 'react'
import { useChatContext } from '../../context'
import { useTranslation } from 'react-i18next'
import { CLEAR_BUTTON } from '@/app/components/base/chat/constants'
const ReStart: FC = () => {
const { t } = useTranslation()
const { notify } = useToastContext()
const { isResponding, onRestart: restartChat } = useChatContext()
const [showConfirm, setShowConfirm] = useState<boolean>(false)
const handleRestart = useCallback(() => {
if (isResponding) {
notify({
type: 'info',
message: t('appDebug.errorMessage.waitForResponse'),
})
return
}
setShowConfirm(true)
}, [isResponding])
const doRestart = useCallback(() => {
restartChat()
setShowConfirm(false)
}, [])
return (
<>
{showConfirm && (
<Confirm
title="重置对话"
confirmText="确认"
cancelText="取消"
content="您确定要重置此对话吗?"
isShow
onCancel={() => setShowConfirm(false)}
onConfirm={doRestart}
/>
)}
<div className="absolute left-3">
<img onClick={handleRestart} src={CLEAR_BUTTON} className="size-6 hover:cursor-pointer" alt="icon" />
</div>
</>
)
}
export default memo(ReStart)
import { FC, memo } from 'react'
import { SEND_BUTTON } from '@/app/components/base/chat/constants'
import { useMessageContext } from '../context'
const Send: FC = () => {
const { handleSend } = useMessageContext()
return <img onClick={handleSend} src={SEND_BUTTON} className="size-6 hover:cursor-pointer" alt="icon" />
}
export default memo(Send)
import { useToastContext } from '@/app/components/base/toast'
import { FC, memo, useCallback, useState } from 'react'
import { useChatContext } from '../../context'
import { useTranslation } from 'react-i18next'
import ActionButton from '@/app/components/base/action-button'
import { RiMicLine } from '@remixicon/react'
import Recorder from 'js-audio-recorder'
import { useMessageContext } from '../context'
const SpeechToText: FC = () => {
const { t } = useTranslation()
const { notify } = useToastContext()
const { config } = useChatContext()
const speechToTextConfig = config.speech_to_text
const { setShowVoiceInput } = useMessageContext()
const handleShowVoiceInput = useCallback(() => {
;(Recorder as any).getPermission().then(
() => {
setShowVoiceInput(true)
},
() => {
notify({ type: 'error', message: t('common.voiceInput.notAllow') })
},
)
}, [t, notify])
return (
<>
{speechToTextConfig?.enabled && (
<ActionButton size="l" onClick={handleShowVoiceInput}>
<RiMicLine className="h-5 w-5" />
</ActionButton>
)}
</>
)
}
export default memo(SpeechToText)
import { FC, memo } from 'react'
import VoiceInput from '@/app/components/base/voice-input'
import { useMessageContext } from '../context'
const Voice: FC = () => {
const { showVoiceInput, setShowVoiceInput, setQuery } = useMessageContext()
return (
<>
{showVoiceInput && <VoiceInput onCancel={() => setShowVoiceInput(false)} onConverted={text => setQuery(text)} />}
</>
)
}
export default memo(Voice)
...@@ -76,7 +76,6 @@ const Chat: FC<ChatProps> = () => { ...@@ -76,7 +76,6 @@ const Chat: FC<ChatProps> = () => {
container.style.paddingBottom = `${footer.clientHeight}px` container.style.paddingBottom = `${footer.clientHeight}px`
handleScrollToBottom() handleScrollToBottom()
}, [handleScrollToBottom]) }, [handleScrollToBottom])
console.log('installedAppInfo')
return ( return (
<div className="relative h-full"> <div className="relative h-full">
<div <div
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!