index.tsx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import * as React from 'react';
  2. import {browserHistory} from 'react-router';
  3. import {Location} from 'history';
  4. import {SelectValue} from 'sentry/types';
  5. import {decodeScalar} from 'sentry/utils/queryString';
  6. import {FILTER_OPTIONS} from './constants';
  7. import {DataFilter} from './types';
  8. type HistogramChildrenProps = {
  9. activeFilter: SelectValue<DataFilter>;
  10. filterOptions: typeof FILTER_OPTIONS;
  11. handleFilterChange: (option: string) => void;
  12. handleResetView: () => void;
  13. isZoomed: boolean;
  14. };
  15. type Props = {
  16. children: (props: HistogramChildrenProps) => React.ReactNode;
  17. location: Location;
  18. zoomKeys: string[];
  19. };
  20. class Histogram extends React.Component<Props> {
  21. isZoomed() {
  22. const {location, zoomKeys} = this.props;
  23. return zoomKeys.map(key => location.query[key]).some(value => value !== undefined);
  24. }
  25. handleResetView = () => {
  26. const {location, zoomKeys} = this.props;
  27. browserHistory.push({
  28. pathname: location.pathname,
  29. query: removeHistogramQueryStrings(location, zoomKeys),
  30. });
  31. };
  32. getActiveFilter() {
  33. const {location} = this.props;
  34. const dataFilter = location.query.dataFilter
  35. ? decodeScalar(location.query.dataFilter)
  36. : FILTER_OPTIONS[0].value;
  37. return FILTER_OPTIONS.find(item => item.value === dataFilter) || FILTER_OPTIONS[0];
  38. }
  39. handleFilterChange = (value: string) => {
  40. const {location, zoomKeys} = this.props;
  41. browserHistory.push({
  42. pathname: location.pathname,
  43. query: {
  44. ...removeHistogramQueryStrings(location, zoomKeys),
  45. dataFilter: value,
  46. },
  47. });
  48. };
  49. render() {
  50. const childrenProps = {
  51. isZoomed: this.isZoomed(),
  52. handleResetView: this.handleResetView,
  53. activeFilter: this.getActiveFilter(),
  54. handleFilterChange: this.handleFilterChange,
  55. filterOptions: FILTER_OPTIONS,
  56. };
  57. return this.props.children(childrenProps);
  58. }
  59. }
  60. export function removeHistogramQueryStrings(location: Location, zoomKeys: string[]) {
  61. const query: Location['query'] = {...location.query, cursor: undefined};
  62. delete query.dataFilter;
  63. // reset all zoom parameters
  64. zoomKeys.forEach(key => delete query[key]);
  65. return query;
  66. }
  67. export default Histogram;