The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
import { SignJWT } from "jose";
import { type ComponentType, useMemo } from "react";
// To run initialization side effects like Mantine styles, dayjs plugins, etc
import "embedding-sdk-bundle";
import { ComponentProvider } from "embedding-sdk-bundle/components/public/ComponentProvider";
import { STORYBOOK_METABASE_INSTANCE_URL } from "embedding-sdk-shared/test/storybook-constants";
import { storybookThemes } from "embedding-sdk-shared/test/storybook-themes";
import type { MetabaseAuthConfig } from "embedding-sdk-shared/types/auth-config";
import type { MetabaseTheme } from "metabase/embedding-sdk/theme";
import { USERS } from "../../../../e2e/support/cypress_data";
const METABASE_JWT_SHARED_SECRET =
// Unjustified type cast. FIXME
(window as any).JWT_SHARED_SECRET || "0".repeat(64);
const secret = new TextEncoder().encode(METABASE_JWT_SHARED_SECRET);
/**
* SDK auth config that signs the jwt on the FE
*/
export const getStorybookSdkAuthConfigForUser = (
user: keyof typeof USERS = "normal",
): MetabaseAuthConfig => ({
metabaseInstanceUrl: STORYBOOK_METABASE_INSTANCE_URL,
fetchRequestToken: async () => {
try {
const jwt = await new SignJWT({
email: USERS[user].email,
exp: Math.round(Date.now() / 1000) + 10 * 60, // 10 minute expiration
})
.setProtectedHeader({ alg: "HS256" }) // algorithm
.setIssuedAt()
.setExpirationTime(Math.round(Date.now() / 1000) + 10 * 60) // token expiration time, e.g., "1 day"
.sign(secret);
return { jwt };
} catch (e) {
console.error("Failed to generate JWT", e);
throw new Error(`Failed to generate JWT for storybook: ${e}`);
}
},
});
export const storybookSdkAuthDefaultConfig =
getStorybookSdkAuthConfigForUser("normal");
export const CommonSdkStoryWrapper = (Story: ComponentType, context: any) => {
const sdkTheme = context.globals.sdkTheme;
const globalTheme: MetabaseTheme | undefined = sdkTheme
? storybookThemes[sdkTheme]
: undefined;
// Per-story theme overrides passed via `parameters.sdkThemeOverride`.
// Useful for stories that render their chat UI inside a fixed overlay and
// need SDK popovers to stack above the host app's modal backdrop.
const themeOverride: MetabaseTheme | undefined =
context.parameters?.sdkThemeOverride;
const theme: MetabaseTheme | undefined = themeOverride
? {
...globalTheme,
...themeOverride,
components: {
...globalTheme?.components,
...themeOverride?.components,
},
}
: globalTheme;
const locale = context.globals.locale;
const user = context.globals.user;
const key = `${user}-${locale}`;
const authConfig = useMemo(() => {
return getStorybookSdkAuthConfigForUser(user);
}, [user]);
return (
<ComponentProvider
authConfig={authConfig}
theme={theme}
key={key}
locale={locale}
>
<Story />
</ComponentProvider>
);
};