search-chat.tsx 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { useState, useEffect, useRef } 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. import { useChatStore } from "../store";
  11. type Item = {
  12. id: number;
  13. name: string;
  14. content: string;
  15. };
  16. export function SearchChatPage() {
  17. const navigate = useNavigate();
  18. const chatStore = useChatStore();
  19. const sessions = chatStore.sessions;
  20. const selectSession = chatStore.selectSession;
  21. const [searchResults, setSearchResults] = useState<Item[]>([]);
  22. const previousValueRef = useRef<string>("");
  23. const searchInputRef = useRef<HTMLInputElement>(null);
  24. const doSearch = (text: string) => {
  25. const lowerCaseText = text.toLowerCase();
  26. const results: Item[] = [];
  27. sessions.forEach((session, index) => {
  28. const fullTextContents: string[] = [];
  29. session.messages.forEach((message) => {
  30. const content = message.content as string;
  31. const lowerCaseContent = content.toLowerCase();
  32. // full text search
  33. let pos = lowerCaseContent.indexOf(lowerCaseText);
  34. while (pos !== -1) {
  35. const start = Math.max(0, pos - 35);
  36. const end = Math.min(content.length, pos + lowerCaseText.length + 35);
  37. fullTextContents.push(content.substring(start, end));
  38. pos = lowerCaseContent.indexOf(
  39. lowerCaseText,
  40. pos + lowerCaseText.length,
  41. );
  42. }
  43. });
  44. if (fullTextContents.length > 0) {
  45. results.push({
  46. id: index,
  47. name: session.topic,
  48. content: fullTextContents.join("... "), // concat content with...
  49. });
  50. }
  51. });
  52. // sort by length of matching content
  53. results.sort((a, b) => b.content.length - a.content.length);
  54. return results;
  55. };
  56. useEffect(() => {
  57. const intervalId = setInterval(() => {
  58. if (searchInputRef.current) {
  59. const currentValue = searchInputRef.current.value;
  60. if (currentValue !== previousValueRef.current) {
  61. if (currentValue.length > 0) {
  62. const result = doSearch(currentValue);
  63. setSearchResults(result);
  64. }
  65. previousValueRef.current = currentValue;
  66. }
  67. }
  68. }, 1000);
  69. // Cleanup the interval on component unmount
  70. return () => clearInterval(intervalId);
  71. }, []);
  72. return (
  73. <ErrorBoundary>
  74. <div className={styles["mask-page"]}>
  75. {/* header */}
  76. <div className="window-header">
  77. <div className="window-header-title">
  78. <div className="window-header-main-title">
  79. {Locale.SearchChat.Page.Title}
  80. </div>
  81. <div className="window-header-submai-title">
  82. {Locale.SearchChat.Page.SubTitle(searchResults.length)}
  83. </div>
  84. </div>
  85. <div className="window-actions">
  86. <div className="window-action-button">
  87. <IconButton
  88. icon={<CloseIcon />}
  89. bordered
  90. onClick={() => navigate(-1)}
  91. />
  92. </div>
  93. </div>
  94. </div>
  95. <div className={styles["mask-page-body"]}>
  96. <div className={styles["mask-filter"]}>
  97. {/**搜索输入框 */}
  98. <input
  99. type="text"
  100. className={styles["search-bar"]}
  101. placeholder={Locale.SearchChat.Page.Search}
  102. autoFocus
  103. ref={searchInputRef}
  104. onKeyDown={(e) => {
  105. if (e.key === "Enter") {
  106. e.preventDefault();
  107. const searchText = e.currentTarget.value;
  108. if (searchText.length > 0) {
  109. const result = doSearch(searchText);
  110. setSearchResults(result);
  111. }
  112. }
  113. }}
  114. />
  115. </div>
  116. <div>
  117. {searchResults.map((item) => (
  118. <div
  119. className={styles["mask-item"]}
  120. key={item.id}
  121. onClick={() => {
  122. navigate(Path.Chat);
  123. selectSession(item.id);
  124. }}
  125. style={{ cursor: "pointer" }}
  126. >
  127. {/** 搜索匹配的文本 */}
  128. <div className={styles["mask-header"]}>
  129. <div className={styles["mask-title"]}>
  130. <div className={styles["mask-name"]}>{item.name}</div>
  131. {item.content.slice(0, 70)}
  132. </div>
  133. </div>
  134. {/** 操作按钮 */}
  135. <div className={styles["mask-actions"]}>
  136. <IconButton
  137. icon={<EyeIcon />}
  138. text={Locale.SearchChat.Item.View}
  139. />
  140. </div>
  141. </div>
  142. ))}
  143. </div>
  144. </div>
  145. </div>
  146. </ErrorBoundary>
  147. );
  148. }