Skip to content

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

Carry the upload destination name in upload state (#80135)

Romeo Van Snick2ca2e0f6d337
diff --git a/frontend/src/metabase/common/hooks/entity-framework/index.ts b/frontend/src/metabase/common/hooks/entity-framework/index.ts
index 8fa30eed8c8..6965b7b0041 100644
--- a/frontend/src/metabase/common/hooks/entity-framework/index.ts
+++ b/frontend/src/metabase/common/hooks/entity-framework/index.ts
@@ -5,4 +5,3 @@
export * from "./use-question-list-query";
export * from "./use-question-query";
-export * from "./use-table-query";
diff --git a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/index.ts b/frontend/src/metabase/common/hooks/entity-framework/use-table-query/index.ts
deleted file mode 100644
index f111ed55087..00000000000
--- a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-table-query";
diff --git a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.ts b/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.ts
deleted file mode 100644
index 9739091c87f..00000000000
--- a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.ts
+++ /dev/null
@@ -1,36 +0,0 @@
-import { skipToken, useGetTableQuery } from "metabase/api";
-import type {
- UseEntityQueryProps,
- UseEntityQueryResult,
-} from "metabase/common/hooks/entity-framework/use-entity-query";
-import { useSelector } from "metabase/redux";
-import { getMetadata } from "metabase/selectors/metadata";
-import type Table from "metabase-lib/v1/metadata/Table";
-import type { GetTableRequest, TableId } from "metabase-types/api";
-
-/**
- * @deprecated use "metabase/api" instead
- */
-export const useTableQuery = ({
- id,
- query,
- reload = false,
- enabled = true,
-}: UseEntityQueryProps<
- TableId,
- GetTableRequest
->): UseEntityQueryResult<Table> => {
- const isActive = enabled && id != null;
- const { isFetching, error } = useGetTableQuery(
- isActive ? { id, ...query } : skipToken,
- { refetchOnMountOrArgChange: reload },
- );
- const data = useSelector((state) =>
- id != null ? (getMetadata(state).table(id) ?? undefined) : undefined,
- );
- return {
- data,
- isLoading: isFetching || (isActive && data == null),
- error,
- };
-};
diff --git a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.unit.spec.tsx b/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.unit.spec.tsx
deleted file mode 100644
index 19805a84687..00000000000
--- a/frontend/src/metabase/common/hooks/entity-framework/use-table-query/use-table-query.unit.spec.tsx
+++ /dev/null
@@ -1,42 +0,0 @@
-import { setupTablesEndpoints } from "__support__/server-mocks";
-import {
- renderWithProviders,
- screen,
- waitForLoaderToBeRemoved,
-} from "__support__/ui";
-import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
-import { createMockTable } from "metabase-types/api/mocks";
-
-import { useTableQuery } from "./use-table-query";
-
-const TEST_TABLE = createMockTable();
-
-const TestComponent = () => {
- const { data, isLoading, error } = useTableQuery({
- id: TEST_TABLE.id,
- });
-
- if (isLoading || error || !data) {
- return <LoadingAndErrorWrapper loading={isLoading} error={error} />;
- }
-
- return <div>{data.name}</div>;
-};
-
-const setup = () => {
- setupTablesEndpoints([TEST_TABLE]);
- renderWithProviders(<TestComponent />);
-};
-
-describe("useTableQuery", () => {
- it("should be initially loading", () => {
- setup();
- expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
- });
-
- it("should show data from the response", async () => {
- setup();
- await waitForLoaderToBeRemoved();
- expect(screen.getByText(TEST_TABLE.name)).toBeInTheDocument();
- });
-});
diff --git a/frontend/src/metabase/redux/store/upload.ts b/frontend/src/metabase/redux/store/upload.ts
index 1e50171549f..09eb7f132c4 100644
--- a/frontend/src/metabase/redux/store/upload.ts
+++ b/frontend/src/metabase/redux/store/upload.ts
@@ -12,6 +12,7 @@ export type FileUpload = {
collectionId?: CollectionId;
modelId?: string;
tableId?: TableId;
+ tableName?: string;
message?: string;
error?: string;
uploadMode?: UploadMode;
diff --git a/frontend/src/metabase/redux/uploads.ts b/frontend/src/metabase/redux/uploads.ts
index b4c514b8c81..bcb79b248cf 100644
--- a/frontend/src/metabase/redux/uploads.ts
+++ b/frontend/src/metabase/redux/uploads.ts
@@ -51,6 +51,24 @@ export interface UploadFileProps {
onUploadComplete?: () => void;
}
+async function fetchTableName(
+ dispatch: Dispatch,
+ tableId: TableId,
+): Promise<string | undefined> {
+ try {
+ const table: { name?: string } = await runRtkEndpoint(
+ { id: tableId },
+ dispatch,
+ tableApi.endpoints.getTable,
+ { forceRefetch: false },
+ );
+ return table.name;
+ } catch (error) {
+ console.error(error);
+ return undefined;
+ }
+}
+
export const uploadFile = createThunkAction(
UPLOAD_FILE_TO_COLLECTION,
({
@@ -64,6 +82,12 @@ export const uploadFile = createThunkAction(
async (dispatch: Dispatch) => {
const id = Date.now();
+ // Resolved before the upload starts, so the status has its destination
+ // on its first render. Fetching it alongside the upload would race the
+ // upload itself, and the status would miss the window it reports on.
+ const tableName =
+ tableId == null ? undefined : await fetchTableName(dispatch, tableId);
+
const clear = () =>
setTimeout(() => {
dispatch(clearUpload({ id }));
@@ -75,6 +99,7 @@ export const uploadFile = createThunkAction(
name: file.name,
collectionId,
tableId,
+ tableName,
}),
);
diff --git a/frontend/src/metabase/redux/uploads.unit.spec.ts b/frontend/src/metabase/redux/uploads.unit.spec.ts
index 8a3e667a5b3..da953f72e93 100644
--- a/frontend/src/metabase/redux/uploads.unit.spec.ts
+++ b/frontend/src/metabase/redux/uploads.unit.spec.ts
@@ -31,6 +31,13 @@ const mockUploadCSV = (valid = true) => {
);
};
+const mockGetTable = (name = "Fancy Table") => {
+ fetchMock.get("glob:*/api/table/123", {
+ id: 123,
+ name,
+ });
+};
+
const mockAppendCSV = (valid = true) => {
fetchMock.post(
"glob:*/api/table/*/append-csv",
@@ -132,6 +139,7 @@ describe("csv uploads", () => {
it("should handle file append success", async () => {
mockAppendCSV();
+ mockGetTable();
await uploadFile({
file,
@@ -148,6 +156,7 @@ describe("csv uploads", () => {
id: now,
name: "test.csv",
tableId: 123,
+ tableName: "Fancy Table",
},
});
@@ -170,6 +179,7 @@ describe("csv uploads", () => {
it("should handle file replace success", async () => {
mockReplaceCSV();
+ mockGetTable();
await uploadFile({
file,
@@ -186,6 +196,7 @@ describe("csv uploads", () => {
id: now,
name: "test.csv",
tableId: 123,
+ tableName: "Fancy Table",
},
});
diff --git a/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.tsx b/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.tsx
index a9ed778ef69..54403392900 100644
--- a/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.tsx
+++ b/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.tsx
@@ -2,12 +2,11 @@ import _ from "underscore";
import ErrorBoundary from "metabase/ErrorBoundary";
import { skipToken, useGetCollectionQuery } from "metabase/api";
-import { useTableQuery } from "metabase/common/hooks";
import { useDispatch, useSelector } from "metabase/redux";
import type { FileUpload } from "metabase/redux/store/upload";
import { clearAllUploads, getAllUploads } from "metabase/redux/uploads";
import { isEmpty } from "metabase/utils/validate";
-import type { CollectionId, TableId } from "metabase-types/api";
+import type { CollectionId } from "metabase-types/api";
import useStatusVisibility from "../../hooks/use-status-visibility";
import { isUploadAborted, isUploadInProgress } from "../../utils";
@@ -28,20 +27,17 @@ export const FileUploadStatus = () => {
"collectionId",
);
- // Unjustified type cast. FIXME
- const tables = Object.keys(groupedTables) as TableId[];
// Unjustified type cast. FIXME
const collections = Object.keys(groupedCollections) as CollectionId[];
return (
<>
- {tables.map((tableId) => {
+ {Object.entries(groupedTables).map(([tableId, tableUploads]) => {
return (
<FileUploadStatusContent
key={`uploads-table-${tableId}`}
- uploads={groupedTables[tableId]}
+ uploads={tableUploads}
resetUploads={resetUploads}
- tableId={tableId}
/>
);
})}
@@ -61,12 +57,10 @@ export const FileUploadStatus = () => {
const FileUploadStatusContent = ({
collectionId,
- tableId,
uploads,
resetUploads,
}: {
collectionId?: CollectionId;
- tableId?: TableId;
uploads: FileUpload[];
resetUploads: () => void;
}) => {
@@ -75,25 +69,21 @@ const FileUploadStatusContent = ({
);
const isVisible = useStatusVisibility(isActive);
- const { isLoading: tableLoading, data: table } = useTableQuery({
- id: tableId,
- enabled: !isEmpty(tableId),
- });
+ // Table uploads carry their destination name in the upload itself, resolved
+ // before the upload started. Collection uploads read it from the cache the
+ // collection page already populated.
const { isLoading: collectionLoading, data: collection } =
useGetCollectionQuery(
isEmpty(collectionId) ? skipToken : { id: collectionId },
);
- const isLoading = !!(tableLoading || collectionLoading);
- const hasData = !!(table || collection);
+ const uploadDestinationName = uploads[0]?.tableName ?? collection?.name;
- if (!isVisible || (isLoading && !hasData)) {
+ if (!isVisible || (collectionLoading && !collection)) {
return null;
}
- const uploadDestination = table ?? collection;
-
- if (!uploadDestination) {
+ if (!uploadDestinationName) {
return null;
}
@@ -102,7 +92,7 @@ const FileUploadStatusContent = ({
<FileUploadStatusLarge
uploads={uploads}
resetUploads={resetUploads}
- uploadDestination={uploadDestination}
+ uploadDestinationName={uploadDestinationName}
/>
</ErrorBoundary>
);
diff --git a/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.unit.spec.tsx b/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.unit.spec.tsx
index 7437337143d..8253b9d72cc 100644
--- a/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.unit.spec.tsx
+++ b/frontend/src/metabase/status/components/FileUploadStatus/FileUploadStatus.unit.spec.tsx
@@ -1,5 +1,3 @@
-import fetchMock from "fetch-mock";
-
import {
setupCollectionByIdEndpoint,
setupCollectionsEndpoints,
@@ -30,13 +28,6 @@ describe("FileUploadStatus", () => {
setupCollectionsEndpoints({
collections: [firstCollection, secondCollection],
});
- fetchMock.get(
- "path:/api/table/123",
- createMockCollection({
- id: 123,
- name: "Fancy Table",
- }),
- );
});
it("Should group uploads by collection", async () => {
@@ -85,6 +76,7 @@ describe("FileUploadStatus", () => {
it("Should show upload status for a table append", async () => {
const uploadOne = createMockUpload({
tableId: 123,
+ tableName: "Fancy Table",
collectionId: undefined,
id: 1,
});
diff --git a/frontend/src/metabase/status/components/FileUploadStatusLarge/FileUploadStatusLarge.tsx b/frontend/src/metabase/status/components/FileUploadStatusLarge/FileUploadStatusLarge.tsx
index aad8507f3d7..0f430d264a1 100644
--- a/frontend/src/metabase/status/components/FileUploadStatusLarge/FileUploadStatusLarge.tsx
+++ b/frontend/src/metabase/status/components/FileUploadStatusLarge/FileUploadStatusLarge.tsx
@@ -6,8 +6,6 @@ import { Link } from "metabase/common/components/Link";
import { PLUGIN_UPLOAD_MANAGEMENT } from "metabase/plugins";
import { type FileUpload, UploadMode } from "metabase/redux/store/upload";
import { Box, Button, Stack } from "metabase/ui";
-import type Table from "metabase-lib/v1/metadata/Table";
-import type { Collection } from "metabase-types/api";
import {
isUploadAborted,
@@ -19,14 +17,14 @@ import StatusLarge from "../StatusLarge";
const UPLOAD_MESSAGE_UPDATE_INTERVAL = 30 * 1000;
export interface FileUploadLargeProps {
- uploadDestination: Collection | Table;
+ uploadDestinationName: string;
uploads: FileUpload[];
resetUploads: () => void;
isActive?: boolean;
}
const FileUploadLarge = ({
- uploadDestination,
+ uploadDestinationName,
uploads,
resetUploads,
isActive,
@@ -46,7 +44,7 @@ const FileUploadLarge = ({
const title =
isLoading && loadingTime > 0
? getLoadingMessage(loadingTime)
- : getTitle(uploads, uploadDestination);
+ : getTitle(uploads, uploadDestinationName);
const status = {
title,
@@ -84,10 +82,7 @@ const getName = (upload: FileUpload) => {
return upload.name;
};
-const getTitle = (
- uploads: FileUpload[],
- uploadDestination: Collection | Table,
-) => {
+const getTitle = (uploads: FileUpload[], uploadDestinationName: string) => {
const isDone = uploads.every(isUploadCompleted);
const isOnlyReplace = uploads.every(
(upload) => upload.uploadMode === UploadMode.replace,
@@ -96,13 +91,13 @@ const getTitle = (
if (isDone) {
if (isOnlyReplace) {
- return t`Data replaced in ${uploadDestination.name}`;
+ return t`Data replaced in ${uploadDestinationName}`;
}
- return t`Data added to ${uploadDestination.name}`;
+ return t`Data added to ${uploadDestinationName}`;
} else if (isError) {
return t`Error uploading your file`;
} else {
- return t`Uploading data to ${uploadDestination.name} …`;
+ return t`Uploading data to ${uploadDestinationName} …`;
}
};