Newer
Older
github-automation-metabase
committed
import type { ReactNode } from "react";
import _ from "underscore";
Oisin Coveney
committed
import type { SdkPluginsConfig } from "embedding-sdk";
import { InteractiveAdHocQuestion } from "embedding-sdk/components/private/InteractiveAdHocQuestion";
import {
SdkError,
SdkLoader,
} from "embedding-sdk/components/private/PublicComponentWrapper";
import { StyledPublicComponentWrapper } from "embedding-sdk/components/public/InteractiveDashboard/InteractiveDashboard.styled";
import { useCommonDashboardParams } from "embedding-sdk/components/public/InteractiveDashboard/use-common-dashboard-params";
import {
type SdkDashboardDisplayProps,
useSdkDashboardParams,
} from "embedding-sdk/hooks/private/use-sdk-dashboard-params";
import { DASHBOARD_DISPLAY_ACTIONS } from "metabase/dashboard/components/DashboardHeader/DashboardHeaderButtonRow/constants";
import { useEmbedTheme } from "metabase/dashboard/hooks";
import { useEmbedFont } from "metabase/dashboard/hooks/use-embed-font";
import { useValidatedEntityId } from "metabase/lib/entity-id/hooks/use-validated-entity-id";
import { PublicOrEmbeddedDashboard } from "metabase/public/containers/PublicOrEmbeddedDashboard/PublicOrEmbeddedDashboard";
import type { PublicOrEmbeddedDashboardEventHandlersProps } from "metabase/public/containers/PublicOrEmbeddedDashboard/types";
Oisin Coveney
committed
import { InteractiveDashboardProvider } from "./context";
export type InteractiveDashboardProps = {
plugins?: SdkPluginsConfig;
Phoomparin Mano
committed
/**
* A custom React component to render the question layout.
* Use namespaced InteractiveQuestion components to build the layout.
*
* @todo pass the question context to the question view component,
* once we have a public-facing question context.
*/
renderDrillThroughQuestion?: () => ReactNode;
github-automation-metabase
committed
drillThroughQuestionHeight?: number;
} & SdkDashboardDisplayProps &
PublicOrEmbeddedDashboardEventHandlersProps;
const InteractiveDashboardInner = ({
dashboardId,
github-automation-metabase
committed
initialParameters = {},
withTitle = true,
withCardTitle = true,
hiddenParameters = [],
github-automation-metabase
committed
drillThroughQuestionHeight,
Oisin Coveney
committed
plugins,
onLoadWithoutCards,
Denis Berezin
committed
className,
style,
Phoomparin Mano
committed
renderDrillThroughQuestion: AdHocQuestionView,
}: InteractiveDashboardProps) => {
const {
displayOptions,
ref,
isFullscreen,
onFullscreenChange,
refreshPeriod,
onRefreshPeriodChange,
setRefreshElapsedHook,
} = useSdkDashboardParams({
dashboardId,
withDownloads,
withTitle,
hiddenParameters,
github-automation-metabase
committed
initialParameters,
const {
adhocQuestionUrl,
onNavigateBackToDashboard,
onEditQuestion,
onNavigateToNewCardFromDashboard,
} = useCommonDashboardParams({
dashboardId,
const { theme } = useEmbedTheme();
const { font } = useEmbedFont();
Oisin Coveney
committed
<StyledPublicComponentWrapper className={className} style={style} ref={ref}>
Denis Berezin
committed
{adhocQuestionUrl ? (
<InteractiveAdHocQuestion
questionPath={adhocQuestionUrl}
github-automation-metabase
committed
title={withTitle}
github-automation-metabase
committed
height={drillThroughQuestionHeight}
plugins={plugins}
onNavigateBack={onNavigateBackToDashboard}
Phoomparin Mano
committed
>
{AdHocQuestionView && <AdHocQuestionView />}
</InteractiveAdHocQuestion>
Denis Berezin
committed
) : (
Oisin Coveney
committed
<InteractiveDashboardProvider
plugins={plugins}
Oisin Coveney
committed
onEditQuestion={onEditQuestion}
dashboardActions={DASHBOARD_DISPLAY_ACTIONS}
Oisin Coveney
committed
>
<PublicOrEmbeddedDashboard
dashboardId={dashboardId}
github-automation-metabase
committed
parameterQueryParams={initialParameters}
Oisin Coveney
committed
hideParameters={displayOptions.hideParameters}
Mahatthana (Kelvin) Nomsawadi
committed
background={displayOptions.background}
Oisin Coveney
committed
titled={displayOptions.titled}
cardTitled={withCardTitle}
theme={theme}
isFullscreen={isFullscreen}
onFullscreenChange={onFullscreenChange}
refreshPeriod={refreshPeriod}
onRefreshPeriodChange={onRefreshPeriodChange}
setRefreshElapsedHook={setRefreshElapsedHook}
font={font}
bordered={displayOptions.bordered}
navigateToNewCardFromDashboard={onNavigateToNewCardFromDashboard}
onLoad={onLoad}
onLoadWithoutCards={onLoadWithoutCards}
isNightMode={false}
onNightModeChange={_.noop}
hasNightModeToggle={false}
Oisin Coveney
committed
/>
</InteractiveDashboardProvider>
Denis Berezin
committed
)}
</StyledPublicComponentWrapper>
);
};
export const InteractiveDashboard = ({
dashboardId,
...rest
}: InteractiveDashboardProps) => {
const { id, isLoading } = useValidatedEntityId({
type: "dashboard",
id: dashboardId,
});
if (isLoading) {
return <SdkLoader />;
}
if (!id) {
return <SdkError message="ID not found" />;
}
return <InteractiveDashboardInner dashboardId={id} {...rest} />;
};