|
| 1 | +import { useState, useEffect } from "react"; |
| 2 | +import { ErrorBoundary } from "./error"; |
| 3 | +import styles from "./mask.module.scss"; |
| 4 | +import { useNavigate } from "react-router-dom"; |
| 5 | +import { IconButton } from "./button"; |
| 6 | +import CloseIcon from "../icons/close.svg"; |
| 7 | +import EyeIcon from "../icons/eye.svg"; |
| 8 | +import Locale from "../locales"; |
| 9 | +import { Path } from "../constant"; |
| 10 | + |
| 11 | +import { useChatStore } from "../store"; |
| 12 | + |
| 13 | +type Item = { |
| 14 | + id: number; |
| 15 | + name: string; |
| 16 | + content: string; |
| 17 | +}; |
| 18 | +export function SearchChatPage() { |
| 19 | + const navigate = useNavigate(); |
| 20 | + |
| 21 | + const chatStore = useChatStore(); |
| 22 | + |
| 23 | + const sessions = chatStore.sessions; |
| 24 | + const selectSession = chatStore.selectSession; |
| 25 | + |
| 26 | + const [searchResults, setSearchResults] = useState<Item[]>([]); |
| 27 | + |
| 28 | + const setDefaultItems = () => { |
| 29 | + setSearchResults( |
| 30 | + sessions.slice(1, 7).map((session, index) => { |
| 31 | + console.log(session.messages[0]); |
| 32 | + return { |
| 33 | + id: index, |
| 34 | + name: session.topic, |
| 35 | + content: session.messages[0].content as string, //.map((m) => m.content).join("\n") |
| 36 | + }; |
| 37 | + }), |
| 38 | + ); |
| 39 | + }; |
| 40 | + useEffect(() => { |
| 41 | + setDefaultItems(); |
| 42 | + }, []); |
| 43 | + |
| 44 | + const doSearch = (text: string) => { |
| 45 | + // 分割关键词 |
| 46 | + const keywords = text.split(" "); |
| 47 | + |
| 48 | + // 存储每个会话的匹配结果 |
| 49 | + const searchResults: Item[] = []; |
| 50 | + |
| 51 | + sessions.forEach((session, index) => { |
| 52 | + let matchCount = 0; |
| 53 | + const contents: string[] = []; |
| 54 | + |
| 55 | + session.messages.forEach((message) => { |
| 56 | + const content = message.content as string; |
| 57 | + const lowerCaseContent = content.toLowerCase(); |
| 58 | + keywords.forEach((keyword) => { |
| 59 | + const pos = lowerCaseContent.indexOf(keyword.toLowerCase()); |
| 60 | + if (pos !== -1) { |
| 61 | + matchCount++; |
| 62 | + // 提取关键词前后70个字符的内容 |
| 63 | + const start = Math.max(0, pos - 35); |
| 64 | + const end = Math.min(content.length, pos + keyword.length + 35); |
| 65 | + contents.push(content.substring(start, end)); |
| 66 | + } |
| 67 | + }); |
| 68 | + }); |
| 69 | + |
| 70 | + if (matchCount > 0) { |
| 71 | + searchResults.push({ |
| 72 | + id: index, |
| 73 | + name: session.topic, |
| 74 | + content: contents.join("... "), // 使用...连接不同消息中的内容 |
| 75 | + }); |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | + // 按匹配数量排序,取前10个结果 |
| 80 | + return searchResults |
| 81 | + .sort((a, b) => b.content.length - a.content.length) |
| 82 | + .slice(0, 10); |
| 83 | + }; |
| 84 | + |
| 85 | + return ( |
| 86 | + <ErrorBoundary> |
| 87 | + <div className={styles["mask-page"]}> |
| 88 | + {/* header */} |
| 89 | + <div className="window-header"> |
| 90 | + <div className="window-header-title"> |
| 91 | + <div className="window-header-main-title"> |
| 92 | + {Locale.SearchChat.Page.Title} |
| 93 | + </div> |
| 94 | + <div className="window-header-submai-title"> |
| 95 | + {Locale.SearchChat.Page.SubTitle(searchResults.length)} |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + <div className="window-actions"> |
| 100 | + <div className="window-action-button"> |
| 101 | + <IconButton |
| 102 | + icon={<CloseIcon />} |
| 103 | + bordered |
| 104 | + onClick={() => navigate(-1)} |
| 105 | + /> |
| 106 | + </div> |
| 107 | + </div> |
| 108 | + </div> |
| 109 | + |
| 110 | + <div className={styles["mask-page-body"]}> |
| 111 | + <div className={styles["mask-filter"]}> |
| 112 | + {/**搜索输入框 */} |
| 113 | + <input |
| 114 | + type="text" |
| 115 | + className={styles["search-bar"]} |
| 116 | + placeholder={Locale.SearchChat.Page.Search} |
| 117 | + autoFocus |
| 118 | + onKeyDown={(e) => { |
| 119 | + if (e.key === "Enter") { |
| 120 | + e.preventDefault(); |
| 121 | + const searchText = e.currentTarget.value; |
| 122 | + if (searchText.length > 0) { |
| 123 | + const result = doSearch(searchText); |
| 124 | + setSearchResults(result); |
| 125 | + } |
| 126 | + } |
| 127 | + }} |
| 128 | + /> |
| 129 | + </div> |
| 130 | + |
| 131 | + <div> |
| 132 | + {searchResults.map((item) => ( |
| 133 | + <div className={styles["mask-item"]} key={item.id}> |
| 134 | + {/** 搜索匹配的文本 */} |
| 135 | + <div className={styles["mask-header"]}> |
| 136 | + <div className={styles["mask-title"]}> |
| 137 | + <div className={styles["mask-name"]}>{item.name}</div> |
| 138 | + {item.content.slice(0, 70)} |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + {/** 操作按钮 */} |
| 142 | + <div className={styles["mask-actions"]}> |
| 143 | + <IconButton |
| 144 | + icon={<EyeIcon />} |
| 145 | + text={Locale.SearchChat.Item.View} |
| 146 | + onClick={() => { |
| 147 | + navigate(Path.Chat); |
| 148 | + selectSession(item.id); |
| 149 | + }} |
| 150 | + /> |
| 151 | + </div> |
| 152 | + </div> |
| 153 | + ))} |
| 154 | + </div> |
| 155 | + </div> |
| 156 | + </div> |
| 157 | + </ErrorBoundary> |
| 158 | + ); |
| 159 | +} |
0 commit comments