Skip to content

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

adding documents to smart links (#62289)

Nick Fitzpatrickafa9b31c4c3f
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx
index 9d9e34203e0..0295bd72e9f 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.tsx
@@ -21,7 +21,7 @@ import {
Text,
UnstyledButton,
} from "metabase/ui";
-import type { RecentItem, SearchResult } from "metabase-types/api";
+import type { SearchResult } from "metabase-types/api";
import {
MenuItemComponent,
@@ -33,13 +33,12 @@ import {
SuggestionPaper,
} from "../../shared/SuggestionPaper";
import { EntitySearchSection } from "../shared/EntitySearchSection";
-import { EMBED_SEARCH_MODELS } from "../shared/constants";
-import { useEntitySearch } from "../shared/useEntitySearch";
+import { EMBED_SEARCH_MODELS, LINK_SEARCH_MODELS } from "../shared/constants";
import { useEntitySuggestions } from "../shared/useEntitySuggestions";
import CommandS from "./CommandSuggestion.module.css";
-interface CommandSuggestionProps {
+export interface CommandSuggestionProps {
items: SearchResult[];
command: (item: CommandItem) => void;
editor: Editor;
@@ -111,30 +110,15 @@ const CommandMenuItem = forwardRef<
);
});
-const CommandSuggestionComponent = forwardRef<
+export const CommandSuggestion = forwardRef<
SuggestionRef,
CommandSuggestionProps
>(function CommandSuggestionComponent({ command, editor, query }, ref) {
const [selectedIndex, setSelectedIndex] = useState(0);
const [showLinkSearch, setShowLinkSearch] = useState(false);
const [showEmbedSearch, setShowEmbedSearch] = useState(false);
- const [pendingLinkMode, setPendingLinkMode] = useState(false);
- const [pendingEmbedMode, setPendingEmbedMode] = useState(false);
const itemRefs = useRef<(HTMLButtonElement | null)[]>([]);
- useEffect(() => {
- if (pendingLinkMode) {
- setShowLinkSearch(true);
- setPendingLinkMode(false);
- }
- if (pendingEmbedMode) {
- setShowEmbedSearch(true);
- setPendingEmbedMode(false);
- }
- }, [pendingLinkMode, pendingEmbedMode]);
-
- const effectiveQuery = query;
-
const allCommandSections: CommandSection[] = useMemo(
() => [
{
@@ -227,39 +211,39 @@ const CommandSuggestionComponent = forwardRef<
const onSelectLinkEntity = useCallback(
(item: { id: number | string; model: string }) => {
- if (showEmbedSearch) {
+ if (showLinkSearch) {
command({
- embedItem: true,
+ selectItem: true,
entityId: item.id,
model: item.model,
});
} else {
command({
- selectItem: true,
+ embedItem: true,
entityId: item.id,
model: item.model,
});
}
},
- [command, showEmbedSearch],
+ [command, showLinkSearch],
);
const executeCommand = (commandName: string) => {
if (commandName === "linkTo") {
- setPendingLinkMode(true);
command({
clearQuery: true,
switchToLinkMode: true,
});
+ setShowLinkSearch(true);
return;
}
if (commandName === "embedQuestion") {
- setPendingEmbedMode(true);
command({
clearQuery: true,
switchToEmbedMode: true,
});
+ setShowEmbedSearch(true);
return;
}
@@ -277,51 +261,25 @@ const CommandSuggestionComponent = forwardRef<
// Use shared entity suggestions for link/embed mode and browse all functionality
const entitySuggestions = useEntitySuggestions({
- query: effectiveQuery,
+ query,
editor,
onSelectEntity: onSelectLinkEntity,
enabled: showLinkSearch || showEmbedSearch || !!query,
- searchModels: showEmbedSearch ? EMBED_SEARCH_MODELS : undefined,
+ searchModels: showLinkSearch ? LINK_SEARCH_MODELS : EMBED_SEARCH_MODELS,
});
const {
- menuItems: linkMenuItems,
- isLoading: isLinkSearchLoading,
+ menuItems: searchMenuItems,
+ isLoading: isSearchLoading,
searchResults,
selectedIndex: entitySelectedIndex,
modal: entityModal,
handlers: entityHandlers,
} = entitySuggestions;
- const { menuItems: searchMenuItems } = useEntitySearch({
- query,
- onSelectRecent: useCallback(
- (item: RecentItem) => {
- command({
- embedItem: true,
- entityId: item.id,
- model: item.model,
- });
- },
- [command],
- ),
- onSelectSearchResult: useCallback(
- (item: SearchResult) => {
- command({
- embedItem: true,
- entityId: item.id,
- model: item.model,
- });
- },
- [command],
- ),
- enabled: !showLinkSearch && !showEmbedSearch && !!query,
- searchModels: EMBED_SEARCH_MODELS,
- });
-
const currentItems = useMemo(() => {
if (showLinkSearch || showEmbedSearch) {
- return linkMenuItems;
+ return searchMenuItems;
}
// When searching in command mode, combine search results with matching commands
@@ -331,18 +289,11 @@ const CommandSuggestionComponent = forwardRef<
}
return commandOptions;
- }, [
- showLinkSearch,
- showEmbedSearch,
- linkMenuItems,
- query,
- searchMenuItems,
- commandOptions,
- ]);
+ }, [showLinkSearch, showEmbedSearch, query, searchMenuItems, commandOptions]);
let totalItems = currentItems.length;
if (showLinkSearch || showEmbedSearch) {
- totalItems = linkMenuItems.length + 1;
+ totalItems = searchMenuItems.length + 1;
} else if (currentItems.length === 0 && query) {
totalItems = 1; // Just the browse all footer
}
@@ -427,7 +378,7 @@ const CommandSuggestionComponent = forwardRef<
},
}));
- if ((showLinkSearch || showEmbedSearch) && isLinkSearchLoading) {
+ if ((showLinkSearch || showEmbedSearch) && isSearchLoading) {
return <LoadingSuggestionPaper aria-label={t`Command Dialog`} />;
}
@@ -435,11 +386,11 @@ const CommandSuggestionComponent = forwardRef<
<SuggestionPaper aria-label={t`Command Dialog`}>
{showLinkSearch || showEmbedSearch ? (
<EntitySearchSection
- menuItems={linkMenuItems}
+ menuItems={searchMenuItems}
selectedIndex={entitySelectedIndex}
onItemSelect={entityHandlers.selectItem}
onFooterClick={entityHandlers.openModal}
- query={effectiveQuery}
+ query={query}
searchResults={searchResults}
modal={entityModal}
onModalSelect={entityHandlers.handleModalSelect}
@@ -545,5 +496,3 @@ const CommandSuggestionComponent = forwardRef<
</SuggestionPaper>
);
});
-
-export const CommandSuggestion = CommandSuggestionComponent;
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.unit.spec.tsx
index 060963e5448..75e2035c664 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/Command/CommandSuggestion.unit.spec.tsx
@@ -1,82 +1,391 @@
+import userEvent from "@testing-library/user-event";
+import type { Editor } from "@tiptap/core";
+import { useState } from "react";
+
+import {
+ setupRecentViewsEndpoints,
+ setupSearchEndpoints,
+} from "__support__/server-mocks";
import { mockSettings } from "__support__/settings";
-import { renderWithProviders, screen } from "__support__/ui";
+import { renderWithProviders, screen, within } from "__support__/ui";
import { PLUGIN_METABOT } from "metabase/plugins";
-import { createMockTokenFeatures } from "metabase-types/api/mocks";
+import { Input } from "metabase/ui";
+import registerVisualizations from "metabase/visualizations/register";
+import { type RecentItem, isRecentTableItem } from "metabase-types/api";
+import {
+ createMockRecentCollectionItem,
+ createMockRecentTableItem,
+ createMockSearchResult,
+ createMockTokenFeatures,
+} from "metabase-types/api/mocks";
+import type { SettingsState } from "metabase-types/store";
import { createMockState } from "metabase-types/store/mocks";
-import { CommandSuggestion } from "./CommandSuggestion";
+import {
+ CommandSuggestion,
+ type CommandSuggestionProps,
+} from "./CommandSuggestion";
-describe("CommandSuggestion", () => {
- const defaultProps = {
- items: [],
- command: jest.fn(),
- editor: {} as any,
- query: "",
- range: { from: 0, to: 0 },
+registerVisualizations();
+
+const SEARCH_ITEMS = [
+ createMockSearchResult({
+ name: "Orders by product",
+ model: "card",
+ display: "bar",
+ id: 1,
+ }),
+ createMockSearchResult({
+ name: "Orders by category",
+ model: "card",
+ display: "pie",
+ id: 2,
+ }),
+ createMockSearchResult({
+ name: "Total accounts",
+ model: "card",
+ display: "scalar",
+ id: 3,
+ }),
+ createMockSearchResult({
+ name: "Orders report",
+ model: "document",
+ id: 4,
+ }),
+ createMockSearchResult({
+ name: "Account quotes",
+ model: "card",
+ id: 5,
+ }),
+];
+
+const RECENT_ITEMS = [
+ createMockRecentCollectionItem({
+ id: 6,
+ name: "Recent Card",
+ model: "card",
+ display: "bar",
+ }),
+ createMockRecentCollectionItem({
+ id: 7,
+ name: "Recent Document",
+ model: "document",
+ }),
+ createMockRecentTableItem({
+ id: 8,
+ display_name: "Recent Table",
+ name: "recent_table",
+ model: "table",
+ }),
+];
+
+const getRecentItemName = (item: RecentItem) =>
+ isRecentTableItem(item) ? (item.display_name ?? item.name) : item.name;
+
+const TestWrapper = (props: CommandSuggestionProps) => {
+ const [query, setQuery] = useState(props.query);
+
+ return (
+ <>
+ <Input
+ value={query}
+ onChange={(e) => setQuery(e.target.value)}
+ aria-label="test-input"
+ />
+ <CommandSuggestion {...props} query={query} />
+ </>
+ );
+};
+
+type SetupProps = {
+ query?: string;
+ settings?: SettingsState;
+};
+
+const setup = ({
+ query = "",
+ settings = mockSettings({}),
+}: SetupProps = {}) => {
+ const command = jest.fn();
+
+ const editor = {
+ commands: {
+ focus: jest.fn(),
+ },
};
- const expectStandardCommandsToBePresent = () => {
- // Main commands
- expect(screen.getByText("Chart")).toBeInTheDocument();
- expect(screen.getByText("Link")).toBeInTheDocument();
-
- // Formatting commands
- expect(screen.getByText("Heading 1")).toBeInTheDocument();
- expect(screen.getByText("Heading 2")).toBeInTheDocument();
- expect(screen.getByText("Heading 3")).toBeInTheDocument();
- expect(screen.getByText("Bullet list")).toBeInTheDocument();
- expect(screen.getByText("Numbered list")).toBeInTheDocument();
- expect(screen.getByText("Quote")).toBeInTheDocument();
- expect(screen.getByText("Code block")).toBeInTheDocument();
+ setupSearchEndpoints(SEARCH_ITEMS);
+ setupRecentViewsEndpoints(RECENT_ITEMS);
+
+ renderWithProviders(
+ <TestWrapper
+ command={command}
+ editor={editor as unknown as Editor}
+ query={query}
+ items={[]}
+ range={{ from: 0, to: 0 }}
+ />,
+ { storeInitialState: createMockState({ settings }) },
+ );
+
+ return {
+ command,
};
+};
- beforeEach(() => {
- jest.clearAllMocks();
+describe("CommandSuggestion", () => {
+ it("renders with default commands", async () => {
+ setup();
+
+ await expectStandardCommandsToBePresent();
});
- describe("when metabot is disabled", () => {
- beforeEach(() => {
- PLUGIN_METABOT.isEnabled = jest.fn(() => false);
+ it("searches for possible card embeds by default", async () => {
+ const { command } = setup({ query: "Ord" });
+
+ // Find cards what were searched for, with apropriate icons
+ expect(
+ within(
+ await screen.findByRole("option", { name: /Orders by product/ }),
+ ).getByRole("img", { name: /bar/ }),
+ ).toBeInTheDocument();
+
+ expect(
+ within(
+ await screen.findByRole("option", { name: /Orders by category/ }),
+ ).getByRole("img", { name: /pie/ }),
+ ).toBeInTheDocument();
+
+ // Should not find things that cannot be embedded as a card
+ expect(
+ screen.queryByRole("option", { name: /Orders report/ }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("option", { name: /Orders by category/ }),
+ );
+
+ // Expect a call with embedItem. This tells us it was a chart embed
+ expect(command).toHaveBeenCalledWith({
+ embedItem: true,
+ entityId: 2,
+ model: "card",
});
+ });
- it("should show all available commands except Metabot", () => {
- const settings = mockSettings({
- "token-features": createMockTokenFeatures({
- metabot_v3: false,
- }),
- });
+ it("supports embedding links", async () => {
+ const { command } = setup();
- renderWithProviders(<CommandSuggestion {...defaultProps} />, {
- storeInitialState: createMockState({
- settings,
- }),
- });
+ await userEvent.click(await screen.findByRole("option", { name: "Link" }));
+
+ await userEvent.type(
+ screen.getByRole("textbox", { name: "test-input" }),
+ "Ord",
+ );
+
+ expect(
+ await screen.findByRole("option", { name: /Orders by product/ }),
+ ).toBeInTheDocument();
+
+ expect(
+ await screen.findByRole("option", { name: /Orders report/ }),
+ ).toBeInTheDocument();
+
+ // Clicking on an option should execute the select item command
+ await userEvent.click(
+ screen.getByRole("option", { name: /Orders report/ }),
+ );
+
+ // Expect a call with embedItem. This tells us it was a chart embed
+ expect(command).toHaveBeenCalledWith({
+ selectItem: true,
+ entityId: 4,
+ model: "document",
+ });
+ });
+
+ it("should include formatting commands for default search, not for Chart search", async () => {
+ const { command } = setup({ query: "Quo" });
+
+ expect(
+ await screen.findByRole("option", { name: /Account quotes/ }),
+ ).toBeInTheDocument();
- expect(screen.queryByText("Ask Metabot")).not.toBeInTheDocument();
- expectStandardCommandsToBePresent();
+ expect(
+ await screen.findByRole("option", { name: "Quote" }),
+ ).toBeInTheDocument();
+
+ await userEvent.clear(screen.getByRole("textbox", { name: "test-input" }));
+
+ await userEvent.click(await screen.findByRole("option", { name: "Chart" }));
+
+ await userEvent.type(
+ screen.getByRole("textbox", { name: "test-input" }),
+ "Quo",
+ );
+
+ expect(
+ await screen.findByRole("option", { name: /Account quotes/ }),
+ ).toBeInTheDocument();
+
+ expect(
+ screen.queryByRole("option", { name: "Quote" }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("option", { name: /Account quotes/ }),
+ );
+
+ expect(command).toHaveBeenCalledWith({
+ embedItem: true,
+ entityId: 5,
+ model: "card",
+ });
+ });
+
+ it("should display recent embeddable items when in embed mode with an empty query", async () => {
+ const { command } = setup({ query: "" });
+
+ await userEvent.click(await screen.findByRole("option", { name: "Chart" }));
+
+ expect(
+ await screen.findByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[0])),
+ }),
+ ).toBeInTheDocument();
+ expect(
+ screen.queryByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[1])),
+ }),
+ ).not.toBeInTheDocument();
+ expect(
+ screen.queryByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[2])),
+ }),
+ ).not.toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[0])),
+ }),
+ );
+
+ expect(command).toHaveBeenCalledWith({
+ embedItem: true,
+ entityId: 6,
+ model: "card",
+ });
+ });
+
+ it("should display recent linkable items when in link mode with an empty query", async () => {
+ const { command } = setup({ query: "" });
+
+ await userEvent.click(await screen.findByRole("option", { name: "Link" }));
+
+ expect(
+ await screen.findByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[0])),
+ }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[1])),
+ }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[2])),
+ }),
+ ).toBeInTheDocument();
+
+ await userEvent.click(
+ screen.getByRole("option", {
+ name: new RegExp(getRecentItemName(RECENT_ITEMS[2])),
+ }),
+ );
+
+ expect(command).toHaveBeenCalledWith({
+ selectItem: true,
+ entityId: 8,
+ model: "table",
});
});
- describe("when metabot is enabled", () => {
+ describe("metabot", () => {
beforeEach(() => {
- PLUGIN_METABOT.isEnabled = jest.fn(() => true);
+ jest.clearAllMocks();
});
- it("should show all available commands including Metabot", () => {
- const settings = mockSettings({
- "token-features": createMockTokenFeatures({
- metabot_v3: true,
- }),
+ describe("when metabot is disabled", () => {
+ beforeEach(() => {
+ PLUGIN_METABOT.isEnabled = jest.fn(() => false);
+ });
+
+ it("should show all available commands except Metabot", async () => {
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures({
+ metabot_v3: false,
+ }),
+ });
+
+ setup({ settings });
+
+ expect(screen.queryByText("Ask Metabot")).not.toBeInTheDocument();
+ await expectStandardCommandsToBePresent();
});
+ });
- renderWithProviders(<CommandSuggestion {...defaultProps} />, {
- storeInitialState: createMockState({
- settings,
- }),
+ describe("when metabot is enabled", () => {
+ beforeEach(() => {
+ PLUGIN_METABOT.isEnabled = jest.fn(() => true);
});
- expect(screen.getByText("Ask Metabot")).toBeInTheDocument();
- expectStandardCommandsToBePresent();
+ it("should show all available commands including Metabot", async () => {
+ const settings = mockSettings({
+ "token-features": createMockTokenFeatures({
+ metabot_v3: true,
+ }),
+ });
+
+ setup({ settings });
+
+ expect(screen.getByText("Ask Metabot")).toBeInTheDocument();
+ await expectStandardCommandsToBePresent();
+ });
});
});
});
+
+const expectStandardCommandsToBePresent = async () => {
+ // Custom Commands
+ expect(
+ await screen.findByRole("option", { name: "Chart" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Link" }),
+ ).toBeInTheDocument();
+
+ // Formatting Commands
+
+ expect(
+ await screen.findByRole("option", { name: "Heading 1" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Heading 2" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Heading 3" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Bullet list" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Numbered list" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Quote" }),
+ ).toBeInTheDocument();
+ expect(
+ await screen.findByRole("option", { name: "Code block" }),
+ ).toBeInTheDocument();
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/SmartLink/SmartLinkNode.tsx b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/SmartLink/SmartLinkNode.tsx
index 92c584f6012..da9dc6300dd 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/SmartLink/SmartLinkNode.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/SmartLink/SmartLinkNode.tsx
@@ -21,6 +21,7 @@ import { useDispatch } from "metabase/lib/redux";
import { type UrlableModel, modelToUrl } from "metabase/lib/urls/modelToUrl";
import { extractEntityId } from "metabase/lib/urls/utils";
import { Icon } from "metabase/ui";
+import { documentApi } from "metabase-enterprise/api";
import { updateMentionsCache } from "metabase-enterprise/documents/documents.slice";
import type {
Card,
@@ -28,13 +29,20 @@ import type {
Collection,
Dashboard,
Database,
+ Document,
SearchModel,
Table,
} from "metabase-types/api";
import styles from "./SmartLinkNode.module.css";
-type SmartLinkEntity = Card | Dashboard | Collection | Table | Database;
+type SmartLinkEntity =
+ | Card
+ | Dashboard
+ | Collection
+ | Table
+ | Database
+ | Document;
// Utility function to parse entity URLs and extract entityId and model
export function parseEntityUrl(
@@ -78,17 +86,22 @@ export function parseEntityUrl(
}
// Match different entity URL patterns
- const patterns = [
- { pattern: /^\/question\/(\d+)/, model: "card" as SearchModel },
- { pattern: /^\/model\/(\d+)/, model: "dataset" as SearchModel },
- { pattern: /^\/dashboard\/(\d+)/, model: "dashboard" as SearchModel },
- { pattern: /^\/collection\/(\d+)/, model: "collection" as SearchModel },
+ const patterns: {
+ pattern: RegExp;
+ model: SearchModel;
+ idIndex?: number;
+ }[] = [
+ { pattern: /^\/question\/(\d+)/, model: "card" },
+ { pattern: /^\/model\/(\d+)/, model: "dataset" },
+ { pattern: /^\/dashboard\/(\d+)/, model: "dashboard" },
+ { pattern: /^\/collection\/(\d+)/, model: "collection" },
{
pattern: /^\/browse\/(\d+)\/table\/(\d+)/,
- model: "table" as SearchModel,
+ model: "table",
idIndex: 2,
},
- { pattern: /^\/browse\/(\d+)/, model: "database" as SearchModel },
+ { pattern: /^\/browse\/(\d+)/, model: "database" },
+ { pattern: /^\/document\/(\d+)/, model: "document" },
];
for (const { pattern, model, idIndex = 1 } of patterns) {
@@ -218,6 +231,15 @@ const useEntityData = (entityId: number | null, model: SearchModel | null) => {
{ skip: !entityId || model !== "database" },
);
+ const documentQuery = documentApi.useGetDocumentQuery(
+ {
+ id: entityId!,
+ },
+ {
+ skip: !entityId || model !== "document",
+ },
+ );
+
// Determine which query is active and return its state
if (model === "card" || model === "dataset") {
return {
@@ -259,6 +281,14 @@ const useEntityData = (entityId: number | null, model: SearchModel | null) => {
};
}
+ if (model === "document") {
+ return {
+ entity: documentQuery.data,
+ isLoading: documentQuery.isLoading,
+ error: documentQuery.error,
+ };
+ }
+
return { entity: null, isLoading: false, error: null };
};
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/constants.ts b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/constants.ts
index f3929ad4709..6ca81588a35 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/constants.ts
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/constants.ts
@@ -8,6 +8,7 @@ export const LINK_SEARCH_MODELS: SearchModel[] = [
"database",
"table",
"collection",
+ "document",
];
export const EMBED_SEARCH_MODELS: SearchModel[] = ["card", "dataset"];
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/suggestionUtils.ts b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/suggestionUtils.ts
index 8ce59a283d5..25965573a23 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/suggestionUtils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/suggestionUtils.ts
@@ -2,7 +2,6 @@ import { getIcon } from "metabase/lib/icon";
import { getName } from "metabase/lib/name";
import type {
Database,
- RecentCollectionItem,
RecentItem,
SearchModel,
SearchResult,
@@ -10,10 +9,8 @@ import type {
import type { MenuItem } from "../../shared/MenuComponents";
-export const isRecentQuestion = (
- item: RecentItem,
-): item is RecentCollectionItem & { model: "card" | "dataset" } =>
- item.model === "card" || item.model === "dataset";
+export const filterRecents = (item: RecentItem, models: SearchModel[]) =>
+ models.includes(item.model);
export function buildSearchMenuItems(
searchResults: SearchResult[],
@@ -35,14 +32,11 @@ export function buildSearchMenuItems(
}
export function buildRecentsMenuItems(
- recents: Array<RecentCollectionItem & { model: "card" | "dataset" }>,
- onSelect: (recent: RecentCollectionItem) => void,
+ recents: Array<RecentItem>,
+ onSelect: (recent: RecentItem) => void,
): MenuItem[] {
return recents.map((recent) => {
- const iconData = getIcon({
- model: recent.model,
- display: recent.display ?? undefined,
- });
+ const iconData = getIcon(recent);
return {
icon: iconData.name,
label: getName(recent),
diff --git a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySearch.ts b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySearch.ts
index 8912eb4c0be..b88c82172f6 100644
--- a/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySearch.ts
+++ b/enterprise/frontend/src/metabase-enterprise/documents/components/Editor/extensions/shared/useEntitySearch.ts
@@ -9,7 +9,7 @@ import { LINK_SEARCH_LIMIT, LINK_SEARCH_MODELS } from "./constants";
import {
buildRecentsMenuItems,
buildSearchMenuItems,
- isRecentQuestion,
+ filterRecents,
} from "./suggestionUtils";
interface UseEntitySearchOptions {
@@ -44,9 +44,11 @@ export function useEntitySearch({
const filteredRecents = useMemo(
() =>
shouldFetchRecents
- ? recents.filter(isRecentQuestion).slice(0, LINK_SEARCH_LIMIT)
+ ? recents
+ .filter((recent) => filterRecents(recent, searchModels))
+ .slice(0, LINK_SEARCH_LIMIT)
: [],
- [recents, shouldFetchRecents],
+ [recents, shouldFetchRecents, searchModels],
);
const { data: searchResponse, isLoading: isSearchLoading } = useSearchQuery(
diff --git a/frontend/src/metabase-types/api/activity.ts b/frontend/src/metabase-types/api/activity.ts
index 1d2085a0991..f642a8f0e13 100644
--- a/frontend/src/metabase-types/api/activity.ts
+++ b/frontend/src/metabase-types/api/activity.ts
@@ -48,7 +48,13 @@ export type RecentTableItem = BaseRecentItem & {
};
export type RecentCollectionItem = BaseRecentItem & {
- model: "collection" | "dashboard" | "card" | "dataset" | "metric";
+ model:
+ | "collection"
+ | "dashboard"
+ | "card"
+ | "dataset"
+ | "metric"
+ | "document";
can_write: boolean;
database_id?: DatabaseId; // for models and questions
parent_collection: {