sidebar.tsx 10.0 KB

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