-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(dashboards): add create link button to table edit #101204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
DominikB2014
wants to merge
2
commits into
master
Choose a base branch
from
dominikbuszowiecki/browse-68-add-ui-to-link-one-dashboard-to-another-in-the-frontend
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
208 changes: 208 additions & 0 deletions
208
static/app/components/modals/widgetBuilder/linkToDashboardModal.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import {Fragment, useCallback, useEffect, useState, type ReactNode} from 'react'; | ||
import {css} from '@emotion/react'; | ||
import styled from '@emotion/styled'; | ||
|
||
import {fetchDashboard, fetchDashboards} from 'sentry/actionCreators/dashboards'; | ||
import type {ModalRenderProps} from 'sentry/actionCreators/modal'; | ||
import {Button} from 'sentry/components/core/button'; | ||
import {ButtonBar} from 'sentry/components/core/button/buttonBar'; | ||
import {Select} from 'sentry/components/core/select'; | ||
import {t, tct} from 'sentry/locale'; | ||
import {space} from 'sentry/styles/space'; | ||
import type {SelectValue} from 'sentry/types/core'; | ||
import useApi from 'sentry/utils/useApi'; | ||
import {useNavigate} from 'sentry/utils/useNavigate'; | ||
import useOrganization from 'sentry/utils/useOrganization'; | ||
import {useParams} from 'sentry/utils/useParams'; | ||
import { | ||
DashboardCreateLimitWrapper, | ||
type DashboardCreateLimitWrapperResult, | ||
} from 'sentry/views/dashboards/createLimitWrapper'; | ||
import type {DashboardDetails, DashboardListItem} from 'sentry/views/dashboards/types'; | ||
import {MAX_WIDGETS} from 'sentry/views/dashboards/types'; | ||
import {getSavedPageFilters} from 'sentry/views/dashboards/utils'; | ||
import {NEW_DASHBOARD_ID} from 'sentry/views/dashboards/widgetBuilder/utils'; | ||
|
||
export type LinkToDashboardModalProps = { | ||
source?: string; // TODO: perhpas make this an enum | ||
}; | ||
|
||
type Props = ModalRenderProps & LinkToDashboardModalProps; | ||
|
||
const SELECT_DASHBOARD_MESSAGE = t('Select a dashboard'); | ||
|
||
export function LinkToDashboardModal({Header, Body, Footer, closeModal}: Props) { | ||
const api = useApi(); | ||
const navigate = useNavigate(); | ||
const organization = useOrganization(); | ||
const [dashboards, setDashboards] = useState<DashboardListItem[] | null>(null); | ||
const [selectedDashboard, setSelectedDashboard] = useState<DashboardDetails | null>( | ||
null | ||
); | ||
const [selectedDashboardId, setSelectedDashboardId] = useState<string | null>(null); | ||
|
||
const {dashboardId: currentDashboardId} = useParams<{dashboardId: string}>(); | ||
|
||
useEffect(() => { | ||
// Track mounted state so we dont call setState on unmounted components | ||
let unmounted = false; | ||
|
||
fetchDashboards(api, organization.slug).then(response => { | ||
// If component has unmounted, dont set state | ||
if (unmounted) { | ||
return; | ||
} | ||
|
||
setDashboards(response); | ||
}); | ||
|
||
return () => { | ||
unmounted = true; | ||
}; | ||
}, [api, organization.slug]); | ||
|
||
useEffect(() => { | ||
// Track mounted state so we dont call setState on unmounted components | ||
let unmounted = false; | ||
|
||
if (selectedDashboardId === NEW_DASHBOARD_ID || selectedDashboardId === null) { | ||
setSelectedDashboard(null); | ||
} else { | ||
fetchDashboard(api, organization.slug, selectedDashboardId).then(response => { | ||
// If component has unmounted, dont set state | ||
if (unmounted) { | ||
return; | ||
} | ||
|
||
setSelectedDashboard(response); | ||
}); | ||
} | ||
|
||
return () => { | ||
unmounted = true; | ||
}; | ||
}, [api, organization.slug, selectedDashboardId]); | ||
|
||
const canSubmit = selectedDashboardId !== null; | ||
|
||
const getOptions = useCallback( | ||
( | ||
hasReachedDashboardLimit: boolean, | ||
isLoading: boolean, | ||
limitMessage: ReactNode | null | ||
) => { | ||
if (dashboards === null) { | ||
return null; | ||
} | ||
|
||
return [ | ||
{ | ||
label: t('+ Create New Dashboard'), | ||
value: 'new', | ||
disabled: hasReachedDashboardLimit || isLoading, | ||
tooltip: hasReachedDashboardLimit ? limitMessage : undefined, | ||
tooltipOptions: {position: 'right', isHoverable: true}, | ||
}, | ||
...dashboards | ||
.filter(dashboard => | ||
// if adding from a dashboard, currentDashboardId will be set and we'll remove it from the list of options | ||
currentDashboardId ? dashboard.id !== currentDashboardId : true | ||
) | ||
.map(({title, id, widgetDisplay}) => ({ | ||
label: title, | ||
value: id, | ||
disabled: widgetDisplay.length >= MAX_WIDGETS, | ||
tooltip: | ||
widgetDisplay.length >= MAX_WIDGETS && | ||
tct('Max widgets ([maxWidgets]) per dashboard reached.', { | ||
maxWidgets: MAX_WIDGETS, | ||
}), | ||
tooltipOptions: {position: 'right'}, | ||
})), | ||
].filter(Boolean) as Array<SelectValue<string>>; | ||
}, | ||
[currentDashboardId, dashboards] | ||
); | ||
|
||
function goToDashboard() { | ||
const dashboardsPath = | ||
selectedDashboardId === NEW_DASHBOARD_ID | ||
? `/organizations/${organization.slug}/dashboards/new/` | ||
: `/organizations/${organization.slug}/dashboard/${selectedDashboardId}/`; | ||
|
||
const pathname = dashboardsPath; | ||
|
||
navigate({ | ||
pathname, | ||
query: { | ||
...(selectedDashboard ? getSavedPageFilters(selectedDashboard) : {}), | ||
}, | ||
}); | ||
closeModal(); | ||
} | ||
|
||
return ( | ||
<Fragment> | ||
<Header closeButton>{t('Link to Dashboard')}</Header> | ||
<Body> | ||
<Wrapper> | ||
<DashboardCreateLimitWrapper> | ||
{({ | ||
Check failure on line 150 in static/app/components/modals/widgetBuilder/linkToDashboardModal.tsx
|
||
hasReachedDashboardLimit, | ||
isLoading, | ||
limitMessage, | ||
}: DashboardCreateLimitWrapperResult) => ( | ||
<Select | ||
disabled={dashboards === null} | ||
name="dashboard" | ||
placeholder={t('Select Dashboard')} | ||
value={selectedDashboardId} | ||
options={getOptions(hasReachedDashboardLimit, isLoading, limitMessage)} | ||
onChange={(option: SelectValue<string>) => { | ||
if (option.disabled) { | ||
return; | ||
} | ||
setSelectedDashboardId(option.value); | ||
}} | ||
/> | ||
)} | ||
</DashboardCreateLimitWrapper> | ||
</Wrapper> | ||
</Body> | ||
|
||
<Footer> | ||
<StyledButtonBar gap="lg"> | ||
<Button | ||
disabled={!canSubmit} | ||
title={canSubmit ? undefined : SELECT_DASHBOARD_MESSAGE} | ||
onClick={() => goToDashboard()} | ||
aria-label={t('Link to dashboard')} | ||
> | ||
{t('Link to dashboard')} | ||
</Button> | ||
</StyledButtonBar> | ||
</Footer> | ||
</Fragment> | ||
); | ||
} | ||
|
||
const Wrapper = styled('div')` | ||
margin-bottom: ${space(2)}; | ||
`; | ||
|
||
const StyledButtonBar = styled(ButtonBar)` | ||
@media (max-width: ${props => props.theme.breakpoints.sm}) { | ||
grid-template-rows: repeat(2, 1fr); | ||
gap: ${space(1.5)}; | ||
width: 100%; | ||
|
||
> button { | ||
width: 100%; | ||
} | ||
} | ||
`; | ||
|
||
export const modalCss = css` | ||
max-width: 700px; | ||
margin: 70px auto; | ||
`; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import useOrganization from 'sentry/utils/useOrganization'; | ||
|
||
export function useHasDrillDownFlows() { | ||
const organization = useOrganization(); | ||
return organization.features.includes('dashboards-drilldown-flow'); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Dashboard Creation Path Inconsistent
The "Create New Dashboard" option uses the string value
'new'
, but the modal's logic checks against theNEW_DASHBOARD_ID
constant. This inconsistency means the new dashboard creation path won't be taken, preventing users from creating new dashboards through this modal.