create.tsx 2.0 KB

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