index.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {Fragment, MouseEventHandler} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import SwitchButton from 'sentry/components/switchButton';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import ResourceTable from 'sentry/views/performance/browser/resources/jsCssView/resourceTable';
  9. import DomainSelector from 'sentry/views/performance/browser/resources/shared/domainSelector';
  10. import SelectControlWithProps from 'sentry/views/performance/browser/resources/shared/selectControlWithProps';
  11. import {
  12. BrowserStarfishFields,
  13. useResourceModuleFilters,
  14. } from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  15. import {useResourcePagesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcePagesQuery';
  16. import {useResourceSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  17. const {
  18. SPAN_OP: RESOURCE_TYPE,
  19. SPAN_DOMAIN,
  20. TRANSACTION,
  21. RESOURCE_RENDER_BLOCKING_STATUS,
  22. } = BrowserStarfishFields;
  23. const DEFAULT_RESOURCE_TYPES = ['resource.script', 'resource.css'];
  24. type Option = {
  25. label: string;
  26. value: string;
  27. };
  28. function JSCSSView() {
  29. const filters = useResourceModuleFilters();
  30. const sort = useResourceSort();
  31. const location = useLocation();
  32. const handleBlockingToggle: MouseEventHandler = () => {
  33. const hasBlocking = filters[RESOURCE_RENDER_BLOCKING_STATUS] === 'blocking';
  34. const newBlocking = hasBlocking ? undefined : 'blocking';
  35. browserHistory.push({
  36. ...location,
  37. query: {
  38. ...location.query,
  39. [RESOURCE_RENDER_BLOCKING_STATUS]: newBlocking,
  40. },
  41. });
  42. };
  43. return (
  44. <Fragment>
  45. <FilterOptionsContainer>
  46. <DomainSelector
  47. value={filters[SPAN_DOMAIN] || ''}
  48. defaultResourceTypes={DEFAULT_RESOURCE_TYPES}
  49. />
  50. <ResourceTypeSelector value={filters[RESOURCE_TYPE] || ''} />
  51. <PageSelector
  52. value={filters[TRANSACTION] || ''}
  53. defaultResourceTypes={DEFAULT_RESOURCE_TYPES}
  54. />
  55. <SwitchContainer>
  56. <SwitchButton
  57. isActive={filters[RESOURCE_RENDER_BLOCKING_STATUS] === 'blocking'}
  58. toggle={handleBlockingToggle}
  59. />
  60. {t('Render Blocking')}
  61. </SwitchContainer>
  62. </FilterOptionsContainer>
  63. <ResourceTable sort={sort} defaultResourceTypes={DEFAULT_RESOURCE_TYPES} />
  64. </Fragment>
  65. );
  66. }
  67. function ResourceTypeSelector({value}: {value?: string}) {
  68. const location = useLocation();
  69. const options: Option[] = [
  70. {value: '', label: 'All'},
  71. {value: 'resource.script', label: `${t('JavaScript')} (.js)`},
  72. {value: 'resource.css', label: `${t('Stylesheet')} (.css)`},
  73. ];
  74. return (
  75. <SelectControlWithProps
  76. inFieldLabel={`${t('Type')}:`}
  77. options={options}
  78. value={value}
  79. onChange={newValue => {
  80. browserHistory.push({
  81. ...location,
  82. query: {
  83. ...location.query,
  84. [RESOURCE_TYPE]: newValue?.value,
  85. },
  86. });
  87. }}
  88. />
  89. );
  90. }
  91. function PageSelector({
  92. value,
  93. defaultResourceTypes,
  94. }: {
  95. defaultResourceTypes?: string[];
  96. value?: string;
  97. }) {
  98. const location = useLocation();
  99. const {data: pages} = useResourcePagesQuery(defaultResourceTypes);
  100. const options: Option[] = [
  101. {value: '', label: 'All'},
  102. ...pages.map(page => ({value: page, label: page})),
  103. ];
  104. return (
  105. <SelectControlWithProps
  106. inFieldLabel={`${t('Page')}:`}
  107. options={options}
  108. value={value}
  109. onChange={newValue => {
  110. browserHistory.push({
  111. ...location,
  112. query: {
  113. ...location.query,
  114. [TRANSACTION]: newValue?.value,
  115. },
  116. });
  117. }}
  118. />
  119. );
  120. }
  121. const SwitchContainer = styled('div')`
  122. display: flex;
  123. align-items: center;
  124. column-gap: ${space(1)};
  125. `;
  126. export const FilterOptionsContainer = styled('div')`
  127. display: grid;
  128. grid-template-columns: repeat(4, 1fr);
  129. gap: ${space(2)};
  130. margin-bottom: ${space(2)};
  131. max-width: 800px;
  132. `;
  133. export default JSCSSView;