File size: 5,413 Bytes
853f6aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { useState, useEffect, useRef, useCallback } from "react";
import { ErrorBoundary } from "./error";
import styles from "./mask.module.scss";
import { useNavigate } from "react-router-dom";
import { IconButton } from "./button";
import CloseIcon from "../icons/close.svg";
import EyeIcon from "../icons/eye.svg";
import Locale from "../locales";
import { Path } from "../constant";

import { useChatStore } from "../store";

type Item = {
  id: number;
  name: string;
  content: string;
};
export function SearchChatPage() {
  const navigate = useNavigate();

  const chatStore = useChatStore();

  const sessions = chatStore.sessions;
  const selectSession = chatStore.selectSession;

  const [searchResults, setSearchResults] = useState<Item[]>([]);

  const previousValueRef = useRef<string>("");
  const searchInputRef = useRef<HTMLInputElement>(null);
  const doSearch = useCallback((text: string) => {
    const lowerCaseText = text.toLowerCase();
    const results: Item[] = [];

    sessions.forEach((session, index) => {
      const fullTextContents: string[] = [];

      session.messages.forEach((message) => {
        const content = message.content as string;
        if (!content.toLowerCase || content === "") return;
        const lowerCaseContent = content.toLowerCase();

        // full text search
        let pos = lowerCaseContent.indexOf(lowerCaseText);
        while (pos !== -1) {
          const start = Math.max(0, pos - 35);
          const end = Math.min(content.length, pos + lowerCaseText.length + 35);
          fullTextContents.push(content.substring(start, end));
          pos = lowerCaseContent.indexOf(
            lowerCaseText,
            pos + lowerCaseText.length,
          );
        }
      });

      if (fullTextContents.length > 0) {
        results.push({
          id: index,
          name: session.topic,
          content: fullTextContents.join("... "), // concat content with...
        });
      }
    });

    // sort by length of matching content
    results.sort((a, b) => b.content.length - a.content.length);

    return results;
  }, [sessions]);

  useEffect(() => {
    const intervalId = setInterval(() => {
      if (searchInputRef.current) {
        const currentValue = searchInputRef.current.value;
        if (currentValue !== previousValueRef.current) {
          if (currentValue.length > 0) {
            const result = doSearch(currentValue);
            setSearchResults(result);
          }
          previousValueRef.current = currentValue;
        }
      }
    }, 1000);

    // Cleanup the interval on component unmount
    return () => clearInterval(intervalId);
  }, [doSearch]);

  return (
    <ErrorBoundary>

      <div className={styles["mask-page"]}>

        {/* header */}

        <div className="window-header">

          <div className="window-header-title">

            <div className="window-header-main-title">

              {Locale.SearchChat.Page.Title}

            </div>

            <div className="window-header-submai-title">

              {Locale.SearchChat.Page.SubTitle(searchResults.length)}

            </div>

          </div>



          <div className="window-actions">

            <div className="window-action-button">

              <IconButton

                icon={<CloseIcon />}

                bordered

                onClick={() => navigate(-1)}

              />

            </div>

          </div>

        </div>



        <div className={styles["mask-page-body"]}>

          <div className={styles["mask-filter"]}>

            {/**搜索输入框 */}

            <input

              type="text"

              className={styles["search-bar"]}

              placeholder={Locale.SearchChat.Page.Search}

              autoFocus

              ref={searchInputRef}

              onKeyDown={(e) => {

                if (e.key === "Enter") {

                  e.preventDefault();

                  const searchText = e.currentTarget.value;

                  if (searchText.length > 0) {

                    const result = doSearch(searchText);

                    setSearchResults(result);

                  }

                }

              }}

            />

          </div>



          <div>

            {searchResults.map((item) => (

              <div

                className={styles["mask-item"]}

                key={item.id}

                onClick={() => {

                  navigate(Path.Chat);

                  selectSession(item.id);

                }}

                style={{ cursor: "pointer" }}

              >

                {/** 搜索匹配的文本 */}

                <div className={styles["mask-header"]}>

                  <div className={styles["mask-title"]}>

                    <div className={styles["mask-name"]}>{item.name}</div>

                    {item.content.slice(0, 70)}

                  </div>

                </div>

                {/** 操作按钮 */}

                <div className={styles["mask-actions"]}>

                  <IconButton

                    icon={<EyeIcon />}

                    text={Locale.SearchChat.Item.View}

                  />

                </div>

              </div>

            ))}

          </div>

        </div>

      </div>

    </ErrorBoundary>
  );
}