sidebar.tsx 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import {Fragment, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import HighlightTopRightPattern from 'sentry-images/pattern/highlight-top-right.svg';
  4. import {LinkButton} from 'sentry/components/button';
  5. import {CompactSelect} from 'sentry/components/compactSelect';
  6. import IdBadge from 'sentry/components/idBadge';
  7. import {SdkDocumentation} from 'sentry/components/onboarding/gettingStartedDoc/sdkDocumentation';
  8. import SidebarPanel from 'sentry/components/sidebar/sidebarPanel';
  9. import type {CommonSidebarProps} from 'sentry/components/sidebar/types';
  10. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  11. import {customMetricPlatforms} from 'sentry/data/platformCategories';
  12. import platforms from 'sentry/data/platforms';
  13. import {t, tct} from 'sentry/locale';
  14. import {space} from 'sentry/styles/space';
  15. import type {Project, SelectValue} from 'sentry/types';
  16. import {METRICS_DOCS_URL} from 'sentry/utils/metrics/constants';
  17. import useOrganization from 'sentry/utils/useOrganization';
  18. import {useCurrentProjectState} from './useCurrentProjectState';
  19. function MetricsOnboardingSidebar(props: CommonSidebarProps) {
  20. const {currentPanel, collapsed, hidePanel, orientation} = props;
  21. const organization = useOrganization();
  22. const isActive = currentPanel === SidebarPanelKey.METRICS_ONBOARDING;
  23. const hasProjectAccess = organization.access.includes('project:read');
  24. const {
  25. projects,
  26. allProjects,
  27. currentProject,
  28. setCurrentProject,
  29. supportedProjects,
  30. unsupportedProjects,
  31. hasDocs,
  32. } = useCurrentProjectState({
  33. isActive,
  34. });
  35. const projectSelectOptions = useMemo(() => {
  36. const supportedProjectItems: SelectValue<string>[] = supportedProjects
  37. .sort((aProject, bProject) => {
  38. // if we're comparing two projects w/ or w/o custom metrics alphabetical sort
  39. if (aProject.hasCustomMetrics === bProject.hasCustomMetrics) {
  40. return aProject.slug.localeCompare(bProject.slug);
  41. }
  42. // otherwise sort by whether or not they have custom metrics
  43. return aProject.hasCustomMetrics ? 1 : -1;
  44. })
  45. .map(project => {
  46. return {
  47. value: project.id,
  48. textValue: project.id,
  49. label: (
  50. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  51. ),
  52. };
  53. });
  54. const unsupportedProjectItems: SelectValue<string>[] = unsupportedProjects.map(
  55. project => {
  56. return {
  57. value: project.id,
  58. textValue: project.id,
  59. label: (
  60. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  61. ),
  62. disabled: true,
  63. };
  64. }
  65. );
  66. return [
  67. {
  68. label: t('Supported'),
  69. options: supportedProjectItems,
  70. },
  71. {
  72. label: t('Unsupported'),
  73. options: unsupportedProjectItems,
  74. },
  75. ];
  76. }, [supportedProjects, unsupportedProjects]);
  77. const selectedProject = currentProject ?? projects[0] ?? allProjects[0];
  78. if (!isActive || !hasProjectAccess || !selectedProject) {
  79. return null;
  80. }
  81. return (
  82. <StyledSidebarPanel
  83. orientation={orientation}
  84. collapsed={collapsed}
  85. hidePanel={hidePanel}
  86. >
  87. <TopRightBackgroundImage src={HighlightTopRightPattern} />
  88. <TaskList>
  89. <Heading>{t('Getting Started with custom metrics')}</Heading>
  90. <HeaderActions>
  91. <div
  92. onClick={e => {
  93. // we need to stop bubbling the CompactSelect click event
  94. // failing to do so will cause the sidebar panel to close
  95. // the event.target will be unmounted by the time the panel listener
  96. // receives the event and assume the click was outside the panel
  97. e.stopPropagation();
  98. }}
  99. >
  100. <CompactSelect
  101. triggerLabel={
  102. currentProject ? (
  103. <StyledIdBadge
  104. project={currentProject}
  105. avatarSize={16}
  106. hideOverflow
  107. disableLink
  108. />
  109. ) : (
  110. t('Select a project')
  111. )
  112. }
  113. value={currentProject?.id}
  114. onChange={opt =>
  115. setCurrentProject(allProjects.find(p => p.id === opt.value))
  116. }
  117. triggerProps={{'aria-label': currentProject?.slug}}
  118. options={projectSelectOptions}
  119. position="bottom-end"
  120. />
  121. </div>
  122. </HeaderActions>
  123. <OnboardingContent currentProject={selectedProject} hasDocs={hasDocs} />
  124. </TaskList>
  125. </StyledSidebarPanel>
  126. );
  127. }
  128. function OnboardingContent({
  129. currentProject,
  130. hasDocs,
  131. }: {
  132. currentProject: Project;
  133. hasDocs: boolean;
  134. }) {
  135. const organization = useOrganization();
  136. const currentPlatform = currentProject.platform
  137. ? platforms.find(p => p.id === currentProject.platform)
  138. : undefined;
  139. const supportsCustomMetrics =
  140. currentProject.platform && customMetricPlatforms.has(currentProject.platform);
  141. if (!supportsCustomMetrics) {
  142. return (
  143. <FallbackContentWrapper>
  144. <div>
  145. {tct('Custom metrics aren’t available for your [platform] project.', {
  146. platform: currentPlatform?.name || currentProject.slug,
  147. })}
  148. </div>
  149. <div>
  150. <LinkButton size="sm" href={METRICS_DOCS_URL} external>
  151. {t('Go to Sentry Documentation')}
  152. </LinkButton>
  153. </div>
  154. </FallbackContentWrapper>
  155. );
  156. }
  157. if (!hasDocs || !currentPlatform) {
  158. return (
  159. <FallbackContentWrapper>
  160. <div>
  161. {tct(
  162. 'Fiddlesticks. This checklist isn’t available for your [project] project yet, but for now, go to Sentry docs for installation details.',
  163. {project: currentProject.slug}
  164. )}
  165. </div>
  166. <div>
  167. <LinkButton size="sm" href={METRICS_DOCS_URL} external>
  168. {t('Read Docs')}
  169. </LinkButton>
  170. </div>
  171. </FallbackContentWrapper>
  172. );
  173. }
  174. return (
  175. <Fragment>
  176. <IntroText>
  177. {tct(
  178. `Adding custom metrics to your [platform] project is simple. Make sure you've got these basics down.`,
  179. {platform: currentPlatform?.name || currentProject.slug}
  180. )}
  181. </IntroText>
  182. <SdkDocumentation
  183. platform={currentPlatform}
  184. organization={organization}
  185. projectSlug={currentProject.slug}
  186. projectId={currentProject.id}
  187. activeProductSelection={[]}
  188. configType="customMetricsOnboarding"
  189. />
  190. </Fragment>
  191. );
  192. }
  193. const IntroText = styled('div')`
  194. padding-top: ${space(3)};
  195. `;
  196. const StyledSidebarPanel = styled(SidebarPanel)`
  197. width: 600px;
  198. max-width: 100%;
  199. `;
  200. const TopRightBackgroundImage = styled('img')`
  201. position: absolute;
  202. top: 0;
  203. right: 0;
  204. width: 60%;
  205. user-select: none;
  206. `;
  207. const TaskList = styled('div')`
  208. display: grid;
  209. grid-auto-flow: row;
  210. grid-template-columns: 100%;
  211. gap: ${space(1)};
  212. margin: 50px ${space(4)} ${space(4)} ${space(4)};
  213. `;
  214. const Heading = styled('div')`
  215. display: flex;
  216. color: ${p => p.theme.activeText};
  217. font-size: ${p => p.theme.fontSizeExtraSmall};
  218. text-transform: uppercase;
  219. font-weight: 600;
  220. line-height: 1;
  221. margin-top: ${space(3)};
  222. `;
  223. const StyledIdBadge = styled(IdBadge)`
  224. overflow: hidden;
  225. white-space: nowrap;
  226. flex-shrink: 1;
  227. `;
  228. const HeaderActions = styled('div')`
  229. display: flex;
  230. flex-direction: row;
  231. justify-content: space-between;
  232. gap: ${space(3)};
  233. `;
  234. const FallbackContentWrapper = styled('div')`
  235. padding: ${space(2)} 0px;
  236. display: flex;
  237. flex-direction: column;
  238. gap: ${space(2)};
  239. `;
  240. export default MetricsOnboardingSidebar;