feat: reactive isMobileScreen

This commit is contained in:
Yidadaa 2023-04-20 23:20:25 +08:00
parent 2390da1165
commit 55281ed548
4 changed files with 34 additions and 18 deletions

View File

@ -10,7 +10,6 @@ import {
import { useChatStore } from "../store"; import { useChatStore } from "../store";
import Locale from "../locales"; import Locale from "../locales";
import { isMobileScreen } from "../utils";
export function ChatItem(props: { export function ChatItem(props: {
onClick?: () => void; onClick?: () => void;

View File

@ -38,9 +38,9 @@ import {
copyToClipboard, copyToClipboard,
downloadAs, downloadAs,
getEmojiUrl, getEmojiUrl,
isMobileScreen,
selectOrCopy, selectOrCopy,
autoGrowTextArea, autoGrowTextArea,
useMobileScreen,
} from "../utils"; } from "../utils";
import dynamic from "next/dynamic"; import dynamic from "next/dynamic";
@ -438,6 +438,7 @@ export function Chat(props: {
const { submitKey, shouldSubmit } = useSubmitHandler(); const { submitKey, shouldSubmit } = useSubmitHandler();
const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom(); const { scrollRef, setAutoScroll, scrollToBottom } = useScrollToBottom();
const [hitBottom, setHitBottom] = useState(false); const [hitBottom, setHitBottom] = useState(false);
const isMobileScreen = useMobileScreen();
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;
@ -468,7 +469,7 @@ export function Chat(props: {
const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1; const rows = inputRef.current ? autoGrowTextArea(inputRef.current) : 1;
const inputRows = Math.min( const inputRows = Math.min(
5, 5,
Math.max(2 + Number(!isMobileScreen()), rows), Math.max(2 + Number(!isMobileScreen), rows),
); );
setInputRows(inputRows); setInputRows(inputRows);
}, },
@ -508,7 +509,7 @@ export function Chat(props: {
setBeforeInput(userInput); setBeforeInput(userInput);
setUserInput(""); setUserInput("");
setPromptHints([]); setPromptHints([]);
if (!isMobileScreen()) inputRef.current?.focus(); if (!isMobileScreen) inputRef.current?.focus();
setAutoScroll(true); setAutoScroll(true);
}; };
@ -640,7 +641,7 @@ export function Chat(props: {
// Auto focus // Auto focus
useEffect(() => { useEffect(() => {
if (props.sideBarShowing && isMobileScreen()) return; if (props.sideBarShowing && isMobileScreen) return;
inputRef.current?.focus(); inputRef.current?.focus();
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
}, []); }, []);
@ -688,7 +689,7 @@ export function Chat(props: {
}} }}
/> />
</div> </div>
{!isMobileScreen() && ( {!isMobileScreen && (
<div className={styles["window-action-button"]}> <div className={styles["window-action-button"]}>
<IconButton <IconButton
icon={chatStore.config.tightBorder ? <MinIcon /> : <MaxIcon />} icon={chatStore.config.tightBorder ? <MinIcon /> : <MaxIcon />}
@ -788,7 +789,7 @@ export function Chat(props: {
} }
onContextMenu={(e) => onRightClick(e, message)} onContextMenu={(e) => onRightClick(e, message)}
onDoubleClickCapture={() => { onDoubleClickCapture={() => {
if (!isMobileScreen()) return; if (!isMobileScreen) return;
setUserInput(message.content); setUserInput(message.content);
}} }}
fontSize={fontSize} fontSize={fontSize}

View File

@ -17,7 +17,7 @@ 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 { getCSSVar, isMobileScreen } from "../utils"; import { getCSSVar, useMobileScreen } from "../utils";
import Locale from "../locales"; import Locale from "../locales";
import { Chat } from "./chat"; import { Chat } from "./chat";
@ -103,17 +103,14 @@ function useDragSideBar() {
window.addEventListener("mousemove", handleMouseMove.current); window.addEventListener("mousemove", handleMouseMove.current);
window.addEventListener("mouseup", handleMouseUp.current); window.addEventListener("mouseup", handleMouseUp.current);
}; };
const isMobileScreen = useMobileScreen();
useEffect(() => { useEffect(() => {
if (isMobileScreen()) { const sideBarWidth = isMobileScreen
return; ? "100vw"
} : `${limit(chatStore.config.sidebarWidth ?? 300)}px`;
document.documentElement.style.setProperty("--sidebar-width", sideBarWidth);
document.documentElement.style.setProperty( }, [chatStore.config.sidebarWidth, isMobileScreen]);
"--sidebar-width",
`${limit(chatStore.config.sidebarWidth ?? 300)}px`,
);
}, [chatStore.config.sidebarWidth]);
return { return {
onDragMouseDown, onDragMouseDown,
@ -148,6 +145,7 @@ function _Home() {
// drag side bar // drag side bar
const { onDragMouseDown } = useDragSideBar(); const { onDragMouseDown } = useDragSideBar();
const isMobileScreen = useMobileScreen();
useSwitchTheme(); useSwitchTheme();
@ -158,7 +156,7 @@ function _Home() {
return ( return (
<div <div
className={`${ className={`${
config.tightBorder && !isMobileScreen() config.tightBorder && !isMobileScreen
? styles["tight-container"] ? styles["tight-container"]
: styles.container : styles.container
}`} }`}

View File

@ -1,4 +1,5 @@
import { EmojiStyle } from "emoji-picker-react"; import { EmojiStyle } from "emoji-picker-react";
import { useEffect, useState } from "react";
import { showToast } from "./components/ui-lib"; import { showToast } from "./components/ui-lib";
import Locale from "./locales"; import Locale from "./locales";
@ -47,6 +48,23 @@ export function isIOS() {
return /iphone|ipad|ipod/.test(userAgent); return /iphone|ipad|ipod/.test(userAgent);
} }
export function useMobileScreen() {
const [isMobileScreen_, setIsMobileScreen] = useState(false);
useEffect(() => {
const onResize = () => {
setIsMobileScreen(isMobileScreen());
};
window.addEventListener("resize", onResize);
return () => {
window.removeEventListener("resize", onResize);
};
}, []);
return isMobileScreen_;
}
export function isMobileScreen() { export function isMobileScreen() {
return window.innerWidth <= 600; return window.innerWidth <= 600;
} }