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 { collectionApi } from "metabase/api";
import { findCollectionById } from "metabase/common/utils/collections";
import type { State } from "metabase/redux/store";
import type { Collection, CollectionId } from "metabase-types/api";
export const getCollectionFromCollectionsTree = (
state: State,
collectionId: CollectionId,
): Collection | undefined => {
const queriesCache = state[collectionApi.reducerPath]?.queries || {};
let collection: Collection | undefined;
let latestQueryTimestamp = 0;
for (const [queryName, queryState] of Object.entries(queriesCache)) {
if (
queryName.startsWith(collectionApi.endpoints.listCollectionsTree.name) &&
queryState?.status === "fulfilled" &&
queryState?.data
) {
if (queryState.fulfilledTimeStamp < latestQueryTimestamp) {
continue;
}
// Unjustified type cast. FIXME
const collectionsTree = queryState.data as Collection[];
const foundCollection = findCollectionById(collectionsTree, collectionId);
if (foundCollection) {
latestQueryTimestamp = queryState.fulfilledTimeStamp;
collection = foundCollection;
}
}
}
return collection;
};