From 328ecd1cfb74d06bc42cf0430b1e629230c3de0a Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 3 May 2023 15:22:44 +0800 Subject: [PATCH 1/4] fix: #1210 change default lang to en --- app/locales/index.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/locales/index.ts b/app/locales/index.ts index 2ce59261..40f0a1ad 100644 --- a/app/locales/index.ts +++ b/app/locales/index.ts @@ -22,6 +22,7 @@ export const AllLangs = [ export type Lang = (typeof AllLangs)[number]; const LANG_KEY = "lang"; +const DEFAULT_LANG = "en"; function getItem(key: string) { try { @@ -41,7 +42,8 @@ function getLanguage() { try { return navigator.language.toLowerCase(); } catch { - return "cn"; + console.log("[Lang] failed to detect user lang."); + return DEFAULT_LANG; } } @@ -60,7 +62,7 @@ export function getLang(): Lang { } } - return "en"; + return DEFAULT_LANG; } export function changeLang(lang: Lang) { From c1b6828ed42c3d978edd98f475758ea39c68a9e3 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 3 May 2023 15:55:46 +0800 Subject: [PATCH 2/4] fix: #1201 wont close prompt list when blur --- app/components/chat.tsx | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 8fc39e31..a7808bb0 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -54,7 +54,7 @@ import styles from "./home.module.scss"; import chatStyle from "./chat.module.scss"; import { ListItem, Modal, showModal } from "./ui-lib"; -import { useNavigate } from "react-router-dom"; +import { useLocation, useNavigate } from "react-router-dom"; import { Path } from "../constant"; import { Avatar } from "./emoji"; import { MaskAvatar, MaskConfig } from "./mask"; @@ -235,7 +235,10 @@ export function PromptHints(props: {
props.onPromptSelect(prompt)} + onClick={() => { + console.log("click hint"); + props.onPromptSelect(prompt); + }} >
{prompt.title}
{prompt.content}
@@ -373,7 +376,7 @@ export function Chat() { const navigate = useNavigate(); const onChatBodyScroll = (e: HTMLElement) => { - const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20; + const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 100; setHitBottom(isTouchBottom); }; @@ -400,7 +403,7 @@ export function Chat() { () => { const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1; const inputRows = Math.min( - 5, + 20, Math.max(2 + Number(!isMobileScreen), rows), ); setInputRows(inputRows); @@ -569,12 +572,9 @@ export function Chat() { } }; - // Auto focus - useEffect(() => { - if (isMobileScreen) return; - inputRef.current?.focus(); - // eslint-disable-next-line react-hooks/exhaustive-deps - }, []); + const location = useLocation(); + const isChat = location.pathname === Path.Chat; + const autoFocus = isChat; // only focus in chat page return (
@@ -764,16 +764,9 @@ export function Chat() { value={userInput} onKeyDown={onInputKeyDown} onFocus={() => setAutoScroll(true)} - onBlur={() => { - setTimeout(() => { - if (document.activeElement !== inputRef.current) { - setAutoScroll(false); - setPromptHints([]); - } - }, 100); - }} - autoFocus + onBlur={() => setAutoScroll(false)} rows={inputRows} + autoFocus={autoFocus} /> } From 58eadd6d7bbcb31fa774d4ade75853bd4bb8ccc5 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 3 May 2023 16:22:37 +0800 Subject: [PATCH 3/4] feat: close #782 select prompt with arrow down / up --- app/components/chat.tsx | 57 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index 7da9b8c8..bae9c5ba 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -224,18 +224,63 @@ export function PromptHints(props: { prompts: Prompt[]; onPromptSelect: (prompt: Prompt) => void; }) { - if (props.prompts.length === 0) return null; + const noPrompts = props.prompts.length === 0; + const [selectIndex, setSelectIndex] = useState(0); + const selectedRef = useRef(null); + useEffect(() => { + setSelectIndex(0); + }, [props.prompts.length]); + + useEffect(() => { + const onKeyDown = (e: KeyboardEvent) => { + if (noPrompts) return; + + // arrow up / down to select prompt + const changeIndex = (delta: number) => { + e.stopPropagation(); + e.preventDefault(); + const nextIndex = Math.max( + 0, + Math.min(props.prompts.length - 1, selectIndex + delta), + ); + setSelectIndex(nextIndex); + selectedRef.current?.scrollIntoView({ + block: "center", + }); + }; + + if (e.key === "ArrowUp") { + changeIndex(1); + } else if (e.key === "ArrowDown") { + changeIndex(-1); + } else if (e.key === "Enter") { + const selectedPrompt = props.prompts.at(selectIndex); + if (selectedPrompt) { + props.onPromptSelect(selectedPrompt); + } + } + }; + + window.addEventListener("keydown", onKeyDown); + + return () => window.removeEventListener("keydown", onKeyDown); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [noPrompts, selectIndex]); + + if (noPrompts) return null; return (
{props.prompts.map((prompt, i) => (
{ - console.log("click hint"); - props.onPromptSelect(prompt); - }} + onClick={() => props.onPromptSelect(prompt)} + onMouseEnter={() => setSelectIndex(i)} >
{prompt.title}
{prompt.content}
From 06268543d04660dd79bbfba2574c10b8f29b4ed1 Mon Sep 17 00:00:00 2001 From: Yidadaa Date: Wed, 3 May 2023 16:24:25 +0800 Subject: [PATCH 4/4] fixup --- app/components/chat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/components/chat.tsx b/app/components/chat.tsx index bae9c5ba..4173fc3a 100644 --- a/app/components/chat.tsx +++ b/app/components/chat.tsx @@ -616,7 +616,7 @@ export function Chat() { const location = useLocation(); const isChat = location.pathname === Path.Chat; - const autoFocus = isChat; // only focus in chat page + const autoFocus = !isMobileScreen || isChat; // only focus in chat page return (