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 { jt, t } from "ttag";
import { useGetTableSelectionInfoQuery } from "metabase/api";
import FormCollectionPicker from "metabase/common/collections/containers/FormCollectionPicker";
import { DelayedLoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper/DelayedLoadingAndErrorWrapper";
import { trackDataStudioTablePublished } from "metabase/common/data-studio/analytics";
import { useMetadataToasts } from "metabase/common/hooks";
import {
Form,
FormErrorMessage,
FormProvider,
FormSubmitButton,
} from "metabase/forms";
import { PLUGIN_LIBRARY, type PublishTablesModalProps } from "metabase/plugins";
import { useNavigate } from "metabase/router";
import { Button, FocusTrap, List, Modal, Stack, Text } from "metabase/ui";
import * as Urls from "metabase/urls";
import { usePublishTablesMutation } from "metabase-enterprise/api";
import type {
BulkTableInfo,
BulkTableRequest,
DatabaseId,
SchemaId,
TableId,
} from "metabase-types/api";
export function PublishTablesModal({
databaseIds,
schemaIds,
tableIds,
isOpened,
onPublish,
onClose,
}: PublishTablesModalProps) {
return (
<Modal
title={
<ModalTitle
databaseIds={databaseIds}
schemaIds={schemaIds}
tableIds={tableIds}
/>
}
opened={isOpened}
onClose={onClose}
>
<FocusTrap.InitialFocus />
<ModalBody
databaseIds={databaseIds}
schemaIds={schemaIds}
tableIds={tableIds}
onPublish={onPublish}
onClose={onClose}
/>
</Modal>
);
}
type ModalTitleProps = {
databaseIds: DatabaseId[] | undefined;
schemaIds: SchemaId[] | undefined;
tableIds: TableId[] | undefined;
};
function ModalTitle({ databaseIds, schemaIds, tableIds }: ModalTitleProps) {
const { data } = useGetTableSelectionInfoQuery({
database_ids: databaseIds,
schema_ids: schemaIds,
table_ids: tableIds,
});
if (!data) {
return null;
}
const { selected_table, unpublished_upstream_tables } = data;
return <>{getTitle(selected_table, unpublished_upstream_tables)}</>;
}
type ModalBodyProps = {
databaseIds: DatabaseId[] | undefined;
schemaIds: SchemaId[] | undefined;
tableIds: TableId[] | undefined;
onPublish: () => void;
onClose: () => void;
};
function ModalBody({
databaseIds,
schemaIds,
tableIds,
onPublish,
onClose,
}: ModalBodyProps) {
const { data, isLoading, error } = useGetTableSelectionInfoQuery({
database_ids: databaseIds,
schema_ids: schemaIds,
table_ids: tableIds,
});
const [publishTables] = usePublishTablesMutation();
const { sendSuccessToast } = useMetadataToasts();
const navigate = useNavigate();
const { data: dataCollection, isLoading: isLoadingDataCollection } =
PLUGIN_LIBRARY.useGetLibraryChildCollectionByType({
type: "library-data",
});
if (isLoading || error != null || data == null || dataCollection == null) {
return (
<DelayedLoadingAndErrorWrapper
loading={isLoading || isLoadingDataCollection || dataCollection == null}
error={error}
/>
);
}
const { selected_table, unpublished_upstream_tables } = data;
const handleSubmit = async ({ collection_id }: BulkTableRequest) => {
const { target_collection: collection } = await publishTables({
database_ids: databaseIds,
schema_ids: schemaIds,
table_ids: tableIds,
collection_id,
}).unwrap();
if (collection != null) {
sendSuccessToast(
t`Published`,
() => navigate(Urls.dataStudioLibrary()),
t`Go to ${collection.name}`,
);
} else {
sendSuccessToast(t`Published`);
}
trackDataStudioTablePublished();
onPublish();
};
return (
<FormProvider<BulkTableRequest>
enableReinitialize
initialValues={{ collection_id: dataCollection.id }}
onSubmit={handleSubmit}
>
<Form>
<Stack gap="sm">
<Text>{t`Publishing a table saves it to the semantic layer.`}</Text>
{unpublished_upstream_tables.length > 0 && (
<>
<Text>{getForeignKeyMessage(selected_table)}</Text>
<List spacing="sm">
{unpublished_upstream_tables.map((table) => (
<List.Item key={table.id} fw="bold">
{table.display_name}
</List.Item>
))}
</List>
</>
)}
<FormCollectionPicker
mt="lg"
name="collection_id"
title={t`Publish to`}
entityType="table"
collectionPickerModalProps={{
options: {
hasLibrary: true,
hasRootCollection: false,
hasPersonalCollections: false,
hasSearch: true,
hasRecents: false,
hasConfirmButtons: true,
canCreateCollections: true,
},
}}
/>
</Stack>
<Modal.Footer>
<FormErrorMessage flex={1} />
<Button
variant="subtle"
color="neutral"
onClick={onClose}
>{t`Cancel`}</Button>
<FormSubmitButton
label={getSubmitButtonLabel(
selected_table,
unpublished_upstream_tables,
)}
variant="filled"
/>
</Modal.Footer>
</Form>
</FormProvider>
);
}
function getTitle(
selectedTable: BulkTableInfo | null,
unpublishedRemappedTables: BulkTableInfo[],
) {
if (selectedTable != null) {
return unpublishedRemappedTables.length > 0
? t`Publish ${selectedTable.display_name} and the tables it depends on?`
: t`Publish ${selectedTable.display_name}?`;
}
return unpublishedRemappedTables.length > 0
? t`Publish these tables and the tables they depend on?`
: t`Publish these tables?`;
}
function getForeignKeyMessage(selectedTable: BulkTableInfo | null) {
return selectedTable != null
? jt`Because ${(
<strong key="table">{selectedTable.display_name}</strong>
)} uses foreign keys to display values from other tables, you'll need to publish these, too:`
: t`Because some of the tables you've selected use foreign keys to display values from other tables, you'll need to publish the tables below, too:`;
}
function getSubmitButtonLabel(
selectedTable: BulkTableInfo | null,
unpublishedRemappedTables: BulkTableInfo[],
) {
return selectedTable != null && unpublishedRemappedTables.length === 0
? t`Publish this table`
: t`Publish these tables`;
}