context.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. import {
  2. createContext,
  3. useCallback,
  4. useContext,
  5. useEffect,
  6. useMemo,
  7. useState,
  8. } from 'react';
  9. import * as Sentry from '@sentry/react';
  10. import isEqual from 'lodash/isEqual';
  11. import type {Field} from 'sentry/components/metrics/metricSamplesTable';
  12. import type {MRI} from 'sentry/types';
  13. import {useInstantRef, useUpdateQuery} from 'sentry/utils/metrics';
  14. import {
  15. emptyMetricsFormulaWidget,
  16. emptyMetricsQueryWidget,
  17. NO_QUERY_ID,
  18. } from 'sentry/utils/metrics/constants';
  19. import {MetricExpressionType, type MetricsWidget} from 'sentry/utils/metrics/types';
  20. import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  21. import type {MetricsSamplesResults} from 'sentry/utils/metrics/useMetricsSamples';
  22. import {decodeInteger, decodeScalar} from 'sentry/utils/queryString';
  23. import useLocationQuery from 'sentry/utils/url/useLocationQuery';
  24. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  25. import usePageFilters from 'sentry/utils/usePageFilters';
  26. import useRouter from 'sentry/utils/useRouter';
  27. import type {FocusAreaSelection} from 'sentry/views/metrics/chart/types';
  28. import {parseMetricWidgetsQueryParam} from 'sentry/views/metrics/utils/parseMetricWidgetsQueryParam';
  29. import {useStructuralSharing} from 'sentry/views/metrics/utils/useStructuralSharing';
  30. export type FocusAreaProps = {
  31. onAdd?: (area: FocusAreaSelection) => void;
  32. onDraw?: () => void;
  33. onRemove?: () => void;
  34. selection?: FocusAreaSelection;
  35. };
  36. interface MetricsContextValue {
  37. addWidget: (type?: MetricExpressionType) => void;
  38. duplicateWidget: (index: number) => void;
  39. focusArea: FocusAreaProps;
  40. hasCustomMetrics: boolean;
  41. hasPerformanceMetrics: boolean;
  42. isDefaultQuery: boolean;
  43. isHasMetricsLoading: boolean;
  44. isMultiChartMode: boolean;
  45. removeWidget: (index: number) => void;
  46. selectedWidgetIndex: number;
  47. setDefaultQuery: (query: Record<string, any> | null) => void;
  48. setHighlightedSampleId: (sample?: string) => void;
  49. setIsMultiChartMode: (value: boolean) => void;
  50. setMetricsSamples: React.Dispatch<
  51. React.SetStateAction<MetricsSamplesResults<Field>['data'] | undefined>
  52. >;
  53. setSelectedWidgetIndex: (index: number) => void;
  54. showQuerySymbols: boolean;
  55. toggleWidgetVisibility: (index: number) => void;
  56. updateWidget: (index: number, data: Partial<Omit<MetricsWidget, 'type'>>) => void;
  57. widgets: MetricsWidget[];
  58. highlightedSampleId?: string;
  59. metricsSamples?: MetricsSamplesResults<Field>['data'];
  60. }
  61. export const MetricsContext = createContext<MetricsContextValue>({
  62. addWidget: () => {},
  63. duplicateWidget: () => {},
  64. focusArea: {},
  65. hasCustomMetrics: false,
  66. hasPerformanceMetrics: false,
  67. highlightedSampleId: undefined,
  68. isDefaultQuery: false,
  69. isMultiChartMode: false,
  70. isHasMetricsLoading: true,
  71. metricsSamples: [],
  72. removeWidget: () => {},
  73. selectedWidgetIndex: 0,
  74. setDefaultQuery: () => {},
  75. setHighlightedSampleId: () => {},
  76. setIsMultiChartMode: () => {},
  77. setMetricsSamples: () => {},
  78. setSelectedWidgetIndex: () => {},
  79. showQuerySymbols: false,
  80. updateWidget: () => {},
  81. widgets: [],
  82. toggleWidgetVisibility: () => {},
  83. });
  84. export function useMetricsContext() {
  85. return useContext(MetricsContext);
  86. }
  87. export function useMetricWidgets(mri: MRI) {
  88. const {widgets: urlWidgets} = useLocationQuery({fields: {widgets: decodeScalar}});
  89. const updateQuery = useUpdateQuery();
  90. const widgets = useStructuralSharing(
  91. useMemo<MetricsWidget[]>(
  92. () => parseMetricWidgetsQueryParam(urlWidgets, mri),
  93. [urlWidgets, mri]
  94. )
  95. );
  96. // We want to have it as a ref, so that we can use it in the setWidget callback
  97. // without needing to generate a new callback every time the location changes
  98. const currentWidgetsRef = useInstantRef(widgets);
  99. const setWidgets = useCallback(
  100. (newWidgets: React.SetStateAction<MetricsWidget[]>) => {
  101. const currentWidgets = currentWidgetsRef.current;
  102. const newData =
  103. typeof newWidgets === 'function' ? newWidgets(currentWidgets) : newWidgets;
  104. updateQuery({widgets: JSON.stringify(newData)});
  105. // We need to update the ref so that the next call to setWidgets in the same render cycle will have the updated value
  106. currentWidgetsRef.current = newData;
  107. },
  108. [updateQuery, currentWidgetsRef]
  109. );
  110. const updateWidget = useCallback(
  111. (index: number, data: Partial<Omit<MetricsWidget, 'type'>>) => {
  112. setWidgets(currentWidgets => {
  113. const newWidgets = [...currentWidgets];
  114. newWidgets[index] = {
  115. ...currentWidgets[index],
  116. ...data,
  117. };
  118. return newWidgets;
  119. });
  120. },
  121. [setWidgets]
  122. );
  123. const duplicateWidget = useCallback(
  124. (index: number) => {
  125. setWidgets(currentWidgets => {
  126. const newWidgets = [...currentWidgets];
  127. const newWidget = {...currentWidgets[index]};
  128. newWidget.id = NO_QUERY_ID;
  129. newWidgets.splice(index + 1, 0, newWidget);
  130. return newWidgets;
  131. });
  132. },
  133. [setWidgets]
  134. );
  135. const addWidget = useCallback(
  136. (type: MetricExpressionType = MetricExpressionType.QUERY) => {
  137. const lastIndexOfSameType = currentWidgetsRef.current.findLastIndex(
  138. w => w.type === type
  139. );
  140. if (lastIndexOfSameType > -1) {
  141. duplicateWidget(lastIndexOfSameType);
  142. } else {
  143. setWidgets(currentWidgets => [
  144. ...currentWidgets,
  145. type === MetricExpressionType.QUERY
  146. ? emptyMetricsQueryWidget
  147. : emptyMetricsFormulaWidget,
  148. ]);
  149. }
  150. },
  151. [currentWidgetsRef, duplicateWidget, setWidgets]
  152. );
  153. const removeWidget = useCallback(
  154. (index: number) => {
  155. setWidgets(currentWidgets => {
  156. let newWidgets = [...currentWidgets];
  157. newWidgets.splice(index, 1);
  158. // Ensure that a visible widget remains
  159. if (!newWidgets.find(w => !w.isHidden)) {
  160. newWidgets = newWidgets.map(w => ({...w, isHidden: false}));
  161. }
  162. return newWidgets;
  163. });
  164. },
  165. [setWidgets]
  166. );
  167. return {
  168. widgets,
  169. updateWidget,
  170. addWidget,
  171. removeWidget,
  172. duplicateWidget,
  173. setWidgets,
  174. };
  175. }
  176. const useDefaultQuery = () => {
  177. const router = useRouter();
  178. const [defaultQuery, setDefaultQuery] = useLocalStorageState<Record<
  179. string,
  180. any
  181. > | null>('ddm:default-query', null);
  182. useEffect(() => {
  183. if (defaultQuery && router.location.query.widgets === undefined) {
  184. router.replace({...router.location, query: defaultQuery});
  185. }
  186. // Only call on page load
  187. // eslint-disable-next-line react-hooks/exhaustive-deps
  188. }, []);
  189. return useMemo(
  190. () => ({
  191. defaultQuery,
  192. setDefaultQuery,
  193. isDefaultQuery: !!defaultQuery && isEqual(defaultQuery, router.location.query),
  194. }),
  195. [defaultQuery, router.location.query, setDefaultQuery]
  196. );
  197. };
  198. export function MetricsContextProvider({children}: {children: React.ReactNode}) {
  199. const router = useRouter();
  200. const updateQuery = useUpdateQuery();
  201. const {multiChartMode} = useLocationQuery({fields: {multiChartMode: decodeInteger}});
  202. const pageFilters = usePageFilters();
  203. const {data: metaCustom, isLoading: isMetaCustomLoading} = useMetricsMeta(
  204. pageFilters.selection,
  205. ['custom']
  206. );
  207. const {data: metaPerformance, isLoading: isMetaPerformanceLoading} = useMetricsMeta(
  208. pageFilters.selection,
  209. ['transactions', 'spans']
  210. );
  211. const isMultiChartMode = multiChartMode === 1;
  212. const {setDefaultQuery, isDefaultQuery} = useDefaultQuery();
  213. const [selectedWidgetIndex, setSelectedWidgetIndex] = useState(0);
  214. const {widgets, updateWidget, addWidget, removeWidget, duplicateWidget, setWidgets} =
  215. useMetricWidgets(metaCustom[0]?.mri);
  216. const [metricsSamples, setMetricsSamples] = useState<
  217. MetricsSamplesResults<Field>['data'] | undefined
  218. >();
  219. const [highlightedSampleId, setHighlightedSampleId] = useState<string | undefined>();
  220. const hasCustomMetrics = useMemo(() => {
  221. return !!metaCustom.length;
  222. }, [metaCustom]);
  223. const hasPerformanceMetrics = useMemo(() => {
  224. return !!metaPerformance.length;
  225. }, [metaPerformance]);
  226. const handleSetSelectedWidgetIndex = useCallback(
  227. (value: number) => {
  228. if (!isMultiChartMode) {
  229. return;
  230. }
  231. setSelectedWidgetIndex(value);
  232. },
  233. [isMultiChartMode]
  234. );
  235. const focusAreaSelection = useMemo<FocusAreaSelection | undefined>(
  236. () => router.location.query.focusArea && JSON.parse(router.location.query.focusArea),
  237. [router.location.query.focusArea]
  238. );
  239. const handleAddFocusArea = useCallback(
  240. (area: FocusAreaSelection) => {
  241. if (!area.range.start || !area.range.end) {
  242. Sentry.metrics.increment('ddm.enhance.range-undefined');
  243. return;
  244. }
  245. Sentry.metrics.increment('ddm.enhance.add');
  246. handleSetSelectedWidgetIndex(area.widgetIndex);
  247. updateQuery({focusArea: JSON.stringify(area)}, {replace: true});
  248. },
  249. [handleSetSelectedWidgetIndex, updateQuery]
  250. );
  251. const handleRemoveFocusArea = useCallback(() => {
  252. Sentry.metrics.increment('ddm.enhance.remove');
  253. updateQuery({focusArea: undefined}, {replace: true});
  254. }, [updateQuery]);
  255. const focusArea = useMemo<FocusAreaProps>(() => {
  256. return {
  257. selection: focusAreaSelection,
  258. onAdd: handleAddFocusArea,
  259. onRemove: handleRemoveFocusArea,
  260. };
  261. }, [focusAreaSelection, handleAddFocusArea, handleRemoveFocusArea]);
  262. const handleAddWidget = useCallback(
  263. (type?: MetricExpressionType) => {
  264. addWidget(type);
  265. handleSetSelectedWidgetIndex(widgets.length);
  266. },
  267. [addWidget, handleSetSelectedWidgetIndex, widgets.length]
  268. );
  269. const handleUpdateWidget = useCallback(
  270. (index: number, data: Partial<MetricsWidget>) => {
  271. updateWidget(index, data);
  272. handleSetSelectedWidgetIndex(index);
  273. if (index === focusAreaSelection?.widgetIndex) {
  274. handleRemoveFocusArea();
  275. }
  276. },
  277. [
  278. updateWidget,
  279. handleSetSelectedWidgetIndex,
  280. focusAreaSelection?.widgetIndex,
  281. handleRemoveFocusArea,
  282. ]
  283. );
  284. const handleDuplicate = useCallback(
  285. (index: number) => {
  286. duplicateWidget(index);
  287. handleSetSelectedWidgetIndex(index + 1);
  288. },
  289. [duplicateWidget, handleSetSelectedWidgetIndex]
  290. );
  291. const handleSetIsMultiChartMode = useCallback(
  292. (value: boolean) => {
  293. updateQuery({multiChartMode: value ? 1 : 0}, {replace: true});
  294. updateWidget(0, {focusedSeries: undefined});
  295. const firstVisibleWidgetIndex = widgets.findIndex(w => !w.isHidden);
  296. setSelectedWidgetIndex(firstVisibleWidgetIndex);
  297. },
  298. [updateQuery, updateWidget, widgets]
  299. );
  300. const toggleWidgetVisibility = useCallback(
  301. (index: number) => {
  302. if (index === selectedWidgetIndex) {
  303. const firstVisibleWidgetIndex = widgets.findIndex(w => !w.isHidden);
  304. setSelectedWidgetIndex(firstVisibleWidgetIndex);
  305. }
  306. if (!isMultiChartMode) {
  307. // Reset the focused series when hiding a widget
  308. setWidgets(currentWidgets => {
  309. return currentWidgets.map(w => ({...w, focusedSeries: undefined}));
  310. });
  311. }
  312. updateWidget(index, {isHidden: !widgets[index].isHidden});
  313. },
  314. [isMultiChartMode, selectedWidgetIndex, setWidgets, updateWidget, widgets]
  315. );
  316. const selectedWidget = widgets[selectedWidgetIndex];
  317. const isSelectionValid = selectedWidget && !selectedWidget.isHidden;
  318. const contextValue = useMemo<MetricsContextValue>(
  319. () => ({
  320. addWidget: handleAddWidget,
  321. selectedWidgetIndex: isSelectionValid
  322. ? selectedWidgetIndex
  323. : widgets.findIndex(w => !w.isHidden),
  324. setSelectedWidgetIndex: handleSetSelectedWidgetIndex,
  325. updateWidget: handleUpdateWidget,
  326. removeWidget,
  327. duplicateWidget: handleDuplicate,
  328. widgets,
  329. hasCustomMetrics,
  330. hasPerformanceMetrics,
  331. isHasMetricsLoading: isMetaCustomLoading || isMetaPerformanceLoading,
  332. focusArea,
  333. setDefaultQuery,
  334. isDefaultQuery,
  335. showQuerySymbols: widgets.length > 1,
  336. highlightedSampleId,
  337. setHighlightedSampleId,
  338. isMultiChartMode: isMultiChartMode,
  339. setIsMultiChartMode: handleSetIsMultiChartMode,
  340. metricsSamples,
  341. setMetricsSamples,
  342. toggleWidgetVisibility,
  343. }),
  344. [
  345. handleAddWidget,
  346. isSelectionValid,
  347. selectedWidgetIndex,
  348. widgets,
  349. handleSetSelectedWidgetIndex,
  350. handleUpdateWidget,
  351. removeWidget,
  352. handleDuplicate,
  353. hasCustomMetrics,
  354. hasPerformanceMetrics,
  355. focusArea,
  356. setDefaultQuery,
  357. isDefaultQuery,
  358. highlightedSampleId,
  359. isMultiChartMode,
  360. handleSetIsMultiChartMode,
  361. metricsSamples,
  362. toggleWidgetVisibility,
  363. isMetaCustomLoading,
  364. isMetaPerformanceLoading,
  365. ]
  366. );
  367. return (
  368. <MetricsContext.Provider value={contextValue}>{children}</MetricsContext.Provider>
  369. );
  370. }