Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More smol fixes #235

Merged
merged 1 commit into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions frontend/src/components/Conversation/Conversation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ export const Conversation = () => {
);

const messageListRef = useRef<HTMLDivElement | null>(null);
const expandingInputRef = useRef<HTMLTextAreaElement | null>(null);

const currConversation = conversationsData?.find(
(conv) => conv.id === params.conversationId
);
Expand Down Expand Up @@ -175,32 +177,35 @@ export const Conversation = () => {
</Transition>

<div className="fixed bottom-0 left-0 lg:left-72 right-0 flex flex-col items-center justify-center backdrop-blur-md pt-0">
{messages.length === 0 && (
{messages.length === 0 && !currentConversationIsQuerying && (
<div className="w-full md:max-w-3xl grid grid-cols-1 md:grid-cols-2 gap-2 justify-between px-2 sm:px-3 my-4">
{templateMessages.map((template) => (
<MessageTemplate
key={template.title}
title={template.title}
text={template.text}
onClick={() =>
onClick={() => {
sendMessageMutation({
message: template.message,
conversationId: params.conversationId ?? "",
})
}
});
expandingInputRef.current?.focus();
}}
/>
))}
</div>
)}
<div className="w-full md:max-w-3xl flex flex-col justify-center items-center pb-4 ml-2 mr-2 mb-2 pl-2 pr-2">
<ExpandingInput
onSubmit={(message: string) =>
onSubmit={(message: string) => {
sendMessageMutation({
message,
conversationId: params.conversationId ?? "",
})
}
});
expandingInputRef.current?.focus();
}}
disabled={currentConversationIsQuerying}
ref={expandingInputRef}
/>
<p className="text-gray-400 text-sm">
Current Connection: {currConnection?.name}
Expand Down
183 changes: 93 additions & 90 deletions frontend/src/components/Conversation/ExpandingInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
ShieldExclamationIcon,
ShieldCheckIcon,
} from "@heroicons/react/24/outline";
import { SetStateAction, useRef, useState } from "react";
import { SetStateAction, forwardRef, useRef, useState } from "react";

