index.tsx 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import {MouseEventHandler} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import Breadcrumbs from 'sentry/components/breadcrumbs';
  5. import FeatureBadge from 'sentry/components/featureBadge';
  6. import SelectControl, {
  7. ControlProps,
  8. } from 'sentry/components/forms/controls/selectControl';
  9. import * as Layout from 'sentry/components/layouts/thirds';
  10. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  11. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  12. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  13. import SwitchButton from 'sentry/components/switchButton';
  14. import {TabList, Tabs} from 'sentry/components/tabs';
  15. import {t} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {useLocation} from 'sentry/utils/useLocation';
  18. import useOrganization from 'sentry/utils/useOrganization';
  19. import {normalizeUrl} from 'sentry/utils/withDomainRequired';
  20. import {ResourceSidebar} from 'sentry/views/performance/browser/resources/resourceSidebar';
  21. import ResourceTable from 'sentry/views/performance/browser/resources/resourceTable';
  22. import {useResourceDomainsQuery} from 'sentry/views/performance/browser/resources/utils/useResourceDomansQuery';
  23. import {
  24. BrowserStarfishFields,
  25. useResourceModuleFilters,
  26. } from 'sentry/views/performance/browser/resources/utils/useResourceFilters';
  27. import {useResourcePagesQuery} from 'sentry/views/performance/browser/resources/utils/useResourcePagesQuery';
  28. import {useResourceSort} from 'sentry/views/performance/browser/resources/utils/useResourceSort';
  29. import {ModulePageProviders} from 'sentry/views/performance/database/modulePageProviders';
  30. const {
  31. RESOURCE_TYPE,
  32. SPAN_DOMAIN,
  33. TRANSACTION,
  34. DESCRIPTION,
  35. RESOURCE_RENDER_BLOCKING_STATUS,
  36. } = BrowserStarfishFields;
  37. type Option = {
  38. label: string;
  39. value: string;
  40. };
  41. function ResourcesLandingPage() {
  42. const organization = useOrganization();
  43. const filters = useResourceModuleFilters();
  44. const sort = useResourceSort();
  45. const location = useLocation();
  46. const handleBlockingToggle: MouseEventHandler = () => {
  47. const hasBlocking = filters[RESOURCE_RENDER_BLOCKING_STATUS] === 'blocking';
  48. const newBlocking = hasBlocking ? undefined : 'blocking';
  49. browserHistory.push({
  50. ...location,
  51. query: {
  52. ...location.query,
  53. [RESOURCE_RENDER_BLOCKING_STATUS]: newBlocking,
  54. },
  55. });
  56. };
  57. return (
  58. <ModulePageProviders title={[t('Performance'), t('Resources')].join(' — ')}>
  59. <Layout.Header>
  60. <Layout.HeaderContent>
  61. <Breadcrumbs
  62. crumbs={[
  63. {
  64. label: 'Performance',
  65. to: normalizeUrl(`/organizations/${organization.slug}/performance/`),
  66. preservePageFilters: true,
  67. },
  68. {
  69. label: 'Resources',
  70. },
  71. ]}
  72. />
  73. <Layout.Title>
  74. {t('Resources')}
  75. <FeatureBadge type="alpha" />
  76. </Layout.Title>
  77. </Layout.HeaderContent>
  78. <StyledTabs>
  79. <TabList hideBorder>
  80. <TabList.Item key="resource.css/script">{t('JS/CSS')}</TabList.Item>
  81. <TabList.Item key="resource.img">{t('Images')}</TabList.Item>
  82. </TabList>
  83. </StyledTabs>
  84. </Layout.Header>
  85. <Layout.Body>
  86. <Layout.Main fullWidth>
  87. <PaddedContainer>
  88. <PageFilterBar condensed>
  89. <ProjectPageFilter />
  90. <DatePageFilter />
  91. </PageFilterBar>
  92. </PaddedContainer>
  93. <FilterOptionsContainer>
  94. <DomainSelector value={filters[SPAN_DOMAIN] || ''} />
  95. <ResourceTypeSelector value={filters[RESOURCE_TYPE] || ''} />
  96. <PageSelector value={filters[TRANSACTION] || ''} />
  97. <SwitchContainer>
  98. <SwitchButton
  99. isActive={filters[RESOURCE_RENDER_BLOCKING_STATUS] === 'blocking'}
  100. toggle={handleBlockingToggle}
  101. />
  102. {t('Render Blocking')}
  103. </SwitchContainer>
  104. </FilterOptionsContainer>
  105. <ResourceTable sort={sort} />
  106. <ResourceSidebar groupId={filters[DESCRIPTION]} />
  107. </Layout.Main>
  108. </Layout.Body>
  109. </ModulePageProviders>
  110. );
  111. }
  112. function DomainSelector({value}: {value?: string}) {
  113. const location = useLocation();
  114. const {data} = useResourceDomainsQuery();
  115. const options: Option[] = [
  116. {value: '', label: 'All'},
  117. ...data.map(domain => ({
  118. value: domain,
  119. label: domain,
  120. })),
  121. ];
  122. return (
  123. <SelectControlWithProps
  124. inFieldLabel={`${t('Domain')}:`}
  125. options={options}
  126. value={value}
  127. onChange={newValue => {
  128. browserHistory.push({
  129. ...location,
  130. query: {
  131. ...location.query,
  132. [SPAN_DOMAIN]: newValue?.value,
  133. },
  134. });
  135. }}
  136. />
  137. );
  138. }
  139. function ResourceTypeSelector({value}: {value?: string}) {
  140. const location = useLocation();
  141. const options: Option[] = [
  142. {value: '', label: 'All'},
  143. {value: 'resource.script', label: `${t('JavaScript')} (.js)`},
  144. {value: 'resource.css', label: `${t('Stylesheet')} (.css)`},
  145. ];
  146. return (
  147. <SelectControlWithProps
  148. inFieldLabel={`${t('Type')}:`}
  149. options={options}
  150. value={value}
  151. onChange={newValue => {
  152. browserHistory.push({
  153. ...location,
  154. query: {
  155. ...location.query,
  156. [RESOURCE_TYPE]: newValue?.value,
  157. },
  158. });
  159. }}
  160. />
  161. );
  162. }
  163. function PageSelector({value}: {value?: string}) {
  164. const location = useLocation();
  165. const {data: pages} = useResourcePagesQuery();
  166. const options: Option[] = [
  167. {value: '', label: 'All'},
  168. ...pages.map(page => ({value: page, label: page})),
  169. ];
  170. return (
  171. <SelectControlWithProps
  172. inFieldLabel={`${t('Page')}:`}
  173. options={options}
  174. value={value}
  175. onChange={newValue => {
  176. browserHistory.push({
  177. ...location,
  178. query: {
  179. ...location.query,
  180. [TRANSACTION]: newValue?.value,
  181. },
  182. });
  183. }}
  184. />
  185. );
  186. }
  187. const SwitchContainer = styled('div')`
  188. display: flex;
  189. align-items: center;
  190. column-gap: ${space(1)};
  191. `;
  192. function SelectControlWithProps(props: ControlProps & {options: Option[]}) {
  193. return <SelectControl {...props} />;
  194. }
  195. export const PaddedContainer = styled('div')`
  196. margin-bottom: ${space(2)};
  197. `;
  198. const StyledTabs = styled(Tabs)`
  199. grid-column: 1/-1;
  200. `;
  201. const FilterOptionsContainer = styled('div')`
  202. display: grid;
  203. grid-template-columns: repeat(4, 1fr);
  204. gap: ${space(2)};
  205. margin-bottom: ${space(2)};
  206. max-width: 800px;
  207. `;
  208. export default ResourcesLandingPage;