Skip to content

Commit 814489d

Browse files
committed
search: working on refactoring to also support tasks
1 parent 114b979 commit 814489d

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/packages/frontend/frame-editors/chat-editor/actions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { redux_name } from "@cocalc/frontend/app-framework";
2323
import { aux_file } from "@cocalc/util/misc";
2424
import type { FragmentId } from "@cocalc/frontend/misc/fragment-id";
2525
import { delay } from "awaiting";
26+
import { getSearchData } from "@cocalc/frontend/chat/filter-messages";
2627

2728
const FRAME_TYPE = "chatroom";
2829

@@ -157,4 +158,12 @@ export class Actions extends CodeEditorActions<ChatEditorState> {
157158
await delay(d);
158159
}
159160
}
161+
162+
getSearchData = () => {
163+
const messages = this.store?.get("messages");
164+
if (messages == null) {
165+
return {};
166+
}
167+
return getSearchData({ messages, threads: false });
168+
};
160169
}

src/packages/frontend/frame-editors/chat-editor/use-search-index.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
2-
import { useEffect, useState } from "react";
2+
import { useEffect, useRef, useState } from "react";
33
import { create, search, insertMultiple } from "@orama/orama";
4-
import { getSearchData } from "@cocalc/frontend/chat/filter-messages";
54
import useCounter from "@cocalc/frontend/app-framework/counter-hook";
65

76
export default function useSearchIndex() {
87
const { actions, project_id, path } = useFrameContext();
8+
const contextRef = useRef<{ project_id: string; path: string }>({
9+
project_id,
10+
path,
11+
});
912
const [index, setIndex] = useState<null | SearchIndex>(null);
1013
const [error, setError] = useState<string>("");
1114
const { val: refresh, inc: doRefresh } = useCounter();
1215
const [indexTime, setIndexTime] = useState<number>(0);
1316

1417
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+
}
1525
(async () => {
1626
try {
1727
setError("");
@@ -57,19 +67,17 @@ class SearchIndex {
5767
},
5868
});
5969

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);
7180
}
72-
await insertMultiple(this.db, docs);
7381
this.state = "ready";
7482
};
7583
}

0 commit comments

Comments
 (0)