Skip to content

Commit ea040b3

Browse files
committed
chat: automatically update search index when messages change
- it's so fast, even for ~1000 messages, let's just automate it.
1 parent 5a30b3e commit ea040b3

File tree

6 files changed

+37
-48
lines changed

6 files changed

+37
-48
lines changed

src/packages/frontend/chat/filter.tsx

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,22 @@ export default function Filter({ actions, search, style }) {
88
setValue(search);
99
}, [search]);
1010

11-
const debouncedSearch = useMemo(
12-
() =>
13-
debounce(actions.setSearch, 200, {
14-
leading: false,
15-
trailing: true,
16-
}),
17-
[actions],
18-
);
11+
const debouncedSearch = useMemo(() => {
12+
return debounce(actions.setSearch, 200, {
13+
leading: false,
14+
trailing: true,
15+
});
16+
}, [actions]);
1917

2018
return (
2119
<Tooltip
2220
title={
23-
!value
24-
? "Show only threads that match this search. Use /re/ for a regular expression, quotes, and dashes to negate."
25-
: undefined
21+
!value ? (
22+
<>
23+
Show only threads that match this filter. Use /re/ for a regular
24+
expression, quotes, and dashes to negate.
25+
</>
26+
) : undefined
2627
}
2728
>
2829
<Input.Search

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

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ Full text search that is better than a simple filter.
99

1010
import { useFrameContext } from "@cocalc/frontend/frame-editors/frame-tree/frame-context";
1111
import type { EditorDescription } from "@cocalc/frontend/frame-editors/frame-tree/types";
12-
import { Button, Card, Input, Tooltip } from "antd";
13-
import { set } from "@cocalc/util/misc";
12+
import { Card, Input } from "antd";
13+
import { path_split, separate_file_extension, set } from "@cocalc/util/misc";
1414
import { useEffect, useMemo, useState } from "react";
1515
import { throttle } from "lodash";
1616
import useSearchIndex from "./use-search-index";
1717
import ShowError from "@cocalc/frontend/components/error";
18-
import { Icon } from "@cocalc/frontend/components/icon";
1918
import StaticMarkdown from "@cocalc/frontend/editors/slate/static-markdown";
2019
import { TimeAgo } from "@cocalc/frontend/components";
20+
import { useEditorRedux } from "@cocalc/frontend/app-framework";
21+
import type { ChatState } from "@cocalc/frontend/chat/store";
2122

2223
interface Props {
2324
font_size: number;
@@ -26,6 +27,9 @@ interface Props {
2627

2728
function Search({ font_size, desc }: Props) {
2829
const { project_id, path, actions, id } = useFrameContext();
30+
const useEditor = useEditorRedux<ChatState>({ project_id, path });
31+
const messages = useEditor("messages");
32+
const [indexedMessages, setIndexedMessages] = useState<any>(messages);
2933
const [search, setSearch] = useState<string>(desc.get("data-search") ?? "");
3034
const [result, setResult] = useState<any>(null);
3135
const saveSearch = useMemo(
@@ -38,7 +42,7 @@ function Search({ font_size, desc }: Props) {
3842
[project_id, path],
3943
);
4044

41-
const { error, setError, index, doRefresh, indexTime } = useSearchIndex();
45+
const { error, setError, index, doRefresh } = useSearchIndex();
4246

4347
useEffect(() => {
4448
if (index == null) {
@@ -54,31 +58,18 @@ function Search({ font_size, desc }: Props) {
5458
})();
5559
}, [search, index]);
5660

61+
useEffect(() => {
62+
if (indexedMessages != messages) {
63+
setIndexedMessages(messages);
64+
doRefresh();
65+
}
66+
}, [messages]);
67+
5768
return (
5869
<div className="smc-vfill">
5970
<Card
6071
title={
61-
<>
62-
Search {path}
63-
<Tooltip
64-
title={
65-
<>
66-
Recreate search index.{" "}
67-
{indexTime ? <>Time: {indexTime}ms</> : undefined}
68-
</>
69-
}
70-
>
71-
<Button
72-
onClick={() => {
73-
doRefresh();
74-
}}
75-
style={{ float: "right" }}
76-
>
77-
<Icon name="reload" />
78-
Refresh
79-
</Button>
80-
</Tooltip>
81-
</>
72+
<>Search Chatroom {separate_file_extension(path_split(path).tail).name}</>
8273
}
8374
style={{ fontSize: font_size }}
8475
>
@@ -90,7 +81,7 @@ function Search({ font_size, desc }: Props) {
9081
<Input.Search
9182
autoFocus
9283
allowClear
93-
placeholder="Search for messages..."
84+
placeholder="Search messages..."
9485
value={search}
9586
onChange={(e) => {
9687
const search = e.target.value ?? "";
@@ -117,7 +108,7 @@ function SearchResult({ hit, actions }) {
117108
<div
118109
style={{
119110
cursor: "pointer",
120-
margin: "5px 0",
111+
margin: "10px 0",
121112
padding: "5px",
122113
border: "1px solid #ccc",
123114
background: "#f8f8f8",

src/packages/frontend/frame-editors/frame-tree/editor.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ const FrameTreeEditor: React.FC<FrameTreeEditorProps> = React.memo(
115115
useEffect(() => {
116116
if (!frameRootRef.current) return;
117117
const observer = new ResizeObserver(() => {
118-
actions.set_resize();
118+
actions.set_resize?.();
119119
});
120120
observer.observe(frameRootRef.current);
121121
return () => observer.disconnect();

src/packages/frontend/frame-editors/frame-tree/frame-tree-drag-bar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const FrameTreeDragBar: React.FC<Props> = React.memo((props: Props) => {
9090
id: frame_tree.get("id"),
9191
pos,
9292
});
93-
actions.set_resize();
93+
actions.set_resize?.();
9494
actions.focus(); // see https://github.com/sagemathinc/cocalc/issues/3269
9595
}
9696

src/packages/frontend/frame-editors/frame-tree/register.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ interface Register {
3737
}
3838

3939
function isAsyncRegister(
40-
opts: Register | AsyncRegister
40+
opts: Register | AsyncRegister,
4141
): opts is AsyncRegister {
4242
return opts["editor"] != null;
4343
}
@@ -71,7 +71,7 @@ export function register_file_editor(opts: Register | AsyncRegister) {
7171
opts.component,
7272
opts.Actions,
7373
opts.asyncData,
74-
is_public
74+
is_public,
7575
);
7676
}
7777
}
@@ -95,7 +95,7 @@ function register(
9595
component: any;
9696
Actions: any;
9797
}>),
98-
is_public: boolean
98+
is_public: boolean,
9999
) {
100100
let data: any = {
101101
icon,
@@ -144,9 +144,7 @@ function register(
144144
if (is_public) return;
145145
const name = redux_name(project_id, path);
146146
const actions = redux.getActions(name);
147-
if (actions) {
148-
actions.save();
149-
}
147+
actions?.save?.();
150148
},
151149
};
152150

@@ -179,7 +177,7 @@ function register(
179177
} else {
180178
if (asyncData == null) {
181179
throw Error(
182-
"either asyncData must be given or components and Actions must be given (or both)"
180+
"either asyncData must be given or components and Actions must be given (or both)",
183181
);
184182
}
185183
let async_data: any = undefined;

src/scripts/g.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ unset DEBUG_CONSOLE
1010

1111
#export COCALC_DISABLE_API_VALIDATION=yes
1212
#export NO_RSPACK_DEV_SERVER=yes
13-
#ulimit -Sv 120000000
1413

1514
while true; do
1615
pnpm hub

0 commit comments

Comments
 (0)