widgetLegendSelectionState.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. this.organization.features.includes('dashboards-releases-on-charts')
  111. ? {
  112. [WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(
  113. 'Releases',
  114. widget.id
  115. )]: false,
  116. }
  117. : {};
  118. }
  119. widgetRequiresLegendUnselection(widget: Widget) {
  120. return (
  121. widget.displayType === DisplayType.AREA || widget.displayType === DisplayType.LINE
  122. );
  123. }
  124. widgetIsChart(widget: Widget) {
  125. return (
  126. widget.displayType === DisplayType.AREA ||
  127. widget.displayType === DisplayType.LINE ||
  128. widget.displayType === DisplayType.BAR ||
  129. widget.displayType === DisplayType.TOP_N
  130. );
  131. }
  132. formatLegendDefaultQuery(widget: Widget) {
  133. return this.organization.features.includes('dashboards-releases-on-charts') &&
  134. this.widgetRequiresLegendUnselection(widget)
  135. ? `${widget.id}${WIDGET_ID_DELIMITER}Releases`
  136. : undefined;
  137. }
  138. // going from selected to query param
  139. encodeLegendQueryParam(widget: Widget, selected: LegendSelection) {
  140. return (
  141. widget.id +
  142. WIDGET_ID_DELIMITER +
  143. Object.keys(selected)
  144. .filter(key => !selected[key])
  145. .map(series =>
  146. encodeURIComponent(
  147. WidgetLegendNameEncoderDecoder.decodeSeriesNameForLegend(series)
  148. )
  149. )
  150. .join(SERIES_LIST_DELIMITER)
  151. );
  152. }
  153. // going from query param to selected
  154. decodeLegendQueryParam(widget: Widget) {
  155. const location = this.location;
  156. const widgetLegendString = decodeList(location.query.unselectedSeries).find(
  157. widgetLegend => widgetLegend.includes(widget.id!)
  158. );
  159. if (widgetLegendString) {
  160. const [_, seriesNameString] = widgetLegendString.split(WIDGET_ID_DELIMITER);
  161. const seriesNames = seriesNameString.split(SERIES_LIST_DELIMITER);
  162. return seriesNames.reduce((acc, series) => {
  163. acc[
  164. decodeURIComponent(
  165. WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend(series, widget.id)
  166. )
  167. ] = false;
  168. return acc;
  169. }, {});
  170. }
  171. return {};
  172. }
  173. // when a widget has been changed/added/deleted update legend to incorporate that
  174. setMultipleWidgetSelectionStateURL(newDashboard: DashboardDetails, newWidget?: Widget) {
  175. const [location] = [this.location];
  176. if (!location.query.unselectedSeries) {
  177. return location.query.unselectedSeries;
  178. }
  179. // if widget was updated it returns updated widget to default selection state
  180. if (newWidget && newDashboard.widgets.includes(newWidget)) {
  181. const formattedDefaultQuery = this.formatLegendDefaultQuery(newWidget);
  182. const newQuery = Array.isArray(location.query.unselectedSeries)
  183. ? location.query.unselectedSeries.map(legend => {
  184. if (legend.includes(newWidget.id!)) {
  185. return this.formatLegendDefaultQuery(newWidget);
  186. }
  187. return legend;
  188. })
  189. : location.query.unselectedSeries.includes(newWidget.id!)
  190. ? formattedDefaultQuery
  191. : [location.query.unselectedSeries, formattedDefaultQuery];
  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.map(legend => {
  198. if (legend.includes(newWidget.id!)) {
  199. return undefined;
  200. }
  201. return legend;
  202. })
  203. : location.query.unselectedSeries.includes(newWidget.id!)
  204. ? []
  205. : location.query.unselectedSeries;
  206. }
  207. // widget added (since added widgets don't have an id until submitted), it sets selection state based on all widgets
  208. const unselectedSeries = newDashboard.widgets.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. return unselectedSeries;
  223. }
  224. }
  225. export default WidgetLegendSelectionState;