Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

6.9 KB
Raw Blame
import { createAction, createReducer } from "@reduxjs/toolkit";

import type { SdkState } from "embedding-sdk-bundle/store/types";
import type { SdkEventHandlersConfig } from "embedding-sdk-bundle/types/events";
import type { MetabasePluginsConfig } from "embedding-sdk-bundle/types/plugins";
import type {
  SdkErrorComponent,
  SdkLoadingError,
} from "embedding-sdk-bundle/types/ui";
import type { SdkUsageProblem } from "embedding-sdk-bundle/types/usage-problem";
import type { MetabaseFetchRequestTokenFn } from "metabase/embedding-sdk/types/refresh-token";
import type { DashboardTabId } from "metabase-types/api";

import { initAuth, refreshTokenAsync } from "./auth";
import {
  clearGuestToken,
  initGuestEmbed,
  refreshGuestSession,
  setGuestTokenFetchError,
  setInitialGuestToken,
} from "./guest-embed";
const SET_IS_GUEST_EMBED = "sdk/SET_IS_GUEST_EMBED";
const SET_METABASE_INSTANCE_VERSION = "sdk/SET_METABASE_INSTANCE_VERSION";
const SET_METABASE_CLIENT_URL = "sdk/SET_METABASE_CLIENT_URL";
const SET_ERROR_COMPONENT = "sdk/SET_ERROR_COMPONENT";
const SET_ERROR = "sdk/SET_ERROR";
const SET_FETCH_REQUEST_TOKEN_FN = "sdk/SET_FETCH_REQUEST_TOKEN_FN";

export const setIsGuestEmbed = createAction<boolean>(SET_IS_GUEST_EMBED);
export const setMetabaseInstanceVersion = createAction<string>(
  SET_METABASE_INSTANCE_VERSION,
);
export const setMetabaseClientUrl = createAction<string>(
  SET_METABASE_CLIENT_URL,
);
export const setErrorComponent = createAction<null | SdkErrorComponent>(
  SET_ERROR_COMPONENT,
);
export const setError = createAction<SdkLoadingError | null>(SET_ERROR);
export const setFetchRefreshTokenFn =
  createAction<null | MetabaseFetchRequestTokenFn>(SET_FETCH_REQUEST_TOKEN_FN);

const SET_PLUGINS = "sdk/SET_PLUGINS";
export const setPlugins = createAction<MetabasePluginsConfig | null>(
  SET_PLUGINS,
);

const SET_EVENT_HANDLERS = "sdk/SET_EVENT_HANDLERS";
export const setEventHandlers = createAction<SdkEventHandlersConfig | null>(
  SET_EVENT_HANDLERS,
);

const SET_USAGE_PROBLEM = "sdk/SET_USAGE_PROBLEM";
export const setUsageProblem = createAction<SdkUsageProblem | null>(
  SET_USAGE_PROBLEM,
);

const SET_PLUGINS_READY = "sdk/SET_PLUGINS_READY";
export const setPluginsReady = createAction<boolean>(SET_PLUGINS_READY);

const SET_SDK_TRACKER_READY = "sdk/SET_SDK_TRACKER_READY";
export const setSdkTrackerReady = createAction<boolean>(SET_SDK_TRACKER_READY);

const SET_INITIAL_DASHBOARD_TAB_ID = "sdk/SET_INITIAL_DASHBOARD_TAB_ID";
export const setInitialDashboardTabId = createAction<DashboardTabId | null>(
  SET_INITIAL_DASHBOARD_TAB_ID,
);

const initialState: SdkState = {
  isGuestEmbed: null,
  metabaseInstanceUrl: "",
  metabaseInstanceVersion: null,
  token: {
    token: null,
    guestTokensByMount: {},
    loading: false,
    error: null,
  },
  initStatus: { status: "uninitialized" },
  error: null,
  plugins: null,
  eventHandlers: null,
  usageProblem: null,
  errorComponent: null,
  fetchRefreshTokenFn: null,
  pluginsReady: false,
  initialDashboardTabId: null,
  sdkTrackerReady: false,
};

