Skip to content

Commit f523b90

Browse files
committed
fix: make command search case-insensitive and reset custom command search source before use
1 parent 448b6ac commit f523b90

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

src/messageComposer/middleware/textComposer/commands.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import type { Channel } from '../../../channel';
1111
import type { TextComposerSuggestion } from '../../types';
1212

1313
type CommandSuggestion = TextComposerSuggestion<CommandResponse>;
14-
class CommandSearchSource extends BaseSearchSource<CommandSuggestion> {
14+
15+
export class CommandSearchSource extends BaseSearchSource<CommandSuggestion> {
1516
readonly type = 'commands';
1617
private channel: Channel;
1718

@@ -25,7 +26,7 @@ class CommandSearchSource extends BaseSearchSource<CommandSuggestion> {
2526
return this.isActive && !this.isLoading && (this.hasNext || hasNewSearchQuery);
2627
};
2728

28-
protected getStateBeforeFirstQuery(newSearchString: string) {
29+
getStateBeforeFirstQuery(newSearchString: string) {
2930
const newState = super.getStateBeforeFirstQuery(newSearchString);
3031
const { items } = this.state.getLatestValue();
3132
return {
@@ -39,7 +40,10 @@ class CommandSearchSource extends BaseSearchSource<CommandSuggestion> {
3940
const commands = channelConfig?.commands || [];
4041
const selectedCommands: (CommandResponse & { name: string })[] = commands.filter(
4142
(command): command is CommandResponse & { name: string } =>
42-
!!(command.name && command.name.indexOf(searchQuery) !== -1),
43+
!!(
44+
command.name &&
45+
command.name.toLowerCase().indexOf(searchQuery.toLowerCase()) !== -1
46+
),
4347
);
4448

4549
// sort alphabetically unless you're matching the first char
@@ -94,10 +98,16 @@ const DEFAULT_OPTIONS: TextComposerMiddlewareOptions = { minChars: 1, trigger: '
9498

9599
export const createCommandsMiddleware = (
96100
channel: Channel,
97-
options?: TextComposerMiddlewareOptions,
101+
options?: Partial<TextComposerMiddlewareOptions> & {
102+
searchSource?: CommandSearchSource;
103+
},
98104
) => {
99105
const finalOptions = mergeWith(DEFAULT_OPTIONS, options ?? {});
100-
const searchSource = new CommandSearchSource(channel);
106+
let searchSource = new CommandSearchSource(channel);
107+
if (options?.searchSource) {
108+
searchSource = options.searchSource;
109+
searchSource.resetState();
110+
}
101111
searchSource.activate();
102112

103113
return {

0 commit comments

Comments
 (0)