|
| 1 | +import { css, cx } from '@emotion/css'; |
| 2 | +import React, { FC } from 'react'; |
| 3 | + |
| 4 | +import { DataLink, GrafanaTheme2, PanelData } from '@grafana/data'; |
| 5 | +import { selectors } from '@grafana/e2e-selectors'; |
| 6 | +import { Icon, useStyles2, ClickOutsideWrapper } from '@grafana/ui'; |
| 7 | +import { DashboardModel } from 'app/features/dashboard/state/DashboardModel'; |
| 8 | +import { PanelModel } from 'app/features/dashboard/state/PanelModel'; |
| 9 | +import { getPanelLinksSupplier } from 'app/features/panel/panellinks/linkSuppliers'; |
| 10 | +import { StoreState, useSelector } from 'app/types'; |
| 11 | + |
| 12 | +import PanelHeaderCorner from './PanelHeaderCorner'; |
| 13 | +import { PanelHeaderLoadingIndicator } from './PanelHeaderLoadingIndicator'; |
| 14 | +import { PanelHeaderMenuTrigger } from './PanelHeaderMenuTrigger'; |
| 15 | +import { PanelHeaderMenuWrapper } from './PanelHeaderMenuWrapper'; |
| 16 | +import { PanelHeaderNotices } from './PanelHeaderNotices'; |
| 17 | + |
| 18 | +export interface Props { |
| 19 | + panel: PanelModel; |
| 20 | + dashboard: DashboardModel; |
| 21 | + title?: string; |
| 22 | + description?: string; |
| 23 | + links?: DataLink[]; |
| 24 | + error?: string; |
| 25 | + alertState?: string; |
| 26 | + isViewing: boolean; |
| 27 | + isEditing: boolean; |
| 28 | + data: PanelData; |
| 29 | +} |
| 30 | + |
| 31 | +export const PanelHeader: FC<Props> = ({ panel, error, isViewing, isEditing, data, alertState, dashboard }) => { |
| 32 | + |
| 33 | + const isFnDashboard = useSelector((state: StoreState) => { |
| 34 | + const { |
| 35 | + fnGlobalState: { FNDashboard }, |
| 36 | + } = state; |
| 37 | + |
| 38 | + return FNDashboard; |
| 39 | + }); |
| 40 | + |
| 41 | + const onCancelQuery = () => panel.getQueryRunner().cancelQuery(); |
| 42 | + const title = panel.getDisplayTitle(); |
| 43 | + const className = cx('panel-header', !(isViewing || isEditing) ? 'grid-drag-handle' : ''); |
| 44 | + const styles = useStyles2(panelStyles); |
| 45 | + |
| 46 | + return ( |
| 47 | + <> |
| 48 | + <PanelHeaderLoadingIndicator state={data.state} onClick={onCancelQuery} /> |
| 49 | + <PanelHeaderCorner |
| 50 | + panel={panel} |
| 51 | + title={panel.title} |
| 52 | + description={panel.description} |
| 53 | + scopedVars={panel.scopedVars} |
| 54 | + links={getPanelLinksSupplier(panel)} |
| 55 | + error={error} |
| 56 | + /> |
| 57 | + <div className={[className, isFnDashboard ? styles.fnPanelHeader : ""].filter(Boolean).join(" ")}> |
| 58 | + <PanelHeaderMenuTrigger data-testid={selectors.components.Panels.Panel.title(title)}> |
| 59 | + {({ closeMenu, panelMenuOpen }) => { |
| 60 | + return ( |
| 61 | + <ClickOutsideWrapper onClick={closeMenu} parent={document}> |
| 62 | + <div className="panel-title"> |
| 63 | + <PanelHeaderNotices frames={data.series} panelId={panel.id} /> |
| 64 | + {alertState ? ( |
| 65 | + <Icon |
| 66 | + name={alertState === 'alerting' ? 'heart-break' : 'heart'} |
| 67 | + className="icon-gf panel-alert-icon" |
| 68 | + style={{ marginRight: '4px' }} |
| 69 | + size="sm" |
| 70 | + /> |
| 71 | + ) : null} |
| 72 | + <h2 className={styles.titleText}>{title}</h2> |
| 73 | + {isFnDashboard |
| 74 | + ? null |
| 75 | + : !dashboard.meta.publicDashboardAccessToken && ( |
| 76 | + <div data-testid="panel-dropdown"> |
| 77 | + <Icon name="angle-down" className="panel-menu-toggle" /> |
| 78 | + {panelMenuOpen ? ( |
| 79 | + <PanelHeaderMenuWrapper panel={panel} dashboard={dashboard} onClose={closeMenu} /> |
| 80 | + ) : null} |
| 81 | + </div> |
| 82 | + )} |
| 83 | + {data.request && data.request.timeInfo && ( |
| 84 | + <span className="panel-time-info"> |
| 85 | + <Icon name="clock-nine" size="sm" /> {data.request.timeInfo} |
| 86 | + </span> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </ClickOutsideWrapper> |
| 90 | + ); |
| 91 | + }} |
| 92 | + </PanelHeaderMenuTrigger> |
| 93 | + </div> |
| 94 | + </> |
| 95 | + ); |
| 96 | +}; |
| 97 | + |
| 98 | +const panelStyles = (theme: GrafanaTheme2) => { |
| 99 | + return { |
| 100 | + fnPanelHeader: css` |
| 101 | + &:hover { |
| 102 | + background-color: initial!important; |
| 103 | + cursor: default!important; |
| 104 | + } |
| 105 | + `, |
| 106 | + titleText: css` |
| 107 | + text-overflow: ellipsis; |
| 108 | + overflow: hidden; |
| 109 | + white-space: nowrap; |
| 110 | + max-width: calc(100% - 38px); |
| 111 | + cursor: pointer; |
| 112 | + font-weight: ${theme.typography.fontWeightMedium}; |
| 113 | + font-size: ${theme.typography.body.fontSize}; |
| 114 | + margin: 0; |
| 115 | +
|
| 116 | + &:hover { |
| 117 | + color: ${theme.colors.text.primary}; |
| 118 | + } |
| 119 | + .panel-has-alert & { |
| 120 | + max-width: calc(100% - 54px); |
| 121 | + } |
| 122 | + `, |
| 123 | + }; |
| 124 | +}; |
0 commit comments