sidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. import {Fragment, useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import HighlightTopRightPattern from 'sentry-images/pattern/highlight-top-right.svg';
  4. import {Button} from 'sentry/components/button';
  5. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  6. import IdBadge from 'sentry/components/idBadge';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import useOnboardingDocs from 'sentry/components/onboardingWizard/useOnboardingDocs';
  9. import OnboardingStep from 'sentry/components/sidebar/onboardingStep';
  10. import SidebarPanel from 'sentry/components/sidebar/sidebarPanel';
  11. import {CommonSidebarProps, SidebarPanelKey} from 'sentry/components/sidebar/types';
  12. import {withoutPerformanceSupport} from 'sentry/data/platformCategories';
  13. import platforms from 'sentry/data/platforms';
  14. import {t, tct} from 'sentry/locale';
  15. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  16. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  17. import pulsingIndicatorStyles from 'sentry/styles/pulsingIndicator';
  18. import {space} from 'sentry/styles/space';
  19. import {Project} from 'sentry/types';
  20. import EventWaiter from 'sentry/utils/eventWaiter';
  21. import useApi from 'sentry/utils/useApi';
  22. import useOrganization from 'sentry/utils/useOrganization';
  23. import usePrevious from 'sentry/utils/usePrevious';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import {filterProjects, generateDocKeys, isPlatformSupported} from './utils';
  26. function PerformanceOnboardingSidebar(props: CommonSidebarProps) {
  27. const {currentPanel, collapsed, hidePanel, orientation} = props;
  28. const isActive = currentPanel === SidebarPanelKey.PERFORMANCE_ONBOARDING;
  29. const organization = useOrganization();
  30. const hasProjectAccess = organization.access.includes('project:read');
  31. const {projects, initiallyLoaded: projectsLoaded} = useProjects();
  32. const [currentProject, setCurrentProject] = useState<Project | undefined>(undefined);
  33. const {selection, isReady} = useLegacyStore(PageFiltersStore);
  34. const {projectsWithoutFirstTransactionEvent, projectsForOnboarding} =
  35. filterProjects(projects);
  36. useEffect(() => {
  37. if (
  38. currentProject ||
  39. projects.length === 0 ||
  40. !isReady ||
  41. !isActive ||
  42. projectsWithoutFirstTransactionEvent.length <= 0
  43. ) {
  44. return;
  45. }
  46. // Establish current project
  47. const projectMap: Record<string, Project> = projects.reduce((acc, project) => {
  48. acc[project.id] = project;
  49. return acc;
  50. }, {});
  51. if (selection.projects.length) {
  52. const projectSelection = selection.projects.map(
  53. projectId => projectMap[String(projectId)]
  54. );
  55. // Among the project selection, find a project that has performance onboarding docs support, and has not sent
  56. // a first transaction event.
  57. const maybeProject = projectSelection.find(project =>
  58. projectsForOnboarding.includes(project)
  59. );
  60. if (maybeProject) {
  61. setCurrentProject(maybeProject);
  62. return;
  63. }
  64. // Among the project selection, find a project that has not sent a first transaction event
  65. const maybeProjectFallback = projectSelection.find(project =>
  66. projectsWithoutFirstTransactionEvent.includes(project)
  67. );
  68. if (maybeProjectFallback) {
  69. setCurrentProject(maybeProjectFallback);
  70. return;
  71. }
  72. }
  73. // Among the projects, find a project that has performance onboarding docs support, and has not sent
  74. // a first transaction event.
  75. if (projectsForOnboarding.length) {
  76. setCurrentProject(projectsForOnboarding[0]);
  77. return;
  78. }
  79. // Otherwise, pick a first project that has not sent a first transaction event.
  80. setCurrentProject(projectsWithoutFirstTransactionEvent[0]);
  81. }, [
  82. selection.projects,
  83. projects,
  84. isActive,
  85. isReady,
  86. projectsForOnboarding,
  87. projectsWithoutFirstTransactionEvent,
  88. currentProject,
  89. ]);
  90. if (
  91. !isActive ||
  92. !hasProjectAccess ||
  93. currentProject === undefined ||
  94. !projectsLoaded ||
  95. !projects ||
  96. projects.length <= 0 ||
  97. projectsWithoutFirstTransactionEvent.length <= 0
  98. ) {
  99. return null;
  100. }
  101. const items: MenuItemProps[] = projectsWithoutFirstTransactionEvent.reduce(
  102. (acc: MenuItemProps[], project) => {
  103. const itemProps: MenuItemProps = {
  104. key: project.id,
  105. label: (
  106. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  107. ),
  108. onAction: function switchProject() {
  109. setCurrentProject(project);
  110. },
  111. };
  112. if (currentProject.id === project.id) {
  113. acc.unshift(itemProps);
  114. } else {
  115. acc.push(itemProps);
  116. }
  117. return acc;
  118. },
  119. []
  120. );
  121. return (
  122. <TaskSidebarPanel
  123. orientation={orientation}
  124. collapsed={collapsed}
  125. hidePanel={hidePanel}
  126. >
  127. <TopRightBackgroundImage src={HighlightTopRightPattern} />
  128. <TaskList>
  129. <Heading>{t('Boost Performance')}</Heading>
  130. <DropdownMenu
  131. items={items}
  132. triggerLabel={
  133. <StyledIdBadge
  134. project={currentProject}
  135. avatarSize={16}
  136. hideOverflow
  137. disableLink
  138. />
  139. }
  140. triggerProps={{'aria-label': currentProject.slug}}
  141. position="bottom-end"
  142. />
  143. <OnboardingContent currentProject={currentProject} />
  144. </TaskList>
  145. </TaskSidebarPanel>
  146. );
  147. }
  148. function OnboardingContent({currentProject}: {currentProject: Project}) {
  149. const api = useApi();
  150. const organization = useOrganization();
  151. const previousProject = usePrevious(currentProject);
  152. const [received, setReceived] = useState<boolean>(false);
  153. useEffect(() => {
  154. if (previousProject.id !== currentProject.id) {
  155. setReceived(false);
  156. }
  157. }, [previousProject.id, currentProject.id]);
  158. const currentPlatform = currentProject.platform
  159. ? platforms.find(p => p.id === currentProject.platform)
  160. : undefined;
  161. const docKeys = currentPlatform ? generateDocKeys(currentPlatform.id) : [];
  162. const {docContents, isLoading, hasOnboardingContents} = useOnboardingDocs({
  163. project: currentProject,
  164. docKeys,
  165. isPlatformSupported: isPlatformSupported(currentPlatform),
  166. });
  167. if (isLoading) {
  168. return <LoadingIndicator />;
  169. }
  170. const doesNotSupportPerformance = currentProject.platform
  171. ? withoutPerformanceSupport.has(currentProject.platform)
  172. : false;
  173. if (doesNotSupportPerformance) {
  174. return (
  175. <Fragment>
  176. <div>
  177. {tct(
  178. 'Fiddlesticks. Performance isn’t available for your [platform] project yet but we’re definitely still working on it. Stay tuned.',
  179. {platform: currentPlatform?.name || currentProject.slug}
  180. )}
  181. </div>
  182. <div>
  183. <Button size="sm" href="https://docs.sentry.io/platforms/" external>
  184. {t('Go to Sentry Documentation')}
  185. </Button>
  186. </div>
  187. </Fragment>
  188. );
  189. }
  190. if (!currentPlatform || !hasOnboardingContents) {
  191. return (
  192. <Fragment>
  193. <div>
  194. {tct(
  195. 'Fiddlesticks. This checklist isn’t available for your [project] project yet, but for now, go to Sentry docs for installation details.',
  196. {project: currentProject.slug}
  197. )}
  198. </div>
  199. <div>
  200. <Button
  201. size="sm"
  202. href="https://docs.sentry.io/product/performance/getting-started/"
  203. external
  204. >
  205. {t('Go to documentation')}
  206. </Button>
  207. </div>
  208. </Fragment>
  209. );
  210. }
  211. return (
  212. <Fragment>
  213. <div>
  214. {tct(
  215. `Adding Performance to your [platform] project is simple. Make sure you've got these basics down.`,
  216. {platform: currentPlatform?.name || currentProject.slug}
  217. )}
  218. </div>
  219. {docKeys.map((docKey, index) => {
  220. let footer: React.ReactNode = null;
  221. if (index === docKeys.length - 1) {
  222. footer = (
  223. <EventWaiter
  224. api={api}
  225. organization={organization}
  226. project={currentProject}
  227. eventType="transaction"
  228. onIssueReceived={() => {
  229. setReceived(true);
  230. }}
  231. >
  232. {() => (received ? <EventReceivedIndicator /> : <EventWaitingIndicator />)}
  233. </EventWaiter>
  234. );
  235. }
  236. return (
  237. <div key={index}>
  238. <OnboardingStep
  239. docContent={docContents[docKey]}
  240. docKey={docKey}
  241. prefix="perf"
  242. project={currentProject}
  243. />
  244. {footer}
  245. </div>
  246. );
  247. })}
  248. </Fragment>
  249. );
  250. }
  251. const TaskSidebarPanel = styled(SidebarPanel)`
  252. width: 450px;
  253. `;
  254. const TopRightBackgroundImage = styled('img')`
  255. position: absolute;
  256. top: 0;
  257. right: 0;
  258. width: 60%;
  259. user-select: none;
  260. `;
  261. const TaskList = styled('div')`
  262. display: grid;
  263. grid-auto-flow: row;
  264. grid-template-columns: 100%;
  265. gap: ${space(1)};
  266. margin: 50px ${space(4)} ${space(4)} ${space(4)};
  267. `;
  268. const Heading = styled('div')`
  269. display: flex;
  270. color: ${p => p.theme.activeText};
  271. font-size: ${p => p.theme.fontSizeExtraSmall};
  272. text-transform: uppercase;
  273. font-weight: 600;
  274. line-height: 1;
  275. margin-top: ${space(3)};
  276. `;
  277. const StyledIdBadge = styled(IdBadge)`
  278. overflow: hidden;
  279. white-space: nowrap;
  280. flex-shrink: 1;
  281. `;
  282. const PulsingIndicator = styled('div')`
  283. ${pulsingIndicatorStyles};
  284. margin-right: ${space(1)};
  285. `;
  286. const EventWaitingIndicator = styled((p: React.HTMLAttributes<HTMLDivElement>) => (
  287. <div {...p}>
  288. <PulsingIndicator />
  289. {t("Waiting for this project's first transaction event")}
  290. </div>
  291. ))`
  292. display: flex;
  293. align-items: center;
  294. flex-grow: 1;
  295. font-size: ${p => p.theme.fontSizeMedium};
  296. color: ${p => p.theme.pink400};
  297. `;
  298. const EventReceivedIndicator = styled((p: React.HTMLAttributes<HTMLDivElement>) => (
  299. <div {...p}>
  300. {'🎉 '}
  301. {t("We've received this project's first transaction event!")}
  302. </div>
  303. ))`
  304. display: flex;
  305. align-items: center;
  306. flex-grow: 1;
  307. font-size: ${p => p.theme.fontSizeMedium};
  308. color: ${p => p.theme.successText};
  309. `;
  310. export default PerformanceOnboardingSidebar;