setupDocs.tsx 12 KB

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