import React, { FC, ReactNode } from 'react'; import { connect, ConnectedProps } from 'react-redux'; import { useLocation } from 'react-router-dom'; import { locationUtil, textUtil } from '@grafana/data'; import { locationService } from '@grafana/runtime'; import { ButtonGroup, ModalsController, ToolbarButton, PageToolbar, useForceUpdate } from '@grafana/ui'; import config from 'app/core/config'; import { toggleKioskMode } from 'app/core/navigation/kiosk'; import { DashboardCommentsModal } from 'app/features/dashboard/components/DashboardComments/DashboardCommentsModal'; import { SaveDashboardDrawer } from 'app/features/dashboard/components/SaveDashboard/SaveDashboardDrawer'; import { ShareModal } from 'app/features/dashboard/components/ShareModal'; import { playlistSrv } from 'app/features/playlist/PlaylistSrv'; import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; import { KioskMode } from 'app/types'; import { setStarred } from '../../../../core/reducers/navBarTree'; import { getDashboardSrv } from '../../services/DashboardSrv'; import { DashboardModel } from '../../state'; import { DashNavButton } from './DashNavButton'; import { DashNavTimeControls } from './DashNavTimeControls'; const mapDispatchToProps = { setStarred, updateTimeZoneForSession, }; const connector = connect(null, mapDispatchToProps); export interface OwnProps { dashboard: DashboardModel; isFullscreen: boolean; kioskMode: KioskMode; hideTimePicker: boolean; folderTitle?: string; title: string; onAddPanel: () => void; } interface DashNavButtonModel { show: (props: Props) => boolean; component: FC>; index?: number | 'end'; } const customLeftActions: DashNavButtonModel[] = []; const customRightActions: DashNavButtonModel[] = []; export function addCustomLeftAction(content: DashNavButtonModel) { customLeftActions.push(content); } export function addCustomRightAction(content: DashNavButtonModel) { customRightActions.push(content); } type Props = OwnProps & ConnectedProps; export const DashNav = React.memo((props) => { const forceUpdate = useForceUpdate(); const onStarDashboard = () => { const dashboardSrv = getDashboardSrv(); const { dashboard, setStarred } = props; dashboardSrv.starDashboard(dashboard.id, dashboard.meta.isStarred).then((newState: any) => { setStarred({ id: dashboard.uid, title: dashboard.title, url: dashboard.meta.url ?? '', isStarred: newState }); dashboard.meta.isStarred = newState; forceUpdate(); }); }; const onClose = () => { locationService.partial({ viewPanel: null }); }; const onToggleTVMode = () => { toggleKioskMode(); }; const onOpenSettings = () => { locationService.partial({ editview: 'settings' }); }; const onPlaylistPrev = () => { playlistSrv.prev(); }; const onPlaylistNext = () => { playlistSrv.next(); }; const onPlaylistStop = () => { playlistSrv.stop(); forceUpdate(); }; const addCustomContent = (actions: DashNavButtonModel[], buttons: ReactNode[]) => { actions.map((action, index) => { const Component = action.component; const element = ; typeof action.index === 'number' ? buttons.splice(action.index, 0, element) : buttons.push(element); }); }; const isPlaylistRunning = () => { return playlistSrv.isPlaying; }; const renderLeftActionsButton = () => { const { dashboard, kioskMode } = props; const { canStar, canShare, isStarred } = dashboard.meta; const buttons: ReactNode[] = []; if (kioskMode !== KioskMode.Off || isPlaylistRunning()) { return []; } if (canStar) { let desc = isStarred ? 'Unmark as favorite' : 'Mark as favorite'; buttons.push( ); } if (canShare) { let desc = 'Share dashboard or panel'; buttons.push( {({ showModal, hideModal }) => ( { showModal(ShareModal, { dashboard, onDismiss: hideModal, }); }} /> )} ); } if (dashboard.uid && config.featureToggles.dashboardComments) { buttons.push( {({ showModal, hideModal }) => ( { showModal(DashboardCommentsModal, { dashboard, onDismiss: hideModal, }); }} /> )} ); } addCustomContent(customLeftActions, buttons); return buttons; }; const renderPlaylistControls = () => { return ( Stop playlist ); }; const renderTimeControls = () => { const { dashboard, updateTimeZoneForSession, hideTimePicker } = props; if (hideTimePicker) { return null; } return ( ); }; const renderRightActionsButton = () => { const { dashboard, onAddPanel, isFullscreen, kioskMode } = props; const { canSave, canEdit, showSettings } = dashboard.meta; const { snapshot } = dashboard; const snapshotUrl = snapshot && snapshot.originalUrl; const buttons: ReactNode[] = []; const tvButton = ( ); if (isPlaylistRunning()) { return [renderPlaylistControls(), renderTimeControls()]; } if (kioskMode === KioskMode.TV) { return [renderTimeControls(), tvButton]; } if (canEdit && !isFullscreen) { buttons.push(); } if (canSave && !isFullscreen) { buttons.push( {({ showModal, hideModal }) => ( { showModal(SaveDashboardDrawer, { dashboard, onDismiss: hideModal, }); }} /> )} ); } if (snapshotUrl) { buttons.push( gotoSnapshotOrigin(snapshotUrl)} icon="link" key="button-snapshot" /> ); } if (showSettings) { buttons.push( ); } addCustomContent(customRightActions, buttons); buttons.push(renderTimeControls()); buttons.push(tvButton); return buttons; }; const gotoSnapshotOrigin = (snapshotUrl: string) => { window.location.href = textUtil.sanitizeUrl(snapshotUrl); }; const { isFullscreen, title, folderTitle } = props; // this ensures the component rerenders when the location changes const location = useLocation(); const titleHref = locationUtil.getUrlForPartial(location, { search: 'open' }); const parentHref = locationUtil.getUrlForPartial(location, { search: 'open', folder: 'current' }); const onGoBack = isFullscreen ? onClose : undefined; return ( {renderRightActionsButton()} ); }); DashNav.displayName = 'DashNav'; export default connector(DashNav);