Skip to content

Commit ea80e0a

Browse files
committed
search: making it more generic -- the fragment
1 parent 814489d commit ea80e0a

File tree

4 files changed

+50
-30
lines changed

4 files changed

+50
-30
lines changed

src/packages/frontend/chat/actions.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,14 @@ export class ChatActions extends Actions<ChatState> {
517517
this.scrollToIndex(Number.MAX_SAFE_INTEGER);
518518
};
519519

520+
// this scrolls the message with given date into view and sets it as the selected message.
520521
scrollToDate = (date) => {
521522
this.clearScrollRequest();
522523
this.frameTreeActions?.set_frame_data({
523524
id: this.frameId,
524525
fragmentId: toMsString(date),
525526
});
527+
this.setFragment(date);
526528
setTimeout(() => {
527529
this.frameTreeActions?.set_frame_data({
528530
id: this.frameId,

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,16 @@ export class Actions extends CodeEditorActions<ChatEditorState> {
159159
}
160160
}
161161

162-
getSearchData = () => {
162+
getSearchIndexData = () => {
163163
const messages = this.store?.get("messages");
164164
if (messages == null) {
165165
return {};
166166
}
167-
return getSearchData({ messages, threads: false });
167+
const data: { [id: string]: string } = {};
168+
const data0 = getSearchData({ messages, threads: false });
169+
for (const id in data0) {
170+
data[id] = data0[id]?.content;
171+
}
172+
return { data, fragmentKey: "chat" };
168173
};
169174
}

src/packages/frontend/frame-editors/chat-editor/search.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ function Search({ font_size, desc }: Props) {
4242
[project_id, path],
4343
);
4444

45-
const { error, setError, index, doRefresh } = useSearchIndex();
45+
const { error, setError, index, doRefresh, fragmentKey } = useSearchIndex();
4646

4747
useEffect(() => {
4848
if (index == null) {
@@ -69,7 +69,10 @@ function Search({ font_size, desc }: Props) {
6969
<div className="smc-vfill">
7070
<Card
7171
title={
72-
<>Search Chatroom {separate_file_extension(path_split(path).tail).name}</>
72+
<>
73+
Search Chatroom{" "}
74+
{separate_file_extension(path_split(path).tail).name}
75+
</>
7376
}
7477
style={{ fontSize: font_size }}
7578
>
@@ -93,7 +96,7 @@ function Search({ font_size, desc }: Props) {
9396
<div className="smc-vfill">
9497
<div style={{ overflow: "auto", padding: "15px" }}>
9598
{result?.hits?.map((hit) => (
96-
<SearchResult key={hit.id} hit={hit} actions={actions} />
99+
<SearchResult key={hit.id} hit={hit} actions={actions} fragmentKey={fragmentKey} />
97100
))}
98101
{result?.hits == null && search?.trim() && <div>No hits</div>}
99102
</div>
@@ -102,7 +105,7 @@ function Search({ font_size, desc }: Props) {
102105
);
103106
}
104107

105-
function SearchResult({ hit, actions }) {
108+
function SearchResult({ hit, actions, fragmentKey }) {
106109
const { document } = hit;
107110
return (
108111
<div
@@ -117,12 +120,12 @@ function SearchResult({ hit, actions }) {
117120
overflow: "hidden",
118121
}}
119122
onClick={() => {
120-
actions.gotoFragment({ chat: document.time });
123+
actions.gotoFragment({ [fragmentKey]: document.id });
121124
}}
122125
>
123-
<TimeAgo style={{ float: "right", color: "#888" }} date={document.time} />
126+
<TimeAgo style={{ float: "right", color: "#888" }} date={parseFloat(document.id)} />
124127
<StaticMarkdown
125-
value={document.message}
128+
value={document.content}
126129
style={{ marginBottom: "-10px" /* account for <p> */ }}
127130
/>
128131
</div>

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default function useSearchIndex() {
1313
const [error, setError] = useState<string>("");
1414
const { val: refresh, inc: doRefresh } = useCounter();
1515
const [indexTime, setIndexTime] = useState<number>(0);
16+
const [fragmentKey, setFragmentKey] = useState<string>("id");
1617

1718
useEffect(() => {
1819
if (
@@ -26,34 +27,40 @@ export default function useSearchIndex() {
2627
try {
2728
setError("");
2829
const t0 = Date.now();
29-
const index = new SearchIndex({ actions });
30-
await index.init();
31-
setIndex(index);
30+
const newIndex = new SearchIndex({ actions });
31+
await newIndex.init();
32+
setFragmentKey(newIndex.fragmentKey ?? "id");
33+
setIndex(newIndex);
3234
setIndexTime(Date.now() - t0);
35+
//index?.close();
3336
} catch (err) {
3437
setError(`${err}`);
3538
}
3639
})();
3740
}, [project_id, path, refresh]);
3841

39-
return { index, error, doRefresh, setError, indexTime };
42+
return { index, error, doRefresh, setError, indexTime, fragmentKey };
4043
}
4144

4245
class SearchIndex {
43-
private actions;
44-
private state: "init" | "ready" | "failed" = "init";
45-
private error: Error | null = null;
46-
private db;
46+
private actions?;
47+
private state: "init" | "ready" | "failed" | "closed" = "init";
48+
private db?;
49+
public fragmentKey?: string = "id";
4750

4851
constructor({ actions }) {
4952
this.actions = actions;
5053
}
5154

52-
getState = () => this.state;
53-
getError = () => this.error;
55+
close = () => {
56+
this.state = "closed";
57+
delete this.actions;
58+
delete this.db;
59+
delete this.fragmentKey;
60+
};
5461

5562
search = async (query) => {
56-
if (this.state != "ready") {
63+
if (this.state != "ready" || this.db == null) {
5764
throw Error("index not ready");
5865
}
5966
return await search(this.db, query);
@@ -62,19 +69,22 @@ class SearchIndex {
6269
init = async () => {
6370
this.db = await create({
6471
schema: {
65-
time: "number",
66-
message: "string",
72+
content: "string",
6773
},
6874
});
6975

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-
});
76+
if (this.actions == null || this.state != "init") {
77+
throw Error("not in init state");
78+
}
79+
const { data, fragmentKey } = this.actions.getSearchIndexData();
80+
this.fragmentKey = fragmentKey;
81+
if (data != null) {
82+
const docs: { id: string; content: string }[] = [];
83+
for (const id in data) {
84+
const content = data[id]?.trim();
85+
if (content) {
86+
docs.push({ id, content });
87+
}
7888
}
7989
await insertMultiple(this.db, docs);
8090
}

0 commit comments

Comments
 (0)