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
Move `ActivityApi` into `metabase/api` and update callers (#75773)
Romeo Van Snickf6353738307a
diff --git a/frontend/src/metabase/api/activity.ts b/frontend/src/metabase/api/activity.tsindex f4d0423e5e7..6aa47a0d479 100644--- a/frontend/src/metabase/api/activity.ts+++ b/frontend/src/metabase/api/activity.ts@@ -1,5 +1,6 @@import type {CreateRecentRequest,+ Dashboard,Field,PopularItem,PopularItemsResponse,@@ -10,7 +11,12 @@ import type {} from "metabase-types/api";import { Api } from "./api";-import { invalidateTags, listTag, provideActivityItemListTags } from "./tags";+import {+ idTag,+ invalidateTags,+ listTag,+ provideActivityItemListTags,+} from "./tags";export const activityApi = Api.injectEndpoints({endpoints: (builder) => ({@@ -67,11 +73,22 @@ export const activityApi = Api.injectEndpoints({invalidateTags(error, [listTag("activity")]),},),+ getMostRecentlyViewedDashboard: builder.query<Dashboard | null, void>({+ query: () => ({+ method: "GET",+ url: "/api/activity/most_recently_viewed_dashboard",+ }),+ providesTags: (dashboard) =>+ dashboard ? [idTag("dashboard", dashboard.id)] : [],+ }),}),});-export const { useListPopularItemsQuery, useLogRecentItemMutation } =- activityApi;+export const {+ useGetMostRecentlyViewedDashboardQuery,+ useListPopularItemsQuery,+ useLogRecentItemMutation,+} = activityApi;type GetRecentsQueryOptions = Parameters<typeof activityApi.useListRecentsQuerydiff --git a/frontend/src/metabase/api/activity.unit.spec.ts b/frontend/src/metabase/api/activity.unit.spec.tsindex 8e5b411c87a..def3431acf4 100644--- a/frontend/src/metabase/api/activity.unit.spec.ts+++ b/frontend/src/metabase/api/activity.unit.spec.ts@@ -3,15 +3,20 @@ import fetchMock from "fetch-mock";import { getStore } from "__support__/entities-store";import {+ findRequests,setupCollectionsEndpoints,+ setupMostRecentlyViewedDashboard,setupRecentViewsEndpoints,} from "__support__/server-mocks";-import { createMockRecentCollectionItem } from "metabase-types/api/mocks";+import {+ createMockDashboard,+ createMockRecentCollectionItem,+} from "metabase-types/api/mocks";import { activityApi } from "./activity";import { Api } from "./api";import { collectionApi } from "./collection";-import { listTag } from "./tags";+import { idTag, listTag } from "./tags";let activeStore: ReturnType<typeof getStore> | undefined;@@ -27,14 +32,14 @@ function setup() {]);activeStore = store;- const recentsCalls = () =>- fetchMock.callHistory.calls("path:/api/activity/recents", {- method: "GET",- }).length;- const collectionCalls = () =>- fetchMock.callHistory.calls("path:/api/collection/tree", {- method: "GET",- }).length;+ const recentsCalls = async () =>+ (await findRequests("GET")).filter((request) =>+ request.url.includes("/api/activity/recents"),+ ).length;+ const collectionCalls = async () =>+ (await findRequests("GET")).filter((request) =>+ request.url.includes("/api/collection/tree"),+ ).length;return { store, recentsCalls, collectionCalls };}@@ -56,8 +61,8 @@ describe("activityApi", () => {store.dispatch(activityApi.endpoints.listRecents.initiate());store.dispatch(collectionApi.endpoints.listCollectionsTree.initiate());- await waitFor(() => expect(recentsCalls()).toBe(1));- await waitFor(() => expect(collectionCalls()).toBe(1));+ await waitFor(async () => expect(await recentsCalls()).toBe(1));+ await waitFor(async () => expect(await collectionCalls()).toBe(1));await store.dispatch(activityApi.endpoints.logRecentItem.initiate({@@ -67,10 +72,36 @@ describe("activityApi", () => {);// Logging a recent must refetch the recents list...- await waitFor(() => expect(recentsCalls()).toBe(2));+ await waitFor(async () => expect(await recentsCalls()).toBe(2));// ...without invalidating the collection list.- expect(collectionCalls()).toBe(1);+ expect(await collectionCalls()).toBe(1);+ });+ });++ describe("getMostRecentlyViewedDashboard provided tags", () => {+ it("refetches when the dashboard it surfaced is edited elsewhere", async () => {+ const dashboard = createMockDashboard({ id: 42 });+ setupMostRecentlyViewedDashboard(dashboard);++ const store = getStore({ [Api.reducerPath]: Api.reducer }, {}, [+ Api.middleware,+ ]);+ activeStore = store;++ const dashboardCalls = async () =>+ (await findRequests("GET")).filter((request) =>+ request.url.includes("/api/activity/most_recently_viewed_dashboard"),+ ).length;++ store.dispatch(+ activityApi.endpoints.getMostRecentlyViewedDashboard.initiate(),+ );+ await waitFor(async () => expect(await dashboardCalls()).toBe(1));++ store.dispatch(Api.util.invalidateTags([idTag("dashboard", 42)]));++ await waitFor(async () => expect(await dashboardCalls()).toBe(2));});});@@ -79,13 +110,13 @@ describe("activityApi", () => {const { store, recentsCalls } = setup();store.dispatch(activityApi.endpoints.listRecents.initiate());- await waitFor(() => expect(recentsCalls()).toBe(1));+ await waitFor(async () => expect(await recentsCalls()).toBe(1));// Editing a card/collection invalidates that model's list tag, which// recents provides, so stale names never linger in recent lists.store.dispatch(Api.util.invalidateTags([listTag("collection")]));- await waitFor(() => expect(recentsCalls()).toBe(2));+ await waitFor(async () => expect(await recentsCalls()).toBe(2));});});});diff --git a/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/AddToDashSelectDashModal.tsx b/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/AddToDashSelectDashModal.tsxindex 6b6b64e9645..2b9f6b26365 100644--- a/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/AddToDashSelectDashModal.tsx+++ b/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/AddToDashSelectDashModal.tsx@@ -1,6 +1,7 @@import { useCallback, useMemo } from "react";import { t } from "ttag";+import { useGetMostRecentlyViewedDashboardQuery } from "metabase/api";import { ROOT_COLLECTION } from "metabase/collections/constants";import { canPlaceEntityInCollectionOrDescendants } from "metabase/data-studio/utils";import { useSelector } from "metabase/redux";@@ -12,7 +13,6 @@ import { DashboardPickerModal } from "../DashboardPicker";import { type OmniPickerItem, isInDbTree } from "../EntityPicker";import { getCollectionType } from "../EntityPicker/utils";-import { useMostRecentlyViewedDashboard } from "./hooks";import { isInPersonalCollection } from "./utils";const getTitle = ({ type }: Card) => {@@ -44,7 +44,7 @@ export const AddToDashSelectDashModal = ({}: AddToDashSelectDashModalProps) => {const personalCollectionId = useSelector(getUserPersonalCollectionId);const { data: mostRecentlyViewedDashboard } =- useMostRecentlyViewedDashboard();+ useGetMostRecentlyViewedDashboardQuery();const onDashboardSelected = (selectedDashboard?: Pick<Dashboard, "id" | "name">,diff --git a/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/hooks.ts b/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/hooks.tsdeleted file mode 100644index 320333549d5..00000000000--- a/frontend/src/metabase/common/components/Pickers/AddToDashSelectDashModal/hooks.ts+++ /dev/null@@ -1,19 +0,0 @@-import { useAsync } from "react-use";--import { ActivityApi } from "metabase/services";-import type { Dashboard } from "metabase-types/api";--export const useMostRecentlyViewedDashboard = () => {- const {- loading: isLoading,- error,- value: data,- } = useAsync(async () => {- const dashboard: Dashboard | undefined =- await ActivityApi.most_recently_viewed_dashboard();-- return dashboard;- });-- return { data, isLoading, error };-};diff --git a/frontend/src/metabase/services.js b/frontend/src/metabase/services.jsindex a1b70fe1013..fbb19a40d10 100644--- a/frontend/src/metabase/services.js+++ b/frontend/src/metabase/services.js@@ -1,11 +1,5 @@import { DELETE, GET, POST, PUT } from "metabase/api/legacy-client";-export const ActivityApi = {- most_recently_viewed_dashboard: GET(- "/api/activity/most_recently_viewed_dashboard",- ),-};-// only available with token loadedexport const GTAPApi = {list: GET("/api/mt/gtap"),