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 { useDisclosure } from "@mantine/hooks";
import { type PropsWithChildren, useMemo } from "react";
import { jt, t } from "ttag";
import { useSdkSelector } from "embedding-sdk-bundle/store";
import { getErrorComponent } from "embedding-sdk-bundle/store/selectors";
import type { SdkErrorComponentProps } from "embedding-sdk-bundle/types";
import { ERROR_DOC_LINKS } from "embedding-sdk-shared/errors";
import type { MetabaseErrorCode } from "embedding-sdk-shared/errors/error-code";
import { EMBEDDING_SDK_PORTAL_ROOT_ELEMENT_ID } from "metabase/embedding-sdk/config";
import {
Alert,
Anchor,
Box,
Center,
Code,
Flex,
Icon,
Portal,
} from "metabase/ui";
export const SdkError = ({
message,
error,
type = "relative",
withCloseButton = false,
}: Omit<SdkErrorComponentProps, "onClose">) => {
const [visible, { close }] = useDisclosure(true);
const CustomError = useSdkSelector(getErrorComponent);
const errorMessage = useMemo(() => {
if (error && "code" in error && typeof error.code === "string") {
// Unjustified type cast. FIXME
const docsLink = ERROR_DOC_LINKS[error.code as MetabaseErrorCode];
if (docsLink) {
return (
<span>
{error.message || message}{" "}
<Anchor
href={docsLink}
target="_blank"
rel="noopener noreferrer"
>{t`Read more.`}</Anchor>
</span>
);
}
}
return message;
}, [message, error]);
if (!visible) {
return null;
}
const handleBannerClose = () => {
close();
};
const ErrorMessageComponent = CustomError || DefaultErrorMessage;
const errorMessageElement = (
<Center h="100%" w="100%" mx="auto" data-testid="sdk-error-container">
<ErrorMessageComponent
type={type}
message={errorMessage}
{...(withCloseButton && {
onClose: handleBannerClose,
})}
/>
</Center>
);
return (
<>
{type === "relative" && errorMessageElement}
{type === "fixed" && (
<SdkPortalErrorWrapper>{errorMessageElement}</SdkPortalErrorWrapper>
)}
</>
);
};
export function SdkPortalErrorWrapper({ children }: PropsWithChildren) {
return (
<Portal target={`#${EMBEDDING_SDK_PORTAL_ROOT_ELEMENT_ID}`}>
<Flex
pos="fixed"
bottom="1rem"
left="1rem"
right="1rem"
style={{ zIndex: 500 }}
align="center"
>
{children}
</Flex>
</Portal>
);
}
const DefaultErrorMessage = ({ message, onClose }: SdkErrorComponentProps) => (
<Box p="sm" maw={600}>
<Alert
color="error"
icon={<Icon name="warning" />}
withCloseButton={Boolean(onClose)}
onClose={onClose}
styles={{
root: { background: "var(--alert-bg)" },
closeButton: {
display: "flex",
alignItems: "center",
justifyContent: "center",
padding: 0,
border: 0,
background: "transparent",
appearance: "none",
cursor: "pointer",
},
}}
>
<Box
style={{
WebkitLineClamp: 2,
WebkitBoxOrient: "vertical",
display: "-webkit-box",
overflow: "hidden",
wordBreak: "break-all",
}}
>
{message}
</Box>
</Alert>
</Box>
);
interface ResourceNotFoundErrorProps {
id: string | number;
}
const ResourceNotFoundError = ({
resource,
id,
}: ResourceNotFoundErrorProps & { resource: string }) => (
<SdkError
message={jt`${resource} ${(
<Code
bg="background_surface-error-subtle"
c="text-secondary"
key="question-id"
>
{id}
</Code>
)} not found. Make sure you pass the correct ID.`}
/>
);
export const QuestionNotFoundError = ({ id }: ResourceNotFoundErrorProps) => (
<ResourceNotFoundError resource={t`Question`} id={id} />
);
export const DashboardNotFoundError = ({ id }: ResourceNotFoundErrorProps) => (
<ResourceNotFoundError resource={t`Dashboard`} id={id} />
);
export const CollectionNotFoundError = ({ id }: ResourceNotFoundErrorProps) => (
<ResourceNotFoundError resource={t`Collection`} id={id} />
);