sortableWidget.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import type {ComponentProps} from 'react';
  2. import styled from '@emotion/styled';
  3. import {LazyRender} from 'sentry/components/lazyRender';
  4. import PanelAlert from 'sentry/components/panels/panelAlert';
  5. import type {User} from 'sentry/types/user';
  6. import useOrganization from 'sentry/utils/useOrganization';
  7. import {useUser} from 'sentry/utils/useUser';
  8. import {useUserTeams} from 'sentry/utils/useUserTeams';
  9. import {checkUserHasEditAccess} from 'sentry/views/dashboards/detail';
  10. import WidgetCard from 'sentry/views/dashboards/widgetCard';
  11. import {DashboardsMEPProvider} from './widgetCard/dashboardsMEPContext';
  12. import {Toolbar} from './widgetCard/toolbar';
  13. import type {DashboardFilters, DashboardPermissions, Widget} from './types';
  14. import type WidgetLegendSelectionState from './widgetLegendSelectionState';
  15. const TABLE_ITEM_LIMIT = 20;
  16. type Props = {
  17. index: string;
  18. isEditingDashboard: boolean;
  19. onDelete: () => void;
  20. onDuplicate: () => void;
  21. onEdit: () => void;
  22. onSetTransactionsDataset: () => void;
  23. widget: Widget;
  24. widgetLegendState: WidgetLegendSelectionState;
  25. widgetLimitReached: boolean;
  26. dashboardCreator?: User;
  27. dashboardFilters?: DashboardFilters;
  28. dashboardPermissions?: DashboardPermissions;
  29. isMobile?: boolean;
  30. isPreview?: boolean;
  31. windowWidth?: number;
  32. };
  33. function SortableWidget(props: Props) {
  34. const {
  35. widget,
  36. isEditingDashboard,
  37. widgetLimitReached,
  38. onDelete,
  39. onEdit,
  40. onDuplicate,
  41. onSetTransactionsDataset,
  42. isPreview,
  43. isMobile,
  44. windowWidth,
  45. index,
  46. dashboardFilters,
  47. widgetLegendState,
  48. dashboardPermissions,
  49. dashboardCreator,
  50. } = props;
  51. const organization = useOrganization();
  52. const currentUser = useUser();
  53. const {teams: userTeams} = useUserTeams();
  54. let hasEditAccess = true;
  55. if (organization.features.includes('dashboards-edit-access')) {
  56. hasEditAccess = checkUserHasEditAccess(
  57. currentUser,
  58. userTeams,
  59. organization,
  60. dashboardPermissions,
  61. dashboardCreator
  62. );
  63. }
  64. const widgetProps: ComponentProps<typeof WidgetCard> = {
  65. widget,
  66. isEditingDashboard,
  67. widgetLimitReached,
  68. hasEditAccess,
  69. onDelete,
  70. onEdit,
  71. onDuplicate,
  72. onSetTransactionsDataset,
  73. showContextMenu: true,
  74. isPreview,
  75. index,
  76. dashboardFilters,
  77. widgetLegendState,
  78. renderErrorMessage: errorMessage => {
  79. return (
  80. typeof errorMessage === 'string' && (
  81. <PanelAlert type="error">{errorMessage}</PanelAlert>
  82. )
  83. );
  84. },
  85. isMobile,
  86. windowWidth,
  87. tableItemLimit: TABLE_ITEM_LIMIT,
  88. };
  89. return (
  90. <GridWidgetWrapper>
  91. <DashboardsMEPProvider>
  92. <LazyRender containerHeight={200} withoutContainer>
  93. <WidgetCard {...widgetProps} />
  94. {props.isEditingDashboard && (
  95. <Toolbar
  96. onEdit={props.onEdit}
  97. onDelete={props.onDelete}
  98. onDuplicate={props.onDuplicate}
  99. isMobile={props.isMobile}
  100. />
  101. )}
  102. </LazyRender>
  103. </DashboardsMEPProvider>
  104. </GridWidgetWrapper>
  105. );
  106. }
  107. export default SortableWidget;
  108. const GridWidgetWrapper = styled('div')`
  109. height: 100%;
  110. `;