From e2368e623e3a9f539e674cc3802287395da2c9b6 Mon Sep 17 00:00:00 2001 From: Kamal Galrani Date: Wed, 20 May 2020 13:24:04 +0530 Subject: [PATCH] Dashboard/Links: Fixes dashboard links by tags not working (#24773) * Fixes dashboard links by tags not working * removes code duplication --- .../components/SubMenu/DashboardLinks.tsx | 8 +- .../SubMenu/DashboardLinksDashboard.tsx | 128 ++++++++++++++++++ .../components/SubMenu/DashboardsDropdown.tsx | 71 ---------- 3 files changed, 132 insertions(+), 75 deletions(-) create mode 100644 public/app/features/dashboard/components/SubMenu/DashboardLinksDashboard.tsx delete mode 100644 public/app/features/dashboard/components/SubMenu/DashboardsDropdown.tsx diff --git a/public/app/features/dashboard/components/SubMenu/DashboardLinks.tsx b/public/app/features/dashboard/components/SubMenu/DashboardLinks.tsx index d2f231f503a..1fbea0c8a1b 100644 --- a/public/app/features/dashboard/components/SubMenu/DashboardLinks.tsx +++ b/public/app/features/dashboard/components/SubMenu/DashboardLinks.tsx @@ -1,7 +1,7 @@ import React, { FC } from 'react'; import { Icon, IconName, Tooltip } from '@grafana/ui'; 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 { DashboardModel } from '../../state'; @@ -20,8 +20,8 @@ export const DashboardLinks: FC = ({ dashboard }) => { const linkInfo = getLinkSrv().getAnchorInfo(link); const key = `${link.title}-$${index}`; - if (link.asDropdown) { - return ; + if (link.type === 'dashboards') { + return ; } const linkElement = ( @@ -30,7 +30,7 @@ export const DashboardLinks: FC = ({ dashboard }) => { href={sanitizeUrl(linkInfo.href)} target={link.targetBlank ? '_blank' : '_self'} > - + {sanitize(linkInfo.title)} ); diff --git a/public/app/features/dashboard/components/SubMenu/DashboardLinksDashboard.tsx b/public/app/features/dashboard/components/SubMenu/DashboardLinksDashboard.tsx new file mode 100644 index 00000000000..919a2d60188 --- /dev/null +++ b/public/app/features/dashboard/components/SubMenu/DashboardLinksDashboard.tsx @@ -0,0 +1,128 @@ +import React, { PureComponent } from 'react'; +import { Icon, Tooltip } from '@grafana/ui'; +import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize'; +import { getBackendSrv } from 'app/core/services/backend_srv'; +import { getLinkSrv } from '../../../panel/panellinks/link_srv'; +import { DashboardLink } from '../../state/DashboardModel'; +import { DashboardSearchHit } from 'app/features/search/types'; + +interface Props { + link: DashboardLink; + linkInfo: { title: string; href: string }; + dashboardId: any; +} + +interface State { + searchHits: DashboardSearchHit[]; +} + +export class DashboardLinksDashboard extends PureComponent { + state = { searchHits: [] as DashboardSearchHit[] }; + componentDidMount() { + if (!this.props.link.asDropdown) { + this.onDropDownClick(); + } + } + + onDropDownClick = () => { + const { dashboardId, link } = this.props; + + const limit = 7; + getBackendSrv() + .search({ tag: link.tags, limit }) + .then((dashboards: DashboardSearchHit[]) => { + const processed = dashboards + .filter(dash => dash.id !== dashboardId) + .map(dash => { + return { + ...dash, + url: getLinkSrv().getLinkUrl(dash), + }; + }); + + this.setState({ + searchHits: processed, + }); + }); + }; + + renderElement = (linkElement: JSX.Element) => { + const { link } = this.props; + + if (link.tooltip) { + return ( +
+ {linkElement}; +
+ ); + } else { + return
{linkElement}
; + } + }; + + renderList = () => { + const { link } = this.props; + const { searchHits } = this.state; + + return ( + <> + {searchHits.length > 0 && + searchHits.map((dashboard: any, index: number) => { + const linkElement = ( + + + {sanitize(dashboard.title)} + + ); + return this.renderElement(linkElement); + })} + + ); + }; + + renderDropdown = () => { + const { link, linkInfo } = this.props; + const { searchHits } = this.state; + + const linkElement = ( + <> + + + {linkInfo.title} + + + + ); + + return this.renderElement(linkElement); + }; + + render() { + if (this.props.link.asDropdown) { + return this.renderDropdown(); + } else { + return this.renderList(); + } + } +} diff --git a/public/app/features/dashboard/components/SubMenu/DashboardsDropdown.tsx b/public/app/features/dashboard/components/SubMenu/DashboardsDropdown.tsx deleted file mode 100644 index 11ffa485391..00000000000 --- a/public/app/features/dashboard/components/SubMenu/DashboardsDropdown.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import React, { PureComponent } from 'react'; -import { Icon } from '@grafana/ui'; -import { sanitize, sanitizeUrl } from '@grafana/data/src/text/sanitize'; -import { getBackendSrv } from 'app/core/services/backend_srv'; -import { DashboardSearchHit } from 'app/features/search/types'; -import { getLinkSrv } from '../../../panel/panellinks/link_srv'; -import { DashboardLink } from '../../state/DashboardModel'; - -interface Props { - link: DashboardLink; - linkInfo: { title: string; href: string }; - dashboardId: any; -} - -interface State { - searchHits: DashboardSearchHit[]; -} - -export class DashboardsDropdown extends PureComponent { - state = { searchHits: [] as DashboardSearchHit[] }; - onDropDownClick = async () => { - const { dashboardId, link } = this.props; - - const limit = 7; - const dashboards = await getBackendSrv().search({ tag: link.tags, limit }); - const processed = dashboards - .filter(dash => dash.id !== dashboardId) - .map(dash => { - return { - ...dash, - url: getLinkSrv().getLinkUrl(dash), - }; - }); - - this.setState({ - searchHits: processed, - }); - }; - - render() { - const { link, linkInfo } = this.props; - const { searchHits } = this.state; - - return ( -
- - - {linkInfo.title} - - -
- ); - } -}