Skip to content
Snippets Groups Projects
Unverified Commit fd626b14 authored by Sean Hatfield's avatar Sean Hatfield Committed by GitHub
Browse files

[FEAT] Improve UX of PromptInput (#939)

improve UX of PromptInput + cleanup props
parent 45f50ce1
No related branches found
No related tags found
No related merge requests found
import React, { useState, useRef } from "react"; import React, { useState, useRef, useEffect } from "react";
import SlashCommandsButton, { import SlashCommandsButton, {
SlashCommands, SlashCommands,
useSlashCommands, useSlashCommands,
...@@ -7,9 +7,7 @@ import { isMobile } from "react-device-detect"; ...@@ -7,9 +7,7 @@ import { isMobile } from "react-device-detect";
import debounce from "lodash.debounce"; import debounce from "lodash.debounce";
import { PaperPlaneRight } from "@phosphor-icons/react"; import { PaperPlaneRight } from "@phosphor-icons/react";
import StopGenerationButton from "./StopGenerationButton"; import StopGenerationButton from "./StopGenerationButton";
export default function PromptInput({ export default function PromptInput({
workspace,
message, message,
submit, submit,
onChange, onChange,
...@@ -19,13 +17,27 @@ export default function PromptInput({ ...@@ -19,13 +17,27 @@ export default function PromptInput({
}) { }) {
const { showSlashCommand, setShowSlashCommand } = useSlashCommands(); const { showSlashCommand, setShowSlashCommand } = useSlashCommands();
const formRef = useRef(null); const formRef = useRef(null);
const textareaRef = useRef(null);
const [_, setFocused] = useState(false); const [_, setFocused] = useState(false);
useEffect(() => {
if (!inputDisabled && textareaRef.current) {
textareaRef.current.focus();
}
resetTextAreaHeight();
}, [inputDisabled]);
const handleSubmit = (e) => { const handleSubmit = (e) => {
setFocused(false); setFocused(false);
submit(e); submit(e);
}; };
const resetTextAreaHeight = () => {
if (textareaRef.current) {
textareaRef.current.style.height = "auto";
}
};
const checkForSlash = (e) => { const checkForSlash = (e) => {
const input = e.target.value; const input = e.target.value;
if (input === "/") setShowSlashCommand(true); if (input === "/") setShowSlashCommand(true);
...@@ -44,14 +56,12 @@ export default function PromptInput({ ...@@ -44,14 +56,12 @@ export default function PromptInput({
const adjustTextArea = (event) => { const adjustTextArea = (event) => {
if (isMobile) return false; if (isMobile) return false;
const element = event.target; const element = event.target;
element.style.height = "1px"; element.style.height = "auto";
element.style.height = element.style.height = `${element.scrollHeight}px`;
event.target.value.length !== 0
? 25 + element.scrollHeight + "px"
: "1px";
}; };
const watchForSlash = debounce(checkForSlash, 300); const watchForSlash = debounce(checkForSlash, 300);
return ( return (
<div className="w-full fixed md:absolute bottom-0 left-0 z-10 md:z-0 flex justify-center items-center"> <div className="w-full fixed md:absolute bottom-0 left-0 z-10 md:z-0 flex justify-center items-center">
<SlashCommands <SlashCommands
...@@ -67,12 +77,13 @@ export default function PromptInput({ ...@@ -67,12 +77,13 @@ export default function PromptInput({
<div className="w-[600px] bg-main-gradient shadow-2xl border border-white/50 rounded-2xl flex flex-col px-4 overflow-hidden"> <div className="w-[600px] bg-main-gradient shadow-2xl border border-white/50 rounded-2xl flex flex-col px-4 overflow-hidden">
<div className="flex items-center w-full border-b-2 border-gray-500/50"> <div className="flex items-center w-full border-b-2 border-gray-500/50">
<textarea <textarea
onKeyUp={adjustTextArea} ref={textareaRef}
onKeyDown={captureEnter}
onChange={(e) => { onChange={(e) => {
onChange(e); onChange(e);
watchForSlash(e); watchForSlash(e);
adjustTextArea(e);
}} }}
onKeyDown={captureEnter}
required={true} required={true}
disabled={inputDisabled} disabled={inputDisabled}
onFocus={() => setFocused(true)} onFocus={() => setFocused(true)}
......
...@@ -114,7 +114,6 @@ export default function ChatContainer({ workspace, knownHistory = [] }) { ...@@ -114,7 +114,6 @@ export default function ChatContainer({ workspace, knownHistory = [] }) {
sendCommand={sendCommand} sendCommand={sendCommand}
/> />
<PromptInput <PromptInput
workspace={workspace}
message={message} message={message}
submit={handleSubmit} submit={handleSubmit}
onChange={handleMessageChange} onChange={handleMessageChange}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment