|
|
|
@ -1,13 +1,16 @@ |
|
|
|
|
import { useCallback } from 'react'; |
|
|
|
|
|
|
|
|
|
import { selectors as e2eSelectors } from '@grafana/e2e-selectors'; |
|
|
|
|
import { config } from '@grafana/runtime'; |
|
|
|
|
import { VizPanel } from '@grafana/scenes'; |
|
|
|
|
import { Menu } from '@grafana/ui'; |
|
|
|
|
import { SceneObject, VizPanel } from '@grafana/scenes'; |
|
|
|
|
import { IconName, Menu } from '@grafana/ui'; |
|
|
|
|
import { contextSrv } from 'app/core/core'; |
|
|
|
|
import { t } from 'app/core/internationalization'; |
|
|
|
|
|
|
|
|
|
import { isPublicDashboardsEnabled } from '../../../dashboard/components/ShareModal/SharePublicDashboard/SharePublicDashboardUtils'; |
|
|
|
|
import { DashboardScene } from '../../scene/DashboardScene'; |
|
|
|
|
import { ShareDrawer } from '../ShareDrawer/ShareDrawer'; |
|
|
|
|
import { SceneShareDrawerState } from '../types'; |
|
|
|
|
|
|
|
|
|
import { ShareExternally } from './share-externally/ShareExternally'; |
|
|
|
|
import { ShareInternally } from './share-internally/ShareInternally'; |
|
|
|
@ -15,59 +18,86 @@ import { ShareSnapshot } from './share-snapshot/ShareSnapshot'; |
|
|
|
|
|
|
|
|
|
const newShareButtonSelector = e2eSelectors.pages.Dashboard.DashNav.newShareButton.menu; |
|
|
|
|
|
|
|
|
|
type CustomDashboardDrawer = new (...args: SceneShareDrawerState[]) => SceneObject; |
|
|
|
|
|
|
|
|
|
export interface ShareDrawerMenuItem { |
|
|
|
|
testId: string; |
|
|
|
|
label: string; |
|
|
|
|
description?: string; |
|
|
|
|
icon: IconName; |
|
|
|
|
renderCondition: boolean; |
|
|
|
|
onClick: (d: DashboardScene) => void; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const customShareDrawerItem: ShareDrawerMenuItem[] = []; |
|
|
|
|
|
|
|
|
|
export function addDashboardShareDrawerItem(item: ShareDrawerMenuItem) { |
|
|
|
|
customShareDrawerItem.push(item); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export default function ShareMenu({ dashboard, panel }: { dashboard: DashboardScene; panel?: VizPanel }) { |
|
|
|
|
const onShareInternallyClick = () => { |
|
|
|
|
const drawer = new ShareDrawer({ |
|
|
|
|
title: t('share-dashboard.menu.share-internally-title', 'Share internally'), |
|
|
|
|
body: new ShareInternally({ panelRef: panel?.getRef() }), |
|
|
|
|
}); |
|
|
|
|
const onMenuItemClick = useCallback( |
|
|
|
|
(title: string, component: CustomDashboardDrawer) => { |
|
|
|
|
const drawer = new ShareDrawer({ |
|
|
|
|
title, |
|
|
|
|
body: new component({ dashboardRef: dashboard.getRef(), panelRef: panel?.getRef() }), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
dashboard.showModal(drawer); |
|
|
|
|
}, |
|
|
|
|
[dashboard, panel] |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
dashboard.showModal(drawer); |
|
|
|
|
}; |
|
|
|
|
const buildMenuItems = useCallback(() => { |
|
|
|
|
const menuItems: ShareDrawerMenuItem[] = []; |
|
|
|
|
|
|
|
|
|
const onShareExternallyClick = () => { |
|
|
|
|
const drawer = new ShareDrawer({ |
|
|
|
|
title: t('share-dashboard.menu.share-externally-title', 'Share externally'), |
|
|
|
|
body: new ShareExternally({}), |
|
|
|
|
menuItems.push({ |
|
|
|
|
testId: newShareButtonSelector.shareInternally, |
|
|
|
|
icon: 'building', |
|
|
|
|
label: t('share-dashboard.menu.share-internally-title', 'Share internally'), |
|
|
|
|
description: t('share-dashboard.menu.share-internally-description', 'Advanced settings'), |
|
|
|
|
renderCondition: true, |
|
|
|
|
onClick: () => |
|
|
|
|
onMenuItemClick(t('share-dashboard.menu.share-internally-title', 'Share internally'), ShareInternally), |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
dashboard.showModal(drawer); |
|
|
|
|
}; |
|
|
|
|
menuItems.push({ |
|
|
|
|
testId: newShareButtonSelector.shareExternally, |
|
|
|
|
icon: 'share-alt', |
|
|
|
|
label: t('share-dashboard.menu.share-externally-title', 'Share externally'), |
|
|
|
|
renderCondition: !panel && isPublicDashboardsEnabled(), |
|
|
|
|
onClick: () => { |
|
|
|
|
onMenuItemClick(t('share-dashboard.menu.share-externally-title', 'Share externally'), ShareExternally); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
const onShareSnapshotClick = () => { |
|
|
|
|
const drawer = new ShareDrawer({ |
|
|
|
|
title: t('share-dashboard.menu.share-snapshot-title', 'Share snapshot'), |
|
|
|
|
body: new ShareSnapshot({ dashboardRef: dashboard.getRef() }), |
|
|
|
|
customShareDrawerItem.forEach((d) => menuItems.push(d)); |
|
|
|
|
|
|
|
|
|
menuItems.push({ |
|
|
|
|
testId: newShareButtonSelector.shareSnapshot, |
|
|
|
|
icon: 'camera', |
|
|
|
|
label: t('share-dashboard.menu.share-snapshot-title', 'Share snapshot'), |
|
|
|
|
renderCondition: contextSrv.isSignedIn && config.snapshotEnabled && dashboard.canEditDashboard(), |
|
|
|
|
onClick: () => { |
|
|
|
|
onMenuItemClick(t('share-dashboard.menu.share-snapshot-title', 'Share snapshot'), ShareSnapshot); |
|
|
|
|
}, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
dashboard.showModal(drawer); |
|
|
|
|
}; |
|
|
|
|
return menuItems.filter((item) => item.renderCondition); |
|
|
|
|
}, [onMenuItemClick, dashboard, panel]); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<Menu data-testid={newShareButtonSelector.container}> |
|
|
|
|
<Menu.Item |
|
|
|
|
testId={newShareButtonSelector.shareInternally} |
|
|
|
|
label={t('share-dashboard.menu.share-internally-title', 'Share internally')} |
|
|
|
|
description={t('share-dashboard.menu.share-internally-description', 'Advanced settings')} |
|
|
|
|
icon="building" |
|
|
|
|
onClick={onShareInternallyClick} |
|
|
|
|
/> |
|
|
|
|
{isPublicDashboardsEnabled() && ( |
|
|
|
|
<Menu.Item |
|
|
|
|
testId={newShareButtonSelector.shareExternally} |
|
|
|
|
label={t('share-dashboard.menu.share-externally-title', 'Share externally')} |
|
|
|
|
icon="share-alt" |
|
|
|
|
onClick={onShareExternallyClick} |
|
|
|
|
/> |
|
|
|
|
)} |
|
|
|
|
{contextSrv.isSignedIn && config.snapshotEnabled && dashboard.canEditDashboard() && ( |
|
|
|
|
{buildMenuItems().map((item) => ( |
|
|
|
|
<Menu.Item |
|
|
|
|
testId={newShareButtonSelector.shareSnapshot} |
|
|
|
|
label={t('share-dashboard.menu.share-snapshot-title', 'Share snapshot')} |
|
|
|
|
icon="camera" |
|
|
|
|
onClick={onShareSnapshotClick} |
|
|
|
|
key={item.label} |
|
|
|
|
testId={item.testId} |
|
|
|
|
label={item.label} |
|
|
|
|
icon={item.icon} |
|
|
|
|
description={item.description} |
|
|
|
|
onClick={() => item.onClick(dashboard)} |
|
|
|
|
/> |
|
|
|
|
)} |
|
|
|
|
))} |
|
|
|
|
</Menu> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|