widgetLegendSelectionState.tsx 8.4 KB

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