fix: re-render if isMobileScreen() changes

Before this change, even with "tight border" enabled, resizing the window from
mobile to non-mobile does not use tight border.

After this change, the "tight border" config is respected as expected.
This commit is contained in:
Jun Wu 2023-04-05 23:48:04 -07:00
parent 03b3f16472
commit 4e0eef4398
4 changed files with 33 additions and 5 deletions

View File

@ -20,6 +20,7 @@ import {
ROLES, ROLES,
createMessage, createMessage,
} from "../store"; } from "../store";
import { useScreen } from "../store/screen";
import { import {
copyToClipboard, copyToClipboard,
@ -348,6 +349,8 @@ export function Chat(props: {
const { scrollRef, setAutoScroll } = useScrollToBottom(); const { scrollRef, setAutoScroll } = useScrollToBottom();
const [hitBottom, setHitBottom] = useState(false); const [hitBottom, setHitBottom] = useState(false);
const isMobile = useScreen(screen => screen.isMobile);
const onChatBodyScroll = (e: HTMLElement) => { const onChatBodyScroll = (e: HTMLElement) => {
const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20; const isTouchBottom = e.scrollTop + e.clientHeight >= e.scrollHeight - 20;
setHitBottom(isTouchBottom); setHitBottom(isTouchBottom);
@ -409,7 +412,7 @@ export function Chat(props: {
chatStore.onUserInput(userInput).then(() => setIsLoading(false)); chatStore.onUserInput(userInput).then(() => setIsLoading(false));
setUserInput(""); setUserInput("");
setPromptHints([]); setPromptHints([]);
if (!isMobileScreen()) inputRef.current?.focus(); if (!isMobile) inputRef.current?.focus();
setAutoScroll(true); setAutoScroll(true);
}; };
@ -499,7 +502,7 @@ export function Chat(props: {
// Auto focus // Auto focus
useEffect(() => { useEffect(() => {
if (props.sideBarShowing && isMobileScreen()) return; if (props.sideBarShowing && isMobile) return;
inputRef.current?.focus(); inputRef.current?.focus();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
@ -635,7 +638,7 @@ export function Chat(props: {
style={{ fontSize: `${fontSize}px` }} style={{ fontSize: `${fontSize}px` }}
onContextMenu={(e) => onRightClick(e, message)} onContextMenu={(e) => onRightClick(e, message)}
onDoubleClickCapture={() => { onDoubleClickCapture={() => {
if (!isMobileScreen()) return; if (!isMobile) return;
setUserInput(message.content); setUserInput(message.content);
}} }}
> >

View File

@ -17,7 +17,8 @@ import LoadingIcon from "../icons/three-dots.svg";
import CloseIcon from "../icons/close.svg"; import CloseIcon from "../icons/close.svg";
import { useChatStore } from "../store"; import { useChatStore } from "../store";
import { isMobileScreen } from "../utils"; import { useScreen } from "../store/screen";
import Locale from "../locales"; import Locale from "../locales";
import { Chat } from "./chat"; import { Chat } from "./chat";
@ -99,6 +100,7 @@ function _Home() {
// setting // setting
const [openSettings, setOpenSettings] = useState(false); const [openSettings, setOpenSettings] = useState(false);
const config = useChatStore((state) => state.config); const config = useChatStore((state) => state.config);
const isMobile = useScreen(screen => screen.isMobile);
useSwitchTheme(); useSwitchTheme();
@ -109,7 +111,7 @@ function _Home() {
return ( return (
<div <div
className={`${ className={`${
config.tightBorder && !isMobileScreen() config.tightBorder && !isMobile
? styles["tight-container"] ? styles["tight-container"]
: styles.container : styles.container
}`} }`}

20
app/store/screen.ts Normal file
View File

@ -0,0 +1,20 @@
import { create } from "zustand";
import { isMobileScreen } from "../utils";
interface Screen {
isMobile: boolean;
update: () => void;
}
export const useScreen = create<Screen>(set => {
const state = {
isMobile: isMobileScreen(),
update: () => set({ isMobile: isMobileScreen() }),
};
if (typeof window !== "undefined") {
window.addEventListener("resize", e => {
state.update();
});
}
return state;
});

View File

@ -49,6 +49,9 @@ export function isIOS() {
} }
export function isMobileScreen() { export function isMobileScreen() {
if (typeof window === "undefined") {
return false;
}
return window.innerWidth <= 600; return window.innerWidth <= 600;
} }