Commit e2368e62 by Kamal Galrani Committed by GitHub

Dashboard/Links: Fixes dashboard links by tags not working (#24773)

* Fixes dashboard links by tags not working

* removes code duplication
parent 793299af
import React, { FC } from 'react'; import React, { FC } from 'react';
import { Icon, IconName, Tooltip } from '@grafana/ui'; import { Icon, IconName, Tooltip } from '@grafana/ui';
import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize'; import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize';
import { DashboardsDropdown } from './DashboardsDropdown'; import { DashboardLinksDashboard } from './DashboardLinksDashboard';
import { getLinkSrv } from '../../../panel/panellinks/link_srv'; import { getLinkSrv } from '../../../panel/panellinks/link_srv';
import { DashboardModel } from '../../state'; import { DashboardModel } from '../../state';
...@@ -20,8 +20,8 @@ export const DashboardLinks: FC<Props> = ({ dashboard }) => { ...@@ -20,8 +20,8 @@ export const DashboardLinks: FC<Props> = ({ dashboard }) => {
const linkInfo = getLinkSrv().getAnchorInfo(link); const linkInfo = getLinkSrv().getAnchorInfo(link);
const key = `${link.title}-$${index}`; const key = `${link.title}-$${index}`;
if (link.asDropdown) { if (link.type === 'dashboards') {
return <DashboardsDropdown key={key} link={link} linkInfo={linkInfo} dashboardId={dashboard.id} />; return <DashboardLinksDashboard key={key} link={link} linkInfo={linkInfo} dashboardId={dashboard.id} />;
} }
const linkElement = ( const linkElement = (
...@@ -30,7 +30,7 @@ export const DashboardLinks: FC<Props> = ({ dashboard }) => { ...@@ -30,7 +30,7 @@ export const DashboardLinks: FC<Props> = ({ dashboard }) => {
href={sanitizeUrl(linkInfo.href)} href={sanitizeUrl(linkInfo.href)}
target={link.targetBlank ? '_blank' : '_self'} target={link.targetBlank ? '_blank' : '_self'}
> >
<Icon name={iconMap[link.icon] as IconName} /> <Icon name={iconMap[link.icon] as IconName} style={{ marginRight: '4px' }} />
<span>{sanitize(linkInfo.title)}</span> <span>{sanitize(linkInfo.title)}</span>
</a> </a>
); );
......
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { Icon } from '@grafana/ui'; import { Icon, Tooltip } from '@grafana/ui';
import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize'; import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize';
import { getBackendSrv } from 'app/core/services/backend_srv'; import { getBackendSrv } from 'app/core/services/backend_srv';
import { DashboardSearchHit } from 'app/features/search/types';
import { getLinkSrv } from '../../../panel/panellinks/link_srv'; import { getLinkSrv } from '../../../panel/panellinks/link_srv';
import { DashboardLink } from '../../state/DashboardModel'; import { DashboardLink } from '../../state/DashboardModel';
import { DashboardSearchHit } from 'app/features/search/types';
interface Props { interface Props {
link: DashboardLink; link: DashboardLink;
...@@ -16,36 +16,83 @@ interface State { ...@@ -16,36 +16,83 @@ interface State {
searchHits: DashboardSearchHit[]; searchHits: DashboardSearchHit[];
} }
export class DashboardsDropdown extends PureComponent<Props, State> { export class DashboardLinksDashboard extends PureComponent<Props, State> {
state = { searchHits: [] as DashboardSearchHit[] }; state = { searchHits: [] as DashboardSearchHit[] };
onDropDownClick = async () => { componentDidMount() {
if (!this.props.link.asDropdown) {
this.onDropDownClick();
}
}
onDropDownClick = () => {
const { dashboardId, link } = this.props; const { dashboardId, link } = this.props;
const limit = 7; const limit = 7;
const dashboards = await getBackendSrv().search({ tag: link.tags, limit }); getBackendSrv()
const processed = dashboards .search({ tag: link.tags, limit })
.filter(dash => dash.id !== dashboardId) .then((dashboards: DashboardSearchHit[]) => {
.map(dash => { const processed = dashboards
return { .filter(dash => dash.id !== dashboardId)
...dash, .map(dash => {
url: getLinkSrv().getLinkUrl(dash), return {
}; ...dash,
url: getLinkSrv().getLinkUrl(dash),
};
});
this.setState({
searchHits: processed,
});
}); });
};
renderElement = (linkElement: JSX.Element) => {
const { link } = this.props;
this.setState({ if (link.tooltip) {
searchHits: processed, return (
}); <div className="gf-form">
<Tooltip content={link.tooltip}>{linkElement}</Tooltip>;
</div>
);
} else {
return <div className="gf-form">{linkElement}</div>;
}
}; };
render() { renderList = () => {
const { link, linkInfo } = this.props; const { link } = this.props;
const { searchHits } = this.state; const { searchHits } = this.state;
return ( return (
<div className="gf-form"> <>
{searchHits.length > 0 &&
searchHits.map((dashboard: any, index: number) => {
const linkElement = (
<a
key={`${dashboard.id}-${index}`}
className="gf-form-label"
href={sanitizeUrl(dashboard.url)}
target={link.targetBlank ? '_blank' : '_self'}
>
<Icon name="apps" style={{ marginRight: '4px' }} />
<span>{sanitize(dashboard.title)}</span>
</a>
);
return this.renderElement(linkElement);
})}
</>
);
};
renderDropdown = () => {
const { link, linkInfo } = this.props;
const { searchHits } = this.state;
const linkElement = (
<>
<a <a
className="gf-form-label pointer" className="gf-form-label pointer"
target={link.targetBlank ? '_blank' : '_self'}
onClick={this.onDropDownClick} onClick={this.onDropDownClick}
data-placement="bottom" data-placement="bottom"
data-toggle="dropdown" data-toggle="dropdown"
...@@ -54,18 +101,28 @@ export class DashboardsDropdown extends PureComponent<Props, State> { ...@@ -54,18 +101,28 @@ export class DashboardsDropdown extends PureComponent<Props, State> {
<span>{linkInfo.title}</span> <span>{linkInfo.title}</span>
</a> </a>
<ul className="dropdown-menu pull-right" role="menu"> <ul className="dropdown-menu pull-right" role="menu">
{searchHits.length > 1 && {searchHits.length > 0 &&
searchHits.map((dashboard: any, index: number) => { searchHits.map((dashboard: any, index: number) => {
return ( return (
<li key={`${dashboard.id}-${index}`}> <li key={`${dashboard.id}-${index}`}>
<a href={sanitizeUrl(dashboard.url)} target={dashboard.target}> <a href={sanitizeUrl(dashboard.url)} target={link.targetBlank ? '_blank' : '_self'}>
{sanitize(dashboard.title)} {sanitize(dashboard.title)}
</a> </a>
</li> </li>
); );
})} })}
</ul> </ul>
</div> </>
); );
return this.renderElement(linkElement);
};
render() {
if (this.props.link.asDropdown) {
return this.renderDropdown();
} else {
return this.renderList();
}
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment