index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import debounce from 'lodash/debounce';
  5. import FeatureBadge from 'sentry/components/featureBadge';
  6. import {t} from 'sentry/locale';
  7. import {space} from 'sentry/styles/space';
  8. import {useLocation} from 'sentry/utils/useLocation';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {RESOURCE_THROUGHPUT_UNIT} from 'sentry/views/performance/browser/resources';
  11. import ResourceTable from 'sentry/views/performance/browser/resources/resourceView/resourceTable';
  12. import {
  13. FONT_FILE_EXTENSIONS,
  14. IMAGE_FILE_EXTENSIONS,
  15. } from 'sentry/views/performance/browser/resources/shared/constants';
  16. import RenderBlockingSelector from 'sentry/views/performance/browser/resources/shared/renderBlockingSelector';
  17. import SelectControlWithProps from 'sentry/views/performance/browser/resources/shared/selectControlWithProps';
  18. import {ResourceSpanOps} from 'sentry/views/performance/browser/resources/shared/types';
  19. import {
  20. BrowserStarfishFields,
  21. useResourceModuleFilters,
  22. } from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  23. import {useResourcePagesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcePagesQuery';
  24. import {useResourceSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  25. import {getResourceTypeFilter} from 'sentry/views/performance/browser/resources/utils/useResourcesQuery';
  26. import {ModuleName} from 'sentry/views/starfish/types';
  27. import {QueryParameterNames} from 'sentry/views/starfish/views/queryParameters';
  28. import {SpanTimeCharts} from 'sentry/views/starfish/views/spans/spanTimeCharts';
  29. import type {ModuleFilters} from 'sentry/views/starfish/views/spans/useModuleFilters';
  30. const {
  31. SPAN_OP: RESOURCE_TYPE,
  32. SPAN_DOMAIN,
  33. TRANSACTION,
  34. RESOURCE_RENDER_BLOCKING_STATUS,
  35. } = BrowserStarfishFields;
  36. export const DEFAULT_RESOURCE_TYPES = [
  37. ResourceSpanOps.SCRIPT,
  38. ResourceSpanOps.CSS,
  39. ResourceSpanOps.FONT,
  40. ResourceSpanOps.IMAGE,
  41. ];
  42. type Option = {
  43. label: string | React.ReactElement;
  44. value: string;
  45. };
  46. function ResourceView() {
  47. const filters = useResourceModuleFilters();
  48. const sort = useResourceSort();
  49. const spanTimeChartsFilters: ModuleFilters = {
  50. 'span.op': `[${DEFAULT_RESOURCE_TYPES.join(',')}]`,
  51. ...(filters[SPAN_DOMAIN] ? {[SPAN_DOMAIN]: filters[SPAN_DOMAIN]} : {}),
  52. };
  53. const extraQuery = getResourceTypeFilter(undefined, DEFAULT_RESOURCE_TYPES);
  54. return (
  55. <Fragment>
  56. <SpanTimeCharts
  57. moduleName={ModuleName.OTHER}
  58. appliedFilters={spanTimeChartsFilters}
  59. throughputUnit={RESOURCE_THROUGHPUT_UNIT}
  60. extraQuery={extraQuery}
  61. />
  62. <FilterOptionsContainer columnCount={3}>
  63. <ResourceTypeSelector value={filters[RESOURCE_TYPE] || ''} />
  64. <TransactionSelector
  65. value={filters[TRANSACTION] || ''}
  66. defaultResourceTypes={DEFAULT_RESOURCE_TYPES}
  67. />
  68. <RenderBlockingSelector value={filters[RESOURCE_RENDER_BLOCKING_STATUS] || ''} />
  69. </FilterOptionsContainer>
  70. <ResourceTable sort={sort} defaultResourceTypes={DEFAULT_RESOURCE_TYPES} />
  71. </Fragment>
  72. );
  73. }
  74. function ResourceTypeSelector({value}: {value?: string}) {
  75. const location = useLocation();
  76. const {features} = useOrganization();
  77. const hasImageView = features.includes('starfish-browser-resource-module-image-view');
  78. const options: Option[] = [
  79. {value: '', label: 'All'},
  80. {value: 'resource.script', label: `${t('JavaScript')} (.js)`},
  81. {value: 'resource.css', label: `${t('Stylesheet')} (.css)`},
  82. {
  83. value: 'resource.font',
  84. label: `${t('Font')} (${FONT_FILE_EXTENSIONS.map(e => `.${e}`).join(', ')})`,
  85. },
  86. ...(hasImageView
  87. ? [
  88. {
  89. value: ResourceSpanOps.IMAGE,
  90. label: (
  91. <span>
  92. {`${t('Image')} (${IMAGE_FILE_EXTENSIONS.map(e => `.${e}`).join(', ')})`}
  93. <FeatureBadge type="new"> </FeatureBadge>
  94. </span>
  95. ),
  96. },
  97. ]
  98. : []),
  99. ];
  100. return (
  101. <SelectControlWithProps
  102. inFieldLabel={`${t('Type')}:`}
  103. options={options}
  104. value={value}
  105. onChange={newValue => {
  106. browserHistory.push({
  107. ...location,
  108. query: {
  109. ...location.query,
  110. [RESOURCE_TYPE]: newValue?.value,
  111. [QueryParameterNames.SPANS_CURSOR]: undefined,
  112. },
  113. });
  114. }}
  115. />
  116. );
  117. }
  118. export function TransactionSelector({
  119. value,
  120. defaultResourceTypes,
  121. }: {
  122. defaultResourceTypes?: string[];
  123. value?: string;
  124. }) {
  125. const [state, setState] = useState({
  126. search: '',
  127. inputChanged: false,
  128. shouldRequeryOnInputChange: false,
  129. });
  130. const location = useLocation();
  131. const {data: pages, isLoading} = useResourcePagesQuery(
  132. defaultResourceTypes,
  133. state.search
  134. );
  135. // If the maximum number of pages is returned, we need to requery on input change to get full results
  136. if (!state.shouldRequeryOnInputChange && pages && pages.length >= 100) {
  137. setState({...state, shouldRequeryOnInputChange: true});
  138. }
  139. // Everytime loading is complete, reset the inputChanged state
  140. useEffect(() => {
  141. if (!isLoading && state.inputChanged) {
  142. setState({...state, inputChanged: false});
  143. }
  144. // eslint-disable-next-line react-hooks/exhaustive-deps
  145. }, [isLoading]);
  146. const optionsReady = !isLoading && !state.inputChanged;
  147. const options: Option[] = optionsReady
  148. ? [{value: '', label: 'All'}, ...pages.map(page => ({value: page, label: page}))]
  149. : [];
  150. // eslint-disable-next-line react-hooks/exhaustive-deps
  151. const debounceUpdateSearch = useCallback(
  152. debounce((search, currentState) => {
  153. setState({...currentState, search});
  154. }, 500),
  155. []
  156. );
  157. return (
  158. <SelectControlWithProps
  159. inFieldLabel={`${t('Page')}:`}
  160. options={options}
  161. value={value}
  162. onInputChange={input => {
  163. if (state.shouldRequeryOnInputChange) {
  164. setState({...state, inputChanged: true});
  165. debounceUpdateSearch(input, state);
  166. }
  167. }}
  168. noOptionsMessage={() => (optionsReady ? undefined : t('Loading...'))}
  169. onChange={newValue => {
  170. browserHistory.push({
  171. ...location,
  172. query: {
  173. ...location.query,
  174. [TRANSACTION]: newValue?.value,
  175. [QueryParameterNames.SPANS_CURSOR]: undefined,
  176. },
  177. });
  178. }}
  179. />
  180. );
  181. }
  182. export const FilterOptionsContainer = styled('div')<{columnCount: number}>`
  183. display: grid;
  184. grid-template-columns: repeat(${props => props.columnCount}, 1fr);
  185. gap: ${space(2)};
  186. margin-bottom: ${space(2)};
  187. max-width: 800px;
  188. `;
  189. export default ResourceView;