context.tsx 12 KB

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