// Libaries import React, { PureComponent, FC, ReactNode } from 'react'; import { connect, ConnectedProps } from 'react-redux'; // Utils & Services import { playlistSrv } from 'app/features/playlist/PlaylistSrv'; // Components import { DashNavButton } from './DashNavButton'; import { DashNavTimeControls } from './DashNavTimeControls'; import { ButtonGroup, ModalsController, ToolbarButton, PageToolbar } from '@grafana/ui'; import { locationUtil, textUtil } from '@grafana/data'; // State import { updateTimeZoneForSession } from 'app/features/profile/state/reducers'; // Types import { DashboardModel } from '../../state'; import { KioskMode } from 'app/types'; import { ShareModal } from 'app/features/dashboard/components/ShareModal'; import { SaveDashboardModalProxy } from 'app/features/dashboard/components/SaveDashboard/SaveDashboardModalProxy'; import { locationService } from '@grafana/runtime'; import { toggleKioskMode } from 'app/core/navigation/kiosk'; import { getDashboardSrv } from '../../services/DashboardSrv'; const mapDispatchToProps = { 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; class DashNav extends PureComponent { constructor(props: Props) { super(props); } onClose = () => { locationService.partial({ viewPanel: null }); }; onToggleTVMode = () => { toggleKioskMode(); }; onOpenSettings = () => { locationService.partial({ editview: 'settings' }); }; onStarDashboard = () => { const { dashboard } = this.props; const dashboardSrv = getDashboardSrv(); dashboardSrv.starDashboard(dashboard.id, dashboard.meta.isStarred).then((newState: any) => { dashboard.meta.isStarred = newState; this.forceUpdate(); }); }; onPlaylistPrev = () => { playlistSrv.prev(); }; onPlaylistNext = () => { playlistSrv.next(); }; onPlaylistStop = () => { playlistSrv.stop(); this.forceUpdate(); }; 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); }); } isPlaylistRunning() { return playlistSrv.isPlaying; } renderLeftActionsButton() { const { dashboard, kioskMode } = this.props; const { canStar, canShare, isStarred } = dashboard.meta; const buttons: ReactNode[] = []; if (kioskMode !== KioskMode.Off || this.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, }); }} /> )} ); } this.addCustomContent(customLeftActions, buttons); return buttons; } renderPlaylistControls() { return ( Stop playlist ); } renderTimeControls() { const { dashboard, updateTimeZoneForSession, hideTimePicker } = this.props; if (hideTimePicker) { return null; } return ( ); } renderRightActionsButton() { const { dashboard, onAddPanel, isFullscreen, kioskMode } = this.props; const { canEdit, showSettings } = dashboard.meta; const { snapshot } = dashboard; const snapshotUrl = snapshot && snapshot.originalUrl; const buttons: ReactNode[] = []; const tvButton = ( ); if (this.isPlaylistRunning()) { return [this.renderPlaylistControls(), this.renderTimeControls()]; } if (kioskMode === KioskMode.TV) { return [this.renderTimeControls(), tvButton]; } if (canEdit && !isFullscreen) { buttons.push(); buttons.push( {({ showModal, hideModal }) => ( { showModal(SaveDashboardModalProxy, { dashboard, onDismiss: hideModal, }); }} /> )} ); } if (snapshotUrl) { buttons.push( this.gotoSnapshotOrigin(snapshotUrl)} icon="link" key="button-snapshot" /> ); } if (showSettings) { buttons.push( ); } this.addCustomContent(customRightActions, buttons); buttons.push(this.renderTimeControls()); buttons.push(tvButton); return buttons; } gotoSnapshotOrigin(snapshotUrl: string) { window.location.href = textUtil.sanitizeUrl(snapshotUrl); } render() { const { isFullscreen, title, folderTitle } = this.props; const onGoBack = isFullscreen ? this.onClose : undefined; const titleHref = locationUtil.updateSearchParams(window.location.href, '?search=open'); const parentHref = locationUtil.updateSearchParams(window.location.href, '?search=open&folder=current'); return ( {this.renderRightActionsButton()} ); } } export default connector(DashNav);