convertWidgetToBuilderStateParams.ts 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import {DisplayType, type Widget, WidgetType} from 'sentry/views/dashboards/types';
  2. import type {WidgetBuilderStateQueryParams} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  3. /**
  4. * Converts a widget to a set of query params that can be used to
  5. * restore the widget builder state.
  6. */
  7. export function convertWidgetToBuilderStateParams(
  8. widget: Widget
  9. ): WidgetBuilderStateQueryParams {
  10. const yAxis = widget.queries.flatMap(q => q.aggregates);
  11. const query = widget.queries.flatMap(q => q.conditions);
  12. const sort = widget.queries.flatMap(q => q.orderby);
  13. let field: string[] = [];
  14. if (
  15. widget.displayType === DisplayType.TABLE ||
  16. widget.displayType === DisplayType.BIG_NUMBER
  17. ) {
  18. field = widget.queries.flatMap(q => q.fields ?? []);
  19. } else {
  20. field = widget.queries.flatMap(q => q.columns);
  21. }
  22. return {
  23. title: widget.title,
  24. description: widget.description ?? '',
  25. dataset: widget.widgetType ?? WidgetType.ERRORS,
  26. displayType: widget.displayType ?? DisplayType.TABLE,
  27. field,
  28. yAxis,
  29. query,
  30. sort,
  31. };
  32. }