widgetLegendSelectionState.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import type {Location} from 'history';
  2. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  3. import type {Organization} from 'sentry/types/organization';
  4. import {decodeList} from 'sentry/utils/queryString';
  5. import WidgetLegendNameEncoderDecoder from 'sentry/views/dashboards/widgetLegendNameEncoderDecoder';
  6. import {type DashboardDetails, DisplayType, type Widget} from './types';
  7. type Props = {
  8. dashboard: DashboardDetails | null;
  9. location: Location;
  10. organization: Organization;
  11. router: InjectedRouter;
  12. };
  13. type LegendSelection = Record<string, boolean>;
  14. const SERIES_LIST_DELIMITER = ',';
  15. const WIDGET_ID_DELIMITER = ':';
  16. const SERIES_NAME_DELIMITER = ';';
  17. class WidgetLegendSelectionState {
  18. dashboard: DashboardDetails | null;
  19. location: Location;
  20. organization: Organization;
  21. router: InjectedRouter;
  22. constructor(props: Props) {
  23. this.dashboard = props.dashboard;
  24. this.location = props.location;
  25. this.organization = props.organization;
  26. this.router = props.router;
  27. }
  28. // Updates legend param when a legend selection has been changed
  29. setWidgetSelectionState(selected: LegendSelection, widget: Widget) {
  30. const [dashboard, location, router] = [this.dashboard, this.location, this.router];
  31. const widgets = dashboard ? dashboard.widgets : [];
  32. let newLegendQuery: string[];
  33. if (!location.query.unselectedSeries && widgets) {
  34. newLegendQuery = widgets
  35. .filter(dashboardWidget => this.widgetIsChart(dashboardWidget))
  36. .map(dashboardWidget => {
  37. return dashboardWidget.id === widget.id
  38. ? this.encodeLegendQueryParam(widget, selected)
  39. : this.formatLegendDefaultQuery(dashboardWidget);
  40. })
  41. .filter(unselectedSeries => unselectedSeries !== undefined);
  42. const thisWidgetWithReleasesWasSelected =
  43. Object.values(selected).filter(value => value === false).length !== 1 &&
  44. Object.keys(selected).includes(`Releases${SERIES_NAME_DELIMITER}${widget.id}`);
  45. const thisWidgetWithoutReleasesWasSelected =
  46. !Object.keys(selected).includes(`Releases${SERIES_NAME_DELIMITER}${widget.id}`) &&
  47. Object.values(selected).filter(value => value === false).length === 1;
  48. if (thisWidgetWithReleasesWasSelected || thisWidgetWithoutReleasesWasSelected) {
  49. router.replace({
  50. query: {
  51. ...location.query,
  52. unselectedSeries: newLegendQuery,
  53. },
  54. });
  55. }
  56. } else if (Array.isArray(location.query.unselectedSeries)) {
  57. let isInQuery = false;
  58. newLegendQuery = location.query.unselectedSeries.map(widgetLegend => {
  59. if (widgetLegend.includes(widget.id!)) {
  60. isInQuery = true;
  61. // names of legend options are stored as seriesName:widgetId and are stored in
  62. // query param as widgetId-seriesName-seriesName2-...
  63. return this.encodeLegendQueryParam(widget, selected);
  64. }
  65. return widgetLegend;
  66. });
  67. const unselectedSeries = isInQuery
  68. ? newLegendQuery
  69. : [
  70. ...location.query.unselectedSeries,
  71. this.encodeLegendQueryParam(widget, selected),
  72. ];
  73. router.replace({
  74. query: {
  75. ...location.query,
  76. unselectedSeries,
  77. },
  78. });
  79. } else {
  80. if (location.query.unselectedSeries?.includes(widget.id!)) {
  81. router.replace({
  82. query: {
  83. ...location.query,
  84. unselectedSeries: [this.encodeLegendQueryParam(widget, selected)],
  85. },
  86. });
  87. } else {
  88. router.replace({
  89. query: {
  90. ...location.query,
  91. unselectedSeries: [
  92. location.query.unselectedSeries,
  93. this.encodeLegendQueryParam(widget, selected),
  94. ],
  95. },
  96. });
  97. }
  98. }
  99. }
  100. // sets unselected legend options by the legend query param
  101. getWidgetSelectionState(widget: Widget): LegendSelection {
  102. const location = this.location;
  103. return location.query.unselectedSeries
  104. ? this.decodeLegendQueryParam(widget)
  105. : this.widgetRequiresLegendUnselection(widget)
  106. ? {
  107. [WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(
  108. 'Releases',
  109. widget.id
  110. )]: false,
  111. }
  112. : {};
  113. }
  114. widgetRequiresLegendUnselection(widget: Widget) {
  115. return (
  116. widget.displayType === DisplayType.AREA || widget.displayType === DisplayType.LINE
  117. );
  118. }
  119. widgetIsChart(widget: Widget) {
  120. return (
  121. widget.displayType === DisplayType.AREA ||
  122. widget.displayType === DisplayType.LINE ||
  123. widget.displayType === DisplayType.BAR ||
  124. widget.displayType === DisplayType.TOP_N
  125. );
  126. }
  127. formatLegendDefaultQuery(widget: Widget) {
  128. return this.widgetRequiresLegendUnselection(widget)
  129. ? `${widget.id}${WIDGET_ID_DELIMITER}Releases`
  130. : undefined;
  131. }
  132. // going from selected to query param
  133. encodeLegendQueryParam(widget: Widget, selected: LegendSelection) {
  134. return (
  135. widget.id +
  136. WIDGET_ID_DELIMITER +
  137. Object.keys(selected)
  138. .filter(key => !selected[key])
  139. .map(series =>
  140. encodeURIComponent(
  141. WidgetLegendNameEncoderDecoder.decodeSeriesNameForLegend(series)!
  142. )
  143. )
  144. .join(SERIES_LIST_DELIMITER)
  145. );
  146. }
  147. // going from query param to selected
  148. decodeLegendQueryParam(widget: Widget) {
  149. const location = this.location;
  150. const widgetLegendString = decodeList(location.query.unselectedSeries).find(
  151. widgetLegend => widgetLegend.includes(widget.id!)
  152. );
  153. if (widgetLegendString) {
  154. const [_, seriesNameString] = widgetLegendString.split(WIDGET_ID_DELIMITER);
  155. const seriesNames = seriesNameString!.split(SERIES_LIST_DELIMITER);
  156. return seriesNames.reduce((acc, series) => {
  157. // @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
  158. acc[
  159. decodeURIComponent(
  160. WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(series, widget.id)
  161. )
  162. ] = false;
  163. return acc;
  164. }, {});
  165. }
  166. return {};
  167. }
  168. // when a widget has been changed/added/deleted update legend to incorporate that
  169. setMultipleWidgetSelectionStateURL(newDashboard: DashboardDetails, newWidget?: Widget) {
  170. const location = this.location;
  171. if (!location.query.unselectedSeries) {
  172. return location.query.unselectedSeries;
  173. }
  174. // if widget was updated it returns updated widget to default selection state
  175. if (newWidget && newDashboard.widgets.includes(newWidget)) {
  176. const formattedDefaultQuery = this.formatLegendDefaultQuery(newWidget);
  177. const newQuery = Array.isArray(location.query.unselectedSeries)
  178. ? location.query.unselectedSeries
  179. .map(legend => {
  180. if (legend.includes(newWidget.id!)) {
  181. return this.formatLegendDefaultQuery(newWidget);
  182. }
  183. return legend;
  184. })
  185. .filter(Boolean)
  186. : location.query.unselectedSeries.includes(newWidget.id!)
  187. ? formattedDefaultQuery
  188. : [location.query.unselectedSeries, formattedDefaultQuery].filter(Boolean);
  189. return newQuery;
  190. }
  191. // if widget was deleted it removes it from the selection query (clean up the url)
  192. if (newWidget) {
  193. return Array.isArray(location.query.unselectedSeries)
  194. ? location.query.unselectedSeries
  195. .map(legend => {
  196. if (legend.includes(newWidget.id!)) {
  197. return undefined;
  198. }
  199. return legend;
  200. })
  201. .filter(Boolean)
  202. : location.query.unselectedSeries.includes(newWidget.id!)
  203. ? []
  204. : location.query.unselectedSeries;
  205. }
  206. // widget added (since added widgets don't have an id until submitted), it sets selection state based on all widgets
  207. const unselectedSeries = newDashboard.widgets
  208. .map(widget => {
  209. if (Array.isArray(location.query.unselectedSeries)) {
  210. const widgetLegendQuery = location.query.unselectedSeries.find(legend =>
  211. legend.includes(widget.id!)
  212. );
  213. if (!widgetLegendQuery && this.widgetRequiresLegendUnselection(widget)) {
  214. return this.formatLegendDefaultQuery(widget);
  215. }
  216. return widgetLegendQuery;
  217. }
  218. return location.query.unselectedSeries?.includes(widget.id!)
  219. ? location.query.unselectedSeries
  220. : this.formatLegendDefaultQuery(widget);
  221. })
  222. .filter(Boolean);
  223. return unselectedSeries;
  224. }
  225. }
  226. export default WidgetLegendSelectionState;