context.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 {
  12. getAbsoluteDateTimeRange,
  13. getDefaultMetricDisplayType,
  14. useInstantRef,
  15. useUpdateQuery,
  16. } from 'sentry/utils/metrics';
  17. import {DEFAULT_SORT_STATE, emptyWidget} from 'sentry/utils/metrics/constants';
  18. import {MetricWidgetQueryParams} from 'sentry/utils/metrics/types';
  19. import {useMetricsMeta} from 'sentry/utils/metrics/useMetricsMeta';
  20. import {decodeList} from 'sentry/utils/queryString';
  21. import {useLocalStorageState} from 'sentry/utils/useLocalStorageState';
  22. import usePageFilters from 'sentry/utils/usePageFilters';
  23. import useRouter from 'sentry/utils/useRouter';
  24. import {FocusAreaSelection} from 'sentry/views/ddm/focusArea';
  25. import {useStructuralSharing} from 'sentry/views/ddm/useStructuralSharing';
  26. export type FocusAreaProps = {
  27. onAdd?: (area: FocusAreaSelection) => void;
  28. onDraw?: () => void;
  29. onRemove?: () => void;
  30. selection?: FocusAreaSelection;
  31. };
  32. interface DDMContextValue {
  33. addWidget: () => void;
  34. duplicateWidget: (index: number) => void;
  35. isDefaultQuery: boolean;
  36. isLoading: boolean;
  37. metricsMeta: ReturnType<typeof useMetricsMeta>['data'];
  38. removeWidget: (index: number) => void;
  39. selectedWidgetIndex: number;
  40. setDefaultQuery: (query: Record<string, any> | null) => void;
  41. setHighlightedSampleId: (sample?: string) => void;
  42. setSelectedWidgetIndex: (index: number) => void;
  43. showQuerySymbols: boolean;
  44. updateWidget: (index: number, data: Partial<MetricWidgetQueryParams>) => void;
  45. widgets: MetricWidgetQueryParams[];
  46. focusArea?: FocusAreaProps;
  47. highlightedSampleId?: string;
  48. }
  49. export const DDMContext = createContext<DDMContextValue>({
  50. addWidget: () => {},
  51. duplicateWidget: () => {},
  52. isDefaultQuery: false,
  53. isLoading: false,
  54. metricsMeta: [],
  55. removeWidget: () => {},
  56. selectedWidgetIndex: 0,
  57. setDefaultQuery: () => {},
  58. setSelectedWidgetIndex: () => {},
  59. showQuerySymbols: false,
  60. updateWidget: () => {},
  61. widgets: [],
  62. highlightedSampleId: undefined,
  63. setHighlightedSampleId: () => {},
  64. focusArea: undefined,
  65. });
  66. export function useDDMContext() {
  67. return useContext(DDMContext);
  68. }
  69. export function useMetricWidgets() {
  70. const router = useRouter();
  71. const updateQuery = useUpdateQuery();
  72. const widgets = useStructuralSharing(
  73. useMemo<MetricWidgetQueryParams[]>(() => {
  74. const currentWidgets = JSON.parse(
  75. router.location.query.widgets ?? JSON.stringify([emptyWidget])
  76. );
  77. return currentWidgets.map((widget: MetricWidgetQueryParams) => {
  78. return {
  79. mri: widget.mri,
  80. op: widget.op,
  81. query: widget.query,
  82. groupBy: decodeList(widget.groupBy),
  83. displayType:
  84. widget.displayType ?? getDefaultMetricDisplayType(widget.mri, widget.op),
  85. focusedSeries: widget.focusedSeries,
  86. showSummaryTable: widget.showSummaryTable ?? true, // temporary default
  87. powerUserMode: widget.powerUserMode,
  88. sort: widget.sort ?? DEFAULT_SORT_STATE,
  89. title: widget.title,
  90. };
  91. });
  92. }, [router.location.query.widgets])
  93. );
  94. // We want to have it as a ref, so that we can use it in the setWidget callback
  95. // without needing to generate a new callback every time the location changes
  96. const currentWidgetsRef = useInstantRef(widgets);
  97. const setWidgets = useCallback(
  98. (newWidgets: React.SetStateAction<MetricWidgetQueryParams[]>) => {
  99. const currentWidgets = currentWidgetsRef.current;
  100. updateQuery({
  101. widgets: JSON.stringify(
  102. typeof newWidgets === 'function' ? newWidgets(currentWidgets) : newWidgets
  103. ),
  104. });
  105. },
  106. [updateQuery, currentWidgetsRef]
  107. );
  108. const updateWidget = useCallback(
  109. (index: number, data: Partial<MetricWidgetQueryParams>) => {
  110. setWidgets(currentWidgets => {
  111. const newWidgets = [...currentWidgets];
  112. newWidgets[index] = {...currentWidgets[index], ...data};
  113. return newWidgets;
  114. });
  115. },
  116. [setWidgets]
  117. );
  118. const addWidget = useCallback(() => {
  119. setWidgets(currentWidgets => {
  120. const lastWidget = currentWidgets.length
  121. ? currentWidgets[currentWidgets.length - 1]
  122. : {};
  123. const newWidget = {
  124. ...emptyWidget,
  125. ...lastWidget,
  126. };
  127. return [...currentWidgets, newWidget];
  128. });
  129. }, [setWidgets]);
  130. const removeWidget = useCallback(
  131. (index: number) => {
  132. setWidgets(currentWidgets => {
  133. const newWidgets = [...currentWidgets];
  134. newWidgets.splice(index, 1);
  135. return newWidgets;
  136. });
  137. },
  138. [setWidgets]
  139. );
  140. const duplicateWidget = useCallback(
  141. (index: number) => {
  142. setWidgets(currentWidgets => {
  143. const newWidgets = [...currentWidgets];
  144. newWidgets.splice(index, 0, currentWidgets[index]);
  145. return newWidgets;
  146. });
  147. },
  148. [setWidgets]
  149. );
  150. return {
  151. widgets,
  152. updateWidget,
  153. addWidget,
  154. removeWidget,
  155. duplicateWidget,
  156. };
  157. }
  158. const useDefaultQuery = () => {
  159. const router = useRouter();
  160. const [defaultQuery, setDefaultQuery] = useLocalStorageState<Record<
  161. string,
  162. any
  163. > | null>('ddm:default-query', null);
  164. useEffect(() => {
  165. if (defaultQuery && router.location.query.widgets === undefined) {
  166. router.replace({...router.location, query: defaultQuery});
  167. }
  168. // Only call on page load
  169. // eslint-disable-next-line react-hooks/exhaustive-deps
  170. }, []);
  171. return useMemo(
  172. () => ({
  173. defaultQuery,
  174. setDefaultQuery,
  175. isDefaultQuery: !!defaultQuery && isEqual(defaultQuery, router.location.query),
  176. }),
  177. [defaultQuery, router.location.query, setDefaultQuery]
  178. );
  179. };
  180. export function DDMContextProvider({children}: {children: React.ReactNode}) {
  181. const router = useRouter();
  182. const updateQuery = useUpdateQuery();
  183. const {setDefaultQuery, isDefaultQuery} = useDefaultQuery();
  184. const [selectedWidgetIndex, setSelectedWidgetIndex] = useState(0);
  185. const {widgets, updateWidget, addWidget, removeWidget, duplicateWidget} =
  186. useMetricWidgets();
  187. const [focusAreaSelection, setFocusAreaSelection] = useState<
  188. FocusAreaSelection | undefined
  189. >();
  190. const [highlightedSampleId, setHighlightedSampleId] = useState<string | undefined>();
  191. const pageFilters = usePageFilters().selection;
  192. const {data: metricsMeta, isLoading} = useMetricsMeta(pageFilters.projects);
  193. const handleAddFocusArea = useCallback(
  194. (area: FocusAreaSelection) => {
  195. if (!area.range.start || !area.range.end) {
  196. Sentry.metrics.increment('ddm.enhance.range-undefined');
  197. return;
  198. }
  199. const dateRange = getAbsoluteDateTimeRange(pageFilters.datetime);
  200. if (area.range.start < dateRange.start) {
  201. area.range.start = dateRange.start;
  202. }
  203. if (area.range.end > dateRange.end) {
  204. area.range.end = dateRange.end;
  205. }
  206. Sentry.metrics.increment('ddm.enhance.add');
  207. setFocusAreaSelection(area);
  208. setSelectedWidgetIndex(area.widgetIndex);
  209. updateQuery({focusArea: JSON.stringify(area)});
  210. },
  211. [updateQuery, pageFilters.datetime]
  212. );
  213. const handleRemoveFocusArea = useCallback(() => {
  214. Sentry.metrics.increment('ddm.enhance.remove');
  215. setFocusAreaSelection(undefined);
  216. updateQuery({focusArea: undefined});
  217. }, [updateQuery]);
  218. // Load focus area from URL
  219. useEffect(() => {
  220. if (focusAreaSelection) {
  221. return;
  222. }
  223. const urlFocusArea = router.location.query.focusArea;
  224. if (urlFocusArea) {
  225. handleAddFocusArea(JSON.parse(urlFocusArea));
  226. }
  227. }, [router, handleAddFocusArea, focusAreaSelection]);
  228. const focusArea = useMemo<FocusAreaProps>(() => {
  229. return {
  230. selection: focusAreaSelection,
  231. onAdd: handleAddFocusArea,
  232. onRemove: handleRemoveFocusArea,
  233. };
  234. }, [focusAreaSelection, handleAddFocusArea, handleRemoveFocusArea]);
  235. const handleAddWidget = useCallback(() => {
  236. addWidget();
  237. setSelectedWidgetIndex(widgets.length);
  238. }, [addWidget, widgets.length]);
  239. const handleUpdateWidget = useCallback(
  240. (index: number, data: Partial<MetricWidgetQueryParams>) => {
  241. updateWidget(index, data);
  242. setSelectedWidgetIndex(index);
  243. if (index === focusAreaSelection?.widgetIndex) {
  244. handleRemoveFocusArea();
  245. }
  246. },
  247. [updateWidget, handleRemoveFocusArea, focusAreaSelection?.widgetIndex]
  248. );
  249. const handleDuplicate = useCallback(
  250. (index: number) => {
  251. duplicateWidget(index);
  252. setSelectedWidgetIndex(index + 1);
  253. },
  254. [duplicateWidget]
  255. );
  256. const contextValue = useMemo<DDMContextValue>(
  257. () => ({
  258. addWidget: handleAddWidget,
  259. selectedWidgetIndex:
  260. selectedWidgetIndex > widgets.length - 1 ? 0 : selectedWidgetIndex,
  261. setSelectedWidgetIndex,
  262. updateWidget: handleUpdateWidget,
  263. removeWidget,
  264. duplicateWidget: handleDuplicate,
  265. widgets,
  266. isLoading,
  267. metricsMeta,
  268. focusArea,
  269. setDefaultQuery,
  270. isDefaultQuery,
  271. showQuerySymbols: widgets.length > 1,
  272. highlightedSampleId,
  273. setHighlightedSampleId,
  274. }),
  275. [
  276. handleAddWidget,
  277. selectedWidgetIndex,
  278. widgets,
  279. handleUpdateWidget,
  280. removeWidget,
  281. handleDuplicate,
  282. isLoading,
  283. metricsMeta,
  284. focusArea,
  285. setDefaultQuery,
  286. isDefaultQuery,
  287. highlightedSampleId,
  288. setHighlightedSampleId,
  289. ]
  290. );
  291. return <DDMContext.Provider value={contextValue}>{children}</DDMContext.Provider>;
  292. }