traceSearchInput.tsx 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. import type React from 'react';
  2. import {Fragment, useCallback, useLayoutEffect, useRef, useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import {InputGroup} from 'sentry/components/inputGroup';
  5. import LoadingIndicator from 'sentry/components/loadingIndicator';
  6. import {SearchBarTrailingButton} from 'sentry/components/searchBar';
  7. import {IconChevron, IconClose, IconSearch} from 'sentry/icons';
  8. import {t} from 'sentry/locale';
  9. import {space} from 'sentry/styles/space';
  10. import type {Organization} from 'sentry/types/organization';
  11. import {trackAnalytics} from 'sentry/utils/analytics';
  12. import useOrganization from 'sentry/utils/useOrganization';
  13. import {traceAnalytics} from 'sentry/views/performance/newTraceDetails/traceAnalytics';
  14. import type {
  15. TraceTree,
  16. TraceTreeNode,
  17. } from 'sentry/views/performance/newTraceDetails/traceModels/traceTree';
  18. import type {TraceSearchState} from 'sentry/views/performance/newTraceDetails/traceState/traceSearch';
  19. import {useTraceState, useTraceStateDispatch} from '../traceState/traceStateProvider';
  20. interface TraceSearchInputProps {
  21. onTraceSearch: (
  22. query: string,
  23. node: TraceTreeNode<TraceTree.NodeValue> | null,
  24. behavior: 'track result' | 'persist'
  25. ) => void;
  26. organization: Organization;
  27. }
  28. const MIN_LOADING_TIME = 300;
  29. export function TraceSearchInput(props: TraceSearchInputProps) {
  30. const organization = useOrganization();
  31. const traceState = useTraceState();
  32. const traceDispatch = useTraceStateDispatch();
  33. const [status, setStatus] = useState<TraceSearchState['status']>();
  34. const timeoutRef = useRef<number | undefined>(undefined);
  35. const statusRef = useRef<TraceSearchState['status']>(status);
  36. statusRef.current = status;
  37. const traceStateRef = useRef(traceState);
  38. traceStateRef.current = traceState;
  39. const onTraceSearch = props.onTraceSearch;
  40. useLayoutEffect(() => {
  41. if (typeof timeoutRef.current === 'number') {
  42. window.clearTimeout(timeoutRef.current);
  43. }
  44. // if status is loading, show loading icon immediately
  45. // if previous status was loading, show loading icon for at least 500ms
  46. if (!statusRef.current && traceState.search.status) {
  47. setStatus([performance.now(), traceState.search.status[1]]);
  48. return;
  49. }
  50. const nextStatus = traceState.search.status;
  51. if (nextStatus) {
  52. const elapsed = performance.now() - nextStatus[0];
  53. if (elapsed > MIN_LOADING_TIME || nextStatus[1] === 'loading') {
  54. setStatus(nextStatus);
  55. return;
  56. }
  57. const schedule = nextStatus[0] + MIN_LOADING_TIME - performance.now();
  58. timeoutRef.current = window.setTimeout(() => {
  59. setStatus(nextStatus);
  60. }, schedule);
  61. } else {
  62. setStatus(nextStatus);
  63. }
  64. }, [traceState.search.status]);
  65. const onSearchFocus = useCallback(() => {
  66. traceAnalytics.trackSearchFocus(organization);
  67. if (traceStateRef.current.rovingTabIndex.node) {
  68. traceDispatch({type: 'clear roving index'});
  69. }
  70. }, [traceDispatch, organization]);
  71. const onChange = useCallback(
  72. (event: React.ChangeEvent<HTMLInputElement>) => {
  73. if (!event.target.value) {
  74. traceDispatch({type: 'clear query'});
  75. return;
  76. }
  77. traceDispatch({type: 'set query', query: event.target.value});
  78. onTraceSearch(
  79. event.target.value,
  80. traceStateRef.current.rovingTabIndex.node ?? traceStateRef.current.search.node,
  81. 'track result'
  82. );
  83. },
  84. [traceDispatch, onTraceSearch]
  85. );
  86. const onSearchClear = useCallback(() => {
  87. trackAnalytics('trace.trace_layout.search_clear', {
  88. organization,
  89. });
  90. traceDispatch({type: 'clear query'});
  91. }, [traceDispatch, organization]);
  92. const onKeyDown = useCallback(
  93. (event: React.KeyboardEvent<HTMLInputElement>) => {
  94. switch (event.key) {
  95. case 'ArrowDown':
  96. trackAnalytics('trace.trace_layout.search_match_navigate', {
  97. organization,
  98. direction: 'next',
  99. interaction: 'arrowKey',
  100. });
  101. traceDispatch({
  102. type: event.shiftKey ? 'go to last match' : 'go to next match',
  103. });
  104. break;
  105. case 'ArrowUp':
  106. trackAnalytics('trace.trace_layout.search_match_navigate', {
  107. organization,
  108. direction: 'prev',
  109. interaction: 'arrowKey',
  110. });
  111. traceDispatch({
  112. type: event.shiftKey ? 'go to first match' : 'go to previous match',
  113. });
  114. break;
  115. case 'Enter':
  116. trackAnalytics('trace.trace_layout.search_match_navigate', {
  117. organization,
  118. direction: event.shiftKey ? 'prev' : 'next',
  119. interaction: 'enterKey',
  120. });
  121. traceDispatch({
  122. type: event.shiftKey ? 'go to previous match' : 'go to next match',
  123. });
  124. break;
  125. default:
  126. }
  127. },
  128. [traceDispatch, organization]
  129. );
  130. const onNextSearchClick = useCallback(() => {
  131. trackAnalytics('trace.trace_layout.search_match_navigate', {
  132. organization,
  133. direction: 'next',
  134. interaction: 'click',
  135. });
  136. if (traceStateRef.current.rovingTabIndex.node) {
  137. traceDispatch({type: 'clear roving index'});
  138. }
  139. traceDispatch({type: 'go to next match'});
  140. }, [traceDispatch, organization]);
  141. const onPreviousSearchClick = useCallback(() => {
  142. trackAnalytics('trace.trace_layout.search_match_navigate', {
  143. organization,
  144. direction: 'prev',
  145. interaction: 'click',
  146. });
  147. if (traceStateRef.current.rovingTabIndex.node) {
  148. traceDispatch({type: 'clear roving index'});
  149. }
  150. traceDispatch({type: 'go to previous match'});
  151. }, [traceDispatch, organization]);
  152. return (
  153. <StyledSearchBar>
  154. <InputGroup.LeadingItems>
  155. <InvisiblePlaceholder />
  156. {status?.[1] === 'loading' ? (
  157. <StyledLoadingIndicator data-test-id="trace-search-loading" size={12} />
  158. ) : (
  159. <StyledSearchIcon
  160. data-test-id="trace-search-success"
  161. color="subText"
  162. size={'xs'}
  163. />
  164. )}
  165. </InputGroup.LeadingItems>
  166. <InputGroup.Input
  167. size="xs"
  168. type="text"
  169. name="query"
  170. autoComplete="off"
  171. placeholder={t('Search in trace')}
  172. defaultValue={traceState.search.query ?? ''}
  173. onChange={onChange}
  174. onKeyDown={onKeyDown}
  175. onFocus={onSearchFocus}
  176. />
  177. <InputGroup.TrailingItems>
  178. <StyledTrailingText data-test-id="trace-search-result-iterator">
  179. {`${
  180. traceState.search.query && !traceState.search.results?.length
  181. ? t('no results')
  182. : traceState.search.query
  183. ? (traceState.search.resultIteratorIndex !== null
  184. ? traceState.search.resultIteratorIndex + 1
  185. : '-') + `/${traceState.search.results?.length ?? 0}`
  186. : ''
  187. }`}
  188. </StyledTrailingText>
  189. {traceState.search.query ? (
  190. <Fragment>
  191. <StyledSearchBarTrailingButton
  192. size="zero"
  193. borderless
  194. icon={<IconChevron size="xs" />}
  195. aria-label={t('Next')}
  196. disabled={status?.[1] === 'loading'}
  197. onClick={onPreviousSearchClick}
  198. />
  199. <StyledSearchBarTrailingButton
  200. size="zero"
  201. borderless
  202. icon={<IconChevron size="xs" direction="down" />}
  203. aria-label={t('Previous')}
  204. disabled={status?.[1] === 'loading'}
  205. onClick={onNextSearchClick}
  206. />
  207. <StyledSearchBarTrailingButton
  208. size="zero"
  209. borderless
  210. disabled={status?.[1] === 'loading'}
  211. onClick={onSearchClear}
  212. icon={<IconClose size="xs" />}
  213. aria-label={t('Clear')}
  214. />
  215. </Fragment>
  216. ) : null}
  217. </InputGroup.TrailingItems>
  218. </StyledSearchBar>
  219. );
  220. }
  221. const InvisiblePlaceholder = styled('div')`
  222. pointer-events: none;
  223. visibility: hidden;
  224. width: 12px;
  225. height: 12px;
  226. `;
  227. const StyledLoadingIndicator = styled(LoadingIndicator)`
  228. margin: 0;
  229. left: 0;
  230. top: 50%;
  231. position: absolute;
  232. transform: translate(-2px, -50%);
  233. animation: showLoadingIndicator 0.3s ease-in-out forwards;
  234. @keyframes showLoadingIndicator {
  235. from {
  236. opacity: 0;
  237. transform: translate(-2px, -50%) scale(0.86);
  238. }
  239. to {
  240. opacity: 1;
  241. transform: translate(-2px, -50%) scale(1);
  242. }
  243. }
  244. .loading-indicator {
  245. border-width: 2px;
  246. }
  247. .loading-message {
  248. display: none;
  249. }
  250. `;
  251. const StyledSearchIcon = styled(IconSearch)`
  252. position: absolute;
  253. left: 0;
  254. top: 50%;
  255. transform: scale(1) translateY(-50%);
  256. animation: showSearchIcon 0.3s ease-in-out forwards;
  257. @keyframes showSearchIcon {
  258. from {
  259. opacity: 0;
  260. transform: scale(0.86) translateY(-50%);
  261. }
  262. to {
  263. opacity: 1;
  264. transform: scale(1) translateY(-50%);
  265. }
  266. }
  267. `;
  268. const StyledSearchBarTrailingButton = styled(SearchBarTrailingButton)`
  269. padding: 0;
  270. &:last-child {
  271. svg {
  272. width: 10px;
  273. height: 10px;
  274. }
  275. }
  276. `;
  277. const StyledTrailingText = styled('span')`
  278. color: ${p => p.theme.subText};
  279. font-size: ${p => p.theme.fontSizeSmall};
  280. `;
  281. const StyledSearchBar = styled(InputGroup)`
  282. flex: 1 1 100%;
  283. margin-bottom: ${space(1)};
  284. > div > div:last-child {
  285. gap: ${space(0.25)};
  286. }
  287. `;