|
1 | 1 | import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
|
2 |
| -import { useEffect, useState } from "react"; |
| 2 | +import { useEffect, useRef, useState } from "react"; |
3 | 3 | import { create, search, insertMultiple } from "@orama/orama";
|
4 |
| -import { getSearchData } from "@cocalc/frontend/chat/filter-messages"; |
5 | 4 | import useCounter from "@cocalc/frontend/app-framework/counter-hook";
|
6 | 5 |
|
7 | 6 | export default function useSearchIndex() {
|
8 | 7 | const { actions, project_id, path } = useFrameContext();
|
| 8 | + const contextRef = useRef<{ project_id: string; path: string }>({ |
| 9 | + project_id, |
| 10 | + path, |
| 11 | + }); |
9 | 12 | const [index, setIndex] = useState<null | SearchIndex>(null);
|
10 | 13 | const [error, setError] = useState<string>("");
|
11 | 14 | const { val: refresh, inc: doRefresh } = useCounter();
|
12 | 15 | const [indexTime, setIndexTime] = useState<number>(0);
|
13 | 16 |
|
14 | 17 | useEffect(() => {
|
| 18 | + if ( |
| 19 | + contextRef.current.project_id != project_id || |
| 20 | + contextRef.current.path != path |
| 21 | + ) { |
| 22 | + contextRef.current = { project_id, path }; |
| 23 | + setIndex(null); |
| 24 | + } |
15 | 25 | (async () => {
|
16 | 26 | try {
|
17 | 27 | setError("");
|
@@ -57,19 +67,17 @@ class SearchIndex {
|
57 | 67 | },
|
58 | 68 | });
|
59 | 69 |
|
60 |
| - const messages = this.actions.store?.get("messages"); |
61 |
| - if (messages == null) { |
62 |
| - return; |
63 |
| - } |
64 |
| - const searchData = getSearchData({ messages, threads: false }); |
65 |
| - const docs: { time: number; message: string }[] = []; |
66 |
| - for (const time in searchData) { |
67 |
| - docs.push({ |
68 |
| - time: parseInt(time), |
69 |
| - message: searchData[time]?.content ?? "", |
70 |
| - }); |
| 70 | + const searchData = this.actions.getSearchData(); |
| 71 | + if (searchData != null) { |
| 72 | + const docs: { time: number; message: string }[] = []; |
| 73 | + for (const time in searchData) { |
| 74 | + docs.push({ |
| 75 | + time: parseInt(time), |
| 76 | + message: searchData[time]?.content ?? "", |
| 77 | + }); |
| 78 | + } |
| 79 | + await insertMultiple(this.db, docs); |
71 | 80 | }
|
72 |
| - await insertMultiple(this.db, docs); |
73 | 81 | this.state = "ready";
|
74 | 82 | };
|
75 | 83 | }
|
0 commit comments