Skip to content
This repository has been archived by the owner on Sep 23, 2024. It is now read-only.

Commit

Permalink
enhancement: reset textarea window size when sending a message
Browse files Browse the repository at this point in the history
  • Loading branch information
clement2026 committed Nov 12, 2023
1 parent 0d3b8d4 commit d6855a4
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
10 changes: 8 additions & 2 deletions src/home/chat-window/message-list/message-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import ErrorBoundary from "../compnent/error-boundary.tsx"
import {maxLoadedVoice} from "../../../config.ts"
import {cx} from "../../../util/util.tsx"
import {useSnapshot} from "valtio/react"
import {addToPlayList, clearPlayList} from "../../../state/control-state.ts"
import {addToPlayList, clearPlayList, controlState} from "../../../state/control-state.ts"
import {HiOutlineChevronDown} from "react-icons/hi2"
import {Row} from "./row.tsx"
import {subscribeKey} from "valtio/utils";
Expand All @@ -21,6 +21,7 @@ type MLProps = {

export const MessageList: React.FC<MLProps> = ({chatProxy}) => {
// console.info("MessageList rendered", new Date().toLocaleString())
const {sendingMessageSignal} = useSnapshot(controlState)
const [messages, setMessages] = useState<Message[]>([])
const containerRef = useRef<HTMLDivElement>(null)
const scrollEndRef = useRef<HTMLDivElement>(null)
Expand Down Expand Up @@ -61,11 +62,16 @@ export const MessageList: React.FC<MLProps> = ({chatProxy}) => {

const scrollToBottom = throttle((behavior?: 'instant' | 'smooth') => {
if (!layoutState.isPAPinning && !layoutState.isPAFloating && scrollEndRef.current) {
// don't scroll message list because it pushed promptory outside screen.
// don't scroll message list if UI of Preview is showing, in case UI of Preview is pushed outside screen.
scrollEndRef.current.scrollIntoView({behavior: behavior ?? "instant"})
}
}, 200)

useEffect(() => {
scrollToBottom()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [sendingMessageSignal]);

useEffect(() => {
return subscribe(chatProxy.messages, () => {
const len = chatProxy.messages.length
Expand Down
29 changes: 19 additions & 10 deletions src/home/chat-window/text-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ const TextArea: React.FC<Props> = ({chatProxy}) => {
event.stopPropagation()
}, [])

const autoGrowHeight = useCallback((e: React.BaseSyntheticEvent) => {
e.currentTarget.style.height = "auto"
e.currentTarget.style.height = `${e.currentTarget.scrollHeight}px`
if (textAreaRef.current) {
if (textAreaRef.current.scrollHeight > textAreaRef.current.clientHeight) {
setInputAreaIsLarge(true)
}
}
}, [])

const resetHeight = useCallback(() => {
if (textAreaRef.current) {
textAreaRef.current.focus()
textAreaRef.current.style.height = `0px`
}
}, [])

const sendAndClearText = useCallback(() => {
if (sendButtonRef.current) {
sendButtonRef.current.blur()
Expand All @@ -38,7 +55,8 @@ const TextArea: React.FC<Props> = ({chatProxy}) => {
controlState.sendingMessageSignal++
chatProxy.inputText = ""
}
textAreaRef.current?.focus()
// reset window size of <textarea> after clearing text
resetHeight()
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])

Expand Down Expand Up @@ -114,15 +132,6 @@ const TextArea: React.FC<Props> = ({chatProxy}) => {
}
}, [inputAreaIsLarge])

const autoGrowHeight = useCallback((e: React.BaseSyntheticEvent) => {
e.currentTarget.style.height = "auto"
e.currentTarget.style.height = `${e.currentTarget.scrollHeight}px`
if (textAreaRef.current) {
if (textAreaRef.current.scrollHeight > textAreaRef.current.clientHeight) {
setInputAreaIsLarge(true)
}
}
}, [])

return (<div className="flex flex-col items-center w-full mt-auto bottom-0 max-w-4xl">
<button
Expand Down

0 comments on commit d6855a4

Please sign in to comment.