export const sdk = createReducer(initialState, (builder) => {
  builder.addCase(refreshTokenAsync.pending, (state) => {
    state.token = { ...state.token, loading: true };
  });

  builder.addCase(refreshTokenAsync.fulfilled, (state, action) => {
    state.token = {
      token: action.payload,
      guestTokensByMount: {},
      loading: false,
      error: null,
    };
  });

  builder.addCase(refreshTokenAsync.rejected, (state, action) => {
    // Unjustified type cast. FIXME
    const error = action.error as Error;
    state.initStatus = { status: "error", error };
  });

  builder.addCase(initAuth.pending, (state) => {
    state.initStatus = { status: "loading" };
  });

  builder.addCase(initAuth.fulfilled, (state) => {
    state.initStatus = { status: "success" };
  });

  builder.addCase(initAuth.rejected, (state, action) => {
    // Unjustified type cast. FIXME
    const error = action.error as Error;
    state.initStatus = { status: "error", error };
  });

  builder.addCase(initGuestEmbed.pending, (state) => {
    state.initStatus = { status: "loading" };
  });

  builder.addCase(initGuestEmbed.fulfilled, (state) => {
    state.initStatus = { status: "success" };
  });

  builder.addCase(initGuestEmbed.rejected, (state, action) => {
    // Unjustified type cast. FIXME
    const error = action.error as Error;
    state.initStatus = { status: "error", error };
  });

  builder.addCase(setIsGuestEmbed, (state, action) => {
    state.isGuestEmbed = action.payload;
  });

  builder.addCase(setPlugins, (state, action) => {
    // At the time of writing, doing `this.state.plugins = action.payload` causes
    // `Type instantiation is excessively deep and possibly infinite.` for
    // this specific action, but it fixes the others.
    return { ...state, plugins: action.payload };
  });

  builder.addCase(setEventHandlers, (state, action) => {
    state.eventHandlers = action.payload;
  });

  builder.addCase(setErrorComponent, (state, action) => {
    state.errorComponent = action.payload;
  });

  builder.addCase(setError, (state, action) => {
    state.error = action.payload;
  });

  builder.addCase(setMetabaseInstanceVersion, (state, action) => {
    state.metabaseInstanceVersion = action.payload;
  });

  builder.addCase(setMetabaseClientUrl, (state, action) => {
    state.metabaseInstanceUrl = action.payload;
  });

  builder.addCase(setFetchRefreshTokenFn, (state, action) => {
    state.fetchRefreshTokenFn = action.payload;
  });

  builder.addCase(setUsageProblem, (state, action) => {
    state.usageProblem = action.payload;
  });

  // Guest embed token management
  builder.addCase(setGuestTokenFetchError, (state, action) => {
    state.token = { ...state.token, loading: false, error: action.payload };
  });

  builder.addCase(setInitialGuestToken, (state, action) => {
    const { mountId, token } = action.payload;
    state.token.guestTokensByMount[mountId] = token;
    state.token.loading = false;
    state.token.error = null;
  });

  builder.addCase(clearGuestToken, (state, action) => {
    delete state.token.guestTokensByMount[action.payload];
  });

  builder.addCase(refreshGuestSession.pending, (state) => {
    state.token = {
      ...state.token,
      loading: true,
    };
  });

  builder.addCase(refreshGuestSession.fulfilled, (state, action) => {
    const { mountId } = action.meta.arg;
    state.token.guestTokensByMount[mountId] = action.payload;
    state.token.loading = false;
    state.token.error = null;
  });

  builder.addCase(refreshGuestSession.rejected, (state, action) => {
    const error = action.error;
    state.token = {
      ...state.token,
      loading: false,
      error,
    };
  });

  builder.addCase(setPluginsReady, (state, action) => {
    state.pluginsReady = action.payload;
  });

  builder.addCase(setInitialDashboardTabId, (state, action) => {
    state.initialDashboardTabId = action.payload;
  });

  builder.addCase(setSdkTrackerReady, (state, action) => {
    state.sdkTrackerReady = action.payload;
  });
});