create.tsx 2.2 KB

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