-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ refactor: refactor the input area to suit the files upload feature…
… (#442) * ♻️ refactor: refactor the input area from ui * ♻️ refactor: refactor ActionBar to a configurable stage
- Loading branch information
Showing
22 changed files
with
559 additions
and
259 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { TextArea } from '@lobehub/ui'; | ||
import { createStyles } from 'antd-style'; | ||
import { memo, useRef } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useSessionStore } from '@/store/session'; | ||
|
||
import { useSendMessage } from './useSend'; | ||
|
||
const useStyles = createStyles(({ css }) => { | ||
return { | ||
textarea: css` | ||
height: 100% !important; | ||
padding: 0 24px; | ||
line-height: 1.5; | ||
`, | ||
textareaContainer: css` | ||
position: relative; | ||
flex: 1; | ||
`, | ||
}; | ||
}); | ||
|
||
const InputArea = memo(() => { | ||
const { t } = useTranslation('common'); | ||
const isChineseInput = useRef(false); | ||
|
||
const { cx, styles } = useStyles(); | ||
|
||
const [loading, message, updateInputMessage] = useSessionStore((s) => [ | ||
!!s.chatLoadingId, | ||
s.inputMessage, | ||
s.updateInputMessage, | ||
]); | ||
|
||
const handleSend = useSendMessage(); | ||
|
||
return ( | ||
<div className={cx(styles.textareaContainer)}> | ||
<TextArea | ||
className={styles.textarea} | ||
onBlur={(e) => { | ||
updateInputMessage(e.target.value); | ||
}} | ||
onChange={(e) => { | ||
updateInputMessage(e.target.value); | ||
}} | ||
onCompositionEnd={() => { | ||
isChineseInput.current = false; | ||
}} | ||
onCompositionStart={() => { | ||
isChineseInput.current = true; | ||
}} | ||
onPressEnter={(e) => { | ||
if (!loading && !e.shiftKey && !isChineseInput.current) { | ||
e.preventDefault(); | ||
handleSend(); | ||
} | ||
}} | ||
placeholder={t('sendPlaceholder', { ns: 'chat' })} | ||
resize={false} | ||
type="pure" | ||
value={message} | ||
/> | ||
</div> | ||
); | ||
}); | ||
|
||
export default InputArea; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { useCallback } from 'react'; | ||
|
||
import { useSessionStore } from '@/store/session'; | ||
|
||
export const useSendMessage = () => { | ||
const [sendMessage, updateInputMessage] = useSessionStore((s) => [ | ||
s.sendMessage, | ||
s.updateInputMessage, | ||
]); | ||
|
||
return useCallback(() => { | ||
const store = useSessionStore.getState(); | ||
if (!!store.chatLoadingId) return; | ||
sendMessage(store.inputMessage); | ||
updateInputMessage(''); | ||
}, []); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Icon, Input } from '@lobehub/ui'; | ||
import { Button, type InputRef } from 'antd'; | ||
import { Loader2, SendHorizonal } from 'lucide-react'; | ||
import { forwardRef, useCallback, useRef } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
import useControlledState from 'use-merge-value'; | ||
|
||
import ActionBar from '@/app/chat/features/ChatInput/ActionBar'; | ||
import SaveTopic from '@/app/chat/features/ChatInput/Topic'; | ||
|
||
import { useStyles } from './style.mobile'; | ||
|
||
export type ChatInputAreaMobile = { | ||
loading?: boolean; | ||
onChange?: (value: string) => void; | ||
onSend?: (value: string) => void; | ||
onStop?: () => void; | ||
value?: string; | ||
}; | ||
|
||
const ChatInputArea = forwardRef<InputRef, ChatInputAreaMobile>( | ||
({ onSend, loading, onChange, onStop, value }) => { | ||
const { t } = useTranslation('chat'); | ||
const [currentValue, setCurrentValue] = useControlledState<string>('', { | ||
onChange: onChange, | ||
value, | ||
}); | ||
const { cx, styles } = useStyles(); | ||
const isChineseInput = useRef(false); | ||
|
||
const handleSend = useCallback(() => { | ||
if (loading) return; | ||
if (onSend) onSend(currentValue); | ||
setCurrentValue(''); | ||
}, [currentValue]); | ||
|
||
return ( | ||
<Flexbox className={cx(styles.container)} gap={12}> | ||
<ActionBar rightAreaStartRender={<SaveTopic />} /> | ||
<Flexbox className={styles.inner} gap={8} horizontal> | ||
<Input | ||
className={cx(styles.input)} | ||
onBlur={(e) => { | ||
setCurrentValue(e.target.value); | ||
}} | ||
onChange={(e) => { | ||
setCurrentValue(e.target.value); | ||
}} | ||
onCompositionEnd={() => { | ||
isChineseInput.current = false; | ||
}} | ||
onCompositionStart={() => { | ||
isChineseInput.current = true; | ||
}} | ||
onPressEnter={(e) => { | ||
if (!loading && !e.shiftKey && !isChineseInput.current) { | ||
e.preventDefault(); | ||
handleSend(); | ||
} | ||
}} | ||
placeholder={t('sendPlaceholder')} | ||
type={'block'} | ||
value={currentValue} | ||
/> | ||
<div> | ||
{loading ? ( | ||
<Button icon={loading && <Icon icon={Loader2} spin />} onClick={onStop} /> | ||
) : ( | ||
<Button icon={<Icon icon={SendHorizonal} />} onClick={handleSend} type={'primary'} /> | ||
)} | ||
</div> | ||
</Flexbox> | ||
</Flexbox> | ||
); | ||
}, | ||
); | ||
|
||
export default ChatInputArea; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { createStyles } from 'antd-style'; | ||
import { rgba } from 'polished'; | ||
|
||
export const useStyles = createStyles(({ css, token }) => { | ||
return { | ||
container: css` | ||
padding: 12px 0; | ||
background: ${token.colorBgLayout}; | ||
border-top: 1px solid ${rgba(token.colorBorder, 0.25)}; | ||
`, | ||
inner: css` | ||
padding: 0 16px; | ||
`, | ||
input: css` | ||
background: ${token.colorFillSecondary} !important; | ||
border: none !important; | ||
`, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { ActionIcon } from '@lobehub/ui'; | ||
import { Popconfirm } from 'antd'; | ||
import { Eraser } from 'lucide-react'; | ||
import { memo } from 'react'; | ||
import { useHotkeys } from 'react-hotkeys-hook'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import HotKeys from '@/components/HotKeys'; | ||
import { CLEAN_MESSAGE_KEY, PREFIX_KEY } from '@/const/hotkeys'; | ||
import { useSessionStore } from '@/store/session'; | ||
|
||
const Clear = memo(() => { | ||
const { t } = useTranslation('setting'); | ||
const [clearMessage] = useSessionStore((s) => [s.clearMessage, s.updateAgentConfig]); | ||
const hotkeys = [PREFIX_KEY, CLEAN_MESSAGE_KEY].join('+'); | ||
|
||
useHotkeys(hotkeys, clearMessage, { | ||
preventDefault: true, | ||
}); | ||
|
||
return ( | ||
<Popconfirm | ||
cancelText={t('cancel', { ns: 'common' })} | ||
okButtonProps={{ danger: true }} | ||
okText={t('ok', { ns: 'common' })} | ||
onConfirm={() => clearMessage()} | ||
placement={'topRight'} | ||
title={t('confirmClearCurrentMessages', { ns: 'chat' })} | ||
> | ||
<ActionIcon | ||
icon={Eraser} | ||
placement={'bottom'} | ||
title={(<HotKeys desc={t('clearCurrentMessages', { ns: 'chat' })} keys={hotkeys} />) as any} | ||
/> | ||
</Popconfirm> | ||
); | ||
}); | ||
|
||
export default Clear; |
Oops, something went wrong.