From f75b238ebeebb09950bcf4a208bc653a1cc86493 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Sun, 9 Jul 2023 17:51:42 +0800 Subject: [PATCH] feat: generate chat suggestions for user --- app/components/chat.module.scss | 18 +++ app/components/chat.tsx | 29 ++++- app/components/home.tsx | 2 + app/store/chat.ts | 224 ++++++++++++++++++++------------ 4 files changed, 190 insertions(+), 83 deletions(-) diff --git a/app/components/chat.module.scss b/app/components/chat.module.scss index 99b2d0228..ec8f0c687 100644 --- a/app/components/chat.module.scss +++ b/app/components/chat.module.scss @@ -475,3 +475,21 @@ bottom: 30px; } } + +.chat-suggestions { + display: flex; + flex-direction: column; + + .chat-suggestion { + display: inline; + white-space: nowrap; + border-radius: 20px; + font-size: 12px; + background-color: var(--white); + color: var(--black); + border: var(--border-in-light); + padding: 4px 10px; + animation: slide-in ease 0.3s; + margin-bottom: 5px; + } +} diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 02c0dd920..5be87ca53 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -606,6 +606,8 @@ export function Chat() { } }; + const [suggestions, setSuggestions] = useState([]); + const doSubmit = (userInput: string) => { if (userInput.trim() === "") return; const matchCommand = chatCommands.match(userInput); @@ -616,7 +618,13 @@ export function Chat() { return; } setIsLoading(true); - chatStore.onUserInput(userInput).then(() => setIsLoading(false)); + setSuggestions([]); + chatStore.onUserInput(userInput).then(() => { + setIsLoading(false); + chatStore + .getSuggestions() + .then((suggestions) => setSuggestions(suggestions)); + }); localStorage.setItem(LAST_INPUT_KEY, userInput); setUserInput(""); setPromptHints([]); @@ -1061,6 +1069,25 @@ export function Chat() { onSearch(""); }} /> + + {suggestions.length > 0 && ( +
+ {suggestions.map((s, i) => { + return ( +
{ + doSubmit(s); + }} + > + {s} +
+ ); + })} +
+ )} +