widgetPreview.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import PanelAlert from 'sentry/components/panels/panelAlert';
  2. import type {TableDataWithTitle} from 'sentry/utils/discover/discoverQuery';
  3. import {useLocation} from 'sentry/utils/useLocation';
  4. import useOrganization from 'sentry/utils/useOrganization';
  5. import usePageFilters from 'sentry/utils/usePageFilters';
  6. import useRouter from 'sentry/utils/useRouter';
  7. import {
  8. type DashboardDetails,
  9. type DashboardFilters,
  10. DisplayType,
  11. WidgetType,
  12. } from 'sentry/views/dashboards/types';
  13. import {useWidgetBuilderContext} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext';
  14. import {convertBuilderStateToWidget} from 'sentry/views/dashboards/widgetBuilder/utils/convertBuilderStateToWidget';
  15. import WidgetCard from 'sentry/views/dashboards/widgetCard';
  16. import WidgetLegendNameEncoderDecoder from 'sentry/views/dashboards/widgetLegendNameEncoderDecoder';
  17. import WidgetLegendSelectionState from 'sentry/views/dashboards/widgetLegendSelectionState';
  18. interface WidgetPreviewProps {
  19. dashboard: DashboardDetails;
  20. dashboardFilters: DashboardFilters;
  21. isWidgetInvalid?: boolean;
  22. onDataFetched?: (tableData: TableDataWithTitle[]) => void;
  23. }
  24. function WidgetPreview({
  25. dashboard,
  26. dashboardFilters,
  27. isWidgetInvalid,
  28. onDataFetched,
  29. }: WidgetPreviewProps) {
  30. const organization = useOrganization();
  31. const location = useLocation();
  32. const router = useRouter();
  33. const pageFilters = usePageFilters();
  34. const {state} = useWidgetBuilderContext();
  35. const widget = convertBuilderStateToWidget(state);
  36. const widgetLegendState = new WidgetLegendSelectionState({
  37. location,
  38. organization,
  39. dashboard,
  40. router,
  41. });
  42. // TODO: The way we create the widget here does not propagate a widget ID
  43. // to pass to the legend encoder decoder.
  44. const unselectedReleasesForCharts = {
  45. [WidgetLegendNameEncoderDecoder.encodeSeriesNameForLegend('Releases', undefined)]:
  46. false,
  47. };
  48. return (
  49. <WidgetCard
  50. disableFullscreen
  51. borderless
  52. isWidgetInvalid={isWidgetInvalid}
  53. shouldResize={state.displayType !== DisplayType.TABLE}
  54. organization={organization}
  55. selection={pageFilters.selection}
  56. widget={widget}
  57. dashboardFilters={dashboardFilters}
  58. isEditingDashboard={false}
  59. widgetLimitReached={false}
  60. showContextMenu={false}
  61. renderErrorMessage={errorMessage =>
  62. typeof errorMessage === 'string' && (
  63. <PanelAlert type="error">{errorMessage}</PanelAlert>
  64. )
  65. }
  66. onLegendSelectChanged={() => {}}
  67. legendOptions={
  68. widgetLegendState.widgetRequiresLegendUnselection(widget)
  69. ? {selected: unselectedReleasesForCharts}
  70. : undefined
  71. }
  72. widgetLegendState={widgetLegendState}
  73. onDataFetched={onDataFetched}
  74. // TODO: This requires the current widget ID and a helper to update the
  75. // dashboard state to be added
  76. onWidgetSplitDecision={() => {}}
  77. // onWidgetSplitDecision={onWidgetSplitDecision}
  78. showConfidenceWarning={widget.widgetType === WidgetType.SPANS}
  79. />
  80. );
  81. }
  82. export default WidgetPreview;