From 354a596fa8b277922fa1a5118c0a077415bc13e9 Mon Sep 17 00:00:00 2001 From: Andision Date: Fri, 1 Dec 2023 04:08:47 +0000 Subject: [PATCH] feat: add search bar in chat-list --- app/components/chat-list.tsx | 87 +++++++++++++++++++++++---------- app/components/home.module.scss | 5 ++ 2 files changed, 65 insertions(+), 27 deletions(-) diff --git a/app/components/chat-list.tsx b/app/components/chat-list.tsx index 33967717d..ff0ebb9d0 100644 --- a/app/components/chat-list.tsx +++ b/app/components/chat-list.tsx @@ -9,15 +9,15 @@ import { OnDragEndResponder, } from "@hello-pangea/dnd"; -import { useChatStore } from "../store"; +import { ChatSession, useChatStore } from "../store"; import Locale from "../locales"; import { Link, useNavigate } from "react-router-dom"; import { Path } from "../constant"; import { MaskAvatar } from "./mask"; import { Mask } from "../store/mask"; -import { useRef, useEffect } from "react"; -import { showConfirm } from "./ui-lib"; +import { useRef, useEffect, useState } from "react"; +import { showConfirm, SearchInput } from "./ui-lib"; import { useMobileScreen } from "../utils"; export function ChatItem(props: { @@ -127,6 +127,26 @@ export function ChatList(props: { narrow?: boolean }) { moveSession(source.index, destination.index); }; + const [chatListSearch, setChatListSearch] = useState(""); + + function haveSearchKeyword(item: ChatSession): boolean { + if (chatListSearch.length === 0) { + return true; + } + + let foundKeyword = false; + + item.messages.forEach((message) => { + // console.log(chatListSearch, message.content, message.content.includes(chatListSearch)) + if (message.content.includes(chatListSearch)) { + foundKeyword = true; + return; + } + }); + + return foundKeyword; + } + return ( @@ -136,31 +156,44 @@ export function ChatList(props: { narrow?: boolean }) { ref={provided.innerRef} {...provided.droppableProps} > - {sessions.map((item, i) => ( - { - navigate(Path.Chat); - selectSession(i); +
+ { + setChatListSearch(e.currentTarget.value); }} - onDelete={async () => { - if ( - (!props.narrow && !isMobileScreen) || - (await showConfirm(Locale.Home.DeleteChat)) - ) { - chatStore.deleteSession(i); - } - }} - narrow={props.narrow} - mask={item.mask} - /> - ))} + placeholder={Locale.Home.Search} + > +
+ + {sessions.map( + (item, i) => + haveSearchKeyword(item) && ( + { + navigate(Path.Chat); + selectSession(i); + }} + onDelete={async () => { + if ( + (!props.narrow && !isMobileScreen) || + (await showConfirm(Locale.Home.DeleteChat)) + ) { + chatStore.deleteSession(i); + } + }} + narrow={props.narrow} + mask={item.mask} + /> + ), + )} {provided.placeholder} )} diff --git a/app/components/home.module.scss b/app/components/home.module.scss index b836d2bec..b0884d248 100644 --- a/app/components/home.module.scss +++ b/app/components/home.module.scss @@ -230,6 +230,11 @@ white-space: nowrap; } +.chat-list-search { + padding: 10px 0px; + margin-bottom: 10px; +} + .narrow-sidebar { .sidebar-title, .sidebar-sub-title {