Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Move `getCollection` selector into `metabase/collections/utils` (#73327)

Romeo Van Snick8d22673fab0a
diff --git a/frontend/src/metabase-types/entities/common.ts b/frontend/src/metabase-types/entities/common.ts
index 31ada552c3d..f92b4eab7e8 100644
--- a/frontend/src/metabase-types/entities/common.ts
+++ b/frontend/src/metabase-types/entities/common.ts
@@ -1,7 +1,6 @@
import type { Collection } from "metabase-types/api";
export type WrappedEntity<Entity> = {
- getCollection: () => Collection;
setCollection: (collection: Collection) => void;
setPinned: (isPinned: boolean) => void;
} & Entity;
diff --git a/frontend/src/metabase/collections/utils.ts b/frontend/src/metabase/collections/utils.ts
index 4e8c5546efc..110097f1db3 100644
--- a/frontend/src/metabase/collections/utils.ts
+++ b/frontend/src/metabase/collections/utils.ts
@@ -5,6 +5,7 @@ import {
canPlaceEntityInCollectionOrDescendants as canPlaceEntityInCollectionOrDescendantsImpl,
getLibraryCollectionType,
} from "metabase/data-studio/utils";
+import { ROOT_COLLECTION } from "metabase/entities/collections";
import { PLUGIN_COLLECTIONS } from "metabase/plugins";
import {
type CardType,
@@ -181,6 +182,24 @@ export function isRootCollection(collection: Pick<Collection, "id">): boolean {
return canonicalCollectionId(collection?.id) === null;
}
+export function normalizedCollection<
+ CollectionType extends Pick<Collection, "id"> = Collection,
+>(collection: CollectionType | null | undefined) {
+ return !collection || isRootCollection(collection)
+ ? ROOT_COLLECTION
+ : collection;
+}
+
+export type ItemWithCollection<CollectionType = Collection> = {
+ collection?: CollectionType;
+};
+
+export function getCollection<
+ CollectionType extends Pick<Collection, "id"> = Collection,
+>(item: ItemWithCollection<CollectionType> | null | undefined) {
+ return normalizedCollection(item?.collection);
+}
+
export function isItemPinned(item: CollectionItem) {
return item.collection_position != null;
}
diff --git a/frontend/src/metabase/collections/utils.unit.spec.ts b/frontend/src/metabase/collections/utils.unit.spec.ts
index 4119524b2b5..8871ee1c993 100644
--- a/frontend/src/metabase/collections/utils.unit.spec.ts
+++ b/frontend/src/metabase/collections/utils.unit.spec.ts
@@ -8,10 +8,14 @@ import {
isRootCollection,
isRootPersonalCollection,
isRootTrashCollection,
+ normalizedCollection,
} from "metabase/collections/utils";
+import { ROOT_COLLECTION } from "metabase/entities/collections/constants";
+import { type Collection, SEARCH_MODELS } from "metabase-types/api";
import {
createMockCollection,
createMockCollectionItem,
+ createMockSearchResult,
} from "metabase-types/api/mocks";
describe("Collections > utils", () => {
@@ -245,4 +249,97 @@ describe("Collections > utils", () => {
expect(pathString).toBe("Home / User / Files / Documents");
});
});
+
+ describe("normalizedCollection", () => {
+ describe("input edge cases", () => {
+ it("returns ROOT_COLLECTION for null", () => {
+ expect(normalizedCollection(null)).toBe(ROOT_COLLECTION);
+ });
+
+ it("returns ROOT_COLLECTION for undefined", () => {
+ expect(normalizedCollection(undefined)).toBe(ROOT_COLLECTION);
+ });
+ });
+
+ describe("root collection forms", () => {
+ it.each([
+ ["id is null", { id: null as never }],
+ ["id is undefined", { id: undefined as never }],
+ ['id is "root"', { id: "root" as const }],
+ ])("returns ROOT_COLLECTION when %s", (_label, override) => {
+ const collection = createMockCollection(override);
+ expect(normalizedCollection(collection)).toBe(ROOT_COLLECTION);
+ });
+ });
+
+ describe("non-root collections", () => {
+ it("returns the collection unchanged for a regular collection", () => {
+ const collection = createMockCollection({ id: 5, name: "Reports" });
+ expect(normalizedCollection(collection)).toBe(collection);
+ });
+
+ it.each([
+ ["personal", { id: 6, personal_owner_id: 1 }],
+ ["trash", { id: 7, type: "trash" as const }],
+ ["library", { id: 8, type: "library" as const }],
+ ["instance-analytics", { id: 9, type: "instance-analytics" as const }],
+ [
+ "official authority level",
+ { id: 10, authority_level: "official" as const },
+ ],
+ ["archived", { id: 11, archived: true }],
+ ["nested (location set)", { id: 12, location: "/3/" }],
+ ])(
+ "returns the collection unchanged for a %s collection",
+ (_label, override) => {
+ const collection = createMockCollection(override);
+ expect(normalizedCollection(collection)).toBe(collection);
+ },
+ );
+
+ it("preserves every field on the input collection", () => {
+ const collection = createMockCollection({
+ id: 42,
+ name: "Special",
+ description: "A description",
+ authority_level: "official",
+ location: "/1/2/",
+ archived: false,
+ });
+ expect(normalizedCollection(collection)).toEqual(collection);
+ });
+ });
+
+ // The helper is invoked from search-result rendering paths
+ // (InfoText, command palette) for every model that can carry a
+ // `collection` field. Exercise each model to guard against a regression
+ // where one model's collection shape is mishandled. Search results carry
+ // CollectionEssentials, so we cast at the boundary the same way the
+ // production call sites do.
+ describe("for every search-result entity type", () => {
+ it.each(SEARCH_MODELS)(
+ "normalizes a non-root collection on a %s result",
+ (model) => {
+ const collection = createMockCollection({ id: 99, name: "Library" });
+ const result = createMockSearchResult({ model, collection });
+ expect(normalizedCollection(result.collection as Collection)).toEqual(
+ collection,
+ );
+ },
+ );
+
+ it.each(SEARCH_MODELS)(
+ "returns ROOT_COLLECTION for a root-collection %s result",
+ (model) => {
+ const result = createMockSearchResult({
+ model,
+ collection: createMockCollection({ id: "root" as const }),
+ });
+ expect(normalizedCollection(result.collection as Collection)).toBe(
+ ROOT_COLLECTION,
+ );
+ },
+ );
+ });
+ });
});
diff --git a/frontend/src/metabase/entities/collections/utils.ts b/frontend/src/metabase/entities/collections/utils.ts
index e4448a9688a..2b5f2ffbf9e 100644
--- a/frontend/src/metabase/entities/collections/utils.ts
+++ b/frontend/src/metabase/entities/collections/utils.ts
@@ -1,5 +1,4 @@
import {
- isRootCollection,
isRootPersonalCollection,
isRootTrashCollection,
isSyncedCollection,
@@ -13,11 +12,7 @@ import { color } from "metabase/ui/colors";
import type { ColorName } from "metabase/ui/colors/types";
import type { Collection, CollectionContentModel } from "metabase-types/api";
-import { PERSONAL_COLLECTIONS, ROOT_COLLECTION } from "./constants";
-
-export function normalizedCollection(collection: Collection) {
- return isRootCollection(collection) ? ROOT_COLLECTION : collection;
-}
+import { PERSONAL_COLLECTIONS } from "./constants";
export function getCollectionIcon(
collection: Partial<Collection>,
diff --git a/frontend/src/metabase/entities/containers/rtk-query/types/entities.ts b/frontend/src/metabase/entities/containers/rtk-query/types/entities.ts
index 820a51a5d1c..23998173f38 100644
--- a/frontend/src/metabase/entities/containers/rtk-query/types/entities.ts
+++ b/frontend/src/metabase/entities/containers/rtk-query/types/entities.ts
@@ -2,7 +2,6 @@ import type { BaseQueryFn, QueryDefinition } from "@reduxjs/toolkit/query";
import type { TagType } from "metabase/api/tags";
import type { Dispatch, State } from "metabase/redux/store";
-import type { Collection } from "metabase-types/api";
import type { UseQuery } from "./rtk";
@@ -93,9 +92,6 @@ export interface EntityDefinition<Entity, EntityWrapper> {
nameOne: string;
normalize: (object: unknown) => { object: unknown };
normalizeList: (list: unknown) => { list: unknown };
- objectSelectors: {
- getCollection: (entity: Entity | EntityWrapper) => Collection | undefined;
- };
rtk: {
getUseGetQuery: (fetchType: FetchType) => {
action?: string;
diff --git a/frontend/src/metabase/entities/dashboards.js b/frontend/src/metabase/entities/dashboards.js
index e18fe371035..0a9eda5124f 100644
--- a/frontend/src/metabase/entities/dashboards.js
+++ b/frontend/src/metabase/entities/dashboards.js
@@ -9,10 +9,7 @@ import {
canonicalCollectionId,
isRootTrashCollection,
} from "metabase/collections/utils";
-import {
- getCollectionType,
- normalizedCollection,
-} from "metabase/entities/collections/utils";
+import { getCollectionType } from "metabase/entities/collections/utils";
import { compose, withAction } from "metabase/redux";
import { addUndo } from "metabase/redux/undo";
@@ -166,11 +163,6 @@ export const Dashboards = createEntity({
return state;
},
- objectSelectors: {
- getCollection: (dashboard) =>
- dashboard && normalizedCollection(dashboard.collection),
- },
-
getAnalyticsMetadata([object], { action }, getState) {
const type = object && getCollectionType(object.collection_id, getState());
return type && `collection=${type}`;
diff --git a/frontend/src/metabase/entities/questions.js b/frontend/src/metabase/entities/questions.js
index 2237e6111c3..b4213813f72 100644
--- a/frontend/src/metabase/entities/questions.js
+++ b/frontend/src/metabase/entities/questions.js
@@ -6,11 +6,7 @@ import {
canonicalCollectionId,
isRootTrashCollection,
} from "metabase/collections/utils";
-import {
- Collections,
- getCollectionType,
- normalizedCollection,
-} from "metabase/entities/collections";
+import { Collections, getCollectionType } from "metabase/entities/collections";
import {
API_UPDATE_QUESTION,
SOFT_RELOAD_CARD,
@@ -148,10 +144,6 @@ export const Questions = createEntity({
},
},
- objectSelectors: {
- getCollection: (card) => card && normalizedCollection(card.collection),
- },
-
reducer: (state = {}, { type, payload, error }) => {
if (type === SOFT_RELOAD_CARD) {
const { id } = payload;
diff --git a/frontend/src/metabase/entities/search.js b/frontend/src/metabase/entities/search.js
index 687bb3396b6..40ae1a84ccd 100644
--- a/frontend/src/metabase/entities/search.js
+++ b/frontend/src/metabase/entities/search.js
@@ -132,16 +132,6 @@ export const Search = createEntity({
},
},
- objectSelectors: {
- getCollection: (object) => {
- const entity = entityForObject(object);
- return entity
- ? (entity?.objectSelectors?.getCollection?.(object) ??
- object?.collection ??
- null)
- : warnEntityAndReturnObject(object);
- },
- },
// delegate to each entity's actionShouldInvalidateLists
actionShouldInvalidateLists(action) {
return (
diff --git a/frontend/src/metabase/entities/tables.js b/frontend/src/metabase/entities/tables.js
index 462cf6aa7d5..2de7ff2a5a9 100644
--- a/frontend/src/metabase/entities/tables.js
+++ b/frontend/src/metabase/entities/tables.js
@@ -347,7 +347,6 @@ export const Tables = createEntity({
return state;
},
- objectSelectors: {},
selectors: {
getObject: (state, { entityId }) => getMetadata(state).table(entityId),
diff --git a/frontend/src/metabase/entities/utils.ts b/frontend/src/metabase/entities/utils.ts
index 46dbbcad3b6..6eb8c26d16b 100644
--- a/frontend/src/metabase/entities/utils.ts
+++ b/frontend/src/metabase/entities/utils.ts
@@ -204,9 +204,6 @@ export type Entity = {
// Selectors
selectors: EntitySelectors;
- // Per-entity object accessors. Each entity defines these with its own object type.
- objectSelectors: Record<string, (object: any, ...args: any[]) => any>;
-
// Reducers
reducer?: EntitiesReducer;
reducers: Record<string, EntitiesReducer | Reducer<Record<string, unknown>>>;
@@ -220,7 +217,7 @@ export type Entity = {
writableProperties?: string[];
// Wrap entity object with bound selectors/actions. The wrapper preserves the
- // input's properties and adds the entity's bound objectSelectors/objectActions
+ // input's properties and adds the entity's bound objectActions
// (e.g. `getName()`, `getIcon()`). Each entity defines its own object methods,
// so we return `any` to allow consumers to use any wrapper-provided method.
wrapEntity: (object: any, dispatch?: AnyDispatch | null) => any;
@@ -276,7 +273,6 @@ type EntityDef = {
createSelectors?: (
defaultSelectors: Partial<EntitySelectors>,
) => Partial<EntitySelectors>;
- objectSelectors?: Partial<Entity["objectSelectors"]>;
// Reducers in entity defs typically destructure `{ type, payload }` from the action
// and accept any payload shape, so we type the action loosely here.
@@ -801,16 +797,6 @@ export function createEntity(def: EntityDef): Entity {
...(def.createSelectors ? def.createSelectors(defaultSelectors) : {}),
} as EntitySelectors;
- entity.objectSelectors = {
- getIcon(_object: EntityObject) {
- return { name: "unknown" };
- },
- getCollection(object: EntityObject) {
- return object.collection;
- },
- ...(def.objectSelectors || {}),
- };
-
// REDUCERS
entity.reducers = {};
@@ -902,17 +888,6 @@ export function createEntity(def: EntityDef): Entity {
this._dispatch = dispatch;
}
}
- // object selectors
- for (const [methodName, method] of Object.entries(entity.objectSelectors)) {
- if (method) {
- EntityWrapper.prototype[methodName] = function (
- this: EntityWrapper,
- ...args: unknown[]
- ) {
- return method(this, ...args);
- };
- }
- }
// object actions
for (const [methodName, method] of Object.entries(entity.objectActions)) {
if (method) {
diff --git a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.tsx b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.tsx
index 7a84650168f..f5cdb069997 100644
--- a/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.tsx
+++ b/frontend/src/metabase/nav/components/search/SearchBar/SearchBar.tsx
@@ -18,7 +18,7 @@ import { RecentsList } from "metabase/nav/components/search/RecentsList";
import { SearchResultsDropdown } from "metabase/nav/components/search/SearchResultsDropdown";
import { zoomInRow } from "metabase/query_builder/actions";
import { useDispatch, useSelector } from "metabase/redux";
-import type { SearchAwareLocation, WrappedResult } from "metabase/search/types";
+import type { SearchAwareLocation } from "metabase/search/types";
import {
getFiltersFromLocation,
getSearchTextFromLocation,
@@ -29,6 +29,7 @@ import { Icon } from "metabase/ui";
import { modelToUrl } from "metabase/urls";
import { isSmallScreen } from "metabase/utils/dom";
import { isWithinIframe } from "metabase/utils/iframe";
+import type { SearchResult } from "metabase-types/api";
import { CommandPaletteTrigger } from "./CommandPaletteTrigger";
import {
@@ -94,7 +95,7 @@ function SearchBarView({ location, onSearchActive, onSearchInactive }: Props) {
}, []);
const onSearchItemSelect = useCallback(
- (result: WrappedResult) => {
+ (result: SearchResult) => {
// if we're already looking at the right model, don't navigate, just update the zoomed in row
const isSameModel = result?.model_id === location?.state?.cardId;
if (isSameModel && result.model === "indexed-entity") {
diff --git a/frontend/src/metabase/nav/components/search/SearchResultsDropdown/SearchResultsDropdown.tsx b/frontend/src/metabase/nav/components/search/SearchResultsDropdown/SearchResultsDropdown.tsx
index eac49bcb409..94680067e1b 100644
--- a/frontend/src/metabase/nav/components/search/SearchResultsDropdown/SearchResultsDropdown.tsx
+++ b/frontend/src/metabase/nav/components/search/SearchResultsDropdown/SearchResultsDropdown.tsx
@@ -2,8 +2,8 @@ import { jt, t } from "ttag";
import type { SearchResultsFooter } from "metabase/nav/components/search/SearchResults";
import { SearchResults } from "metabase/nav/components/search/SearchResults";
-import type { WrappedResult } from "metabase/search/types";
import { Icon, Text, rem } from "metabase/ui";
+import type { SearchResult } from "metabase-types/api";
import {
SearchDropdownFooter,
@@ -13,7 +13,7 @@ import { MIN_RESULTS_FOR_FOOTER_TEXT } from "./constants";
export type SearchResultsDropdownProps = {
searchText: string;
- onSearchItemSelect: (item: WrappedResult) => void;
+ onSearchItemSelect: (item: SearchResult) => void;
goToSearchApp: () => void;
isSearchBar?: boolean;
};
diff --git a/frontend/src/metabase/palette/hooks/useCommandPalette.tsx b/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
index cdaf15ee7fa..5121cb0b428 100644
--- a/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
+++ b/frontend/src/metabase/palette/hooks/useCommandPalette.tsx
@@ -5,6 +5,7 @@ import { useDebounce } from "react-use";
import { jt, t } from "ttag";
import { useListRecentsQuery, useSearchQuery } from "metabase/api";
+import { getCollection } from "metabase/collections/utils";
import { useSetting } from "metabase/common/hooks";
import { getIcon } from "metabase/common/utils/icon";
import { ROOT_COLLECTION } from "metabase/entities/collections/constants";
@@ -352,7 +353,7 @@ export const getSearchResultSubtext = (wrappedSearchResult: any) => {
);
} else {
return (
- <SubtitleText>{wrappedSearchResult.getCollection?.()?.name}</SubtitleText>
+ <SubtitleText>{getCollection(wrappedSearchResult)?.name}</SubtitleText>
);
}
};
diff --git a/frontend/src/metabase/search/components/InfoText/InfoText.tsx b/frontend/src/metabase/search/components/InfoText/InfoText.tsx
index 635d2681bc5..df34390169d 100644
--- a/frontend/src/metabase/search/components/InfoText/InfoText.tsx
+++ b/frontend/src/metabase/search/components/InfoText/InfoText.tsx
@@ -1,11 +1,11 @@
-import type { WrappedResult } from "metabase/search/types";
import { Group } from "metabase/ui";
+import type { SearchResult } from "metabase-types/api";
import { InfoTextAssetLink } from "./InfoTextAssetLink";
import { InfoTextEditedInfo } from "./InfoTextEditedInfo";
type InfoTextProps = {
- result: WrappedResult;
+ result: SearchResult;
isCompact?: boolean;
showLinks?: boolean;
};
diff --git a/frontend/src/metabase/search/components/InfoText/InfoText.unit.spec.tsx b/frontend/src/metabase/search/components/InfoText/InfoText.unit.spec.tsx
index dfa862d3d82..99cb78100a5 100644
--- a/frontend/src/metabase/search/components/InfoText/InfoText.unit.spec.tsx
+++ b/frontend/src/metabase/search/components/InfoText/InfoText.unit.spec.tsx
@@ -8,7 +8,6 @@ import {
setupUsersEndpoints,
} from "__support__/server-mocks";
import { renderWithProviders, screen, waitFor } from "__support__/ui";
-import type { WrappedResult } from "metabase/search/types";
import type { SearchModel, SearchResult } from "metabase-types/api";
import {
createMockCollection,
@@ -77,14 +76,10 @@ async function setup({
});
setupUserRecipientsEndpoint({ users: [MOCK_USER, MOCK_OTHER_USER] });
- const result = createSearchResult({ model, ...resultProps });
-
- const getCollection = jest.fn(() => result.collection);
-
- const wrappedResult: WrappedResult = {
- ...result,
- getCollection,
- };
+ const wrappedResult: SearchResult = createSearchResult({
+ model,
+ ...resultProps,
+ });
renderWithProviders(
<InfoText result={wrappedResult} isCompact={isCompact} />,
@@ -98,10 +93,6 @@ async function setup({
// await waitforAssetLinkLoadingTextToBeRemoved()
await waitForLoadingTextToBeRemoved();
-
- return {
- getCollection,
- };
}
describe("InfoText", () => {
diff --git a/frontend/src/metabase/search/components/InfoText/InfoTextAssetLink.tsx b/frontend/src/metabase/search/components/InfoText/InfoTextAssetLink.tsx
index 53262d14516..97651bdb5fa 100644
--- a/frontend/src/metabase/search/components/InfoText/InfoTextAssetLink.tsx
+++ b/frontend/src/metabase/search/components/InfoText/InfoTextAssetLink.tsx
@@ -2,16 +2,16 @@ import { t } from "ttag";
import { useDatabaseQuery, useTableQuery } from "metabase/common/hooks";
import { SearchResultLink } from "metabase/search/components/SearchResultLink";
-import type { WrappedResult } from "metabase/search/types";
import { Box, Icon, Text } from "metabase/ui";
import { browseDatabase, browseSchema, tableRowsQuery } from "metabase/urls";
import type Database from "metabase-lib/v1/metadata/Database";
+import type { SearchResult } from "metabase-types/api";
import type { InfoTextData } from "./get-info-text";
import { getInfoText } from "./get-info-text";
type InfoTextAssetLinkProps = {
- result: WrappedResult;
+ result: SearchResult;
showLinks?: boolean;
};
diff --git a/frontend/src/metabase/search/components/InfoText/InfoTextEditedInfo.tsx b/frontend/src/metabase/search/components/InfoText/InfoTextEditedInfo.tsx
index 2f3ae38adb1..6d998378df0 100644
--- a/frontend/src/metabase/search/components/InfoText/InfoTextEditedInfo.tsx
+++ b/frontend/src/metabase/search/components/InfoText/InfoTextEditedInfo.tsx
@@ -3,11 +3,10 @@ import { t } from "ttag";
import { isNull } from "underscore";
import { useListUserRecipientsQuery } from "metabase/api";
-import type { WrappedResult } from "metabase/search/types";
import { Text, Tooltip } from "metabase/ui";
import { getRelativeTime } from "metabase/utils/time-dayjs";
import { isNotNull } from "metabase/utils/types";
-import type { UserListResult } from "metabase-types/api";
+import type { SearchResult, UserListResult } from "metabase-types/api";
import {
LastEditedInfoText,
@@ -34,7 +33,7 @@ export const InfoTextEditedInfo = ({
result,
isCompact,
}: {
- result: WrappedResult;
+ result: SearchResult;
isCompact?: boolean;
}) => {
const { isLoading, data, error } = useListUserRecipientsQuery();
diff --git a/frontend/src/metabase/search/components/InfoText/get-info-text.tsx b/frontend/src/metabase/search/components/InfoText/get-info-text.tsx
index fc4dcb94792..b39d4a69559 100644
--- a/frontend/src/metabase/search/components/InfoText/get-info-text.tsx
+++ b/frontend/src/metabase/search/components/InfoText/get-info-text.tsx
@@ -1,16 +1,16 @@
import { t } from "ttag";
+import { getCollection } from "metabase/collections/utils";
import {
PLUGIN_COLLECTIONS,
PLUGIN_COLLECTION_COMPONENTS,
} from "metabase/plugins";
-import type { WrappedResult } from "metabase/search/types";
import { Box, Icon } from "metabase/ui";
import {
collection as collectionUrl,
dashboard as dashboardUrl,
} from "metabase/urls";
-import type { Collection } from "metabase-types/api";
+import type { Collection, SearchResult } from "metabase-types/api";
export type InfoTextData = {
link?: string | null;
@@ -18,7 +18,7 @@ export type InfoTextData = {
label?: string | null;
};
-export const getInfoText = (result: WrappedResult): InfoTextData => {
+export const getInfoText = (result: SearchResult): InfoTextData => {
switch (result.model) {
case "collection":
return getCollectionInfoText(result);
@@ -35,7 +35,7 @@ export const getInfoText = (result: WrappedResult): InfoTextData => {
}
};
-const getCardInfoText = (result: WrappedResult): InfoTextData => {
+const getCardInfoText = (result: SearchResult): InfoTextData => {
if (result.dashboard) {
return {
label: result.dashboard.name,
@@ -51,7 +51,7 @@ const getCardInfoText = (result: WrappedResult): InfoTextData => {
return getCollectionResult(result);
};
-const getActionInfoText = (result: WrappedResult): InfoTextData => {
+const getActionInfoText = (result: SearchResult): InfoTextData => {
return {
label: result.model_name,
};
@@ -61,12 +61,12 @@ const getDatabaseInfoText = (): InfoTextData => {
label: t`Database`,
};
};
-const getCollectionInfoText = (result: WrappedResult): InfoTextData => {
- const collection: Partial<Collection> = result.getCollection();
+const getCollectionInfoText = (result: SearchResult): InfoTextData => {
+ const collection = getCollection(result) ?? {};
if (
PLUGIN_COLLECTIONS.isRegularCollection(collection) ||
- !collection.authority_level
+ !("authority_level" in collection && collection.authority_level)
) {
return {
label: t`Collection`,
@@ -78,20 +78,21 @@ const getCollectionInfoText = (result: WrappedResult): InfoTextData => {
};
};
-const getCollectionResult = (result: WrappedResult): InfoTextData => {
- const collection = result.getCollection();
- const colUrl = collectionUrl(collection);
+const getCollectionResult = (result: SearchResult): InfoTextData => {
+ const collection = getCollection(result);
+ const colUrl = collectionUrl(collection as Collection);
const collectionName = collection.name;
return collectionName
? {
- icon: collection.authority_level ? (
- <Box ml="-1.5px" display="inherit" pos="relative" top="-0.5px">
- <PLUGIN_COLLECTION_COMPONENTS.CollectionAuthorityLevelIcon
- size={12}
- collection={collection}
- />
- </Box>
- ) : null,
+ icon:
+ "authority_level" in collection && collection.authority_level ? (
+ <Box ml="-1.5px" display="inherit" pos="relative" top="-0.5px">
+ <PLUGIN_COLLECTION_COMPONENTS.CollectionAuthorityLevelIcon
+ size={12}
+ collection={collection}
+ />
+ </Box>
+ ) : null,
link: colUrl,
label: collectionName,
}
diff --git a/frontend/src/metabase/search/components/SearchResult/SearchResult.tsx b/frontend/src/metabase/search/components/SearchResult/SearchResult.tsx
index 43310e69033..d3cab8697cc 100644
--- a/frontend/src/metabase/search/components/SearchResult/SearchResult.tsx
+++ b/frontend/src/metabase/search/components/SearchResult/SearchResult.tsx
@@ -6,11 +6,10 @@ import { push } from "react-router-redux";
import { PLUGIN_MODERATION } from "metabase/plugins";
import { useDispatch } from "metabase/redux";
import { trackSearchClick } from "metabase/search/analytics";
-import type { WrappedResult } from "metabase/search/types";
import { Group, Icon, Loader } from "metabase/ui";
import { modelToUrl } from "metabase/urls";
import { isSyncCompleted } from "metabase/utils/syncing";
-import type { SearchContext } from "metabase-types/api";
+import type { SearchContext, SearchResult } from "metabase-types/api";
import { InfoText } from "../InfoText";
@@ -40,10 +39,10 @@ export function SearchResult({
searchRequestId,
searchTerm,
}: {
- result: WrappedResult;
+ result: SearchResult;
compact?: boolean;
showDescription?: boolean;
- onClick?: (result: WrappedResult) => void;
+ onClick?: (result: SearchResult) => void;
isSelected?: boolean;
className?: string;
index: number;
@@ -52,7 +51,7 @@ export function SearchResult({
searchRequestId?: string;
searchTerm?: string;
}) {
- const { name, model, description, moderated_status }: WrappedResult = result;
+ const { name, model, description, moderated_status }: SearchResult = result;
const showXRayButton =
result.model === "indexed-entity" &&
@@ -177,7 +176,7 @@ export function SearchResult({
);
}
-const isItemActive = (result: WrappedResult) => {
+const isItemActive = (result: SearchResult) => {
if (result.model !== "table") {
return true;
}
@@ -185,7 +184,7 @@ const isItemActive = (result: WrappedResult) => {
return isSyncCompleted(result);
};
-const isItemLoading = (result: WrappedResult) => {
+const isItemLoading = (result: SearchResult) => {
if (result.model !== "database" && result.model !== "table") {
return false;
}
diff --git a/frontend/src/metabase/search/components/SearchResult/components/ItemIcon.tsx b/frontend/src/metabase/search/components/SearchResult/components/ItemIcon.tsx
index 0df264a91fc..bdf1ee1c3b9 100644
--- a/frontend/src/metabase/search/components/SearchResult/components/ItemIcon.tsx
+++ b/frontend/src/metabase/search/components/SearchResult/components/ItemIcon.tsx
@@ -1,14 +1,12 @@
-import type { WrappedResult } from "metabase/search/types";
import { Icon } from "metabase/ui";
-import type { RecentItem, SearchModel } from "metabase-types/api";
+import type { RecentItem, SearchModel, SearchResult } from "metabase-types/api";
import { CollectionIcon } from "./CollectionIcon";
import { DefaultIcon } from "./DefaultIcon";
import { IconWrapper } from "./ItemIcon.styled";
-import { isWrappedResult } from "./utils";
export interface IconComponentProps {
- item: WrappedResult | RecentItem;
+ item: SearchResult | RecentItem;
type: SearchModel;
}
@@ -26,7 +24,7 @@ const IconComponent = ({ item, type }: IconComponentProps) => {
interface ItemIconProps {
active: boolean;
- item: WrappedResult | RecentItem;
+ item: SearchResult | RecentItem;
type: SearchModel;
"data-testid"?: string;
}
@@ -37,7 +35,7 @@ export const ItemIcon = ({
type,
"data-testid": dataTestId,
}: ItemIconProps) => {
- const archived = Boolean(isWrappedResult(item) && item.archived);
+ const archived = Boolean("archived" in item && item.archived);
return (
<IconWrapper
diff --git a/frontend/src/metabase/search/components/SearchResult/components/utils.ts b/frontend/src/metabase/search/components/SearchResult/components/utils.ts
deleted file mode 100644
index 19fef81dd73..00000000000
--- a/frontend/src/metabase/search/components/SearchResult/components/utils.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import type { WrappedResult } from "metabase/search/types";
-
-import type { IconComponentProps } from "./ItemIcon";
-
-export const isWrappedResult = (
- item: IconComponentProps["item"],
-): item is WrappedResult => item && "getIcon" in item;
diff --git a/frontend/src/metabase/search/components/SearchResult/tests/SearchResult-Collections.unit.spec.tsx b/frontend/src/metabase/search/components/SearchResult/tests/SearchResult-Collections.unit.spec.tsx
index 280bc90ffda..ab32deed0e7 100644
--- a/frontend/src/metabase/search/components/SearchResult/tests/SearchResult-Collections.unit.spec.tsx
+++ b/frontend/src/metabase/search/components/SearchResult/tests/SearchResult-Collections.unit.spec.tsx
@@ -13,8 +13,10 @@ import {
import { reinitialize } from "metabase/plugins";
import { createMockState } from "metabase/redux/store/mocks";
import { SearchResult } from "metabase/search/components/SearchResult";
-import type { WrappedResult } from "metabase/search/types";
-import type { TokenFeatures } from "metabase-types/api";
+import type {
+ SearchResult as ApiSearchResult,
+ TokenFeatures,
+} from "metabase-types/api";
import {
createMockCollection,
createMockTokenFeatures,
@@ -51,7 +53,7 @@ const officialCollectionResult = createWrappedSearchResult({
interface SetupOpts {
tokenFeatures?: Partial<TokenFeatures>;
enterprisePlugins?: Parameters<typeof setupEnterpriseOnlyPlugin>[0][];
- result: WrappedResult;
+ result: ApiSearchResult;
}
const setup = ({
diff --git a/frontend/src/metabase/search/components/SearchResult/tests/SearchResult.unit.spec.tsx b/frontend/src/metabase/search/components/SearchResult/tests/SearchResult.unit.spec.tsx
index 696188db138..0face3acc10 100644
--- a/frontend/src/metabase/search/components/SearchResult/tests/SearchResult.unit.spec.tsx
+++ b/frontend/src/metabase/search/components/SearchResult/tests/SearchResult.unit.spec.tsx
@@ -9,8 +9,8 @@ import {
import { getIcon, renderWithProviders, screen } from "__support__/ui";
import { SearchResult } from "metabase/search/components/SearchResult/SearchResult";
import { createWrappedSearchResult } from "metabase/search/components/SearchResult/tests/util";
-import type { WrappedResult } from "metabase/search/types";
import { modelToUrl } from "metabase/urls";
+import type { SearchResult as ApiSearchResult } from "metabase-types/api";
import { createMockCollection, createMockUser } from "metabase-types/api/mocks";
const TEST_REGULAR_COLLECTION = createMockCollection({
@@ -38,7 +38,7 @@ const TEST_RESULT_INDEXED_ENTITY = createWrappedSearchResult({
const USER = createMockUser();
-const setup = ({ result }: { result: WrappedResult }) => {
+const setup = ({ result }: { result: ApiSearchResult }) => {
setupCollectionByIdEndpoint({
collections: [TEST_REGULAR_COLLECTION],
});
diff --git a/frontend/src/metabase/search/components/SearchResult/tests/util.ts b/frontend/src/metabase/search/components/SearchResult/tests/util.ts
index df03e416fe4..cd5c01ea851 100644
--- a/frontend/src/metabase/search/components/SearchResult/tests/util.ts
+++ b/frontend/src/metabase/search/components/SearchResult/tests/util.ts
@@ -1,13 +1,6 @@
-import type { WrappedResult } from "metabase/search/types";
+import type { SearchResult } from "metabase-types/api";
import { createMockSearchResult } from "metabase-types/api/mocks";
export const createWrappedSearchResult = (
- options: Partial<WrappedResult>,
-): WrappedResult => {
- const result = createMockSearchResult(options);
-
- return {
- ...result,
- getCollection: options.getCollection ?? (() => result.collection),
- };
-};
+ options: Partial<SearchResult>,
+): SearchResult => createMockSearchResult(options);
diff --git a/frontend/src/metabase/search/containers/SearchResultSection.tsx b/frontend/src/metabase/search/containers/SearchResultSection.tsx
index efc58bfa2a3..bc4510c0016 100644
--- a/frontend/src/metabase/search/containers/SearchResultSection.tsx
+++ b/frontend/src/metabase/search/containers/SearchResultSection.tsx
@@ -1,8 +1,8 @@
import { msgid, ngettext } from "ttag";
import { SearchResult } from "metabase/search/components/SearchResult";
-import type { WrappedResult } from "metabase/search/types";
import { Paper, Stack, Text } from "metabase/ui";
+import type { SearchResult as ApiSearchResult } from "metabase-types/api";
export const SearchResultSection = ({
results,
@@ -13,7 +13,7 @@ export const SearchResultSection = ({
page,
pageSize,
}: {
- results: WrappedResult[];
+ results: ApiSearchResult[];
totalResults: number;
searchEngine?: string;
searchRequestId?: string;
diff --git a/frontend/src/metabase/search/types.ts b/frontend/src/metabase/search/types.ts
index 2940aaf70f4..9b0d2b9379c 100644
--- a/frontend/src/metabase/search/types.ts
+++ b/frontend/src/metabase/search/types.ts
@@ -3,15 +3,7 @@ import type { ComponentType } from "react";
import type { SearchFilterKeys } from "metabase/search/constants";
import type { IconName } from "metabase/ui";
-import type {
- EnabledSearchModel,
- SearchResult,
- UserId,
-} from "metabase-types/api";
-
-export interface WrappedResult extends SearchResult {
- getCollection: () => SearchResult["collection"];
-}
+import type { EnabledSearchModel, UserId } from "metabase-types/api";
export type TypeFilterProps = EnabledSearchModel[];
export type CreatedByFilterProps = UserId[];