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 { useCallback } from "react";
import type {
CollectionAuthorityLevelConfig,
CollectionInstanceAnaltyicsConfig,
} from "metabase/common/collections/types";
import type { IconData, ObjectWithModel } from "metabase/common/utils/icon";
import { useGetIconBase } from "metabase/hooks/use-icon";
import { type ItemWithCollection, PLUGIN_LIBRARY } from "metabase/plugins";
import type {
Bookmark,
Collection,
CollectionId,
CollectionType,
} from "metabase-types/api";
import {
COLLECTION_TYPES,
INSTANCE_ANALYTICS_COLLECTION,
OFFICIAL_COLLECTION,
REGULAR_COLLECTION,
REMOTE_SYNC_COLLECTION,
} from "./constants";
export function isRegularCollection({
authority_level,
type,
}: Bookmark | Partial<Collection>) {
// Root, personal collections don't have `authority_level`
return (
(!authority_level || authority_level === REGULAR_COLLECTION.type) &&
type !== "instance-analytics"
);
}
export function getCollectionType({
authority_level,
type,
}: Partial<Collection>):
| CollectionAuthorityLevelConfig
| CollectionInstanceAnaltyicsConfig {
return (
COLLECTION_TYPES?.[String(type || authority_level)] ?? REGULAR_COLLECTION
);
}
export function isInstanceAnalyticsCollection(
collection?: Pick<Collection, "type"> | null,
): boolean {
return (
!!collection && getCollectionType(collection).type === "instance-analytics"
);
}
export function isSyncedCollection(
collection: Pick<Collection, "is_remote_synced">,
): boolean {
return collection.is_remote_synced === true;
}
export const useGetIcon = () => {
const getIconBase = useGetIconBase();
return useCallback(
(
item: ObjectWithModel,
{ isTenantUser = false }: { isTenantUser?: boolean } = {},
): IconData => {
const collectionType = getCollectionType({
// Unjustified type cast. FIXME
type: (item.type as CollectionType) || item.collection_type,
}).type;
if (collectionType === "instance-analytics") {
return {
name: INSTANCE_ANALYTICS_COLLECTION.icon,
};
}
if (item.model === "collection") {
// Library collections keep their special icon regardless of sync status
if (PLUGIN_LIBRARY.isLibraryCollectionType(item.type)) {
return getIconBase(item);
}
// tenant users see the normal icon, they don't know what a synced collection is
if (item.is_remote_synced && !isTenantUser) {
return {
name: REMOTE_SYNC_COLLECTION.icon,
};
}
if (
item.authority_level === "official" ||
item.collection_authority_level === "official"
) {
return {
name: OFFICIAL_COLLECTION.icon,
color: OFFICIAL_COLLECTION.color,
tooltip: OFFICIAL_COLLECTION.tooltips?.default,
};
}
}
if (item.model === "dataset" && item.moderated_status === "verified") {
return { name: "model_with_badge" };
}
return getIconBase(item);
},
[getIconBase],
);
};
/** Removes items from the array that belong to the instance analytics collection or one of its children */
export const filterOutItemsFromInstanceAnalytics = <
Item extends ItemWithCollection,
>(
items: Item[],
) => {
/** Cache of ids of instance analytics collections */
const cache = new Set<CollectionId>();
return items.filter((item) => {
if (cache.has(item.collection.id)) {
return false;
}
const ancestors = item.collection.effective_ancestors || [];
const path = [item.collection, ...ancestors];
if (path.some(isInstanceAnalyticsCollection)) {
path.map((c) => c.id).forEach((id) => cache.add(id));
return false;
}
return true;
});
};