Skip to content

Commit 699ff25

Browse files
committed
feat: add MentionsSearchSource with support for discarding obsolete middleware results
1 parent cdf85bc commit 699ff25

File tree

8 files changed

+363
-63
lines changed

8 files changed

+363
-63
lines changed

src/channel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ export class Channel {
161161
this._messageComposer = new MessageComposer({ channel: this });
162162
this._messageComposer.textComposer.use([
163163
createCommandsMiddleware(this),
164-
createMentionsMiddleware(client),
164+
createMentionsMiddleware(this),
165165
] as TextComposerMiddleware[]);
166166
}
167167

src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export const DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE = 100;
33
export const DEFAULT_MESSAGE_SET_PAGINATION = { hasNext: false, hasPrev: false };
44
export const DEFAULT_UPLOAD_SIZE_LIMIT_BYTES = 100 * 1024 * 1024; // 100 MB
55
export const API_MAX_FILES_ALLOWED_PER_MESSAGE = 10;
6+
export const MAX_CHANNEL_MEMBER_COUNT_IN_CHANNEL_QUERY = 100;

src/messageComposer/middleware/commands.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ class CommandSearchSource extends BaseSearchSource<CommandSuggestion> {
1717
this.channel = channel;
1818
}
1919

20-
protected query(searchQuery: string) {
20+
canExecuteQuery = (newSearchString?: string) => {
21+
const hasNewSearchQuery = typeof newSearchString !== 'undefined';
22+
return this.isActive && !this.isLoading && (this.hasNext || hasNewSearchQuery);
23+
};
24+
25+
query(searchQuery: string) {
2126
const channelConfig = this.channel.getConfig();
2227
const commands = channelConfig?.commands || [];
2328
const selectedCommands: (CommandResponse & { name: string })[] = commands.filter(
@@ -80,6 +85,9 @@ export const createCommandsMiddleware = (
8085
options?: TextComposerMiddlewareOptions,
8186
) => {
8287
const finalOptions = mergeWith(DEFAULT_OPTIONS, options ?? {});
88+
const searchSource = new CommandSearchSource(channel);
89+
searchSource.activate();
90+
8391
return {
8492
id: finalOptions.trigger,
8593
onChange: ({ input, nextHandler }: MiddlewareParams<CommandSuggestion>) => {
@@ -92,13 +100,15 @@ export const createCommandsMiddleware = (
92100
);
93101

94102
if (!lastToken || lastToken.length < finalOptions.minChars) {
95-
// check whether suggestions already exist and if so remove them
96-
return nextHandler(input);
103+
const hasStaleSuggestions =
104+
input.state.suggestions?.trigger === finalOptions.trigger;
105+
const newInput = { ...input };
106+
if (hasStaleSuggestions) {
107+
delete newInput.state.suggestions;
108+
}
109+
return nextHandler(newInput);
97110
}
98111

99-
const searchSource = new CommandSearchSource(channel);
100-
searchSource.activate();
101-
102112
return Promise.resolve({
103113
state: {
104114
...state,

0 commit comments

Comments
 (0)