sidebar.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. import type {ReactNode} from 'react';
  2. import {Fragment, useEffect, useMemo, useState} from 'react';
  3. import styled from '@emotion/styled';
  4. import {PlatformIcon} from 'platformicons';
  5. import HighlightTopRightPattern from 'sentry-images/pattern/highlight-top-right.svg';
  6. import {LinkButton} from 'sentry/components/button';
  7. import {CompactSelect} from 'sentry/components/compactSelect';
  8. import {FeedbackOnboardingLayout} from 'sentry/components/feedback/feedbackOnboarding/feedbackOnboardingLayout';
  9. import {CRASH_REPORT_HASH} from 'sentry/components/feedback/useFeedbackOnboarding';
  10. import RadioGroup from 'sentry/components/forms/controls/radioGroup';
  11. import IdBadge from 'sentry/components/idBadge';
  12. import LoadingIndicator from 'sentry/components/loadingIndicator';
  13. import {FeedbackOnboardingWebApiBanner} from 'sentry/components/onboarding/gettingStartedDoc/utils/feedbackOnboarding';
  14. import useCurrentProjectState from 'sentry/components/onboarding/gettingStartedDoc/utils/useCurrentProjectState';
  15. import {useLoadGettingStarted} from 'sentry/components/onboarding/gettingStartedDoc/utils/useLoadGettingStarted';
  16. import {PlatformOptionDropdown} from 'sentry/components/replaysOnboarding/platformOptionDropdown';
  17. import {replayJsFrameworkOptions} from 'sentry/components/replaysOnboarding/utils';
  18. import SidebarPanel from 'sentry/components/sidebar/sidebarPanel';
  19. import type {CommonSidebarProps} from 'sentry/components/sidebar/types';
  20. import {SidebarPanelKey} from 'sentry/components/sidebar/types';
  21. import TextOverflow from 'sentry/components/textOverflow';
  22. import {
  23. feedbackCrashApiPlatforms,
  24. feedbackNpmPlatforms,
  25. feedbackOnboardingPlatforms,
  26. feedbackWebApiPlatforms,
  27. feedbackWidgetPlatforms,
  28. replayBackendPlatforms,
  29. replayJsLoaderInstructionsPlatformList,
  30. } from 'sentry/data/platformCategories';
  31. import platforms, {otherPlatform} from 'sentry/data/platforms';
  32. import {t, tct} from 'sentry/locale';
  33. import {space} from 'sentry/styles/space';
  34. import type {SelectValue} from 'sentry/types/core';
  35. import type {PlatformKey, Project} from 'sentry/types/project';
  36. import {trackAnalytics} from 'sentry/utils/analytics';
  37. import {useLocation} from 'sentry/utils/useLocation';
  38. import useOrganization from 'sentry/utils/useOrganization';
  39. import useUrlParams from 'sentry/utils/useUrlParams';
  40. function FeedbackOnboardingSidebar(props: CommonSidebarProps) {
  41. const {currentPanel, collapsed, hidePanel, orientation} = props;
  42. const organization = useOrganization();
  43. const isActive = currentPanel === SidebarPanelKey.FEEDBACK_ONBOARDING;
  44. const hasProjectAccess = organization.access.includes('project:read');
  45. const {allProjects, currentProject, setCurrentProject} = useCurrentProjectState({
  46. currentPanel,
  47. targetPanel: SidebarPanelKey.FEEDBACK_ONBOARDING,
  48. onboardingPlatforms: feedbackOnboardingPlatforms,
  49. allPlatforms: feedbackOnboardingPlatforms,
  50. });
  51. const projectSelectOptions = useMemo(() => {
  52. const supportedProjectItems: SelectValue<string>[] = allProjects
  53. .sort((aProject, bProject) => {
  54. // if we're comparing two projects w/ or w/o feedback alphabetical sort
  55. if (aProject.hasNewFeedbacks === bProject.hasNewFeedbacks) {
  56. return aProject.slug.localeCompare(bProject.slug);
  57. }
  58. // otherwise sort by whether or not they have feedback
  59. return aProject.hasNewFeedbacks ? 1 : -1;
  60. })
  61. .map(project => {
  62. return {
  63. value: project.id,
  64. textValue: project.id,
  65. label: (
  66. <StyledIdBadge project={project} avatarSize={16} hideOverflow disableLink />
  67. ),
  68. };
  69. });
  70. return [
  71. {
  72. label: t('Supported'),
  73. options: supportedProjectItems,
  74. },
  75. ];
  76. }, [allProjects]);
  77. useEffect(() => {
  78. if (isActive && currentProject && hasProjectAccess) {
  79. // this tracks clicks from any source: feedback index, issue details feedback tab, banner callout, etc
  80. trackAnalytics('feedback.list-view-setup-sidebar', {
  81. organization,
  82. platform: currentProject?.platform ?? 'unknown',
  83. });
  84. }
  85. }, [organization, currentProject, isActive, hasProjectAccess]);
  86. if (!isActive || !hasProjectAccess || !currentProject) {
  87. return null;
  88. }
  89. return (
  90. <TaskSidebarPanel
  91. orientation={orientation}
  92. collapsed={collapsed}
  93. hidePanel={hidePanel}
  94. >
  95. <TopRightBackgroundImage src={HighlightTopRightPattern} />
  96. <TaskList>
  97. <Heading>{t('Getting Started with User Feedback')}</Heading>
  98. <HeaderActions>
  99. <div
  100. onClick={e => {
  101. // we need to stop bubbling the CompactSelect click event
  102. // failing to do so will cause the sidebar panel to close
  103. // the event.target will be unmounted by the time the panel listener
  104. // receives the event and assume the click was outside the panel
  105. e.stopPropagation();
  106. }}
  107. >
  108. <CompactSelect
  109. triggerLabel={
  110. currentProject ? (
  111. <StyledIdBadge
  112. project={currentProject}
  113. avatarSize={16}
  114. hideOverflow
  115. disableLink
  116. />
  117. ) : (
  118. t('Select a project')
  119. )
  120. }
  121. value={currentProject?.id}
  122. onChange={opt =>
  123. setCurrentProject(allProjects.find(p => p.id === opt.value))
  124. }
  125. triggerProps={{'aria-label': currentProject?.slug}}
  126. options={projectSelectOptions}
  127. position="bottom-end"
  128. />
  129. </div>
  130. </HeaderActions>
  131. <OnboardingContent currentProject={currentProject} />
  132. </TaskList>
  133. </TaskSidebarPanel>
  134. );
  135. }
  136. function OnboardingContent({currentProject}: {currentProject: Project}) {
  137. const organization = useOrganization();
  138. const jsFrameworkSelectOptions = replayJsFrameworkOptions().map(platform => {
  139. return {
  140. value: platform.id,
  141. textValue: platform.name,
  142. label: (
  143. <PlatformLabel>
  144. <PlatformIcon platform={platform.id} size={16} />
  145. <TextOverflow>{platform.name}</TextOverflow>
  146. </PlatformLabel>
  147. ),
  148. };
  149. });
  150. const [jsFramework, setJsFramework] = useState<{
  151. value: PlatformKey;
  152. label?: ReactNode;
  153. textValue?: string;
  154. }>(jsFrameworkSelectOptions[0]);
  155. const defaultTab = 'npm';
  156. const location = useLocation();
  157. const crashReportOnboarding = location.hash === CRASH_REPORT_HASH;
  158. const {getParamValue: setupMode, setParamValue: setSetupMode} = useUrlParams(
  159. 'mode',
  160. defaultTab
  161. );
  162. const currentPlatform = currentProject.platform
  163. ? platforms.find(p => p.id === currentProject.platform) ?? otherPlatform
  164. : otherPlatform;
  165. const webBackendPlatform = replayBackendPlatforms.includes(currentPlatform.id);
  166. const showJsFrameworkInstructions = webBackendPlatform && setupMode() === 'npm';
  167. const crashApiPlatform = feedbackCrashApiPlatforms.includes(currentPlatform.id);
  168. const widgetPlatform = feedbackWidgetPlatforms.includes(currentPlatform.id);
  169. const webApiPlatform = feedbackWebApiPlatforms.includes(currentPlatform.id);
  170. const npmOnlyFramework = feedbackNpmPlatforms
  171. .filter((p): p is PlatformKey => p !== 'javascript')
  172. .includes(currentPlatform.id);
  173. const showRadioButtons =
  174. replayJsLoaderInstructionsPlatformList.includes(currentPlatform.id) &&
  175. !crashReportOnboarding;
  176. const jsFrameworkPlatform =
  177. replayJsFrameworkOptions().find(p => p.id === jsFramework.value) ??
  178. replayJsFrameworkOptions()[0];
  179. const {
  180. isLoading,
  181. docs: newDocs,
  182. dsn,
  183. projectKeyId,
  184. } = useLoadGettingStarted({
  185. platform:
  186. showJsFrameworkInstructions && !crashReportOnboarding
  187. ? jsFrameworkPlatform
  188. : currentPlatform,
  189. projSlug: currentProject.slug,
  190. productType: 'feedback',
  191. orgSlug: organization.slug,
  192. });
  193. // New onboarding docs for initial loading of JS Framework options
  194. const {docs: jsFrameworkDocs} = useLoadGettingStarted({
  195. platform: jsFrameworkPlatform,
  196. projSlug: currentProject.slug,
  197. orgSlug: organization.slug,
  198. });
  199. if (webApiPlatform && !crashReportOnboarding) {
  200. return <FeedbackOnboardingWebApiBanner />;
  201. }
  202. const radioButtons = (
  203. <Header>
  204. {showRadioButtons ? (
  205. <StyledRadioGroup
  206. label="mode"
  207. choices={[
  208. [
  209. 'npm',
  210. webBackendPlatform ? (
  211. <PlatformSelect key="platform-select">
  212. {tct('I use [platformSelect]', {
  213. platformSelect: (
  214. <CompactSelect
  215. triggerLabel={jsFramework.label}
  216. value={jsFramework.value}
  217. onChange={setJsFramework}
  218. options={jsFrameworkSelectOptions}
  219. position="bottom-end"
  220. key={jsFramework.textValue}
  221. disabled={setupMode() === 'jsLoader'}
  222. />
  223. ),
  224. })}
  225. {jsFrameworkDocs?.platformOptions && (
  226. <PlatformOptionDropdown
  227. platformOptions={jsFrameworkDocs?.platformOptions}
  228. disabled={setupMode() === 'jsLoader'}
  229. />
  230. )}
  231. </PlatformSelect>
  232. ) : (
  233. t('I use NPM or Yarn')
  234. ),
  235. ],
  236. ['jsLoader', t('I use HTML templates (Loader Script)')],
  237. ]}
  238. value={setupMode()}
  239. onChange={setSetupMode}
  240. tooltipPosition={'top-start'}
  241. />
  242. ) : (
  243. newDocs?.platformOptions &&
  244. widgetPlatform &&
  245. !crashReportOnboarding &&
  246. !isLoading && (
  247. <PlatformSelect>
  248. {tct("I'm using [platformSelect]", {
  249. platformSelect: (
  250. <PlatformOptionDropdown platformOptions={newDocs?.platformOptions} />
  251. ),
  252. })}
  253. </PlatformSelect>
  254. )
  255. )}
  256. </Header>
  257. );
  258. if (isLoading) {
  259. return (
  260. <Fragment>
  261. {radioButtons}
  262. <LoadingIndicator />
  263. </Fragment>
  264. );
  265. }
  266. // No platform or not supported or no docs
  267. if (
  268. !currentPlatform ||
  269. !feedbackOnboardingPlatforms.includes(currentPlatform.id) ||
  270. !newDocs ||
  271. !dsn ||
  272. !projectKeyId
  273. ) {
  274. return (
  275. <Fragment>
  276. <div>
  277. {tct(
  278. 'Fiddlesticks. This checklist isn’t available for your [project] project yet, but for now, go to Sentry docs for installation details.',
  279. {project: currentProject.slug}
  280. )}
  281. </div>
  282. <div>
  283. <LinkButton
  284. size="sm"
  285. href="https://docs.sentry.io/platforms/javascript/user-feedback/"
  286. external
  287. >
  288. {t('Read Docs')}
  289. </LinkButton>
  290. </div>
  291. </Fragment>
  292. );
  293. }
  294. function getConfig() {
  295. if (crashReportOnboarding) {
  296. return 'crashReportOnboarding';
  297. }
  298. if (crashApiPlatform) {
  299. return 'feedbackOnboardingCrashApi';
  300. }
  301. if (
  302. setupMode() === 'npm' || // switched to NPM option
  303. (!setupMode() && defaultTab === 'npm' && widgetPlatform) || // default value for FE frameworks when ?mode={...} in URL is not set yet
  304. npmOnlyFramework // even if '?mode=jsLoader', only show npm instructions for FE frameworks)
  305. ) {
  306. return 'feedbackOnboardingNpm';
  307. }
  308. return 'feedbackOnboardingJsLoader';
  309. }
  310. return (
  311. <Fragment>
  312. {radioButtons}
  313. <FeedbackOnboardingLayout
  314. docsConfig={newDocs}
  315. dsn={dsn}
  316. activeProductSelection={[]}
  317. platformKey={currentPlatform.id}
  318. projectId={currentProject.id}
  319. projectSlug={currentProject.slug}
  320. configType={getConfig()}
  321. projectKeyId={projectKeyId}
  322. />
  323. </Fragment>
  324. );
  325. }
  326. const Header = styled('div')`
  327. padding: ${space(1)} 0;
  328. `;
  329. const TaskSidebarPanel = styled(SidebarPanel)`
  330. width: 600px;
  331. max-width: 100%;
  332. `;
  333. const TopRightBackgroundImage = styled('img')`
  334. position: absolute;
  335. top: 0;
  336. right: 0;
  337. width: 60%;
  338. user-select: none;
  339. `;
  340. const TaskList = styled('div')`
  341. display: grid;
  342. grid-auto-flow: row;
  343. grid-template-columns: 100%;
  344. gap: ${space(1)};
  345. margin: 50px ${space(4)} ${space(4)} ${space(4)};
  346. `;
  347. const Heading = styled('div')`
  348. display: flex;
  349. color: ${p => p.theme.activeText};
  350. font-size: ${p => p.theme.fontSizeExtraSmall};
  351. text-transform: uppercase;
  352. font-weight: ${p => p.theme.fontWeightBold};
  353. line-height: 1;
  354. margin-top: ${space(3)};
  355. `;
  356. const StyledIdBadge = styled(IdBadge)`
  357. overflow: hidden;
  358. white-space: nowrap;
  359. flex-shrink: 1;
  360. `;
  361. const HeaderActions = styled('div')`
  362. display: flex;
  363. flex-direction: row;
  364. justify-content: space-between;
  365. gap: ${space(3)};
  366. `;
  367. const PlatformLabel = styled('div')`
  368. display: flex;
  369. gap: ${space(1)};
  370. align-items: center;
  371. `;
  372. const PlatformSelect = styled('div')`
  373. display: flex;
  374. gap: ${space(1)};
  375. align-items: center;
  376. flex-wrap: wrap;
  377. `;
  378. const StyledRadioGroup = styled(RadioGroup)`
  379. padding: ${space(1)} 0;
  380. `;
  381. export default FeedbackOnboardingSidebar;