forked from GetStream/stream-chat-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseCommandTrigger.ts
More file actions
99 lines (85 loc) Β· 3.05 KB
/
useCommandTrigger.ts
File metadata and controls
99 lines (85 loc) Β· 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { CommandItem } from '../../CommandItem/CommandItem';
import { useChannelStateContext } from '../../../context/ChannelStateContext';
import { useTranslationContext } from '../../../context';
import type { CommandResponse } from 'stream-chat';
import type { CommandTriggerSetting } from '../DefaultTriggerProvider';
import type { DefaultStreamChatGenerics } from '../../../types/types';
type ValidCommand<
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
> = Required<Pick<CommandResponse<StreamChatGenerics>, 'name'>> &
Omit<CommandResponse<StreamChatGenerics>, 'name'>;
export const useCommandTrigger = <
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
>(): CommandTriggerSetting<StreamChatGenerics> => {
const { channelConfig } =
useChannelStateContext<StreamChatGenerics>('useCommandTrigger');
const { t } = useTranslationContext('useCommandTrigger');
const commands = channelConfig?.commands;
return {
component: CommandItem,
dataProvider: (query, text, onReady) => {
if (text.indexOf('/') !== 0 || !commands) {
return [];
}
const selectedCommands = commands.filter(
(command) => command.name?.indexOf(query) !== -1,
);
// sort alphabetically unless you're matching the first char
selectedCommands.sort((a, b) => {
let nameA = a.name?.toLowerCase();
let nameB = b.name?.toLowerCase();
if (nameA?.indexOf(query) === 0) {
nameA = `0${nameA}`;
}
if (nameB?.indexOf(query) === 0) {
nameB = `0${nameB}`;
}
// Should confirm possible null / undefined when TS is fully implemented
if (nameA != null && nameB != null) {
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
}
return 0;
});
const result = selectedCommands.slice(0, 5);
if (onReady)
onReady(
result
.filter(
(
result,
): result is CommandResponse<StreamChatGenerics> & { name: string } =>
result.name !== undefined,
)
.map((commandData) => {
const translatedCommandData: ValidCommand<StreamChatGenerics> = {
name: commandData.name,
};
if (commandData.args)
translatedCommandData.args = t(`${commandData.name}-command-args`, {
defaultValue: commandData.args,
});
if (commandData.description)
translatedCommandData.description = t(
`${commandData.name}-command-description`,
{
defaultValue: commandData.description,
},
);
return translatedCommandData;
}),
query,
);
return result;
},
output: (entity) => ({
caretPosition: 'next',
key: entity.name,
text: `/${entity.name}`,
}),
};
};