Skip to content

Commit

Permalink
feat: Support urlParams (#20)
Browse files Browse the repository at this point in the history
* feat: Support urlParams

* 0.1.9
  • Loading branch information
kgabryje committed Mar 21, 2024
1 parent 68feb16 commit 744808c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@preset-sdk/embedded",
"version": "0.1.8",
"version": "0.1.9",
"description": "Frontend SDK for embedding Preset data analytics into your own application",
"access": "public",
"keywords": [
Expand Down
18 changes: 11 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export type UiConfigType = {
visible?: boolean
expanded?: boolean
}
urlParams?: {
[key: string]: any
}
}

export type EmbedDashboardParams = {
Expand Down Expand Up @@ -92,14 +95,15 @@ export async function embedDashboard({
async function mountIframe(): Promise<Switchboard> {
return new Promise(resolve => {
const iframe = document.createElement('iframe');
const dashboardConfig = dashboardUiConfig ? `?uiConfig=${calculateConfig()}` : ""
const dashboardConfigUrlParams = dashboardUiConfig ? {uiConfig: `${calculateConfig()}`} : undefined;
const filterConfig = dashboardUiConfig?.filters || {}
const filterConfigKeys = Object.keys(filterConfig)
const filterConfigUrlParams = filterConfigKeys.length > 0
? "&"
+ filterConfigKeys
.map(key => DASHBOARD_UI_FILTER_CONFIG_URL_PARAM_KEY[key] + '=' + filterConfig[key]).join('&')
: ""
const filterConfigUrlParams = Object.fromEntries(filterConfigKeys.map(
key => [DASHBOARD_UI_FILTER_CONFIG_URL_PARAM_KEY[key], filterConfig[key]]))

// Allow url query parameters from dashboardUiConfig.urlParams to override the ones from filterConfig
const urlParams = {...dashboardConfigUrlParams, ...filterConfigUrlParams, ...dashboardUiConfig?.urlParams}
const urlParamsString = Object.keys(urlParams).length ? '?' + new URLSearchParams(urlParams).toString() : ''

// setup the iframe's sandbox configuration
iframe.sandbox.add("allow-same-origin"); // needed for postMessage to work
Expand All @@ -117,7 +121,7 @@ export async function embedDashboard({
resolve(switchboard);
});

iframe.src = `${supersetDomain}/embedded/${id}${dashboardConfig}${filterConfigUrlParams}`;
iframe.src = `${supersetDomain}/embedded/${id}${urlParamsString}`;
mountPoint?.replaceChildren(iframe);
log('placed the iframe')
});
Expand Down

0 comments on commit 744808c

Please sign in to comment.