|
| 1 | +import React from 'react'; |
| 2 | +import { useTranslation } from 'react-i18next'; |
| 3 | +import { useDispatch, useSelector } from 'react-redux'; |
| 4 | +import { IconButton, IconButtonProps, styled } from '@mui/material'; |
| 5 | +import { Theme } from '@mui/material/styles'; |
| 6 | +import ViewGridPlusIcon from 'mdi-material-ui/ViewGridPlus'; |
| 7 | +import { StyledComponent } from '@emotion/styled'; |
| 8 | + |
| 9 | +import type { PanelDefinition } from '@perses-dev/core'; |
| 10 | +import { InfoTooltip } from '@perses-dev/components'; |
| 11 | + |
| 12 | +import { dashboardsAddPersesPanelExternally } from '../../../store/actions'; |
| 13 | + |
| 14 | +export const HeaderIconButton: StyledComponent<IconButtonProps & { theme?: Theme }> = styled( |
| 15 | + IconButton, |
| 16 | +)(({ theme }) => ({ |
| 17 | + borderRadius: theme.shape.borderRadius, |
| 18 | + padding: '4px', |
| 19 | +})); |
| 20 | + |
| 21 | +function createPanelDefinition(query: string, name: string, description: string): PanelDefinition { |
| 22 | + return { |
| 23 | + kind: 'Panel', |
| 24 | + spec: { |
| 25 | + display: { |
| 26 | + name: name, |
| 27 | + description: description, |
| 28 | + }, |
| 29 | + plugin: { |
| 30 | + kind: 'TimeSeriesChart', |
| 31 | + spec: {}, |
| 32 | + }, |
| 33 | + queries: [ |
| 34 | + { |
| 35 | + kind: 'TimeSeriesQuery', |
| 36 | + spec: { |
| 37 | + plugin: { |
| 38 | + kind: 'PrometheusTimeSeriesQuery', |
| 39 | + spec: { |
| 40 | + query: query, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }, |
| 47 | + }; |
| 48 | +} |
| 49 | + |
| 50 | +type AddToDashboardButtonProps = { |
| 51 | + query: string; |
| 52 | + name?: string; |
| 53 | + description?: string; |
| 54 | +}; |
| 55 | + |
| 56 | +export const AddToDashboardButton: React.FC<AddToDashboardButtonProps> = ({ |
| 57 | + query, |
| 58 | + name, |
| 59 | + description, |
| 60 | +}) => { |
| 61 | + const dispatch = useDispatch(); |
| 62 | + const { t } = useTranslation(process.env.I18N_NAMESPACE); |
| 63 | + const isCustomDashboardOpen: boolean = useSelector( |
| 64 | + (s: any) => s.plugins?.mp?.dashboards?.isOpened, |
| 65 | + ); |
| 66 | + const addToPersesDashboard = React.useCallback(() => { |
| 67 | + const panelDefinition = createPanelDefinition(query, name, description); |
| 68 | + dispatch(dashboardsAddPersesPanelExternally(panelDefinition)); |
| 69 | + }, [query, name, description, dispatch]); |
| 70 | + |
| 71 | + if (!isCustomDashboardOpen) { |
| 72 | + // No dashboard is opened - nothing to add to. |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <InfoTooltip description={t('Add To Dashboard')}> |
| 78 | + <HeaderIconButton aria-label="panel description" size="small"> |
| 79 | + <ViewGridPlusIcon |
| 80 | + aria-describedby="info-tooltip" |
| 81 | + aria-hidden={false} |
| 82 | + fontSize="inherit" |
| 83 | + sx={{ color: (theme) => theme.palette.text.secondary }} |
| 84 | + onClick={addToPersesDashboard} |
| 85 | + /> |
| 86 | + </HeaderIconButton> |
| 87 | + </InfoTooltip> |
| 88 | + ); |
| 89 | +}; |
0 commit comments