mirror of https://github.com/grafana/grafana
Dashboards API: v0 k8s dashboards saving (#98695)
* Dashboards API: v0 k8s dashboards saving * Build dashboard url with a slug * fix test * fix test --------- Co-authored-by: Ryan McKinley <ryantxu@gmail.com>pull/98710/head
parent
2992fbf6ef
commit
79d8201b49
@ -1,4 +1,4 @@ |
||||
import { getDashboardUrl } from './urlBuilders'; |
||||
import { getDashboardUrl } from './getDashboardUrl'; |
||||
|
||||
describe('dashboard utils', () => { |
||||
it('Can getUrl', () => { |
@ -0,0 +1,79 @@ |
||||
import { UrlQueryMap, urlUtil } from '@grafana/data'; |
||||
import { config, locationSearchToObject } from '@grafana/runtime'; |
||||
|
||||
export interface DashboardUrlOptions { |
||||
uid?: string; |
||||
slug?: string; |
||||
subPath?: string; |
||||
updateQuery?: UrlQueryMap; |
||||
/** Set to location.search to preserve current params */ |
||||
currentQueryParams: string; |
||||
/** * Returns solo panel route instead */ |
||||
soloRoute?: boolean; |
||||
/** return render url */ |
||||
render?: boolean; |
||||
/** Return an absolute URL */ |
||||
absolute?: boolean; |
||||
// Add tz to query params
|
||||
timeZone?: string; |
||||
// Check if we are on the home dashboard
|
||||
isHomeDashboard?: boolean; |
||||
} |
||||
|
||||
export function getDashboardUrl(options: DashboardUrlOptions) { |
||||
let path = `/d/${options.uid}`; |
||||
|
||||
if (!options.uid) { |
||||
path = '/dashboard/new'; |
||||
} |
||||
|
||||
if (options.soloRoute) { |
||||
path = `/d-solo/${options.uid}`; |
||||
} |
||||
|
||||
if (options.slug) { |
||||
path += `/${options.slug}`; |
||||
} |
||||
|
||||
if (options.subPath) { |
||||
path += options.subPath; |
||||
} |
||||
|
||||
if (options.render) { |
||||
path = '/render' + path; |
||||
|
||||
options.updateQuery = { |
||||
...options.updateQuery, |
||||
width: options.updateQuery?.width || 1000, |
||||
height: options.updateQuery?.height || 500, |
||||
tz: options.timeZone, |
||||
}; |
||||
} |
||||
|
||||
if (options.isHomeDashboard) { |
||||
path = '/'; |
||||
} |
||||
|
||||
const params = options.currentQueryParams ? locationSearchToObject(options.currentQueryParams) : {}; |
||||
|
||||
delete params['shareView']; |
||||
|
||||
if (options.updateQuery) { |
||||
for (const key in options.updateQuery) { |
||||
// removing params with null | undefined
|
||||
if (options.updateQuery[key] === null || options.updateQuery[key] === undefined) { |
||||
delete params[key]; |
||||
} else { |
||||
params[key] = options.updateQuery[key]; |
||||
} |
||||
} |
||||
} |
||||
|
||||
const relativeUrl = urlUtil.renderUrl(path, params); |
||||
|
||||
if (options.absolute) { |
||||
return config.appUrl + relativeUrl.slice(1); |
||||
} |
||||
|
||||
return relativeUrl; |
||||
} |
Loading…
Reference in new issue