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 { useEffect, useRef } from "react";
import { printUsageProblemToConsole } from "embedding-sdk-bundle/lib/print-usage-problem";
import { getSdkUsageProblem } from "embedding-sdk-bundle/lib/usage-problem";
import { useSdkDispatch, useSdkSelector } from "embedding-sdk-bundle/store";
import { setUsageProblem } from "embedding-sdk-bundle/store/reducer";
import {
getHasTokenFeature,
getInitStatus,
getIsGuestEmbedRaw,
getUsageProblem,
} from "embedding-sdk-bundle/store/selectors";
import type { MetabaseAuthConfig } from "embedding-sdk-shared/types/auth-config";
import { EMBEDDING_SDK_CONFIG } from "metabase/embedding-sdk/config";
import type { MetabaseEmbeddingSessionToken } from "metabase/embedding-sdk/types/refresh-token";
import { getSetting, getTokenFeature, useSetting } from "metabase/settings";
export function useSdkUsageProblem({
authConfig,
allowConsoleLog = true,
session,
isLocalHost,
}: {
authConfig: MetabaseAuthConfig;
allowConsoleLog?: boolean;
session: MetabaseEmbeddingSessionToken | null;
isLocalHost?: boolean;
}) {
const isGuestEmbed = useSdkSelector(getIsGuestEmbedRaw);
const hasLoggedRef = useRef(false);
const dispatch = useSdkDispatch();
// When the setting haven't been loaded or failed to query, we assume that the
// feature is _enabled_ first. Otherwise, when a user's instance is temporarily down,
// their customer would see an alarming error message on production.
const isEnabled =
useSetting(EMBEDDING_SDK_CONFIG.enableEmbeddingSettingKey) ?? true;
const hasTokenFeature = useSdkSelector(getHasTokenFeature);
const isDevelopmentMode = useSdkSelector((state) => {
// Assume that we are not in development mode until the setting is loaded
if (!getSetting(state, "token-features")) {
return false;
}
return getTokenFeature(state, "development_mode");
});
// Sync the computed usage problem to the store whenever inputs change.
// This lets other consumers (e.g. SDK components that stop rendering on
// license errors) read the problem from the store.
useEffect(() => {
dispatch(
setUsageProblem(
getSdkUsageProblem({
isGuestEmbed,
authConfig,
hasTokenFeature,
isEnabled,
isDevelopmentMode,
session,
isLocalHost,
}),
),
);
}, [
isGuestEmbed,
authConfig,
hasTokenFeature,
isEnabled,
isDevelopmentMode,
session,
isLocalHost,
dispatch,
]);
// Read the problem from the store rather than from a local useMemo so that
// external dispatches (e.g. the "Hide" button calling setUsageProblem(null))
// are reflected here.
const usageProblem = useSdkSelector(getUsageProblem);
const initStatus = useSdkSelector(getInitStatus);
// Until init finishes, the problem can be wrongly inferred from the assumed
// settings values above, before the real ones load.
const isInitFinished =
initStatus.status === "success" || initStatus.status === "error";
useEffect(() => {
// Log the problem to the console once.
if (
!hasLoggedRef.current &&
allowConsoleLog &&
isInitFinished &&
usageProblem
) {
printUsageProblemToConsole(usageProblem);
hasLoggedRef.current = true;
}
}, [usageProblem, allowConsoleLog, isInitFinished, dispatch]);
return usageProblem;
}