Skip to content

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

Fix: Weird menu state shown when there are no questions in recents (#62985)

github-actions[bot]e370820778f6
diff --git a/e2e/test/scenarios/documents/documents.cy.spec.ts b/e2e/test/scenarios/documents/documents.cy.spec.ts
index 22948d8cb98..b60b9d53f41 100644
--- a/e2e/test/scenarios/documents/documents.cy.spec.ts
+++ b/e2e/test/scenarios/documents/documents.cy.spec.ts
@@ -302,11 +302,11 @@ H.describeWithSnowplowEE("documents", () => {
idAlias: "documentId",
});
- cy.get("@documentId").then((id) => cy.visit(`/document/${id}`));
H.addPostgresDatabase();
});
it("should support typing with a markdown syntax", () => {
+ cy.get("@documentId").then((id) => cy.visit(`/document/${id}`));
H.documentContent().click();
H.addToDocument("# This is a heading level 1");
@@ -460,6 +460,7 @@ H.describeWithSnowplowEE("documents", () => {
dashboard_id: id,
});
});
+ cy.get("@documentId").then((id) => cy.visit(`/document/${id}`));
});
it("should support keyboard and mouse selection in suggestions without double highlight", () => {
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.tsx b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.tsx
index 3be8f35de36..bad08051ed5 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.tsx
@@ -49,18 +49,6 @@ export interface CommandSuggestionProps {
query: string;
}
-// interface CommandItem {
-// command?: string;
-// clearQuery?: boolean;
-// switchToLinkMode?: boolean;
-// switchToEmbedMode?: boolean;
-// selectItem?: boolean;
-// embedItem?: boolean;
-// entityId?: number | string;
-// model?: string;
-// document?: Document | null;
-// }
-
interface SuggestionRef {
onKeyDown: (props: { event: KeyboardEvent }) => boolean;
}
@@ -236,6 +224,24 @@ export const CommandSuggestion = forwardRef<
[command, showLinkSearch, document],
);
+ // Use shared entity suggestions for link/embed mode and browse all functionality
+ const entitySuggestions = useEntitySuggestions({
+ query,
+ editor,
+ onSelectEntity: onSelectLinkEntity,
+ enabled: true,
+ searchModels: showLinkSearch ? LINK_SEARCH_MODELS : EMBED_SEARCH_MODELS,
+ });
+
+ const {
+ menuItems: searchMenuItems,
+ isLoading: isSearchLoading,
+ searchResults,
+ selectedIndex: entitySelectedIndex,
+ modal: entityModal,
+ handlers: entityHandlers,
+ } = entitySuggestions;
+
const executeCommand = (commandName: string) => {
if (commandName === "linkTo") {
command({
@@ -251,6 +257,10 @@ export const CommandSuggestion = forwardRef<
clearQuery: true,
switchToEmbedMode: true,
});
+
+ if (searchMenuItems.length === 0) {
+ entityHandlers.openModal();
+ }
setShowEmbedSearch(true);
return;
}
@@ -268,24 +278,6 @@ export const CommandSuggestion = forwardRef<
});
};
- // Use shared entity suggestions for link/embed mode and browse all functionality
- const entitySuggestions = useEntitySuggestions({
- query,
- editor,
- onSelectEntity: onSelectLinkEntity,
- enabled: showLinkSearch || showEmbedSearch || !!query,
- searchModels: showLinkSearch ? LINK_SEARCH_MODELS : EMBED_SEARCH_MODELS,
- });
-
- const {
- menuItems: searchMenuItems,
- isLoading: isSearchLoading,
- searchResults,
- selectedIndex: entitySelectedIndex,
- modal: entityModal,
- handlers: entityHandlers,
- } = entitySuggestions;
-
const currentItems = useMemo(() => {
if (showLinkSearch || showEmbedSearch) {
return searchMenuItems;
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.unit.spec.tsx
index 609c582003a..66adcc26346 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/Command/CommandSuggestion.unit.spec.tsx
@@ -316,6 +316,77 @@ describe("CommandSuggestion", () => {
});
});
+ it("should auto-open the Browse all modal when clicking Chart with no recent items", async () => {
+ // Setup with empty recent items to reproduce the bug
+ setupSearchEndpoints(SEARCH_ITEMS);
+ setupRecentViewsEndpoints([]);
+
+ const command = jest.fn();
+ const editor = {
+ commands: {
+ focus: jest.fn(),
+ },
+ };
+
+ renderWithProviders(
+ <TestWrapper
+ command={command}
+ editor={editor as unknown as Editor}
+ query=""
+ items={[]}
+ range={{ from: 0, to: 0 }}
+ />,
+ { storeInitialState: createMockState({ settings: mockSettings({}) }) },
+ );
+
+ await userEvent.click(await screen.findByRole("option", { name: "Chart" }));
+
+ // The modal should auto-open, so we should see it
+ expect(await screen.findByRole("dialog")).toBeInTheDocument();
+ });
+
+ it("should not show redundant divider when no recent items exist", async () => {
+ // Setup with empty recent items to reproduce the bug
+ setupSearchEndpoints(SEARCH_ITEMS);
+ setupRecentViewsEndpoints([]);
+
+ const command = jest.fn();
+ const editor = {
+ commands: {
+ focus: jest.fn(),
+ },
+ };
+
+ renderWithProviders(
+ <TestWrapper
+ command={command}
+ editor={editor as unknown as Editor}
+ query=""
+ items={[]}
+ range={{ from: 0, to: 0 }}
+ />,
+ { storeInitialState: createMockState({ settings: mockSettings({}) }) },
+ );
+
+ await userEvent.click(await screen.findByRole("option", { name: "Chart" }));
+
+ // Wait for the modal to auto-open
+ const modal = await screen.findByLabelText("Choose a question or model");
+ expect(modal).toBeInTheDocument();
+
+ // Close the modal to test the menu state without auto-opening
+ const closeButton = within(modal).getByRole("button", { name: /close/i });
+ await userEvent.click(closeButton);
+
+ // Now the menu should show Browse all without a redundant divider
+ expect(await screen.findByText("Browse all")).toBeInTheDocument();
+
+ // There should be no menu items above Browse all, so no divider should be present
+ const menuContainer = screen.getByLabelText("Command Dialog");
+ const dividers = within(menuContainer).queryAllByRole("separator");
+ expect(dividers).toHaveLength(0);
+ });
+
describe("metabot", () => {
beforeEach(() => {
jest.clearAllMocks();
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/EntitySearchSection.tsx b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/EntitySearchSection.tsx
index 42071dfb822..d7cecc359f7 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/EntitySearchSection.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/EntitySearchSection.tsx
@@ -51,7 +51,7 @@ export function EntitySearchSection({
<Text size="md" c="text-medium">{t`No results found`}</Text>
</Box>
) : null}
- <Divider my="sm" mx="sm" />
+ {menuItems.length > 0 && <Divider my="sm" mx="sm" />}
<SearchResultsFooter
isSelected={selectedIndex === menuItems.length}
onClick={onFooterClick}
diff --git a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/useEntitySearch.ts b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/useEntitySearch.ts
index 6098249087e..bd438f74538 100644
--- a/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/useEntitySearch.ts
+++ b/enterprise/frontend/src/metabase-enterprise/rich_text_editing/tiptap/extensions/shared/useEntitySearch.ts
@@ -50,7 +50,7 @@ export function useEntitySearch({
const { data: recents = [], isLoading: isRecentsLoading } =
useListRecentsQuery(undefined, {
- refetchOnMountOrArgChange: true,
+ refetchOnMountOrArgChange: 10, // only refetch if the cache is more than 10 seconds stale
skip: !shouldFetchRecents,
});
diff --git a/frontend/src/metabase/api/activity.ts b/frontend/src/metabase/api/activity.ts
index d1b6afc839a..e2ccc7d32db 100644
--- a/frontend/src/metabase/api/activity.ts
+++ b/frontend/src/metabase/api/activity.ts
@@ -80,6 +80,10 @@ export const activityApi = Api.injectEndpoints({
export const { useListPopularItemsQuery, useLogRecentItemMutation } =
activityApi;
+type GetRecentsQueryOptions = Parameters<
+ typeof activityApi.useListRecentsQuery
+>[1];
+
// Makes it possible and type-safe to use the `include_metadata` parameter
// in the `useListRecentsQuery` hook. If `include_metadata` is set to `true`,
// the returned data will include the `result_metadata` property
@@ -91,10 +95,7 @@ export function useListRecentsQuery<T extends boolean | undefined = undefined>(
params?:
| ({ include_metadata?: T } & Omit<RecentsRequest, "include_metadata">)
| void,
- options?: {
- refetchOnMountOrArgChange?: boolean;
- skip?: boolean;
- },
+ options?: GetRecentsQueryOptions,
) {
type ResultType = T extends true ? RecentItemWithMetadata : RecentItem;
return activityApi.endpoints.listRecents.useQuery(