index.tsx 6.6 KB

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