sidebar.tsx 7.7 KB

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