2021-12-02 17:49:13 +01:00
|
|
|
// @flow
|
2021-12-06 19:06:40 +01:00
|
|
|
import { EMOTES_48px as EMOTES } from 'constants/emotes';
|
2021-12-02 17:49:13 +01:00
|
|
|
import { matchSorter } from 'match-sorter';
|
2021-12-06 20:39:39 +01:00
|
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
2021-12-02 17:49:13 +01:00
|
|
|
import * as KEYCODES from 'constants/keycodes';
|
2021-12-06 18:28:36 +01:00
|
|
|
import Autocomplete from '@mui/material/Autocomplete';
|
2021-12-07 15:51:55 +01:00
|
|
|
import BusyIndicator from 'component/common/busy-indicator';
|
2021-12-06 21:38:13 +01:00
|
|
|
import EMOJIS from 'emoji-dictionary';
|
2021-12-07 15:51:55 +01:00
|
|
|
import Popper from '@mui/material/Popper';
|
2021-12-02 17:49:13 +01:00
|
|
|
import React from 'react';
|
2021-12-06 20:39:39 +01:00
|
|
|
import useLighthouse from 'effects/use-lighthouse';
|
2021-12-02 17:49:13 +01:00
|
|
|
import useThrottle from 'effects/use-throttle';
|
2022-01-26 13:27:59 +01:00
|
|
|
import { parseURI } from 'util/lbryURI';
|
2022-02-02 13:49:02 +01:00
|
|
|
import TextareaSuggestionsOption from './render-option';
|
|
|
|
import TextareaSuggestionsInput from './render-input';
|
|
|
|
import TextareaSuggestionsGroup from './render-group';
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const SUGGESTION_REGEX = new RegExp(
|
2021-12-10 16:12:18 +01:00
|
|
|
'((?:^| |\n)@[^\\s=&#$@%?:;/\\"<>%{}|^~[]*(?::[\\w]+)?)|((?:^| |\n):[\\w+-]*:?)',
|
2021-12-06 19:06:40 +01:00
|
|
|
'gm'
|
|
|
|
);
|
|
|
|
|
|
|
|
/** Regex Explained step-by-step:
|
|
|
|
*
|
2021-12-10 16:12:18 +01:00
|
|
|
* 1) ()|() = different capturing groups (either Mention or Emote)
|
2021-12-06 19:06:40 +01:00
|
|
|
* 2) (?:^| |\n) = only allow for: sentence beginning, space or newline before the match (no words or symbols)
|
|
|
|
* 3) [^\s=&#$@%?:;/\\"<>%{}|^~[]* = anything, except the characters inside
|
|
|
|
* 4) (?::[\w]+)? = A mention can be matched with a ':' as a claim modifier with words or digits after as ID digits,
|
|
|
|
* or else it's everything before the ':' (will then match the winning uri for the mention behind since has no canonical ID)
|
|
|
|
* 5) :\w*:? = the emote Regex, possible to be matched with a ':' at the end to consider previously typed emotes
|
|
|
|
*
|
|
|
|
*/
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 20:39:39 +01:00
|
|
|
const SEARCH_SIZE = 10;
|
|
|
|
const LIGHTHOUSE_MIN_CHARACTERS = 3;
|
|
|
|
const INPUT_DEBOUNCE_MS = 1000;
|
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
const EMOJI_MIN_CHARACTERS = 2;
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
type Props = {
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalCommentors?: Array<string>,
|
|
|
|
canonicalCreatorUri?: string,
|
2021-12-06 20:39:39 +01:00
|
|
|
canonicalSearch?: Array<string>,
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalSubscriptions?: Array<string>,
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalTop?: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
className?: string,
|
2021-12-06 18:28:36 +01:00
|
|
|
commentorUris?: Array<string>,
|
|
|
|
disabled?: boolean,
|
2021-12-07 15:51:55 +01:00
|
|
|
hasNewResolvedResults?: boolean,
|
2021-12-06 18:28:36 +01:00
|
|
|
id: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
inputRef: any,
|
|
|
|
isLivestream?: boolean,
|
|
|
|
maxLength?: number,
|
|
|
|
placeholder?: string,
|
2021-12-07 19:17:29 +01:00
|
|
|
searchQuery?: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
type?: string,
|
2021-12-06 18:28:36 +01:00
|
|
|
uri?: string,
|
2021-12-02 17:49:13 +01:00
|
|
|
value: any,
|
2022-02-04 21:59:11 +01:00
|
|
|
autoFocus?: boolean,
|
|
|
|
submitButtonRef?: any,
|
2022-04-06 04:39:27 +02:00
|
|
|
spellCheck?: boolean,
|
2022-02-04 21:59:11 +01:00
|
|
|
claimIsMine?: boolean,
|
2022-02-07 20:30:42 +01:00
|
|
|
slimInput?: boolean,
|
2022-02-02 13:49:02 +01:00
|
|
|
doResolveUris: (uris: Array<string>, cache: boolean) => void,
|
|
|
|
doSetMentionSearchResults: (query: string, uris: Array<string>) => void,
|
2021-12-06 18:28:36 +01:00
|
|
|
onBlur: (any) => any,
|
2021-12-02 17:49:13 +01:00
|
|
|
onChange: (any) => any,
|
2021-12-06 18:28:36 +01:00
|
|
|
onFocus: (any) => any,
|
2022-02-04 21:59:11 +01:00
|
|
|
toggleSelectors: () => any,
|
2022-02-02 13:47:04 +01:00
|
|
|
handleTip: (isLBC: boolean) => any,
|
|
|
|
handleSubmit: () => any,
|
2022-02-04 21:59:11 +01:00
|
|
|
handlePreventClick?: () => void,
|
2021-12-02 17:49:13 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default function TextareaWithSuggestions(props: Props) {
|
|
|
|
const {
|
|
|
|
canonicalCommentors,
|
|
|
|
canonicalCreatorUri,
|
2021-12-06 20:39:39 +01:00
|
|
|
canonicalSearch,
|
2021-12-06 18:28:36 +01:00
|
|
|
canonicalSubscriptions: canonicalSubs,
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalTop,
|
2021-12-02 17:49:13 +01:00
|
|
|
className,
|
|
|
|
commentorUris,
|
2021-12-06 18:28:36 +01:00
|
|
|
disabled,
|
2021-12-07 15:51:55 +01:00
|
|
|
hasNewResolvedResults,
|
2021-12-06 18:28:36 +01:00
|
|
|
id,
|
2021-12-02 17:49:13 +01:00
|
|
|
inputRef,
|
|
|
|
isLivestream,
|
|
|
|
maxLength,
|
|
|
|
placeholder,
|
2021-12-07 19:17:29 +01:00
|
|
|
searchQuery,
|
2021-12-02 17:49:13 +01:00
|
|
|
type,
|
2021-12-06 19:06:40 +01:00
|
|
|
value: messageValue,
|
2022-02-04 21:59:11 +01:00
|
|
|
autoFocus,
|
|
|
|
submitButtonRef,
|
2022-04-06 04:39:27 +02:00
|
|
|
spellCheck,
|
2022-02-04 21:59:11 +01:00
|
|
|
claimIsMine,
|
2022-02-07 20:30:42 +01:00
|
|
|
slimInput,
|
2021-12-06 18:28:36 +01:00
|
|
|
doResolveUris,
|
2021-12-07 15:51:55 +01:00
|
|
|
doSetMentionSearchResults,
|
2021-12-06 18:28:36 +01:00
|
|
|
onBlur,
|
2021-12-02 17:49:13 +01:00
|
|
|
onChange,
|
2021-12-06 18:28:36 +01:00
|
|
|
onFocus,
|
2022-02-04 21:59:11 +01:00
|
|
|
toggleSelectors,
|
2022-02-02 13:47:04 +01:00
|
|
|
handleTip,
|
|
|
|
handleSubmit,
|
2022-02-04 21:59:11 +01:00
|
|
|
handlePreventClick,
|
2021-12-02 17:49:13 +01:00
|
|
|
} = props;
|
|
|
|
|
2022-04-06 04:39:27 +02:00
|
|
|
const inputDefaultProps = { className, placeholder, maxLength, spellCheck, type, disabled };
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const [suggestionValue, setSuggestionValue] = React.useState(undefined);
|
2021-12-06 18:28:36 +01:00
|
|
|
const [highlightedSuggestion, setHighlightedSuggestion] = React.useState('');
|
|
|
|
const [shouldClose, setClose] = React.useState();
|
2021-12-06 20:39:39 +01:00
|
|
|
const [debouncedTerm, setDebouncedTerm] = React.useState('');
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const suggestionTerm = suggestionValue && suggestionValue.term;
|
2022-02-02 13:49:02 +01:00
|
|
|
const isEmote = Boolean(suggestionValue && suggestionValue.isEmote);
|
2021-12-06 20:39:39 +01:00
|
|
|
const isMention = suggestionValue && !suggestionValue.isEmote;
|
2022-01-26 13:27:59 +01:00
|
|
|
|
|
|
|
let invalidTerm = suggestionTerm && isMention && suggestionTerm.charAt(1) === ':';
|
|
|
|
if (isMention && suggestionTerm) {
|
|
|
|
try {
|
|
|
|
parseURI(suggestionTerm);
|
|
|
|
} catch (error) {
|
|
|
|
invalidTerm = true;
|
|
|
|
}
|
|
|
|
}
|
2021-12-06 20:39:39 +01:00
|
|
|
|
|
|
|
const additionalOptions = { isBackgroundSearch: false, [SEARCH_OPTIONS.CLAIM_TYPE]: SEARCH_OPTIONS.INCLUDE_CHANNELS };
|
2022-02-02 13:49:02 +01:00
|
|
|
const { results, loading } = useLighthouse(debouncedTerm, false, SEARCH_SIZE, additionalOptions, 0);
|
2021-12-06 20:39:39 +01:00
|
|
|
const stringifiedResults = JSON.stringify(results);
|
|
|
|
|
|
|
|
const hasMinLength = suggestionTerm && isMention && suggestionTerm.length >= LIGHTHOUSE_MIN_CHARACTERS;
|
|
|
|
const isTyping = isMention && debouncedTerm !== suggestionTerm;
|
2021-12-07 15:51:55 +01:00
|
|
|
const showPlaceholder =
|
2021-12-09 21:17:29 +01:00
|
|
|
isMention && !invalidTerm && (isTyping || loading || (results && results.length > 0 && !hasNewResolvedResults));
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const shouldFilter = (uri, previous) => uri !== canonicalCreatorUri && (!previous || !previous.includes(uri));
|
|
|
|
const filteredCommentors = canonicalCommentors && canonicalCommentors.filter((uri) => shouldFilter(uri));
|
|
|
|
const filteredSubs = canonicalSubs && canonicalSubs.filter((uri) => shouldFilter(uri, filteredCommentors));
|
2021-12-07 19:17:29 +01:00
|
|
|
const filteredTop =
|
|
|
|
canonicalTop &&
|
|
|
|
shouldFilter(canonicalTop, filteredSubs) &&
|
|
|
|
shouldFilter(canonicalTop, filteredCommentors) &&
|
|
|
|
canonicalTop;
|
2021-12-06 20:39:39 +01:00
|
|
|
const filteredSearch =
|
|
|
|
canonicalSearch &&
|
2021-12-07 19:17:29 +01:00
|
|
|
canonicalSearch.filter(
|
|
|
|
(uri) => shouldFilter(uri, filteredSubs) && shouldFilter(uri, filteredCommentors) && uri !== filteredTop
|
|
|
|
);
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
let emoteNames;
|
|
|
|
let emojiNames;
|
2021-12-06 18:28:36 +01:00
|
|
|
const allOptions = [];
|
2021-12-06 19:06:40 +01:00
|
|
|
if (isEmote) {
|
2022-01-05 21:20:38 +01:00
|
|
|
emoteNames = EMOTES.map(({ name }) => name);
|
2021-12-06 21:38:13 +01:00
|
|
|
const hasMinEmojiLength = suggestionTerm && suggestionTerm.length > EMOJI_MIN_CHARACTERS;
|
2021-12-07 17:05:10 +01:00
|
|
|
// Filter because our emotes are priority from default emojis, like :eggplant:
|
|
|
|
emojiNames = hasMinEmojiLength ? EMOJIS.names.filter((name) => !emoteNames.includes(`:${name}:`)) : [];
|
2021-12-06 21:38:13 +01:00
|
|
|
const emotesAndEmojis = [...emoteNames, ...emojiNames];
|
|
|
|
|
|
|
|
allOptions.push(...emotesAndEmojis);
|
2021-12-06 19:06:40 +01:00
|
|
|
} else {
|
|
|
|
if (canonicalCreatorUri) allOptions.push(canonicalCreatorUri);
|
|
|
|
if (filteredSubs) allOptions.push(...filteredSubs);
|
|
|
|
if (filteredCommentors) allOptions.push(...filteredCommentors);
|
2021-12-07 19:17:29 +01:00
|
|
|
if (filteredTop) allOptions.push(filteredTop);
|
2021-12-06 20:39:39 +01:00
|
|
|
if (filteredSearch) allOptions.push(...filteredSearch);
|
2021-12-06 19:06:40 +01:00
|
|
|
}
|
2021-12-06 18:28:36 +01:00
|
|
|
|
|
|
|
const allOptionsGrouped =
|
|
|
|
allOptions.length > 0
|
|
|
|
? allOptions.map((option) => {
|
2021-12-06 19:06:40 +01:00
|
|
|
const groupName = isEmote
|
2021-12-06 21:38:13 +01:00
|
|
|
? (emoteNames.includes(option) && __('Emotes')) || (emojiNames.includes(option) && __('Emojis'))
|
2021-12-06 19:06:40 +01:00
|
|
|
: (canonicalCreatorUri === option && __('Creator')) ||
|
|
|
|
(filteredSubs && filteredSubs.includes(option) && __('Following')) ||
|
2021-12-06 20:39:39 +01:00
|
|
|
(filteredCommentors && filteredCommentors.includes(option) && __('From Comments')) ||
|
2021-12-07 19:17:29 +01:00
|
|
|
(filteredTop && filteredTop === option && 'Top') ||
|
2021-12-06 20:39:39 +01:00
|
|
|
(filteredSearch && filteredSearch.includes(option) && __('From Search'));
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 21:38:13 +01:00
|
|
|
let emoteLabel;
|
|
|
|
if (isEmote) {
|
|
|
|
// $FlowFixMe
|
2022-02-02 13:49:02 +01:00
|
|
|
emoteLabel = `:${option.replace(/:/g, '')}:`;
|
2021-12-06 21:38:13 +01:00
|
|
|
}
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
return {
|
2021-12-06 21:38:13 +01:00
|
|
|
label: emoteLabel || option.replace('lbry://', '').replace('#', ':'),
|
2021-12-06 18:28:36 +01:00
|
|
|
group: groupName,
|
|
|
|
};
|
|
|
|
})
|
|
|
|
: [];
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-07 19:17:29 +01:00
|
|
|
const allMatches =
|
|
|
|
useSuggestionMatch(
|
|
|
|
suggestionTerm || '',
|
|
|
|
allOptionsGrouped.map(({ label }) => label)
|
|
|
|
) || [];
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
/** --------- **/
|
|
|
|
/** Functions **/
|
|
|
|
/** --------- **/
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
function handleInputChange(value: string) {
|
|
|
|
onChange({ target: { value } });
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
const cursorIndex = inputRef && inputRef.current && inputRef.current.selectionStart;
|
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const suggestionMatches = value.match(SUGGESTION_REGEX);
|
|
|
|
|
|
|
|
if (!suggestionMatches) {
|
2021-12-07 19:17:29 +01:00
|
|
|
if (suggestionValue) setSuggestionValue(null);
|
2021-12-06 19:06:40 +01:00
|
|
|
return; // Exit here and avoid unnecessary behavior
|
|
|
|
}
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const exec = SUGGESTION_REGEX.exec(value);
|
|
|
|
|
|
|
|
const previousLastIndexes = [];
|
2021-12-10 16:12:18 +01:00
|
|
|
let isEmote = exec && exec[2];
|
2021-12-06 19:06:40 +01:00
|
|
|
let currentSuggestionIndex = exec && exec.index;
|
|
|
|
let currentLastIndex = exec && SUGGESTION_REGEX.lastIndex;
|
|
|
|
let currentSuggestionValue =
|
|
|
|
cursorIndex >= currentSuggestionIndex &&
|
|
|
|
cursorIndex <= currentLastIndex &&
|
|
|
|
suggestionMatches &&
|
|
|
|
suggestionMatches[0];
|
|
|
|
|
|
|
|
if (suggestionMatches && suggestionMatches.length > 1) {
|
|
|
|
currentSuggestionValue = suggestionMatches.find((match, index) => {
|
|
|
|
const previousLastIndex = previousLastIndexes[index - 1] || 0;
|
|
|
|
const valueWithoutPrevious = value.substring(previousLastIndex);
|
|
|
|
|
|
|
|
const tempRe = new RegExp(SUGGESTION_REGEX);
|
|
|
|
const tempExec = tempRe.exec(valueWithoutPrevious);
|
|
|
|
|
|
|
|
if (tempExec) {
|
2021-12-10 16:12:18 +01:00
|
|
|
isEmote = tempExec && tempExec[2];
|
2021-12-06 19:06:40 +01:00
|
|
|
currentSuggestionIndex = previousLastIndex + tempExec.index;
|
|
|
|
currentLastIndex = previousLastIndex + tempRe.lastIndex;
|
|
|
|
previousLastIndexes.push(currentLastIndex);
|
|
|
|
}
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
// the current mention term will be the one on the text cursor's range,
|
|
|
|
// in case of there being more in the same comment message
|
2021-12-06 19:06:40 +01:00
|
|
|
if (previousLastIndexes) {
|
|
|
|
return cursorIndex >= currentSuggestionIndex && cursorIndex <= currentLastIndex;
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
});
|
2021-12-06 19:06:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (currentSuggestionValue) {
|
|
|
|
const token = isEmote ? ':' : '@';
|
|
|
|
const tokenIndex = currentSuggestionValue.indexOf(token);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2022-01-13 19:36:07 +01:00
|
|
|
if (inputRef && inputRef.current) inputRef.current.setAttribute('typing-term', '');
|
2021-12-02 17:49:13 +01:00
|
|
|
// $FlowFixMe
|
2021-12-06 19:06:40 +01:00
|
|
|
setSuggestionValue({
|
|
|
|
beforeTerm: currentSuggestionValue.substring(0, tokenIndex), // in case of a space or newline
|
|
|
|
term: currentSuggestionValue.substring(tokenIndex),
|
|
|
|
index: currentSuggestionIndex,
|
|
|
|
lastIndex: currentLastIndex,
|
|
|
|
isEmote,
|
|
|
|
});
|
2021-12-07 19:17:29 +01:00
|
|
|
} else if (suggestionValue) {
|
2022-01-14 22:07:21 +01:00
|
|
|
inputRef.current.removeAttribute('typing-term');
|
2021-12-07 19:17:29 +01:00
|
|
|
setSuggestionValue(null);
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSelect = React.useCallback(
|
2022-01-13 19:36:07 +01:00
|
|
|
(selectedValue: string, key?: number) => {
|
2021-12-02 17:49:13 +01:00
|
|
|
if (!suggestionValue) return;
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
const elem = inputRef && inputRef.current;
|
2021-12-06 19:06:40 +01:00
|
|
|
const newCursorPos = suggestionValue.beforeTerm.length + suggestionValue.index + selectedValue.length + 1;
|
2021-12-06 18:28:36 +01:00
|
|
|
|
2021-12-06 19:06:40 +01:00
|
|
|
const contentBegin = messageValue.substring(0, suggestionValue.index);
|
|
|
|
const replaceValue = suggestionValue.beforeTerm + selectedValue;
|
|
|
|
const contentEnd =
|
|
|
|
messageValue.length > suggestionValue.lastIndex
|
|
|
|
? messageValue.substring(suggestionValue.lastIndex, messageValue.length)
|
|
|
|
: ' ';
|
|
|
|
|
|
|
|
const newValue = contentBegin + replaceValue + contentEnd;
|
2021-12-02 17:49:13 +01:00
|
|
|
|
|
|
|
onChange({ target: { value: newValue } });
|
2021-12-07 19:17:29 +01:00
|
|
|
setSuggestionValue(null);
|
2022-01-13 19:36:07 +01:00
|
|
|
|
|
|
|
// no keycode === was selected with TAB (function was called by effect) or on click
|
|
|
|
// ENTER is handled on commentCreate after attempting to send on livestream
|
|
|
|
if (!key && inputRef && inputRef.current) inputRef.current.removeAttribute('typing-term');
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
elem.focus();
|
|
|
|
elem.setSelectionRange(newCursorPos, newCursorPos);
|
2021-12-02 17:49:13 +01:00
|
|
|
},
|
2021-12-06 19:06:40 +01:00
|
|
|
[messageValue, inputRef, onChange, suggestionValue]
|
2021-12-02 17:49:13 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/** ------- **/
|
|
|
|
/** Effects **/
|
|
|
|
/** ------- **/
|
|
|
|
|
2022-02-04 21:59:11 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!autoFocus) return;
|
|
|
|
|
|
|
|
const inputElement = inputRef && inputRef.current;
|
2022-02-08 13:39:35 +01:00
|
|
|
if (inputElement) {
|
|
|
|
inputElement.focus();
|
|
|
|
if (messageValue) inputElement.setSelectionRange(messageValue.length, messageValue.length);
|
|
|
|
}
|
2022-02-09 19:26:43 +01:00
|
|
|
// do NOT listen to messageValue change, otherwise will autofocus while typing
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [autoFocus, inputRef]);
|
2022-02-04 21:59:11 +01:00
|
|
|
|
2021-12-06 20:39:39 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
if (!isMention) return;
|
|
|
|
|
2021-12-09 21:17:29 +01:00
|
|
|
if (isTyping && suggestionTerm && !invalidTerm) {
|
2021-12-07 19:17:29 +01:00
|
|
|
const timer = setTimeout(() => {
|
|
|
|
setDebouncedTerm(!hasMinLength ? '' : suggestionTerm);
|
|
|
|
}, INPUT_DEBOUNCE_MS);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
2021-12-07 19:17:29 +01:00
|
|
|
return () => clearTimeout(timer);
|
|
|
|
}
|
2021-12-09 21:17:29 +01:00
|
|
|
}, [hasMinLength, invalidTerm, isMention, isTyping, suggestionTerm]);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (!stringifiedResults) return;
|
|
|
|
|
|
|
|
const arrayResults = JSON.parse(stringifiedResults);
|
2021-12-07 19:17:29 +01:00
|
|
|
if (debouncedTerm && arrayResults && arrayResults.length > 0) {
|
2022-02-02 13:49:02 +01:00
|
|
|
doResolveUris([debouncedTerm, ...arrayResults], true);
|
2021-12-07 19:17:29 +01:00
|
|
|
doSetMentionSearchResults(debouncedTerm, arrayResults);
|
2021-12-06 20:39:39 +01:00
|
|
|
}
|
2021-12-07 19:17:29 +01:00
|
|
|
}, [debouncedTerm, doResolveUris, doSetMentionSearchResults, stringifiedResults, suggestionTerm]);
|
2021-12-06 20:39:39 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
// Only resolve commentors on Livestreams when first trying to mention/looking for it
|
2021-12-02 17:49:13 +01:00
|
|
|
React.useEffect(() => {
|
2022-02-02 13:49:02 +01:00
|
|
|
if (isLivestream && commentorUris && suggestionTerm) doResolveUris(commentorUris, true);
|
2021-12-06 18:28:36 +01:00
|
|
|
}, [commentorUris, doResolveUris, isLivestream, suggestionTerm]);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
// Allow selecting with TAB key
|
2021-12-02 17:49:13 +01:00
|
|
|
React.useEffect(() => {
|
2021-12-06 19:06:40 +01:00
|
|
|
if (!suggestionTerm) return; // only if there is a term, or else can't tab to navigate page
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
function handleKeyDown(e: SyntheticKeyboardEvent<*>) {
|
|
|
|
const { keyCode } = e;
|
|
|
|
|
2021-12-06 18:28:36 +01:00
|
|
|
if (highlightedSuggestion && keyCode === KEYCODES.TAB) {
|
2021-12-02 17:49:13 +01:00
|
|
|
e.preventDefault();
|
2021-12-06 19:06:40 +01:00
|
|
|
handleSelect(highlightedSuggestion.label);
|
2021-12-02 17:49:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.addEventListener('keydown', handleKeyDown);
|
|
|
|
|
|
|
|
return () => window.removeEventListener('keydown', handleKeyDown);
|
2021-12-06 19:06:40 +01:00
|
|
|
}, [handleSelect, highlightedSuggestion, suggestionTerm]);
|
2021-12-02 17:49:13 +01:00
|
|
|
|
2022-01-14 22:07:21 +01:00
|
|
|
// Prevent keyboard keys Up and Down being overriden by MUI listeners when not in use
|
2022-01-14 18:55:42 +01:00
|
|
|
React.useEffect(() => {
|
|
|
|
const inputElement = inputRef && inputRef.current;
|
|
|
|
|
|
|
|
function overrideKeyHandling(event) {
|
2022-01-14 22:07:21 +01:00
|
|
|
const { keyCode } = event;
|
|
|
|
|
|
|
|
if (!suggestionTerm && (keyCode === KEYCODES.UP || keyCode === KEYCODES.DOWN)) {
|
2022-01-14 18:55:42 +01:00
|
|
|
event.stopPropagation();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inputElement) {
|
|
|
|
inputElement.addEventListener('keydown', overrideKeyHandling);
|
|
|
|
}
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
if (inputElement) {
|
|
|
|
inputElement.removeEventListener('keydown', overrideKeyHandling);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}, [inputRef, suggestionTerm]);
|
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
/** ------ **/
|
|
|
|
/** Render **/
|
|
|
|
/** ------ **/
|
|
|
|
|
|
|
|
return (
|
2021-12-06 18:28:36 +01:00
|
|
|
<Autocomplete
|
2021-12-07 14:14:48 +01:00
|
|
|
PopperComponent={AutocompletePopper}
|
2021-12-06 18:28:36 +01:00
|
|
|
autoHighlight
|
|
|
|
disableClearable
|
2021-12-07 19:17:29 +01:00
|
|
|
filterOptions={(options) => options.filter(({ label }) => allMatches.includes(label))}
|
2021-12-06 18:28:36 +01:00
|
|
|
freeSolo
|
|
|
|
fullWidth
|
2021-12-07 19:17:29 +01:00
|
|
|
getOptionLabel={(option) => option.label || ''}
|
2021-12-06 18:28:36 +01:00
|
|
|
groupBy={(option) => option.group}
|
|
|
|
id={id}
|
2021-12-06 19:06:40 +01:00
|
|
|
inputValue={messageValue}
|
2021-12-07 19:17:29 +01:00
|
|
|
loading={allMatches.length === 0 || showPlaceholder}
|
2021-12-07 15:51:55 +01:00
|
|
|
loadingText={showPlaceholder ? <BusyIndicator message={__('Searching...')} /> : __('Nothing found')}
|
2021-12-06 19:06:40 +01:00
|
|
|
onBlur={() => onBlur && onBlur()}
|
2021-12-06 18:28:36 +01:00
|
|
|
/* Different from onInputChange, onChange is only used for the selected value,
|
|
|
|
so here it is acting simply as a selection handler (see it as onSelect) */
|
2022-01-13 19:36:07 +01:00
|
|
|
onChange={(event, value) => handleSelect(value.label, event.keyCode)}
|
2021-12-06 18:28:36 +01:00
|
|
|
onClose={(event, reason) => reason !== 'selectOption' && setClose(true)}
|
2021-12-06 19:06:40 +01:00
|
|
|
onFocus={() => onFocus && onFocus()}
|
2021-12-06 18:28:36 +01:00
|
|
|
onHighlightChange={(event, option) => setHighlightedSuggestion(option)}
|
|
|
|
onInputChange={(event, value, reason) => reason === 'input' && handleInputChange(value)}
|
|
|
|
onOpen={() => suggestionTerm && setClose(false)}
|
|
|
|
/* 'open' is for the popper box component, set to check for a valid term
|
2021-12-07 15:51:55 +01:00
|
|
|
or else it will be displayed all the time as empty (no options) */
|
2021-12-06 18:28:36 +01:00
|
|
|
open={!!suggestionTerm && !shouldClose}
|
|
|
|
options={allOptionsGrouped}
|
2022-02-02 13:49:02 +01:00
|
|
|
renderGroup={({ group, children }) => (
|
|
|
|
<TextareaSuggestionsGroup groupName={group} suggestionTerm={suggestionTerm} searchQuery={searchQuery}>
|
|
|
|
{children}
|
|
|
|
</TextareaSuggestionsGroup>
|
|
|
|
)}
|
|
|
|
renderInput={(params) => (
|
|
|
|
<TextareaSuggestionsInput
|
|
|
|
params={params}
|
|
|
|
messageValue={messageValue}
|
|
|
|
inputRef={inputRef}
|
|
|
|
inputDefaultProps={inputDefaultProps}
|
2022-02-04 21:59:11 +01:00
|
|
|
toggleSelectors={toggleSelectors}
|
2022-02-02 13:49:02 +01:00
|
|
|
handleTip={handleTip}
|
|
|
|
handleSubmit={handleSubmit}
|
2022-02-04 21:59:11 +01:00
|
|
|
handlePreventClick={handlePreventClick}
|
|
|
|
submitButtonRef={submitButtonRef}
|
|
|
|
claimIsMine={claimIsMine}
|
2022-02-07 20:30:42 +01:00
|
|
|
slimInput={slimInput}
|
2022-02-02 13:49:02 +01:00
|
|
|
/>
|
|
|
|
)}
|
|
|
|
renderOption={(optionProps, option) => (
|
|
|
|
<TextareaSuggestionsOption label={option.label} isEmote={isEmote} optionProps={optionProps} />
|
|
|
|
)}
|
2021-12-06 18:28:36 +01:00
|
|
|
/>
|
2021-12-02 17:49:13 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-02-02 13:49:02 +01:00
|
|
|
const AutocompletePopper = (props: any) => <Popper {...props} placement="top" />;
|
2021-12-07 14:14:48 +01:00
|
|
|
|
2021-12-02 17:49:13 +01:00
|
|
|
function useSuggestionMatch(term: string, list: Array<string>) {
|
|
|
|
const throttledTerm = useThrottle(term);
|
|
|
|
|
|
|
|
return React.useMemo(() => {
|
|
|
|
return !throttledTerm || throttledTerm.trim() === ''
|
|
|
|
? undefined
|
|
|
|
: matchSorter(list, term, { keys: [(item) => item] });
|
|
|
|
}, [list, term, throttledTerm]);
|
|
|
|
}
|