| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {TypeaheadResult} from './Types'; |
| b69ab31 | | | 9 | import type {ReactProps} from './utils'; |
| b69ab31 | | | 10 | |
| b69ab31 | | | 11 | import {useCallback, useEffect, useMemo, useRef, useState} from 'react'; |
| b69ab31 | | | 12 | import {debounce} from 'shared/debounce'; |
| b69ab31 | | | 13 | import {Icon} from './Icon'; |
| b69ab31 | | | 14 | import {Subtle} from './Subtle'; |
| b69ab31 | | | 15 | import {TextField} from './TextField'; |
| b69ab31 | | | 16 | import {extractTokens, TokensList, tokensToString} from './Tokens'; |
| b69ab31 | | | 17 | |
| b69ab31 | | | 18 | export function Typeahead({ |
| b69ab31 | | | 19 | tokenString, |
| b69ab31 | | | 20 | setTokenString, |
| b69ab31 | | | 21 | fetchTokens, |
| b69ab31 | | | 22 | onSaveNewToken, |
| b69ab31 | | | 23 | onClickToken, |
| b69ab31 | | | 24 | renderExtra, |
| b69ab31 | | | 25 | maxTokens, |
| b69ab31 | | | 26 | autoFocus, |
| b69ab31 | | | 27 | debounceInterval, |
| b69ab31 | | | 28 | ...rest |
| b69ab31 | | | 29 | }: { |
| b69ab31 | | | 30 | tokenString: string; |
| b69ab31 | | | 31 | setTokenString: (newValue: string) => void; |
| b69ab31 | | | 32 | fetchTokens: ( |
| b69ab31 | | | 33 | prefix: string, |
| b69ab31 | | | 34 | ) => Promise<{values: Array<TypeaheadResult>; fetchStartTimestamp: number}>; |
| b69ab31 | | | 35 | onSaveNewToken?: (newValue: string) => void; |
| b69ab31 | | | 36 | onClickToken?: (token: string) => void; |
| b69ab31 | | | 37 | /** Render more content below typeahead, useful for buttons that can add new tokens */ |
| b69ab31 | | | 38 | renderExtra?: (saveNewValue: (value: string) => void) => React.ReactNode; |
| b69ab31 | | | 39 | maxTokens?: number; |
| b69ab31 | | | 40 | autoFocus: boolean; |
| b69ab31 | | | 41 | debounceInterval?: number; |
| b69ab31 | | | 42 | } & ReactProps<HTMLInputElement>) { |
| b69ab31 | | | 43 | const ref = useRef<HTMLInputElement>(null); |
| b69ab31 | | | 44 | |
| b69ab31 | | | 45 | useEffect(() => { |
| b69ab31 | | | 46 | if (ref.current && autoFocus) { |
| b69ab31 | | | 47 | ref.current?.focus(); |
| b69ab31 | | | 48 | } |
| b69ab31 | | | 49 | }, [autoFocus, ref]); |
| b69ab31 | | | 50 | |
| b69ab31 | | | 51 | const [tokens, remaining] = extractTokens(tokenString); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const [typeaheadSuggestions, setTypeaheadSuggestions] = useState<TypeaheadSuggestions>(undefined); |
| b69ab31 | | | 54 | |
| b69ab31 | | | 55 | const [selectedSuggestionIndex, setSelectedIndex] = useState(0); |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | const fetchTokenHandler = useCallback( |
| b69ab31 | | | 58 | (value: string, previousTokens: Array<string>) => { |
| b69ab31 | | | 59 | fetchTokens(value).then(({values, fetchStartTimestamp}) => { |
| b69ab31 | | | 60 | // don't show typeahead suggestions that are already entered |
| b69ab31 | | | 61 | const newValues = values.filter(v => !previousTokens.includes(v.value)); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | setTypeaheadSuggestions(last => |
| b69ab31 | | | 64 | last?.type === 'success' && last.timestamp > fetchStartTimestamp |
| b69ab31 | | | 65 | ? // this result is older than the one we've already set: ignore it |
| b69ab31 | | | 66 | last |
| b69ab31 | | | 67 | : {type: 'success', values: newValues, timestamp: fetchStartTimestamp}, |
| b69ab31 | | | 68 | ); |
| b69ab31 | | | 69 | }); |
| b69ab31 | | | 70 | }, |
| b69ab31 | | | 71 | [fetchTokens], |
| b69ab31 | | | 72 | ); |
| b69ab31 | | | 73 | |
| b69ab31 | | | 74 | const debouncedFetchTokenHandler = useMemo(() => { |
| b69ab31 | | | 75 | return debounce(fetchTokenHandler, debounceInterval ?? 0); |
| b69ab31 | | | 76 | }, [debounceInterval, fetchTokenHandler]); |
| b69ab31 | | | 77 | |
| b69ab31 | | | 78 | const onInput = (event: {target: EventTarget | null}) => { |
| b69ab31 | | | 79 | const newValue = (event?.target as HTMLInputElement)?.value; |
| b69ab31 | | | 80 | setTokenString(tokensToString(tokens, newValue)); |
| b69ab31 | | | 81 | |
| b69ab31 | | | 82 | if (typeaheadSuggestions?.type !== 'success' || typeaheadSuggestions.values.length === 0) { |
| b69ab31 | | | 83 | setTypeaheadSuggestions({type: 'loading'}); |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | |
| b69ab31 | | | 86 | debounceInterval |
| b69ab31 | | | 87 | ? debouncedFetchTokenHandler(newValue, tokens) |
| b69ab31 | | | 88 | : fetchTokenHandler(newValue, tokens); |
| b69ab31 | | | 89 | }; |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | const saveNewValue = (value: string | undefined) => { |
| b69ab31 | | | 92 | if (value && !tokens.includes(value)) { |
| b69ab31 | | | 93 | setTokenString(tokensToString([...tokens, value], '')); |
| b69ab31 | | | 94 | // clear out typeahead |
| b69ab31 | | | 95 | setTypeaheadSuggestions({type: 'success', values: [], timestamp: Date.now()}); |
| b69ab31 | | | 96 | |
| b69ab31 | | | 97 | onSaveNewToken?.(value); |
| b69ab31 | | | 98 | } |
| b69ab31 | | | 99 | }; |
| b69ab31 | | | 100 | |
| b69ab31 | | | 101 | return ( |
| b69ab31 | | | 102 | <> |
| b69ab31 | | | 103 | <div |
| b69ab31 | | | 104 | className="commit-info-tokenized-field" |
| b69ab31 | | | 105 | onKeyDown={event => { |
| b69ab31 | | | 106 | if (event.key === 'Backspace' && ref.current?.value.length === 0) { |
| b69ab31 | | | 107 | // pop one token off |
| b69ab31 | | | 108 | setTokenString(tokensToString(tokens.slice(0, -1), '')); |
| b69ab31 | | | 109 | return; |
| b69ab31 | | | 110 | } |
| b69ab31 | | | 111 | |
| b69ab31 | | | 112 | const values = (typeaheadSuggestions as TypeaheadSuggestions & {type: 'success'})?.values; |
| b69ab31 | | | 113 | if (values == null) { |
| b69ab31 | | | 114 | return; |
| b69ab31 | | | 115 | } |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | if (event.key === 'ArrowDown') { |
| b69ab31 | | | 118 | setSelectedIndex(last => Math.min(last + 1, values.length - 1)); |
| b69ab31 | | | 119 | event.preventDefault(); |
| b69ab31 | | | 120 | } else if (event.key === 'ArrowUp') { |
| b69ab31 | | | 121 | // allow -1, so you can up arrow "above" the top, to make it highlight nothing |
| b69ab31 | | | 122 | setSelectedIndex(last => Math.max(last - 1, -1)); |
| b69ab31 | | | 123 | event.preventDefault(); |
| b69ab31 | | | 124 | } else if (event.key === 'Enter') { |
| b69ab31 | | | 125 | saveNewValue(values[selectedSuggestionIndex].value); |
| b69ab31 | | | 126 | event.preventDefault(); |
| b69ab31 | | | 127 | } |
| b69ab31 | | | 128 | }}> |
| b69ab31 | | | 129 | <TokensList |
| b69ab31 | | | 130 | tokens={tokens} |
| b69ab31 | | | 131 | onClickToken={onClickToken} |
| b69ab31 | | | 132 | onClickX={(token: string) => { |
| b69ab31 | | | 133 | setTokenString( |
| b69ab31 | | | 134 | tokensToString( |
| b69ab31 | | | 135 | tokens.filter(t => t !== token), |
| b69ab31 | | | 136 | // keep anything already typed in |
| b69ab31 | | | 137 | ref.current?.value ?? '', |
| b69ab31 | | | 138 | ), |
| b69ab31 | | | 139 | ); |
| b69ab31 | | | 140 | }} |
| b69ab31 | | | 141 | /> |
| b69ab31 | | | 142 | {tokens.length >= (maxTokens ?? Infinity) ? null : ( |
| b69ab31 | | | 143 | <div className="commit-info-field-with-typeahead"> |
| b69ab31 | | | 144 | <TextField ref={ref} value={remaining} onInput={onInput} {...rest} /> |
| b69ab31 | | | 145 | {typeaheadSuggestions?.type === 'loading' || |
| b69ab31 | | | 146 | (typeaheadSuggestions?.values?.length ?? 0) > 0 ? ( |
| b69ab31 | | | 147 | <div className="typeahead-suggestions tooltip tooltip-bottom"> |
| b69ab31 | | | 148 | <div className="tooltip-arrow tooltip-arrow-bottom" /> |
| b69ab31 | | | 149 | {typeaheadSuggestions?.type === 'loading' ? ( |
| b69ab31 | | | 150 | <Icon icon="loading" /> |
| b69ab31 | | | 151 | ) : ( |
| b69ab31 | | | 152 | typeaheadSuggestions?.values.map((suggestion, index) => ( |
| b69ab31 | | | 153 | <span |
| b69ab31 | | | 154 | key={suggestion.value} |
| b69ab31 | | | 155 | className={ |
| b69ab31 | | | 156 | 'suggestion' + |
| b69ab31 | | | 157 | (index === selectedSuggestionIndex ? ' selected-suggestion' : '') |
| b69ab31 | | | 158 | } |
| b69ab31 | | | 159 | onMouseDown={() => { |
| b69ab31 | | | 160 | saveNewValue(suggestion.value); |
| b69ab31 | | | 161 | }}> |
| b69ab31 | | | 162 | {suggestion.image && <ImageWithFallback src={suggestion.image} />} |
| b69ab31 | | | 163 | <span className="suggestion-label"> |
| b69ab31 | | | 164 | <span>{suggestion.label}</span> |
| b69ab31 | | | 165 | {(suggestion.detail || suggestion.label !== suggestion.value) && ( |
| b69ab31 | | | 166 | <Subtle>{suggestion.detail ?? suggestion.value}</Subtle> |
| b69ab31 | | | 167 | )} |
| b69ab31 | | | 168 | </span> |
| b69ab31 | | | 169 | </span> |
| b69ab31 | | | 170 | )) |
| b69ab31 | | | 171 | )} |
| b69ab31 | | | 172 | </div> |
| b69ab31 | | | 173 | ) : null} |
| b69ab31 | | | 174 | </div> |
| b69ab31 | | | 175 | )} |
| b69ab31 | | | 176 | </div> |
| b69ab31 | | | 177 | {renderExtra?.(saveNewValue)} |
| b69ab31 | | | 178 | </> |
| b69ab31 | | | 179 | ); |
| b69ab31 | | | 180 | } |
| b69ab31 | | | 181 | |
| b69ab31 | | | 182 | const TRANSPARENT_1PX_GIF = |
| b69ab31 | | | 183 | 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='; |
| b69ab31 | | | 184 | function ImageWithFallback({ |
| b69ab31 | | | 185 | src, |
| b69ab31 | | | 186 | ...rest |
| b69ab31 | | | 187 | }: {src: string} & React.DetailedHTMLProps< |
| b69ab31 | | | 188 | React.ImgHTMLAttributes<HTMLImageElement>, |
| b69ab31 | | | 189 | HTMLImageElement |
| b69ab31 | | | 190 | >) { |
| b69ab31 | | | 191 | return ( |
| b69ab31 | | | 192 | <img |
| b69ab31 | | | 193 | src={src} |
| b69ab31 | | | 194 | onError={e => { |
| b69ab31 | | | 195 | // Images that fail to load would show a broken image icon. |
| b69ab31 | | | 196 | // Instead, on error we can replace the image src with a transparent 1x1 gif to hide it |
| b69ab31 | | | 197 | // and use our CSS fallback. |
| b69ab31 | | | 198 | if (e.target) { |
| b69ab31 | | | 199 | (e.target as HTMLImageElement).src = TRANSPARENT_1PX_GIF; |
| b69ab31 | | | 200 | } |
| b69ab31 | | | 201 | }} |
| b69ab31 | | | 202 | {...rest} |
| b69ab31 | | | 203 | /> |
| b69ab31 | | | 204 | ); |
| b69ab31 | | | 205 | } |
| b69ab31 | | | 206 | |
| b69ab31 | | | 207 | type TypeaheadSuggestions = |
| b69ab31 | | | 208 | | { |
| b69ab31 | | | 209 | type: 'loading'; |
| b69ab31 | | | 210 | } |
| b69ab31 | | | 211 | | {type: 'success'; values: Array<TypeaheadResult>; timestamp: number} |
| b69ab31 | | | 212 | | undefined; |