sidebar.tsx 10 KB

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