|
| 1 | +import {Fragment, useCallback, useEffect, useState, type ReactNode} from 'react'; |
| 2 | +import {css} from '@emotion/react'; |
| 3 | +import styled from '@emotion/styled'; |
| 4 | + |
| 5 | +import {fetchDashboard, fetchDashboards} from 'sentry/actionCreators/dashboards'; |
| 6 | +import type {ModalRenderProps} from 'sentry/actionCreators/modal'; |
| 7 | +import {Button} from 'sentry/components/core/button'; |
| 8 | +import {ButtonBar} from 'sentry/components/core/button/buttonBar'; |
| 9 | +import {Select} from 'sentry/components/core/select'; |
| 10 | +import {t, tct} from 'sentry/locale'; |
| 11 | +import {space} from 'sentry/styles/space'; |
| 12 | +import type {SelectValue} from 'sentry/types/core'; |
| 13 | +import useApi from 'sentry/utils/useApi'; |
| 14 | +import useOrganization from 'sentry/utils/useOrganization'; |
| 15 | +import {useParams} from 'sentry/utils/useParams'; |
| 16 | +import {DashboardCreateLimitWrapper} from 'sentry/views/dashboards/createLimitWrapper'; |
| 17 | +import type {DashboardDetails, DashboardListItem} from 'sentry/views/dashboards/types'; |
| 18 | +import {MAX_WIDGETS} from 'sentry/views/dashboards/types'; |
| 19 | +import {NEW_DASHBOARD_ID} from 'sentry/views/dashboards/widgetBuilder/utils'; |
| 20 | + |
| 21 | +export type LinkToDashboardModalProps = { |
| 22 | + source?: string; // TODO: perhpas make this an enum |
| 23 | +}; |
| 24 | + |
| 25 | +type Props = ModalRenderProps & LinkToDashboardModalProps; |
| 26 | + |
| 27 | +const SELECT_DASHBOARD_MESSAGE = t('Select a dashboard'); |
| 28 | + |
| 29 | +export function LinkToDashboardModal({Header, Body, Footer, closeModal}: Props) { |
| 30 | + const api = useApi(); |
| 31 | + const organization = useOrganization(); |
| 32 | + const [dashboards, setDashboards] = useState<DashboardListItem[] | null>(null); |
| 33 | + const [_, setSelectedDashboard] = useState<DashboardDetails | null>(null); |
| 34 | + const [selectedDashboardId, setSelectedDashboardId] = useState<string | null>(null); |
| 35 | + |
| 36 | + const {dashboardId: currentDashboardId} = useParams<{dashboardId: string}>(); |
| 37 | + |
| 38 | + useEffect(() => { |
| 39 | + // Track mounted state so we dont call setState on unmounted components |
| 40 | + let unmounted = false; |
| 41 | + |
| 42 | + fetchDashboards(api, organization.slug).then(response => { |
| 43 | + // If component has unmounted, dont set state |
| 44 | + if (unmounted) { |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + setDashboards(response); |
| 49 | + }); |
| 50 | + |
| 51 | + return () => { |
| 52 | + unmounted = true; |
| 53 | + }; |
| 54 | + }, [api, organization.slug]); |
| 55 | + |
| 56 | + useEffect(() => { |
| 57 | + // Track mounted state so we dont call setState on unmounted components |
| 58 | + let unmounted = false; |
| 59 | + |
| 60 | + if (selectedDashboardId === NEW_DASHBOARD_ID || selectedDashboardId === null) { |
| 61 | + setSelectedDashboard(null); |
| 62 | + } else { |
| 63 | + fetchDashboard(api, organization.slug, selectedDashboardId).then(response => { |
| 64 | + // If component has unmounted, dont set state |
| 65 | + if (unmounted) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + setSelectedDashboard(response); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + return () => { |
| 74 | + unmounted = true; |
| 75 | + }; |
| 76 | + }, [api, organization.slug, selectedDashboardId]); |
| 77 | + |
| 78 | + const canSubmit = selectedDashboardId !== null; |
| 79 | + |
| 80 | + const getOptions = useCallback( |
| 81 | + ( |
| 82 | + hasReachedDashboardLimit: boolean, |
| 83 | + isLoading: boolean, |
| 84 | + limitMessage: ReactNode | null |
| 85 | + ) => { |
| 86 | + if (dashboards === null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return [ |
| 91 | + { |
| 92 | + label: t('+ Create New Dashboard'), |
| 93 | + value: 'new', |
| 94 | + disabled: hasReachedDashboardLimit || isLoading, |
| 95 | + tooltip: hasReachedDashboardLimit ? limitMessage : undefined, |
| 96 | + tooltipOptions: {position: 'right', isHoverable: true}, |
| 97 | + }, |
| 98 | + ...dashboards |
| 99 | + .filter(dashboard => |
| 100 | + // if adding from a dashboard, currentDashboardId will be set and we'll remove it from the list of options |
| 101 | + currentDashboardId ? dashboard.id !== currentDashboardId : true |
| 102 | + ) |
| 103 | + .map(({title, id, widgetDisplay}) => ({ |
| 104 | + label: title, |
| 105 | + value: id, |
| 106 | + disabled: widgetDisplay.length >= MAX_WIDGETS, |
| 107 | + tooltip: |
| 108 | + widgetDisplay.length >= MAX_WIDGETS && |
| 109 | + tct('Max widgets ([maxWidgets]) per dashboard reached.', { |
| 110 | + maxWidgets: MAX_WIDGETS, |
| 111 | + }), |
| 112 | + tooltipOptions: {position: 'right'}, |
| 113 | + })), |
| 114 | + ].filter(Boolean) as Array<SelectValue<string>>; |
| 115 | + }, |
| 116 | + [currentDashboardId, dashboards] |
| 117 | + ); |
| 118 | + |
| 119 | + function linkToDashboard() { |
| 120 | + // TODO: Update the local state of the widget to include the links |
| 121 | + // When the user clicks `save widget` we should update the dashboard widget link on the backend |
| 122 | + closeModal(); |
| 123 | + } |
| 124 | + |
| 125 | + return ( |
| 126 | + <Fragment> |
| 127 | + <Header closeButton>{t('Link to Dashboard')}</Header> |
| 128 | + <Body> |
| 129 | + <Wrapper> |
| 130 | + <DashboardCreateLimitWrapper> |
| 131 | + {({hasReachedDashboardLimit, isLoading, limitMessage}) => ( |
| 132 | + <Select |
| 133 | + disabled={dashboards === null} |
| 134 | + name="dashboard" |
| 135 | + placeholder={t('Select Dashboard')} |
| 136 | + value={selectedDashboardId} |
| 137 | + options={getOptions(hasReachedDashboardLimit, isLoading, limitMessage)} |
| 138 | + onChange={(option: SelectValue<string>) => { |
| 139 | + if (option.disabled) { |
| 140 | + return; |
| 141 | + } |
| 142 | + setSelectedDashboardId(option.value); |
| 143 | + }} |
| 144 | + /> |
| 145 | + )} |
| 146 | + </DashboardCreateLimitWrapper> |
| 147 | + </Wrapper> |
| 148 | + </Body> |
| 149 | + <Footer> |
| 150 | + <StyledButtonBar gap="lg"> |
| 151 | + <Button |
| 152 | + disabled={!canSubmit} |
| 153 | + title={canSubmit ? undefined : SELECT_DASHBOARD_MESSAGE} |
| 154 | + onClick={() => linkToDashboard()} |
| 155 | + aria-label={t('Link to dashboard')} |
| 156 | + > |
| 157 | + {t('Link to dashboard')} |
| 158 | + </Button> |
| 159 | + </StyledButtonBar> |
| 160 | + </Footer> |
| 161 | + </Fragment> |
| 162 | + ); |
| 163 | +} |
| 164 | + |
| 165 | +const Wrapper = styled('div')` |
| 166 | + margin-bottom: ${space(2)}; |
| 167 | +`; |
| 168 | + |
| 169 | +const StyledButtonBar = styled(ButtonBar)` |
| 170 | + @media (max-width: ${props => props.theme.breakpoints.sm}) { |
| 171 | + grid-template-rows: repeat(2, 1fr); |
| 172 | + gap: ${space(1.5)}; |
| 173 | + width: 100%; |
| 174 | +
|
| 175 | + > button { |
| 176 | + width: 100%; |
| 177 | + } |
| 178 | + } |
| 179 | +`; |
| 180 | + |
| 181 | +export const modalCss = css` |
| 182 | + max-width: 700px; |
| 183 | + margin: 70px auto; |
| 184 | +`; |
0 commit comments