widgetLegendSelectionState.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. !isInQuery
  68. ? router.replace({
  69. query: {
  70. ...location.query,
  71. unselectedSeries: [
  72. ...location.query.unselectedSeries,
  73. this.encodeLegendQueryParam(widget, selected),
  74. ],
  75. },
  76. })
  77. : router.replace({
  78. query: {
  79. ...location.query,
  80. unselectedSeries: newLegendQuery,
  81. },
  82. });
  83. } else {
  84. if (location.query.unselectedSeries?.includes(widget.id!)) {
  85. router.replace({
  86. query: {
  87. ...location.query,
  88. unselectedSeries: [this.encodeLegendQueryParam(widget, selected)],
  89. },
  90. });
  91. } else {
  92. router.replace({
  93. query: {
  94. ...location.query,
  95. unselectedSeries: [
  96. location.query.unselectedSeries,
  97. this.encodeLegendQueryParam(widget, selected),
  98. ],
  99. },
  100. });
  101. }
  102. }
  103. }
  104. // sets unselected legend options by the legend query param
  105. getWidgetSelectionState(widget: Widget): LegendSelection {
  106. const location = this.location;
  107. return location.query.unselectedSeries
  108. ? this.decodeLegendQueryParam(widget)
  109. : this.widgetRequiresLegendUnselection(widget)
  110. ? {
  111. [WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(
  112. 'Releases',
  113. widget.id
  114. )]: false,
  115. }
  116. : {};
  117. }
  118. widgetRequiresLegendUnselection(widget: Widget) {
  119. return (
  120. widget.displayType === DisplayType.AREA || widget.displayType === DisplayType.LINE
  121. );
  122. }
  123. widgetIsChart(widget: Widget) {
  124. return (
  125. widget.displayType === DisplayType.AREA ||
  126. widget.displayType === DisplayType.LINE ||
  127. widget.displayType === DisplayType.BAR ||
  128. widget.displayType === DisplayType.TOP_N
  129. );
  130. }
  131. formatLegendDefaultQuery(widget: Widget) {
  132. return this.widgetRequiresLegendUnselection(widget)
  133. ? `${widget.id}${WIDGET_ID_DELIMITER}Releases`
  134. : undefined;
  135. }
  136. // going from selected to query param
  137. encodeLegendQueryParam(widget: Widget, selected: LegendSelection) {
  138. return (
  139. widget.id +
  140. WIDGET_ID_DELIMITER +
  141. Object.keys(selected)
  142. .filter(key => !selected[key])
  143. .map(series =>
  144. encodeURIComponent(
  145. WidgetLegendNameEncoderDecoder.decodeSeriesNameForLegend(series)
  146. )
  147. )
  148. .join(SERIES_LIST_DELIMITER)
  149. );
  150. }
  151. // going from query param to selected
  152. decodeLegendQueryParam(widget: Widget) {
  153. const location = this.location;
  154. const widgetLegendString = decodeList(location.query.unselectedSeries).find(
  155. widgetLegend => widgetLegend.includes(widget.id!)
  156. );
  157. if (widgetLegendString) {
  158. const [_, seriesNameString] = widgetLegendString.split(WIDGET_ID_DELIMITER);
  159. const seriesNames = seriesNameString.split(SERIES_LIST_DELIMITER);
  160. return seriesNames.reduce((acc, series) => {
  161. acc[
  162. decodeURIComponent(
  163. WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(series, widget.id)
  164. )
  165. ] = false;
  166. return acc;
  167. }, {});
  168. }
  169. return {};
  170. }
  171. // when a widget has been changed/added/deleted update legend to incorporate that
  172. setMultipleWidgetSelectionStateURL(newDashboard: DashboardDetails, newWidget?: Widget) {
  173. const location = this.location;
  174. if (!location.query.unselectedSeries) {
  175. return location.query.unselectedSeries;
  176. }
  177. // if widget was updated it returns updated widget to default selection state
  178. if (newWidget && newDashboard.widgets.includes(newWidget)) {
  179. const formattedDefaultQuery = this.formatLegendDefaultQuery(newWidget);
  180. const newQuery = Array.isArray(location.query.unselectedSeries)
  181. ? location.query.unselectedSeries
  182. .map(legend => {
  183. if (legend.includes(newWidget.id!)) {
  184. return this.formatLegendDefaultQuery(newWidget);
  185. }
  186. return legend;
  187. })
  188. .filter(Boolean)
  189. : location.query.unselectedSeries.includes(newWidget.id!)
  190. ? formattedDefaultQuery
  191. : [location.query.unselectedSeries, formattedDefaultQuery].filter(Boolean);
  192. return newQuery;
  193. }
  194. // if widget was deleted it removes it from the selection query (clean up the url)
  195. if (newWidget) {
  196. return Array.isArray(location.query.unselectedSeries)
  197. ? location.query.unselectedSeries
  198. .map(legend => {
  199. if (legend.includes(newWidget.id!)) {
  200. return undefined;
  201. }
  202. return legend;
  203. })
  204. .filter(Boolean)
  205. : location.query.unselectedSeries.includes(newWidget.id!)
  206. ? []
  207. : location.query.unselectedSeries;
  208. }
  209. // widget added (since added widgets don't have an id until submitted), it sets selection state based on all widgets
  210. const unselectedSeries = newDashboard.widgets
  211. .map(widget => {
  212. if (Array.isArray(location.query.unselectedSeries)) {
  213. const widgetLegendQuery = location.query.unselectedSeries.find(legend =>
  214. legend.includes(widget.id!)
  215. );
  216. if (!widgetLegendQuery && this.widgetRequiresLegendUnselection(widget)) {
  217. return this.formatLegendDefaultQuery(widget);
  218. }
  219. return widgetLegendQuery;
  220. }
  221. return location.query.unselectedSeries?.includes(widget.id!)
  222. ? location.query.unselectedSeries
  223. : this.formatLegendDefaultQuery(widget);
  224. })
  225. .filter(Boolean);
  226. return unselectedSeries;
  227. }
  228. }
  229. export default WidgetLegendSelectionState;