create.tsx 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import {useEffect, useState} from 'react';
  2. import Feature from 'sentry/components/acl/feature';
  3. import {Alert} from 'sentry/components/alert';
  4. import ErrorBoundary from 'sentry/components/errorBoundary';
  5. import * as Layout from 'sentry/components/layouts/thirds';
  6. import {t} from 'sentry/locale';
  7. import type {RouteComponentProps} from 'sentry/types/legacyReactRouter';
  8. import type {Organization} from 'sentry/types/organization';
  9. import {browserHistory} from 'sentry/utils/browserHistory';
  10. import withOrganization from 'sentry/utils/withOrganization';
  11. import {EMPTY_DASHBOARD, getDashboardTemplates} from './data';
  12. import DashboardDetail from './detail';
  13. import type {Widget} from './types';
  14. import {DashboardState} from './types';
  15. import {cloneDashboard, constructWidgetFromQuery} from './utils';
  16. type Props = RouteComponentProps<{templateId?: string; widgetId?: string}, {}> & {
  17. children: React.ReactNode;
  18. organization: Organization;
  19. };
  20. function CreateDashboard(props: Props) {
  21. const {location, organization} = props;
  22. const {templateId} = props.params;
  23. const [newWidget, setNewWidget] = useState<Widget | undefined>();
  24. function renderDisabled() {
  25. return (
  26. <Layout.Page withPadding>
  27. <Alert type="warning">{t("You don't have access to this feature")}</Alert>
  28. </Layout.Page>
  29. );
  30. }
  31. const template = templateId
  32. ? getDashboardTemplates(organization).find(
  33. dashboardTemplate => dashboardTemplate.id === templateId
  34. )
  35. : undefined;
  36. const dashboard = template ? cloneDashboard(template) : cloneDashboard(EMPTY_DASHBOARD);
  37. const initialState = template ? DashboardState.PREVIEW : DashboardState.CREATE;
  38. useEffect(() => {
  39. const constructedWidget = constructWidgetFromQuery(location.query);
  40. setNewWidget(constructedWidget);
  41. if (constructedWidget) {
  42. browserHistory.replace(location.pathname);
  43. }
  44. }, [location.pathname, location.query]);
  45. return (
  46. <Feature
  47. features="dashboards-edit"
  48. organization={props.organization}
  49. renderDisabled={renderDisabled}
  50. >
  51. <ErrorBoundary>
  52. <DashboardDetail
  53. {...props}
  54. initialState={initialState}
  55. dashboard={dashboard}
  56. dashboards={[]}
  57. newWidget={newWidget}
  58. onSetNewWidget={() => setNewWidget(undefined)}
  59. />
  60. </ErrorBoundary>
  61. </Feature>
  62. );
  63. }
  64. export default withOrganization(CreateDashboard);