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
Convert getExpandedCollectionsById to TypeScript and fix the stale ExpandedCollection type (#77538)
Andrei Chernykh17782894af77
diff --git a/frontend/src/metabase-types/api/collection.ts b/frontend/src/metabase-types/api/collection.tsindex 9fe9a80a40c..67795ee6759 100644--- a/frontend/src/metabase-types/api/collection.ts+++ b/frontend/src/metabase-types/api/collection.ts@@ -87,7 +87,7 @@ export interface Collection {// Assigned on FEoriginalName?: string;- path?: CollectionId[];+ path?: CollectionId[] | null;}export const COLLECTION_ITEM_MODELS = [diff --git a/frontend/src/metabase/common/collections/constants.ts b/frontend/src/metabase/common/collections/constants.tsindex cb50701ba18..3045ec5343f 100644--- a/frontend/src/metabase/common/collections/constants.ts+++ b/frontend/src/metabase/common/collections/constants.ts@@ -1,10 +1,7 @@import { t } from "ttag";-import type { CollectionId } from "metabase-types/api";-export const ROOT_COLLECTION = {- // Unjustified type cast. FIXME- id: "root" as CollectionId,+ id: "root" as const,get name() {return t`Our analytics`;},@@ -26,8 +23,7 @@ export const PERSONAL_COLLECTION = {// fake collection for admins that contains all other user's collectionsexport const PERSONAL_COLLECTIONS = {- // Unjustified type cast. FIXME- id: "personal" as CollectionId,+ id: "personal" as const,get name() {return t`All personal collections`;},diff --git a/frontend/src/metabase/common/collections/getExpandedCollectionsById.js b/frontend/src/metabase/common/collections/getExpandedCollectionsById.tssimilarity index 61%rename from frontend/src/metabase/common/collections/getExpandedCollectionsById.jsrename to frontend/src/metabase/common/collections/getExpandedCollectionsById.tsindex b2dd326e9ce..26fc951b703 100644--- a/frontend/src/metabase/common/collections/getExpandedCollectionsById.js+++ b/frontend/src/metabase/common/collections/getExpandedCollectionsById.ts@@ -1,21 +1,34 @@import { t } from "ttag";import _ from "underscore";+import type { ExpandedCollection } from "metabase/redux/store";+import type { Collection, CollectionId } from "metabase-types/api";+import {PERSONAL_COLLECTION,PERSONAL_COLLECTIONS,ROOT_COLLECTION,} from "./constants";+// the fake root/personal constants carry only display fields; these fill in+// the rest of the Collection contract+const FAKE_COLLECTION_DEFAULTS = {+ description: null,+ can_write: false,+ can_restore: false,+ can_delete: false,+ namespace: null,+} as const satisfies Partial<Collection>;+// given list of collections with { id, name, location } returns a map of ids to// expanded collection objects like { id, name, location, path, children }// including a root collectionfunction getExpandedCollectionsById(- collections,- userPersonalCollectionId,- collectionFilter = () => true,-) {- const collectionsById = {};+ collections: Collection[],+ userPersonalCollectionId: CollectionId | null | undefined,+ collectionFilter: (collection: Collection) => boolean = () => true,+): Record<CollectionId, ExpandedCollection> {+ const collectionsById: Record<CollectionId, ExpandedCollection> = {};const filteredCollections = collections.filter(collectionFilter);for (const c of filteredCollections) {collectionsById[c.id] = {@@ -32,15 +45,17 @@ function getExpandedCollectionsById(}// "Our Analytics"- collectionsById[ROOT_COLLECTION.id] = {- ...ROOT_COLLECTION,- name: collectionsById[ROOT_COLLECTION.id]- ? ROOT_COLLECTION.name- : t`Collections`,- parent: null,- children: [],- ...(collectionsById[ROOT_COLLECTION.id] || {}),- };+ const existingRoot = collectionsById[ROOT_COLLECTION.id];+ const root: ExpandedCollection = existingRoot+ ? { ...FAKE_COLLECTION_DEFAULTS, ...ROOT_COLLECTION, ...existingRoot }+ : {+ ...FAKE_COLLECTION_DEFAULTS,+ ...ROOT_COLLECTION,+ name: t`Collections`,+ parent: null,+ children: [],+ };+ collectionsById[ROOT_COLLECTION.id] = root;// "My personal collection"if (@@ -48,23 +63,23 @@ function getExpandedCollectionsById(!!collectionsById[userPersonalCollectionId]) {const personalCollection = collectionsById[userPersonalCollectionId];- collectionsById[ROOT_COLLECTION.id].children.push({+ root.children.push({+ ...FAKE_COLLECTION_DEFAULTS,...PERSONAL_COLLECTION,id: userPersonalCollectionId,- parent: collectionsById[ROOT_COLLECTION.id],+ parent: root,children: personalCollection?.children || [],});}// "Personal Collections"collectionsById[PERSONAL_COLLECTIONS.id] = {+ ...FAKE_COLLECTION_DEFAULTS,...PERSONAL_COLLECTIONS,- parent: collectionsById[ROOT_COLLECTION.id],+ parent: root,children: [],};- collectionsById[ROOT_COLLECTION.id].children.push(- collectionsById[PERSONAL_COLLECTIONS.id],- );+ root.children.push(collectionsById[PERSONAL_COLLECTIONS.id]);// iterate over original collections so we don't include ROOT_COLLECTION as// a child of itself@@ -72,15 +87,14 @@ function getExpandedCollectionsById(const c = collectionsById[id];// don't add root as parent of itselfif (c.path && c.id !== ROOT_COLLECTION.id) {- let parentId;+ let parentId: CollectionId | undefined;// move personal collections into PERSONAL_COLLECTIONS fake collectionif (c.personal_owner_id != null) {parentId = PERSONAL_COLLECTIONS.id;} else {// Find the closest parent that the user has permissions for- const parentIdIndex = _.findLastIndex(- c.path,- (p) => collectionsById[p],+ const parentIdIndex = c.path.findLastIndex(+ (p) => collectionsById[p] != null,);parentId = parentIdIndex >= 0 ? c.path[parentIdIndex] : undefined;}@@ -101,10 +115,10 @@ function getExpandedCollectionsById(// remove PERSONAL_COLLECTIONS collection if there are none or just one (the user's own)if (collectionsById[PERSONAL_COLLECTIONS.id].children.length <= 1) {- delete collectionsById[PERSONAL_COLLECTIONS.id];- collectionsById[ROOT_COLLECTION.id].children = collectionsById[- ROOT_COLLECTION.id- ].children.filter((c) => c.id !== PERSONAL_COLLECTIONS.id);+ root.children = root.children.filter(+ (c) => c.id !== PERSONAL_COLLECTIONS.id,+ );+ return _.omit(collectionsById, PERSONAL_COLLECTIONS.id);}return collectionsById;diff --git a/frontend/src/metabase/dashboard/components/QuestionPicker/QuestionPicker.tsx b/frontend/src/metabase/dashboard/components/QuestionPicker/QuestionPicker.tsxindex 2793ee63499..88452e0763d 100644--- a/frontend/src/metabase/dashboard/components/QuestionPicker/QuestionPicker.tsx+++ b/frontend/src/metabase/dashboard/components/QuestionPicker/QuestionPicker.tsx@@ -23,7 +23,7 @@ import {} from "metabase/selectors/user";import { Button, Flex, Icon, Input, TextInput } from "metabase/ui";import { SEARCH_DEBOUNCE_DURATION } from "metabase/utils/constants";-import type { Collection, CollectionId } from "metabase-types/api";+import type { CollectionId } from "metabase-types/api";import { QuestionList } from "./QuestionList";import S from "./QuestionPicker.module.css";@@ -43,11 +43,7 @@ export function QuestionPicker({ onSelect }: QuestionPickerProps) {const userPersonalCollectionId = useSelector(getUserPersonalCollectionId);const baseCollectionsById = useMemo(() =>- // Unjustified type cast. FIXME- getExpandedCollectionsById(- allCollectionsList,- userPersonalCollectionId,- ) as Record<CollectionId, Collection>,+ getExpandedCollectionsById(allCollectionsList, userPersonalCollectionId),[allCollectionsList, userPersonalCollectionId],);const getIcon = useGetIcon();diff --git a/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.ts b/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.tsindex f8911cd146b..8fc27c1c50c 100644--- a/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.ts+++ b/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.ts@@ -7,24 +7,14 @@ import getExpandedCollectionsById from "metabase/common/collections/getExpandedCimport { useSetting } from "metabase/common/hooks/use-setting";import { PLUGIN_TENANTS } from "metabase/plugins";import { useSelector } from "metabase/redux";+import type { ExpandedCollection } from "metabase/redux/store";import { getUserPersonalCollectionId } from "metabase/selectors/user";-import type { Collection, CollectionId } from "metabase-types/api";+import type { CollectionId } from "metabase-types/api";-// getExpandedCollectionsById produces path as CollectionId[] at runtime,-// but ExpandedCollection types it as string, so we use a corrected type.-export type ExpandedCollectionNode = Collection & {- path: CollectionId[] | null;- parent: Collection | null;- children: Collection[];- is_personal?: boolean;-};+export const SHARED_TENANT_COLLECTIONS_ROOT_ID: CollectionId =+ "shared-tenant-collections-root";-export const SHARED_TENANT_COLLECTIONS_ROOT_ID =- // Unjustified type cast. FIXME- "shared-tenant-collections-root" as CollectionId;--// Unjustified type cast. FIXME-export const COLLECTIONS_TOP_LEVEL_ID = "collections-top-level" as CollectionId;+export const COLLECTIONS_TOP_LEVEL_ID: CollectionId = "collections-top-level";/*** When tenants are enabled, fetches shared tenant collections and merges them@@ -38,8 +28,8 @@ export const COLLECTIONS_TOP_LEVEL_ID = "collections-top-level" as CollectionId;* └── Shared collection B*/export function useCollectionsWithTenants(- collectionsById: Record<CollectionId, Collection>,-): Record<CollectionId, Collection> {+ collectionsById: Record<CollectionId, ExpandedCollection>,+): Record<CollectionId, ExpandedCollection> {const useTenants = useSetting("use-tenants");const userPersonalCollectionId = useSelector(getUserPersonalCollectionId);const isTenantsActive = useTenants && PLUGIN_TENANTS.isEnabled;@@ -62,7 +52,7 @@ export function useCollectionsWithTenants(const sharedCollectionsById = getExpandedCollectionsById(sharedTenantCollections,userPersonalCollectionId,- ) as Record<CollectionId, ExpandedCollectionNode>;+ );const displayName =PLUGIN_TENANTS.getNamespaceDisplayName(@@ -88,18 +78,15 @@ export function useCollectionsWithTenants(* "Our analytics" and "Shared collections" as siblings.*/export function mergeSharedCollections(- baseCollectionsById: Record<CollectionId, Collection>,- sharedCollectionsById: Record<CollectionId, ExpandedCollectionNode>,+ baseCollectionsById: Record<CollectionId, ExpandedCollection>,+ sharedCollectionsById: Record<CollectionId, ExpandedCollection>,displayName: string,-): Record<CollectionId, Collection> {+): Record<CollectionId, ExpandedCollection> {const sharedRoot = sharedCollectionsById[ROOT_COLLECTION.id];- // Unjustified type cast. FIXME- const rootCollection = baseCollectionsById[- ROOT_COLLECTION.id- ] as ExpandedCollectionNode;+ const rootCollection = baseCollectionsById[ROOT_COLLECTION.id];// Create the top-level "Collections" node that parents both namespaces- const syntheticTopLevel: ExpandedCollectionNode = {+ const syntheticTopLevel: ExpandedCollection = {id: COLLECTIONS_TOP_LEVEL_ID,name: t`Collections`,description: null,@@ -114,7 +101,7 @@ export function mergeSharedCollections(};// Create the shared collections synthetic root as a sibling of Our analytics- const sharedSyntheticRoot: ExpandedCollectionNode = {+ const sharedSyntheticRoot: ExpandedCollection = {id: SHARED_TENANT_COLLECTIONS_ROOT_ID,name: displayName,description: null,@@ -147,7 +134,7 @@ export function mergeSharedCollections(...rootCollection,path: [COLLECTIONS_TOP_LEVEL_ID],parent: syntheticTopLevel,- } as ExpandedCollectionNode;+ };// Merge shared collections with rewritten pathsfor (const [id, collection] of Object.entries(sharedCollectionsById)) {@@ -155,8 +142,7 @@ export function mergeSharedCollections(continue;}- // Unjustified type cast. FIXME- mergedCollectionsById[id as CollectionId] = {+ mergedCollectionsById[id] = {...collection,// Rewrite path: Collections > Shared collections > ...@@ -177,7 +163,7 @@ export function mergeSharedCollections(collection.parent?.id === ROOT_COLLECTION.id? sharedSyntheticRoot: collection.parent,- } as ExpandedCollectionNode;+ };}// Rewrite paths for all base collections (children of Our Analytics / root)@@ -188,15 +174,13 @@ export function mergeSharedCollections(continue; // already rewritten above}- // Unjustified type cast. FIXME- const collectionNode = collection as ExpandedCollectionNode;+ const collectionNode = collection;if (!collectionNode.path) {continue;}- // Unjustified type cast. FIXME- mergedCollectionsById[id as CollectionId] = {+ mergedCollectionsById[id] = {...collectionNode,// Rewrite path: Collections > Our Analytics > ...path: [COLLECTIONS_TOP_LEVEL_ID, ...collectionNode.path],diff --git a/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.unit.spec.ts b/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.unit.spec.tsindex cd5fa367f82..26654bab8c9 100644--- a/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.unit.spec.ts+++ b/frontend/src/metabase/dashboard/components/QuestionPicker/hooks/use-collections-with-tenants.unit.spec.ts@@ -3,6 +3,7 @@ import { setupCollectionTreeEndpoint } from "__support__/server-mocks/collectionimport { mockSettings } from "__support__/settings";import { renderHookWithProviders, waitFor } from "__support__/ui";import { ROOT_COLLECTION } from "metabase/common/collections/constants";+import type { ExpandedCollection } from "metabase/redux/store";import { createMockState } from "metabase/redux/store/mocks";import type { Collection, CollectionId } from "metabase-types/api";import {@@ -12,7 +13,6 @@ import {import {COLLECTIONS_TOP_LEVEL_ID,- type ExpandedCollectionNode,SHARED_TENANT_COLLECTIONS_ROOT_ID,mergeSharedCollections,useCollectionsWithTenants,@@ -20,11 +20,7 @@ import {const createMockExpandedCollection = (overrides: Partial<Collection> & { path?: CollectionId[] | null },-): Collection & {- parent: Collection | null;- path: CollectionId[];- children: Collection[];-} => ({+): ExpandedCollection => ({...createMockCollection(overrides),path: overrides.path ?? [],parent: null,@@ -44,10 +40,9 @@ function setupHook({path: [],});- // Unjustified type cast. FIXME- const collectionsById = {+ const collectionsById: Record<CollectionId, ExpandedCollection> = {[ROOT_COLLECTION.id]: baseRoot,- } as Record<CollectionId, Collection>;+ };return renderHookWithProviders(() => useCollectionsWithTenants(collectionsById),@@ -144,14 +139,13 @@ function setup() {ourAnalyticsSubCollection.children = [ourAnalyticsNestedCollection];ourAnalyticsNestedCollection.parent = ourAnalyticsSubCollection;- // Unjustified type cast. FIXME- const baseCollectionsById = {+ const baseCollectionsById: Record<CollectionId, ExpandedCollection> = {[ROOT_COLLECTION.id]: baseRoot,// Unjustified type cast. FIXME[200 as CollectionId]: ourAnalyticsSubCollection,// Unjustified type cast. FIXME[201 as CollectionId]: ourAnalyticsNestedCollection,- } as Record<CollectionId, Collection>;+ };const sharedRoot = createMockExpandedCollection({...ROOT_COLLECTION,@@ -204,7 +198,7 @@ describe("mergeSharedCollections", () => {// Unjustified type cast. FIXMEconst expanded = collectionsById as Record<CollectionId,- ExpandedCollectionNode+ ExpandedCollection>;const topLevel = expanded[COLLECTIONS_TOP_LEVEL_ID];@@ -248,7 +242,7 @@ describe("mergeSharedCollections", () => {// Unjustified type cast. FIXMEconst expanded = collectionsById as Record<CollectionId,- ExpandedCollectionNode+ ExpandedCollection>;const mergedTenantA = expanded[tenantA.id];diff --git a/frontend/src/metabase/redux/store/collection.ts b/frontend/src/metabase/redux/store/collection.tsindex fcf8e9f301a..e435372d6ba 100644--- a/frontend/src/metabase/redux/store/collection.ts+++ b/frontend/src/metabase/redux/store/collection.ts@@ -1,11 +1,10 @@import type { Collection, CollectionId, IconName } from "metabase-types/api";-// see entities/collections/getExpandedCollectionsById.js-export type ExpandedCollection = Collection & {- path: string;- parent: ExpandedCollection;+// the shape metabase/common/collections/getExpandedCollectionsById builds+export type ExpandedCollection = Omit<Collection, "path" | "children"> & {+ path: CollectionId[] | null;+ parent: ExpandedCollection | null;children: ExpandedCollection[];- is_personal: boolean;};export type CollectionTreeItem = {