feat: support-search-in-chats

This commit is contained in:
Hk-Gosuto
2024-02-02 21:39:43 +08:00
parent ecdfb6c753
commit 341161037e
4 changed files with 453 additions and 9 deletions

View File

@@ -1,4 +1,4 @@
import { useEffect, useRef, useMemo } from "react";
import { useEffect, useRef, useMemo, useState } from "react";
import styles from "./home.module.scss";
@@ -11,6 +11,7 @@ import CloseIcon from "../icons/close.svg";
import DeleteIcon from "../icons/delete.svg";
import MaskIcon from "../icons/mask.svg";
import PluginIcon from "../icons/plugin.svg";
import SearchIcon from "../icons/search.svg";
import DragIcon from "../icons/drag.svg";
import Locale from "../locales";
@@ -30,6 +31,7 @@ import { Link, useNavigate } from "react-router-dom";
import { isIOS, useMobileScreen } from "../utils";
import dynamic from "next/dynamic";
import { showConfirm, showToast } from "./ui-lib";
import { SearchBar, SearchInputRef } from "./search-bar";
const ChatList = dynamic(async () => (await import("./chat-list")).ChatList, {
loading: () => null,
@@ -71,6 +73,9 @@ function useDragSideBar() {
}
});
};
const expandSidebar = () => {
config.update((config) => (config.sidebarWidth = MAX_SIDEBAR_WIDTH));
};
const onDragStart = (e: MouseEvent) => {
// Remembers the initial width each time the mouse is pressed
@@ -125,6 +130,7 @@ function useDragSideBar() {
return {
onDragStart,
shouldNarrow,
expandSidebar,
};
}
@@ -132,7 +138,7 @@ export function SideBar(props: { className?: string }) {
const chatStore = useChatStore();
// drag side bar
const { onDragStart, shouldNarrow } = useDragSideBar();
const { expandSidebar, onDragStart, shouldNarrow } = useDragSideBar();
const navigate = useNavigate();
const config = useAppConfig();
const isMobileScreen = useMobileScreen();
@@ -141,6 +147,19 @@ export function SideBar(props: { className?: string }) {
[isMobileScreen],
);
// search bar
const searchBarRef = useRef<SearchInputRef>(null);
const [isSearching, setIsSearching] = useState(false);
useEffect(() => {
if (shouldNarrow) stopSearch();
}, [shouldNarrow]);
const stopSearch = () => {
setIsSearching(false);
searchBarRef.current?.clearInput();
};
useHotKey();
return (
@@ -186,19 +205,47 @@ export function SideBar(props: { className?: string }) {
onClick={() => navigate(Path.Plugins, { state: { fromHome: true } })}
shadow
/>
{shouldNarrow && (
<IconButton
icon={<SearchIcon />}
className={styles["sidebar-bar-button"]}
onClick={() => {
expandSidebar();
// use setTimeout to avoid the input element not ready
setTimeout(() => {
searchBarRef.current?.inputElement?.focus();
}, 0);
}}
shadow
/>
)}
</div>
<div
className={styles["sidebar-body"]}
onClick={(e) => {
if (e.target === e.currentTarget) {
navigate(Path.Home);
}
}}
className={
styles["sidebar-search-bar"] +
" " +
(isSearching ? styles["sidebar-search-bar-isSearching"] : "")
}
>
<ChatList narrow={shouldNarrow} />
{!shouldNarrow && (
<SearchBar ref={searchBarRef} setIsSearching={setIsSearching} />
)}
</div>
{!isSearching && (
<div
className={styles["sidebar-body"]}
onClick={(e) => {
if (e.target === e.currentTarget) {
navigate(Path.Home);
}
}}
>
<ChatList narrow={shouldNarrow} />
</div>
)}
<div className={styles["sidebar-tail"]}>
<div className={styles["sidebar-actions"]}>
<div className={styles["sidebar-action"] + " " + styles.mobile}>
@@ -233,6 +280,7 @@ export function SideBar(props: { className?: string }) {
} else {
navigate(Path.NewChat);
}
stopSearch();
}}
shadow
/>