-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
Suggestions.tsx
183 lines (156 loc) · 6.42 KB
/
Suggestions.tsx
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type {ForwardedRef} from 'react';
import React, {forwardRef, useCallback, useContext, useEffect, useImperativeHandle, useRef} from 'react';
import type {NativeSyntheticEvent, TextInputSelectionChangeEventData} from 'react-native';
import {View} from 'react-native';
import type {MeasureParentContainerAndCursorCallback} from '@components/AutoCompleteSuggestions/types';
import type {TextSelection} from '@components/Composer/types';
import {DragAndDropContext} from '@components/DragAndDrop/Provider';
import usePrevious from '@hooks/usePrevious';
import type {SuggestionsRef} from './ReportActionCompose';
import SuggestionEmoji from './SuggestionEmoji';
import SuggestionMention from './SuggestionMention';
type SuggestionProps = {
/** The current input value */
value: string;
/** The current selection value */
selection: TextSelection;
/** Callback to update the current selection */
setSelection: (newSelection: TextSelection) => void;
/** Callback to update the comment draft */
updateComment: (newComment: string, shouldDebounceSaveComment?: boolean) => void;
/** Measures the parent container's position and dimensions. Also add cursor coordinates */
measureParentContainerAndReportCursor: (callback: MeasureParentContainerAndCursorCallback) => void;
/** Report composer focus state */
isComposerFocused?: boolean;
/** Callback to reset the keyboard input */
resetKeyboardInput?: () => void;
/** Whether the auto suggestion picker is large */
isAutoSuggestionPickerLarge?: boolean;
/** The height of the composer */
composerHeight?: number;
/** If current composer is connected with report from group policy */
isGroupPolicyReport: boolean;
/** The policyID of the report connected to current composer */
policyID?: string;
};
/**
* This component contains the individual suggestion components.
* If you want to add a new suggestion type, add it here.
*
*/
function Suggestions(
{
value,
selection,
setSelection,
updateComment,
resetKeyboardInput,
measureParentContainerAndReportCursor,
isAutoSuggestionPickerLarge = true,
isComposerFocused,
isGroupPolicyReport,
policyID,
}: SuggestionProps,
ref: ForwardedRef<SuggestionsRef>,
) {
const suggestionEmojiRef = useRef<SuggestionsRef>(null);
const suggestionMentionRef = useRef<SuggestionsRef>(null);
const {isDraggingOver} = useContext(DragAndDropContext);
const prevIsDraggingOver = usePrevious(isDraggingOver);
const getSuggestions = useCallback(() => {
if (suggestionEmojiRef.current?.getSuggestions) {
const emojiSuggestions = suggestionEmojiRef.current.getSuggestions();
if (emojiSuggestions.length > 0) {
return emojiSuggestions;
}
}
if (suggestionMentionRef.current?.getSuggestions) {
const mentionSuggestions = suggestionMentionRef.current.getSuggestions();
if (mentionSuggestions.length > 0) {
return mentionSuggestions;
}
}
return [];
}, []);
/**
* Clean data related to EmojiSuggestions
*/
const resetSuggestions = useCallback(() => {
suggestionEmojiRef.current?.resetSuggestions();
suggestionMentionRef.current?.resetSuggestions();
}, []);
/**
* Listens for keyboard shortcuts and applies the action
*/
const triggerHotkeyActions = useCallback((e: KeyboardEvent) => {
const emojiHandler = suggestionEmojiRef.current?.triggerHotkeyActions(e);
const mentionHandler = suggestionMentionRef.current?.triggerHotkeyActions(e);
return emojiHandler ?? mentionHandler;
}, []);
const onSelectionChange = useCallback((e: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
const emojiHandler = suggestionEmojiRef.current?.onSelectionChange?.(e);
suggestionMentionRef.current?.onSelectionChange?.(e);
return emojiHandler;
}, []);
const updateShouldShowSuggestionMenuToFalse = useCallback(() => {
suggestionEmojiRef.current?.updateShouldShowSuggestionMenuToFalse();
suggestionMentionRef.current?.updateShouldShowSuggestionMenuToFalse();
}, []);
const setShouldBlockSuggestionCalc = useCallback((shouldBlock: boolean) => {
suggestionEmojiRef.current?.setShouldBlockSuggestionCalc(shouldBlock);
suggestionMentionRef.current?.setShouldBlockSuggestionCalc(shouldBlock);
}, []);
const getIsSuggestionsMenuVisible = useCallback((): boolean => {
const isEmojiVisible = suggestionEmojiRef.current?.getIsSuggestionsMenuVisible() ?? false;
const isSuggestionVisible = suggestionMentionRef.current?.getIsSuggestionsMenuVisible() ?? false;
return isEmojiVisible || isSuggestionVisible;
}, []);
useImperativeHandle(
ref,
() => ({
resetSuggestions,
onSelectionChange,
triggerHotkeyActions,
updateShouldShowSuggestionMenuToFalse,
setShouldBlockSuggestionCalc,
getSuggestions,
getIsSuggestionsMenuVisible,
}),
[onSelectionChange, resetSuggestions, setShouldBlockSuggestionCalc, triggerHotkeyActions, updateShouldShowSuggestionMenuToFalse, getSuggestions, getIsSuggestionsMenuVisible],
);
useEffect(() => {
if (!(!prevIsDraggingOver && isDraggingOver)) {
return;
}
updateShouldShowSuggestionMenuToFalse();
}, [isDraggingOver, prevIsDraggingOver, updateShouldShowSuggestionMenuToFalse]);
const baseProps = {
value,
setSelection,
selection,
updateComment,
isAutoSuggestionPickerLarge,
measureParentContainerAndReportCursor,
isComposerFocused,
isGroupPolicyReport,
policyID,
};
return (
<View testID="suggestions">
<SuggestionEmoji
ref={suggestionEmojiRef}
// eslint-disable-next-line react/jsx-props-no-spreading
{...baseProps}
resetKeyboardInput={resetKeyboardInput}
/>
<SuggestionMention
ref={suggestionMentionRef}
// eslint-disable-next-line react/jsx-props-no-spreading
{...baseProps}
/>
</View>
);
}
Suggestions.displayName = 'Suggestions';
export default forwardRef(Suggestions);
export type {SuggestionProps};