sidebar.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 {CompactSelect} from 'sentry/components/compactSelect';
  6. import IdBadge from 'sentry/components/idBadge';
  7. import LoadingIndicator from 'sentry/components/loadingIndicator';
  8. import useOnboardingDocs from 'sentry/components/onboardingWizard/useOnboardingDocs';
  9. import useCurrentProjectState from 'sentry/components/replaysOnboarding/useCurrentProjectState';
  10. import {
  11. generateDocKeys,
  12. isPlatformSupported,
  13. } from 'sentry/components/replaysOnboarding/utils';
  14. import {DocumentationWrapper} from 'sentry/components/sidebar/onboardingStep';
  15. import SidebarPanel from 'sentry/components/sidebar/sidebarPanel';
  16. import {CommonSidebarProps, SidebarPanelKey} from 'sentry/components/sidebar/types';
  17. import {replayPlatforms} from 'sentry/data/platformCategories';
  18. import platforms from 'sentry/data/platforms';
  19. import {t, tct} from 'sentry/locale';
  20. import pulsingIndicatorStyles from 'sentry/styles/pulsingIndicator';
  21. import {space} from 'sentry/styles/space';
  22. import {Project, SelectValue} from 'sentry/types';
  23. import EventWaiter from 'sentry/utils/eventWaiter';
  24. import useApi from 'sentry/utils/useApi';
  25. import useOrganization from 'sentry/utils/useOrganization';
  26. import usePrevious from 'sentry/utils/usePrevious';
  27. function ReplaysOnboardingSidebar(props: CommonSidebarProps) {
  28. const {currentPanel, collapsed, hidePanel, orientation} = props;
  29. const organization = useOrganization();
  30. const isActive = currentPanel === SidebarPanelKey.REPLAYS_ONBOARDING;
  31. const hasProjectAccess = organization.access.includes('project:read');
  32. const {
  33. projects,
  34. allProjects,
  35. currentProject,
  36. setCurrentProject,
  37. supportedProjects,
  38. unsupportedProjects,
  39. } = useCurrentProjectState({
  40. currentPanel,
  41. });
  42. const projectSelectOptions = useMemo(() => {
  43. const supportedProjectItems: SelectValue<string>[] = supportedProjects
  44. .sort((aProject, bProject) => {
  45. // if we're comparing two projects w/ or w/o replays alphabetical sort
  46. if (aProject.hasReplays === bProject.hasReplays) {
  47. return aProject.slug.localeCompare(bProject.slug);
  48. }
  49. // otherwise sort by whether or not they have replays
  50. return aProject.hasReplays ? 1 : -1;
  51. })
  52. .map(project => {
  53. return {
  54. value: project.id,
  55. textValue: project.id,
  56. label: (
  57. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  58. ),
  59. };
  60. });
  61. const unsupportedProjectItems: SelectValue<string>[] = unsupportedProjects.map(
  62. project => {
  63. return {
  64. value: project.id,
  65. textValue: project.id,
  66. label: (
  67. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  68. ),
  69. disabled: true,
  70. };
  71. }
  72. );
  73. return [
  74. {
  75. label: t('Supported'),
  76. options: supportedProjectItems,
  77. },
  78. {
  79. label: t('Unsupported'),
  80. options: unsupportedProjectItems,
  81. },
  82. ];
  83. }, [supportedProjects, unsupportedProjects]);
  84. const selectedProject = currentProject ?? projects[0] ?? allProjects[0];
  85. if (!isActive || !hasProjectAccess || !selectedProject) {
  86. return null;
  87. }
  88. return (
  89. <TaskSidebarPanel
  90. orientation={orientation}
  91. collapsed={collapsed}
  92. hidePanel={hidePanel}
  93. >
  94. <TopRightBackgroundImage src={HighlightTopRightPattern} />
  95. <TaskList>
  96. <Heading>{t('Getting Started with Session Replay')}</Heading>
  97. <div
  98. onClick={e => {
  99. // we need to stop bubbling the CompactSelect click event
  100. // failing to do so will cause the sidebar panel to close
  101. // the event.target will be unmounted by the time the panel listener
  102. // receives the event and assume the click was outside the panel
  103. e.stopPropagation();
  104. }}
  105. >
  106. <CompactSelect
  107. triggerLabel={
  108. currentProject ? (
  109. <StyledIdBadge
  110. project={currentProject}
  111. avatarSize={16}
  112. hideOverflow
  113. disableLink
  114. />
  115. ) : (
  116. t('Select a project')
  117. )
  118. }
  119. value={currentProject?.id}
  120. onChange={opt => setCurrentProject(allProjects.find(p => p.id === opt.value))}
  121. triggerProps={{'aria-label': currentProject?.slug}}
  122. options={projectSelectOptions}
  123. position="bottom-end"
  124. />
  125. </div>
  126. <OnboardingContent currentProject={selectedProject} />
  127. </TaskList>
  128. </TaskSidebarPanel>
  129. );
  130. }
  131. function OnboardingContent({currentProject}: {currentProject: Project}) {
  132. const api = useApi();
  133. const organization = useOrganization();
  134. const previousProject = usePrevious(currentProject);
  135. const [received, setReceived] = useState<boolean>(false);
  136. useEffect(() => {
  137. if (previousProject.id !== currentProject.id) {
  138. setReceived(false);
  139. }
  140. }, [previousProject.id, currentProject.id]);
  141. const currentPlatform = currentProject.platform
  142. ? platforms.find(p => p.id === currentProject.platform)
  143. : undefined;
  144. const docKeys = currentPlatform ? generateDocKeys(currentPlatform.id) : [];
  145. const {docContents, isLoading, hasOnboardingContents} = useOnboardingDocs({
  146. project: currentProject,
  147. docKeys,
  148. isPlatformSupported: isPlatformSupported(currentPlatform),
  149. });
  150. if (isLoading) {
  151. return <LoadingIndicator />;
  152. }
  153. const doesNotSupportReplay = currentProject.platform
  154. ? !replayPlatforms.includes(currentProject.platform)
  155. : true;
  156. if (doesNotSupportReplay) {
  157. return (
  158. <Fragment>
  159. <div>
  160. {tct(
  161. 'Session Replay isn’t available for your [platform] project. It supports all browser JavaScript applications. It is built to work with @sentry/browser and our browser framework SDKs.',
  162. {platform: currentPlatform?.name || currentProject.slug}
  163. )}
  164. </div>
  165. <div>
  166. <Button size="sm" href="https://docs.sentry.io/platforms/" external>
  167. {t('Go to Sentry Documentation')}
  168. </Button>
  169. </div>
  170. </Fragment>
  171. );
  172. }
  173. if (!currentPlatform || !hasOnboardingContents) {
  174. return (
  175. <Fragment>
  176. <div>
  177. {tct(
  178. 'Fiddlesticks. This checklist isn’t available for your [project] project yet, but for now, go to Sentry docs for installation details.',
  179. {project: currentProject.slug}
  180. )}
  181. </div>
  182. <div>
  183. <Button
  184. size="sm"
  185. href="https://docs.sentry.io/platforms/javascript/session-replay/"
  186. external
  187. >
  188. {t('Read Docs')}
  189. </Button>
  190. </div>
  191. </Fragment>
  192. );
  193. }
  194. return (
  195. <Fragment>
  196. <div>
  197. {tct(
  198. `Adding Session Replay to your [platform] project is simple. Make sure you've got these basics down.`,
  199. {platform: currentPlatform?.name || currentProject.slug}
  200. )}
  201. </div>
  202. {docKeys.map((docKey, index) => {
  203. let footer: React.ReactNode = null;
  204. if (index === docKeys.length - 1) {
  205. footer = (
  206. <EventWaiter
  207. api={api}
  208. organization={organization}
  209. project={currentProject}
  210. eventType="replay"
  211. onIssueReceived={() => {
  212. setReceived(true);
  213. }}
  214. >
  215. {() => (received ? <EventReceivedIndicator /> : <EventWaitingIndicator />)}
  216. </EventWaiter>
  217. );
  218. }
  219. return (
  220. <div key={index}>
  221. <OnboardingStepV2 step={index + 1} content={docContents[docKey]} />
  222. {footer}
  223. </div>
  224. );
  225. })}
  226. </Fragment>
  227. );
  228. }
  229. // TODO: we'll have to move this into a folder for common consumption w/ Profiling, Performance etc.
  230. interface OnboardingStepV2Props {
  231. content: string;
  232. step: number;
  233. }
  234. function OnboardingStepV2({step, content}: OnboardingStepV2Props) {
  235. return (
  236. <OnboardingStepContainer>
  237. <div>
  238. <TaskStepNumber>{step}</TaskStepNumber>
  239. </div>
  240. <div>
  241. <DocumentationWrapper dangerouslySetInnerHTML={{__html: content}} />
  242. </div>
  243. </OnboardingStepContainer>
  244. );
  245. }
  246. const OnboardingStepContainer = styled('div')`
  247. display: flex;
  248. & > :last-child {
  249. overflow: hidden;
  250. }
  251. `;
  252. const TaskStepNumber = styled('div')`
  253. display: flex;
  254. margin-right: ${space(1.5)};
  255. background-color: ${p => p.theme.yellow300};
  256. border-radius: 50%;
  257. font-weight: bold;
  258. height: ${space(4)};
  259. width: ${space(4)};
  260. justify-content: center;
  261. align-items: center;
  262. `;
  263. const TaskSidebarPanel = styled(SidebarPanel)`
  264. width: 600px;
  265. max-width: 100%;
  266. `;
  267. const TopRightBackgroundImage = styled('img')`
  268. position: absolute;
  269. top: 0;
  270. right: 0;
  271. width: 60%;
  272. user-select: none;
  273. `;
  274. const TaskList = styled('div')`
  275. display: grid;
  276. grid-auto-flow: row;
  277. grid-template-columns: 100%;
  278. gap: ${space(1)};
  279. margin: 50px ${space(4)} ${space(4)} ${space(4)};
  280. `;
  281. const Heading = styled('div')`
  282. display: flex;
  283. color: ${p => p.theme.activeText};
  284. font-size: ${p => p.theme.fontSizeExtraSmall};
  285. text-transform: uppercase;
  286. font-weight: 600;
  287. line-height: 1;
  288. margin-top: ${space(3)};
  289. `;
  290. const StyledIdBadge = styled(IdBadge)`
  291. overflow: hidden;
  292. white-space: nowrap;
  293. flex-shrink: 1;
  294. `;
  295. const PulsingIndicator = styled('div')`
  296. ${pulsingIndicatorStyles};
  297. margin-right: ${space(1)};
  298. `;
  299. const EventWaitingIndicator = styled((p: React.HTMLAttributes<HTMLDivElement>) => (
  300. <div {...p}>
  301. <PulsingIndicator />
  302. {t("Waiting for this project's first user session")}
  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.pink400};
  310. `;
  311. const EventReceivedIndicator = styled((p: React.HTMLAttributes<HTMLDivElement>) => (
  312. <div {...p}>
  313. {'🎉 '}
  314. {t("We've received this project's first user session!")}
  315. </div>
  316. ))`
  317. display: flex;
  318. align-items: center;
  319. flex-grow: 1;
  320. font-size: ${p => p.theme.fontSizeMedium};
  321. color: ${p => p.theme.successText};
  322. `;
  323. export default ReplaysOnboardingSidebar;