From 18347953f88b39dbb891b77f17e5419e3a92e3d9 Mon Sep 17 00:00:00 2001 From: Lucas Janon Date: Tue, 30 Jul 2024 14:45:04 -0300 Subject: [PATCH] UI/UX improvements & cleanup --- src/components/Cmdk/components/Cmdk.tsx | 5 +- src/components/Cmdk/components/CmdkChat.tsx | 88 +++++---------------- 2 files changed, 24 insertions(+), 69 deletions(-) diff --git a/src/components/Cmdk/components/Cmdk.tsx b/src/components/Cmdk/components/Cmdk.tsx index e0e2105c..71d01b2c 100644 --- a/src/components/Cmdk/components/Cmdk.tsx +++ b/src/components/Cmdk/components/Cmdk.tsx @@ -176,6 +176,7 @@ export const Cmdk: React.FC = ({ isOpen, setIsCmdkOpen }) => { const isHome = activePage === "home"; const onValueChange = React.useCallback((value: string) => { + console.log(value); setInputValue(value); }, []); @@ -238,6 +239,7 @@ export const Cmdk: React.FC = ({ isOpen, setIsCmdkOpen }) => { popPage(); } }} + shouldFilter={false} > {activePage === "chat" && (
@@ -318,7 +320,6 @@ function Home({ onSelect={() => { goToChat(); }} - value={inputValue} > Chat with the docs @@ -342,7 +343,7 @@ function Home({ router.push("/"); }} > - + Go to "Home" section ({ - "& .MuiOutlinedInput-root": { - fieldset: { - borderColor: `${twTheme.colors.green[900]}`, - }, - "&:not(.Mui-disabled):hover fieldset": { - borderColor: `${twTheme.colors.green[600]} !important`, - }, - "&.Mui-disabled fieldset": { - borderColor: twTheme.colors.grey[700], - }, - }, -})); - import { Command } from "cmdk"; +import React, { useEffect, useRef } from "react"; import { cmdkChatQuestions } from "../cmdk.constants"; import { ArrowUpIcon } from "./ArrowUpIcon"; import { LoadingDots } from "./LoadingDots"; import { MarkdownMessage } from "./MarkdownMesage"; -export enum MessageRole { - User = "user", - Assistant = "assistant", -} - -export enum MessageStatus { - Pending = "pending", - InProgress = "in-progress", - Complete = "complete", -} - -export interface Message { - role: MessageRole; - content: string; - status: MessageStatus; -} - -interface NewMessageAction { - type: "new"; - message: Message; -} - -interface UpdateMessageAction { - type: "update"; - index: number; - message: Partial; -} - -interface AppendContentAction { - type: "append-content"; - index: number; - content: string; -} - -interface ResetAction { - type: "reset"; -} - -type MessageAction = NewMessageAction | UpdateMessageAction | AppendContentAction | ResetAction; - -const AssistantMessage: React.FC<{ children: React.ReactNode; messageClasses?: string }> = ({ +const AssistantMessage: React.FC<{ children: React.ReactNode; className?: string; messageClasses?: string }> = ({ children, + className, messageClasses, }) => { return ( -
+
@@ -81,10 +24,15 @@ const AssistantMessage: React.FC<{ children: React.ReactNode; messageClasses?: s }; export const CmdkChat: React.FC = ({ initialValue, setCmdkInputValue }) => { + const inputRef = useRef(null); const { messages, append, handleSubmit, input, handleInputChange, error, isLoading, setInput } = useChat({}); const isLoadingAssistantMessage = isLoading; + useEffect(() => { + if (inputRef.current) inputRef.current.focus(); + }, []); + useEffect(() => { if (initialValue && input.length === 0) { setInput(initialValue); @@ -107,7 +55,7 @@ export const CmdkChat: React.FC = ({ initialValue, setCmdkInputVa {!error && messages.map((message, index) => { switch (message.role) { - case MessageRole.User: + case "user": return (
= ({ initialValue, setCmdkInputVa
); - case MessageRole.Assistant: + case "assistant": return ( - + ); @@ -143,11 +95,11 @@ export const CmdkChat: React.FC = ({ initialValue, setCmdkInputVa })} {isLoadingAssistantMessage && Boolean(messages.length) && messages[messages.length - 1]?.role === "user" && ( - + )} - {messages.length === 0 && input.length === 0 && ( + {messages.length === 0 && ( {cmdkChatQuestions.map((question) => { const key = question.replace(/\s+/g, "_"); @@ -172,6 +124,7 @@ export const CmdkChat: React.FC = ({ initialValue, setCmdkInputVa
= ({ initialValue, setCmdkInputVa if (isLoading) { return; } - handleSubmit(e as any); + handleSubmit(e); return; case "Backspace": e.stopPropagation(); @@ -212,6 +165,7 @@ export const CmdkChat: React.FC = ({ initialValue, setCmdkInputVa "dark:bg-grey-600 bg-grey-200": input.length === 0, } )} + onClick={handleSubmit} > 0} className="w-2 h-2" />