widgetContainer.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. import {useEffect, useState} from 'react';
  2. import styled from '@emotion/styled';
  3. import omit from 'lodash/omit';
  4. import pick from 'lodash/pick';
  5. import * as qs from 'query-string';
  6. import type {SelectOption} from 'sentry/components/compactSelect';
  7. import {CompactSelect} from 'sentry/components/compactSelect';
  8. import {CompositeSelect} from 'sentry/components/compactSelect/composite';
  9. import DropdownButton from 'sentry/components/dropdownButton';
  10. import {IconEllipsis} from 'sentry/icons/iconEllipsis';
  11. import {t} from 'sentry/locale';
  12. import {space} from 'sentry/styles/space';
  13. import type {Organization} from 'sentry/types/organization';
  14. import {trackAnalytics} from 'sentry/utils/analytics';
  15. import type EventView from 'sentry/utils/discover/eventView';
  16. import type {Field} from 'sentry/utils/discover/fields';
  17. import {DisplayModes, SavedQueryDatasets} from 'sentry/utils/discover/types';
  18. import {useMEPSettingContext} from 'sentry/utils/performance/contexts/metricsEnhancedSetting';
  19. import {usePerformanceDisplayType} from 'sentry/utils/performance/contexts/performanceDisplayContext';
  20. import {useNavigate} from 'sentry/utils/useNavigate';
  21. import useOrganization from 'sentry/utils/useOrganization';
  22. import withOrganization from 'sentry/utils/withOrganization';
  23. import {hasDatasetSelector} from 'sentry/views/dashboards/utils';
  24. import MobileReleaseComparisonListWidget from 'sentry/views/performance/landing/widgets/widgets/mobileReleaseComparisonListWidget';
  25. import {PerformanceScoreListWidget} from 'sentry/views/performance/landing/widgets/widgets/performanceScoreListWidget';
  26. import {GenericPerformanceWidgetDataType} from '../types';
  27. import {_setChartSetting, filterAllowedChartsMetrics, getChartSetting} from '../utils';
  28. import type {ChartDefinition, PerformanceWidgetSetting} from '../widgetDefinitions';
  29. import {WIDGET_DEFINITIONS} from '../widgetDefinitions';
  30. import {HistogramWidget} from '../widgets/histogramWidget';
  31. import {LineChartListWidget} from '../widgets/lineChartListWidget';
  32. import {PerformanceScoreWidget} from '../widgets/performanceScoreWidget';
  33. import {SingleFieldAreaWidget} from '../widgets/singleFieldAreaWidget';
  34. import {StackedAreaChartListWidget} from '../widgets/stackedAreaChartListWidget';
  35. import {TrendsWidget} from '../widgets/trendsWidget';
  36. import {VitalWidget} from '../widgets/vitalWidget';
  37. import type {ChartRowProps} from './widgetChartRow';
  38. interface Props extends ChartRowProps {
  39. allowedCharts: PerformanceWidgetSetting[];
  40. chartHeight: number;
  41. defaultChartSetting: PerformanceWidgetSetting;
  42. eventView: EventView;
  43. index: number;
  44. organization: Organization;
  45. rowChartSettings: PerformanceWidgetSetting[];
  46. setRowChartSettings: (settings: PerformanceWidgetSetting[]) => void;
  47. withStaticFilters: boolean;
  48. chartColor?: string;
  49. forceDefaultChartSetting?: boolean;
  50. }
  51. function trackChartSettingChange(
  52. previousChartSetting: PerformanceWidgetSetting,
  53. chartSetting: PerformanceWidgetSetting,
  54. fromDefault: boolean,
  55. organization: Organization
  56. ) {
  57. trackAnalytics('performance_views.landingv3.widget.switch', {
  58. organization,
  59. from_widget: previousChartSetting,
  60. to_widget: chartSetting,
  61. from_default: fromDefault,
  62. is_new_menu: organization.features.includes('performance-new-widget-designs'),
  63. });
  64. }
  65. function WidgetContainerInner(props: Props) {
  66. const {
  67. organization,
  68. index,
  69. chartHeight,
  70. rowChartSettings,
  71. setRowChartSettings,
  72. ...rest
  73. } = props;
  74. const performanceType = usePerformanceDisplayType();
  75. let _chartSetting = getChartSetting(
  76. index,
  77. chartHeight,
  78. performanceType,
  79. rest.defaultChartSetting,
  80. rest.forceDefaultChartSetting
  81. );
  82. const mepSetting = useMEPSettingContext();
  83. const allowedCharts = filterAllowedChartsMetrics(
  84. props.organization,
  85. props.allowedCharts,
  86. mepSetting
  87. );
  88. if (!allowedCharts.includes(_chartSetting)) {
  89. _chartSetting = rest.defaultChartSetting;
  90. }
  91. const [chartSetting, setChartSettingState] = useState(_chartSetting);
  92. const setChartSetting = (setting: PerformanceWidgetSetting) => {
  93. if (!props.forceDefaultChartSetting) {
  94. _setChartSetting(index, chartHeight, performanceType, setting);
  95. }
  96. setChartSettingState(setting);
  97. const newSettings = [...rowChartSettings];
  98. newSettings[index] = setting;
  99. setRowChartSettings(newSettings);
  100. trackChartSettingChange(
  101. chartSetting,
  102. setting,
  103. rest.defaultChartSetting === chartSetting,
  104. organization
  105. );
  106. };
  107. useEffect(() => {
  108. setChartSettingState(_chartSetting);
  109. }, [rest.defaultChartSetting, _chartSetting]);
  110. const chartDefinition = WIDGET_DEFINITIONS({organization})[chartSetting];
  111. // Construct an EventView that matches this widget's definition. The
  112. // `eventView` from the props is the _landing page_ EventView, which is different
  113. const widgetEventView = makeEventViewForWidget(props.eventView, chartDefinition);
  114. const showNewWidgetDesign = organization.features.includes(
  115. 'performance-new-widget-designs'
  116. );
  117. const widgetProps = {
  118. ...chartDefinition,
  119. chartSetting,
  120. chartDefinition,
  121. InteractiveTitle:
  122. showNewWidgetDesign && allowedCharts.length > 2
  123. ? (containerProps: any) => (
  124. <WidgetInteractiveTitle
  125. {...containerProps}
  126. eventView={widgetEventView}
  127. allowedCharts={allowedCharts}
  128. chartSetting={chartSetting}
  129. setChartSetting={setChartSetting}
  130. rowChartSettings={rowChartSettings}
  131. />
  132. )
  133. : null,
  134. ContainerActions: !showNewWidgetDesign
  135. ? (containerProps: any) => (
  136. <WidgetContainerActions
  137. {...containerProps}
  138. eventView={widgetEventView}
  139. allowedCharts={allowedCharts}
  140. chartSetting={chartSetting}
  141. setChartSetting={setChartSetting}
  142. rowChartSettings={rowChartSettings}
  143. />
  144. )
  145. : null,
  146. };
  147. const passedProps = pick(props, [
  148. 'eventView',
  149. 'location',
  150. 'organization',
  151. 'chartHeight',
  152. 'withStaticFilters',
  153. ]);
  154. const titleTooltip = showNewWidgetDesign ? '' : widgetProps.titleTooltip;
  155. switch (widgetProps.dataType) {
  156. case GenericPerformanceWidgetDataType.TRENDS:
  157. return (
  158. <TrendsWidget {...passedProps} {...widgetProps} titleTooltip={titleTooltip} />
  159. );
  160. case GenericPerformanceWidgetDataType.AREA:
  161. return (
  162. <SingleFieldAreaWidget
  163. {...passedProps}
  164. {...widgetProps}
  165. titleTooltip={titleTooltip}
  166. />
  167. );
  168. case GenericPerformanceWidgetDataType.VITALS:
  169. return (
  170. <VitalWidget {...passedProps} {...widgetProps} titleTooltip={titleTooltip} />
  171. );
  172. case GenericPerformanceWidgetDataType.LINE_LIST:
  173. return (
  174. <LineChartListWidget
  175. {...passedProps}
  176. {...widgetProps}
  177. titleTooltip={titleTooltip}
  178. />
  179. );
  180. case GenericPerformanceWidgetDataType.HISTOGRAM:
  181. return (
  182. <HistogramWidget {...passedProps} {...widgetProps} titleTooltip={titleTooltip} />
  183. );
  184. case GenericPerformanceWidgetDataType.STACKED_AREA:
  185. return <StackedAreaChartListWidget {...passedProps} {...widgetProps} />;
  186. case GenericPerformanceWidgetDataType.PERFORMANCE_SCORE_LIST:
  187. return <PerformanceScoreListWidget {...passedProps} {...widgetProps} />;
  188. case GenericPerformanceWidgetDataType.PERFORMANCE_SCORE:
  189. return <PerformanceScoreWidget {...passedProps} {...widgetProps} />;
  190. case GenericPerformanceWidgetDataType.SLOW_SCREENS_BY_TTID:
  191. case GenericPerformanceWidgetDataType.SLOW_SCREENS_BY_COLD_START:
  192. case GenericPerformanceWidgetDataType.SLOW_SCREENS_BY_WARM_START:
  193. return <MobileReleaseComparisonListWidget {...passedProps} {...widgetProps} />;
  194. default:
  195. throw new Error(`Widget type "${widgetProps.dataType}" has no implementation.`);
  196. }
  197. }
  198. export function WidgetInteractiveTitle({
  199. chartSetting,
  200. eventView,
  201. setChartSetting,
  202. allowedCharts,
  203. rowChartSettings,
  204. }: {
  205. allowedCharts: PerformanceWidgetSetting[];
  206. chartSetting: PerformanceWidgetSetting;
  207. eventView: EventView;
  208. rowChartSettings: PerformanceWidgetSetting[];
  209. setChartSetting: (setting: PerformanceWidgetSetting) => void;
  210. }) {
  211. const navigate = useNavigate();
  212. const organization = useOrganization();
  213. const menuOptions: SelectOption<string>[] = [];
  214. const settingsMap = WIDGET_DEFINITIONS({organization});
  215. for (const setting of allowedCharts) {
  216. const options = settingsMap[setting];
  217. menuOptions.push({
  218. value: setting,
  219. label: options.title,
  220. disabled: setting !== chartSetting && rowChartSettings.includes(setting),
  221. });
  222. }
  223. const chartDefinition = WIDGET_DEFINITIONS({organization})[chartSetting];
  224. if (chartDefinition.allowsOpenInDiscover) {
  225. menuOptions.push({label: t('Open in Discover'), value: 'open_in_discover'});
  226. }
  227. const handleChange = (option: {value: string | number}) => {
  228. if (option.value === 'open_in_discover') {
  229. navigate(getEventViewDiscoverPath(organization, eventView));
  230. } else {
  231. setChartSetting(option.value as PerformanceWidgetSetting);
  232. }
  233. };
  234. return (
  235. <StyledCompactSelect
  236. options={menuOptions}
  237. value={chartSetting}
  238. onChange={handleChange}
  239. triggerProps={{borderless: true, size: 'zero'}}
  240. offset={4}
  241. />
  242. );
  243. }
  244. const StyledCompactSelect = styled(CompactSelect)`
  245. /* Reset font-weight set by HeaderTitleLegend, buttons are already bold and
  246. * setting this higher up causes it to trickle into the menues */
  247. font-weight: ${p => p.theme.fontWeightNormal};
  248. margin: -${space(0.5)} -${space(1)} -${space(0.25)};
  249. min-width: 0;
  250. button {
  251. padding: ${space(0.5)} ${space(1)};
  252. font-size: ${p => p.theme.fontSizeLarge};
  253. }
  254. `;
  255. export function WidgetContainerActions({
  256. chartSetting,
  257. eventView,
  258. setChartSetting,
  259. allowedCharts,
  260. rowChartSettings,
  261. }: {
  262. allowedCharts: PerformanceWidgetSetting[];
  263. chartSetting: PerformanceWidgetSetting;
  264. eventView: EventView;
  265. rowChartSettings: PerformanceWidgetSetting[];
  266. setChartSetting: (setting: PerformanceWidgetSetting) => void;
  267. }) {
  268. const navigate = useNavigate();
  269. const organization = useOrganization();
  270. const menuOptions: SelectOption<PerformanceWidgetSetting>[] = [];
  271. const settingsMap = WIDGET_DEFINITIONS({organization});
  272. for (const setting of allowedCharts) {
  273. const options = settingsMap[setting];
  274. menuOptions.push({
  275. value: setting,
  276. label: options.title,
  277. disabled: setting !== chartSetting && rowChartSettings.includes(setting),
  278. });
  279. }
  280. const chartDefinition = WIDGET_DEFINITIONS({organization})[chartSetting];
  281. function handleWidgetActionChange(value: string) {
  282. if (value === 'open_in_discover') {
  283. navigate(getEventViewDiscoverPath(organization, eventView));
  284. }
  285. }
  286. return (
  287. <CompositeSelect
  288. trigger={triggerProps => (
  289. <DropdownButton
  290. {...triggerProps}
  291. size="xs"
  292. borderless
  293. showChevron={false}
  294. icon={<IconEllipsis aria-label={t('More')} />}
  295. />
  296. )}
  297. position="bottom-end"
  298. >
  299. <CompositeSelect.Region
  300. label={t('Display')}
  301. options={menuOptions}
  302. value={chartSetting}
  303. onChange={opt => setChartSetting(opt.value)}
  304. />
  305. {chartDefinition.allowsOpenInDiscover && (
  306. <CompositeSelect.Region
  307. label={t('Other')}
  308. options={[{label: t('Open in Discover'), value: 'open_in_discover'}]}
  309. value=""
  310. onChange={opt => handleWidgetActionChange(opt.value)}
  311. />
  312. )}
  313. </CompositeSelect>
  314. );
  315. }
  316. const getEventViewDiscoverPath = (
  317. organization: Organization,
  318. eventView: EventView
  319. ): string => {
  320. const discoverUrlTarget = eventView.getResultsViewUrlTarget(
  321. organization.slug,
  322. false,
  323. hasDatasetSelector(organization) ? SavedQueryDatasets.TRANSACTIONS : undefined
  324. );
  325. // The landing page EventView has some additional conditions, but
  326. // `EventView#getResultsViewUrlTarget` omits those! Get them manually
  327. discoverUrlTarget.query.query = eventView.getQueryWithAdditionalConditions();
  328. return `${discoverUrlTarget.pathname}?${qs.stringify(
  329. omit(discoverUrlTarget.query, ['widths']) // Column widths are not useful in this case
  330. )}`;
  331. };
  332. /**
  333. * Constructs an `EventView` that matches a widget's chart definition.
  334. * @param baseEventView Any valid event view. The easiest way to make a new EventView is to clone an existing one, because `EventView#constructor` takes too many abstract arguments
  335. * @param chartDefinition
  336. */
  337. const makeEventViewForWidget = (
  338. baseEventView: EventView,
  339. chartDefinition: ChartDefinition
  340. ): EventView => {
  341. const widgetEventView = baseEventView.clone();
  342. widgetEventView.name = chartDefinition.title;
  343. widgetEventView.yAxis = chartDefinition.fields[0]; // All current widgets only have one field
  344. widgetEventView.display = DisplayModes.PREVIOUS;
  345. widgetEventView.fields = ['transaction', 'project', ...chartDefinition.fields].map(
  346. fieldName => ({field: fieldName}) as Field
  347. );
  348. return widgetEventView;
  349. };
  350. const WidgetContainer = withOrganization(WidgetContainerInner);
  351. export default WidgetContainer;