import { Switch, SwitchField, SwitchGroup } from "../Catalyst/switch";
import { Description, Fieldset, Label } from "../Catalyst/fieldset";
Expand All @@ -19,7 +19,6 @@ import {
import { useClickOutside } from "../Library/utils";
import { useQuery } from "@tanstack/react-query";


type ExpandingInputProps = {
onSubmit: (value: string) => void;
disabled: boolean;
Expand Down Expand Up @@ -71,9 +70,7 @@ const MessageSettingsPopup: React.FC<MessageSettingsPopupProps> = ({
<Fieldset>
<SwitchGroup>
<SwitchField>
<Label className="flex items-center">
Data Security{" "}
</Label>
<Label className="flex items-center">Data Security </Label>
<Description>Hide your data from the AI model</Description>
<Switch
color="green"
Expand All @@ -90,103 +87,109 @@ const MessageSettingsPopup: React.FC<MessageSettingsPopupProps> = ({
</Transition>
);
};
const ExpandingInput: React.FC<ExpandingInputProps> = ({
onSubmit,
disabled,
}) => {
const [inputValue, setInputValue] = useState("");
const [messageSettingsShown, setMessageSettingsShown] = useState(false);
const currConnection = useGetRelatedConnection();
const { data: messageOptions } = useQuery(
getMessageOptions(currConnection?.id)
);
const settingsCogRef = useRef<HTMLDivElement | null>(null);

const handleChange = (e: {
target: {
value: SetStateAction<string>;
style: { height: string };
scrollHeight: any;
const ExpandingInput = forwardRef<HTMLTextAreaElement, ExpandingInputProps>(
({ onSubmit, disabled }, ref) => {
const [inputValue, setInputValue] = useState("");
const [messageSettingsShown, setMessageSettingsShown] = useState(false);
const currConnection = useGetRelatedConnection();
const { data: messageOptions } = useQuery(
getMessageOptions(currConnection?.id)
);
const settingsCogRef = useRef<HTMLDivElement | null>(null);

const handleChange = (e: {
target: {
value: SetStateAction<string>;
style: { height: string };
scrollHeight: any;
};
}) => {
setInputValue(e.target.value);
e.target.style.height = "auto"; // Reset textarea height
e.target.style.height = `${e.target.scrollHeight}px`; // Set textarea height based on content
};
}) => {
setInputValue(e.target.value);
e.target.style.height = "auto"; // Reset textarea height
e.target.style.height = `${e.target.scrollHeight}px`; // Set textarea height based on content
};

const handleSubmit = () => {
if (disabled) return;
if (inputValue.length === 0) return;
onSubmit(inputValue);
setInputValue("");
};
const handleSubmit = () => {
if (disabled) return;
if (inputValue.length === 0) return;
onSubmit(inputValue);
setInputValue("");
};

const handleKeyPress = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();
const handleKeyPress = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSubmit();

// Reset textarea height
e.currentTarget.style.height = "auto";
}
};
// Reset textarea height
e.currentTarget.style.height = "auto";
}
};

return (
<div className="flex flex-col justify-center w-full relative mb-4">
<textarea
disabled={disabled}
name="email"
id="email"
className={classNames(
disabled ? "placeholder:text-gray-600" : "placeholder:text-gray-400",
"block rounded-xl border p-4 text-gray-900 shadow-sm sm:text-md sm:leading-6 resize-none dark:text-gray-200 dark:bg-gray-900 dark:border-gray-600 pl-12 sm:pl-20 pr-12 overflow-y-hidden mr-1"
)}
style={{ height: "auto" }}
rows={1}
placeholder="Enter your message here..."
value={inputValue}
onChange={handleChange}
onKeyDown={handleKeyPress}
/>
<div className="absolute left-0 top-0 w-full">
<MessageSettingsPopup
isShown={messageSettingsShown}
setIsShown={setMessageSettingsShown}
settingsCogRef={settingsCogRef}
return (
<div className="flex flex-col justify-center w-full relative mb-4">
<textarea
name="email"
id="email"
className={classNames(
disabled
? "placeholder:text-gray-600 text-gray-800 dark:text-gray-400 dark:bg-gray-800 focus:ring-0"
: "placeholder:text-gray-400 text-gray-900 dark:text-gray-200 dark:bg-gray-900",
"block rounded-xl border p-4 shadow-sm sm:text-md sm:leading-6 resize-none dark:border-gray-600 pl-12 sm:pl-20 pr-12 overflow-y-hidden mr-1"
)}
style={{ height: "auto" }}
rows={1}
placeholder="Enter your message here..."
value={inputValue}
onChange={handleChange}
onKeyDown={handleKeyPress}
ref={ref}
/>
</div>
<div
className={
"group absolute flex items-center left-0"
}
>
<div ref={settingsCogRef} onClick={() => setMessageSettingsShown((prev) => !prev)} className="hover:cursor-pointer hover:bg-white/10 dark:text-gray-400 ml-2 p-1 rounded-md transition-all duration-150">
<Cog6ToothIcon
className={"hover:-rotate-6 h-6 w-6 [&>path]:stroke-[2]"}
<div className="absolute left-0 top-0 w-full">
<MessageSettingsPopup
isShown={messageSettingsShown}
setIsShown={setMessageSettingsShown}
settingsCogRef={settingsCogRef}
/>
</div>
<div className="dark:text-gray-400 ml-1 invisible sm:visible">
{messageOptions?.secure_data ? <ShieldCheckIcon className="h-6 w-6 text-green-500 [&>path]:stroke-[2]" /> : <ShieldExclamationIcon className="h-6 w-6 text-gray-400 [&>path]:stroke-[2]" />}
<div className={"group absolute flex items-center left-0"}>
<div
ref={settingsCogRef}
onClick={() => setMessageSettingsShown((prev) => !prev)}
className="hover:cursor-pointer hover:bg-white/10 dark:text-gray-400 ml-2 p-1 rounded-md transition-all duration-150"
>
<Cog6ToothIcon
className={"hover:-rotate-6 h-6 w-6 [&>path]:stroke-[2]"}
/>
</div>
<div className="dark:text-gray-400 ml-1 invisible sm:visible">
{messageOptions?.secure_data ? (
<ShieldCheckIcon className="h-6 w-6 text-green-500 [&>path]:stroke-[2]" />
) : (
<ShieldExclamationIcon className="h-6 w-6 text-gray-400 [&>path]:stroke-[2]" />
)}
</div>
</div>
</div>
<div
onClick={handleSubmit}
className={classNames(
inputValue.length > 0
? "dark:text-gray-700 dark:bg-gray-300 dark:hover:cursor-pointer"
: "",
"group absolute right-0 mr-4 -rotate-90 dark:text-gray-400 p-1 rounded-md transition-all duration-150"
)}
>
<PaperAirplaneIcon
<div
onClick={handleSubmit}
className={classNames(
inputValue.length > 0 ? "group-hover:-rotate-6" : "",
"h-6 w-6 [&>path]:stroke-[2]"
inputValue.length > 0 && !disabled
? "dark:text-gray-700 dark:bg-gray-300 dark:hover:cursor-pointer"
: "",
"group absolute right-0 mr-4 -rotate-90 dark:text-gray-400 p-1 rounded-md transition-all duration-150"
)}
></PaperAirplaneIcon>
>
<PaperAirplaneIcon
className={classNames(
inputValue.length > 0 ? "group-hover:-rotate-6" : "",
"h-6 w-6 [&>path]:stroke-[2]"
)}
></PaperAirplaneIcon>
</div>
</div>
</div>
);
};
);
}
);

export default ExpandingInput;
Loading