setupDocs.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. import {Fragment, useCallback, useEffect, useState} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {motion} from 'framer-motion';
  5. import * as qs from 'query-string';
  6. import {loadDocs} from 'sentry/actionCreators/projects';
  7. import {Alert} from 'sentry/components/alert';
  8. import ExternalLink from 'sentry/components/links/externalLink';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import {DocumentationWrapper} from 'sentry/components/onboarding/documentationWrapper';
  11. import {Footer} from 'sentry/components/onboarding/footer';
  12. import {FooterWithViewSampleErrorButton} from 'sentry/components/onboarding/footerWithViewSampleErrorButton';
  13. import {PlatformKey} from 'sentry/data/platformCategories';
  14. import platforms from 'sentry/data/platforms';
  15. import {t, tct} from 'sentry/locale';
  16. import {space} from 'sentry/styles/space';
  17. import {Project} from 'sentry/types';
  18. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  19. import getDynamicText from 'sentry/utils/getDynamicText';
  20. import {platformToIntegrationMap} from 'sentry/utils/integrationUtil';
  21. import useApi from 'sentry/utils/useApi';
  22. import {useExperiment} from 'sentry/utils/useExperiment';
  23. import useOrganization from 'sentry/utils/useOrganization';
  24. import useProjects from 'sentry/utils/useProjects';
  25. import FirstEventFooter from './components/firstEventFooter';
  26. import FullIntroduction from './components/fullIntroduction';
  27. import ProjectSidebarSection from './components/projectSidebarSection';
  28. import IntegrationSetup from './integrationSetup';
  29. import {StepProps} from './types';
  30. import {usePersistedOnboardingState} from './utils';
  31. /**
  32. * The documentation will include the following string should it be missing the
  33. * verification example, which currently a lot of docs are.
  34. */
  35. const INCOMPLETE_DOC_FLAG = 'TODO-ADD-VERIFICATION-EXAMPLE';
  36. type PlatformDoc = {html: string; link: string};
  37. type Props = {
  38. search: string;
  39. } & StepProps;
  40. function ProjectDocs(props: {
  41. hasError: boolean;
  42. onRetry: () => void;
  43. platform: PlatformKey | null;
  44. platformDocs: PlatformDoc | null;
  45. project: Project;
  46. }) {
  47. const testOnlyAlert = (
  48. <Alert type="warning">
  49. Platform documentation is not rendered in for tests in CI
  50. </Alert>
  51. );
  52. const missingExampleWarning = () => {
  53. const missingExample =
  54. props.platformDocs && props.platformDocs.html.includes(INCOMPLETE_DOC_FLAG);
  55. if (!missingExample) {
  56. return null;
  57. }
  58. return (
  59. <Alert type="warning" showIcon>
  60. {tct(
  61. `Looks like this getting started example is still undergoing some
  62. work and doesn't include an example for triggering an event quite
  63. yet. If you have trouble sending your first event be sure to consult
  64. the [docsLink:full documentation] for [platform].`,
  65. {
  66. docsLink: <ExternalLink href={props.platformDocs?.link} />,
  67. platform: platforms.find(p => p.id === props.platform)?.name,
  68. }
  69. )}
  70. </Alert>
  71. );
  72. };
  73. const docs = props.platformDocs !== null && (
  74. <DocsWrapper key={props.platformDocs.html}>
  75. <DocumentationWrapper dangerouslySetInnerHTML={{__html: props.platformDocs.html}} />
  76. {missingExampleWarning()}
  77. </DocsWrapper>
  78. );
  79. const loadingError = (
  80. <LoadingError
  81. message={t(
  82. 'Failed to load documentation for the %s platform.',
  83. props.project?.platform
  84. )}
  85. onRetry={props.onRetry}
  86. />
  87. );
  88. const currentPlatform = props.platform ?? props.project?.platform ?? 'other';
  89. return (
  90. <Fragment>
  91. <FullIntroduction currentPlatform={currentPlatform} />
  92. {getDynamicText({
  93. value: !props.hasError ? docs : loadingError,
  94. fixed: testOnlyAlert,
  95. })}
  96. </Fragment>
  97. );
  98. }
  99. function SetupDocs({search, route, router, location}: Props) {
  100. const api = useApi();
  101. const organization = useOrganization();
  102. const {projects: rawProjects} = useProjects();
  103. const [clientState, setClientState] = usePersistedOnboardingState();
  104. const {logExperiment, experimentAssignment} = useExperiment(
  105. 'OnboardingNewFooterExperiment',
  106. {
  107. logExperimentOnMount: false,
  108. }
  109. );
  110. const singleSelectPlatform = !!organization?.features.includes(
  111. 'onboarding-remove-multiselect-platform'
  112. );
  113. const heartbeatFooter = !!organization?.features.includes(
  114. 'onboarding-heartbeat-footer'
  115. );
  116. const selectedPlatforms = clientState?.selectedPlatforms || [];
  117. const platformToProjectIdMap = clientState?.platformToProjectIdMap || {};
  118. // id is really slug here
  119. const projectSlugs = selectedPlatforms
  120. .map(platform => platformToProjectIdMap[platform])
  121. .filter((slug): slug is string => slug !== undefined);
  122. const selectedProjectsSet = new Set(projectSlugs);
  123. // get projects in the order they appear in selectedPlatforms
  124. const projects = projectSlugs
  125. .map(slug => rawProjects.find(project => project.slug === slug))
  126. .filter((project): project is Project => project !== undefined);
  127. // SDK instrumentation
  128. const [hasError, setHasError] = useState(false);
  129. const [platformDocs, setPlatformDocs] = useState<PlatformDoc | null>(null);
  130. const [loadedPlatform, setLoadedPlatform] = useState<PlatformKey | null>(null);
  131. // store what projects have sent first event in state based project.firstEvent
  132. const [hasFirstEventMap, setHasFirstEventMap] = useState<Record<string, boolean>>(
  133. projects.reduce((accum, project: Project) => {
  134. accum[project.id] = !!project.firstEvent;
  135. return accum;
  136. }, {} as Record<string, boolean>)
  137. );
  138. const checkProjectHasFirstEvent = (project: Project) => {
  139. return !!hasFirstEventMap[project.id];
  140. };
  141. const {project_id: rawProjectId} = qs.parse(search);
  142. const rawProjectIndex = projects.findIndex(p => p.id === rawProjectId);
  143. const firstProjectNoError = projects.findIndex(p => selectedProjectsSet.has(p.slug));
  144. // Select a project based on search params. If non exist, use the first project without first event.
  145. const projectIndex = rawProjectIndex >= 0 ? rawProjectIndex : firstProjectNoError;
  146. const project = projects[projectIndex];
  147. // find the next project that doesn't have a first event
  148. const nextProject = projects.find(
  149. (p, i) => i > projectIndex && !checkProjectHasFirstEvent(p)
  150. );
  151. const integrationSlug = project?.platform && platformToIntegrationMap[project.platform];
  152. const [integrationUseManualSetup, setIntegrationUseManualSetup] = useState(false);
  153. const currentPlatform = loadedPlatform ?? project?.platform ?? 'other';
  154. const fetchData = useCallback(async () => {
  155. // TODO: add better error handling logic
  156. if (!project?.platform) {
  157. return;
  158. }
  159. if (integrationSlug && !integrationUseManualSetup) {
  160. setLoadedPlatform(project.platform);
  161. setPlatformDocs(null);
  162. setHasError(false);
  163. return;
  164. }
  165. try {
  166. const loadedDocs = await loadDocs(
  167. api,
  168. organization.slug,
  169. project.slug,
  170. project.platform
  171. );
  172. setPlatformDocs(loadedDocs);
  173. setLoadedPlatform(project.platform);
  174. setHasError(false);
  175. } catch (error) {
  176. setHasError(error);
  177. throw error;
  178. }
  179. }, [project, api, organization, integrationSlug, integrationUseManualSetup]);
  180. useEffect(() => {
  181. fetchData();
  182. }, [fetchData]);
  183. // log experiment on mount if feature enabled
  184. useEffect(() => {
  185. if (heartbeatFooter) {
  186. logExperiment();
  187. }
  188. }, [logExperiment, heartbeatFooter]);
  189. if (!project) {
  190. return null;
  191. }
  192. const setNewProject = (newProjectId: string) => {
  193. setLoadedPlatform(null);
  194. setPlatformDocs(null);
  195. setHasError(false);
  196. setIntegrationUseManualSetup(false);
  197. const searchParams = new URLSearchParams({
  198. sub_step: 'project',
  199. project_id: newProjectId,
  200. });
  201. browserHistory.push(`${window.location.pathname}?${searchParams}`);
  202. clientState &&
  203. setClientState({
  204. ...clientState,
  205. state: 'projects_selected',
  206. url: `setup-docs/?${searchParams}`,
  207. });
  208. };
  209. const selectProject = (newProjectId: string) => {
  210. const matchedProject = projects.find(p => p.id === newProjectId);
  211. trackAdvancedAnalyticsEvent('growth.onboarding_clicked_project_in_sidebar', {
  212. organization,
  213. platform: matchedProject?.platform || 'unknown',
  214. });
  215. setNewProject(newProjectId);
  216. };
  217. return (
  218. <Fragment>
  219. <Wrapper>
  220. {!singleSelectPlatform && (
  221. <SidebarWrapper>
  222. <ProjectSidebarSection
  223. projects={projects}
  224. selectedPlatformToProjectIdMap={Object.fromEntries(
  225. selectedPlatforms.map(platform => [
  226. platform,
  227. platformToProjectIdMap[platform],
  228. ])
  229. )}
  230. activeProject={project}
  231. {...{checkProjectHasFirstEvent, selectProject}}
  232. />
  233. </SidebarWrapper>
  234. )}
  235. <MainContent>
  236. {integrationSlug && !integrationUseManualSetup ? (
  237. <IntegrationSetup
  238. integrationSlug={integrationSlug}
  239. project={project}
  240. onClickManualSetup={() => {
  241. setIntegrationUseManualSetup(true);
  242. }}
  243. />
  244. ) : (
  245. <ProjectDocs
  246. platform={loadedPlatform}
  247. project={project}
  248. hasError={hasError}
  249. platformDocs={platformDocs}
  250. onRetry={fetchData}
  251. />
  252. )}
  253. </MainContent>
  254. </Wrapper>
  255. {heartbeatFooter ? (
  256. experimentAssignment === 'variant2' ? (
  257. <FooterWithViewSampleErrorButton
  258. projectSlug={project.slug}
  259. projectId={project.id}
  260. route={route}
  261. router={router}
  262. location={location}
  263. newOrg
  264. />
  265. ) : experimentAssignment === 'variant1' ? (
  266. <Footer
  267. projectSlug={project.slug}
  268. projectId={project.id}
  269. route={route}
  270. router={router}
  271. location={location}
  272. newOrg
  273. />
  274. ) : (
  275. <FirstEventFooter
  276. project={project}
  277. organization={organization}
  278. isLast={!nextProject}
  279. hasFirstEvent={checkProjectHasFirstEvent(project)}
  280. onClickSetupLater={() => {
  281. const orgIssuesURL = `/organizations/${organization.slug}/issues/?project=${project.id}&referrer=onboarding-setup-docs`;
  282. trackAdvancedAnalyticsEvent(
  283. 'growth.onboarding_clicked_setup_platform_later',
  284. {
  285. organization,
  286. platform: currentPlatform,
  287. project_index: projectIndex,
  288. }
  289. );
  290. if (!project.platform || !clientState) {
  291. browserHistory.push(orgIssuesURL);
  292. return;
  293. }
  294. // if we have a next project, switch to that
  295. if (nextProject) {
  296. setNewProject(nextProject.id);
  297. } else {
  298. setClientState({
  299. ...clientState,
  300. state: 'finished',
  301. });
  302. browserHistory.push(orgIssuesURL);
  303. }
  304. }}
  305. handleFirstIssueReceived={() => {
  306. const newHasFirstEventMap = {...hasFirstEventMap, [project.id]: true};
  307. setHasFirstEventMap(newHasFirstEventMap);
  308. }}
  309. />
  310. )
  311. ) : (
  312. <FirstEventFooter
  313. project={project}
  314. organization={organization}
  315. isLast={!nextProject}
  316. hasFirstEvent={checkProjectHasFirstEvent(project)}
  317. onClickSetupLater={() => {
  318. const orgIssuesURL = `/organizations/${organization.slug}/issues/?project=${project.id}&referrer=onboarding-setup-docs`;
  319. trackAdvancedAnalyticsEvent(
  320. 'growth.onboarding_clicked_setup_platform_later',
  321. {
  322. organization,
  323. platform: currentPlatform,
  324. project_index: projectIndex,
  325. }
  326. );
  327. if (!project.platform || !clientState) {
  328. browserHistory.push(orgIssuesURL);
  329. return;
  330. }
  331. // if we have a next project, switch to that
  332. if (nextProject) {
  333. setNewProject(nextProject.id);
  334. } else {
  335. setClientState({
  336. ...clientState,
  337. state: 'finished',
  338. });
  339. browserHistory.push(orgIssuesURL);
  340. }
  341. }}
  342. handleFirstIssueReceived={() => {
  343. const newHasFirstEventMap = {...hasFirstEventMap, [project.id]: true};
  344. setHasFirstEventMap(newHasFirstEventMap);
  345. }}
  346. />
  347. )}
  348. </Fragment>
  349. );
  350. }
  351. export default SetupDocs;
  352. const AnimatedContentWrapper = styled(motion.div)`
  353. overflow: hidden;
  354. `;
  355. AnimatedContentWrapper.defaultProps = {
  356. initial: {
  357. height: 0,
  358. },
  359. animate: {
  360. height: 'auto',
  361. },
  362. exit: {
  363. height: 0,
  364. },
  365. };
  366. const DocsWrapper = styled(motion.div)``;
  367. DocsWrapper.defaultProps = {
  368. initial: {opacity: 0, y: 40},
  369. animate: {opacity: 1, y: 0},
  370. exit: {opacity: 0},
  371. };
  372. const Wrapper = styled('div')`
  373. display: flex;
  374. flex-direction: row;
  375. margin: ${space(2)};
  376. justify-content: center;
  377. `;
  378. const MainContent = styled('div')`
  379. max-width: 850px;
  380. min-width: 0;
  381. flex-grow: 1;
  382. `;
  383. // the number icon will be space(2) + 30px to the left of the margin of center column
  384. // so we need to offset the right margin by that much
  385. // also hide the sidebar if the screen is too small
  386. const SidebarWrapper = styled('div')`
  387. margin: ${space(1)} calc(${space(2)} + 30px + ${space(4)}) 0 ${space(2)};
  388. @media (max-width: 1150px) {
  389. display: none;
  390. }
  391. flex-basis: 240px;
  392. flex-grow: 0;
  393. flex-shrink: 0;
  394. min-width: 240px;
  395. `;