Skip to content

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

feat(enterprise): table data editing plugin (#61908)

Konstantin Darutkinccd51b88fafb
diff --git a/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Default.png b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Default.png
new file mode 100644
index 00000000000..c94308360ed
Binary files /dev/null and b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Default.png differ
diff --git a/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Value_Format.png b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Value_Format.png
new file mode 100644
index 00000000000..1d72848a709
Binary files /dev/null and b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_Value_Format.png differ
diff --git a/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_With_Seconds.png b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_With_Seconds.png
new file mode 100644
index 00000000000..a70e5a41c48
Binary files /dev/null and b/.loki/reference/chrome_laptop_Components_Inputs_DateTimePicker_With_Seconds.png differ
diff --git a/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts b/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts
new file mode 100644
index 00000000000..f61c38a4c26
--- /dev/null
+++ b/e2e/test/scenarios/table-editing/table-editing.cy.spec.ts
@@ -0,0 +1,518 @@
+import dayjs from "dayjs";
+
+import { SAMPLE_DB_ID } from "e2e/support/cypress_data";
+import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";
+import { resetSnowplow } from "e2e/support/helpers/e2e-snowplow-helpers";
+
+const { H } = cy;
+
+const { ORDERS_ID, PRODUCTS_ID, PEOPLE_ID } = SAMPLE_DATABASE;
+
+describe("scenarios > table-editing", () => {
+ beforeEach(() => {
+ resetSnowplow();
+
+ H.restore();
+ cy.signInAsAdmin();
+
+ H.activateToken("bleeding-edge");
+ setTableEditingEnabledForDB(SAMPLE_DB_ID);
+
+ cy.intercept("GET", "/api/database").as("getDatabases");
+ cy.intercept("GET", "/api/table/*").as("getTable");
+ });
+
+ it("should show edit icon on table browser", () => {
+ openTableBrowser();
+ getTableEditIcon("People").should("be.visible");
+ });
+
+ it("should not show edit icon on table browser if user is not admin", () => {
+ cy.signInAsNormalUser();
+
+ openTableBrowser();
+ getTableEditIcon("People").should("not.exist");
+ });
+
+ it("should allow to open table data edit mode", () => {
+ openTableBrowser();
+ openTableEdit("People");
+
+ H.expectUnstructuredSnowplowEvent({
+ event: "edit_data_button_clicked",
+ triggered_from: "table-browser",
+ target_id: PEOPLE_ID,
+ });
+
+ cy.wait("@getTable");
+
+ cy.findByTestId("edit-table-data-root")
+ .should("be.visible")
+ .within(() => {
+ cy.findByText("People").should("be.visible");
+ cy.findByTestId("head-crumbs-container")
+ .findByText("Edit")
+ .should("be.visible");
+
+ cy.findByTestId("table-root").should("be.visible");
+ });
+
+ cy.findByTestId("head-crumbs-container").findByText("People").click();
+
+ cy.findByTestId("query-builder-root").should("be.visible");
+ });
+
+ describe("db setting is disabled", () => {
+ it("should not allow to open table data view by default", () => {
+ setTableEditingEnabledForDB(SAMPLE_DB_ID, false);
+ openTableBrowser();
+ getTableEditIcon("People").should("not.exist");
+ });
+ });
+
+ describe("non-admin user", () => {
+ beforeEach(() => {
+ cy.signInAsNormalUser();
+ });
+
+ it("should not allow to open table data view", () => {
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+ cy.findByTestId("edit-table-data-restricted").should("be.visible");
+ });
+ });
+
+ describe("table edit mode", () => {
+ beforeEach(() => {
+ resetSnowplow();
+
+ cy.intercept("GET", `/api/table/${ORDERS_ID}/query_metadata`).as(
+ "getOrdersTable",
+ );
+
+ cy.intercept("POST", "api/dataset").as("getTableDataQuery");
+ cy.intercept("POST", "api/ee/action-v2/execute-bulk").as(
+ "updateTableData",
+ );
+
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+
+ cy.wait("@getOrdersTable");
+ });
+
+ it("should allow to filter table data", () => {
+ cy.findByTestId("edit-table-data-root").findByText("Filter").click();
+
+ H.popover().within(() => {
+ cy.findByText("Discount").click();
+ cy.findByText("Between").click();
+ });
+
+ H.menu().findByText("Not empty").click();
+ H.popover().findByText("Apply filter").click();
+
+ cy.wait("@getTableDataQuery");
+
+ cy.findByTestId("filters-visibility-control").should("have.text", "1");
+
+ // check that filter persists after page refresh
+ cy.reload();
+ cy.wait("@getTableDataQuery");
+
+ cy.findByTestId("filters-visibility-control").should("have.text", "1");
+
+ cy.findByTestId("qb-filters-panel")
+ .should("be.visible")
+ .within(() => {
+ cy.icon("close").click();
+ });
+
+ cy.wait("@getTableDataQuery");
+ cy.findByTestId("filters-visibility-control").should("not.exist");
+ });
+
+ it("should allow to sort table data", () => {
+ cy.findByTestId("table-header").within(() => {
+ cy.findByText("Quantity").click();
+
+ cy.findAllByTestId("header-sort-indicator").should("have.length", 1);
+
+ cy.findByText("Quantity")
+ .closest("[role=button]")
+ .findByLabelText("chevronup icon")
+ .should("be.visible");
+
+ cy.findByText("Quantity").click();
+
+ cy.findByText("Quantity")
+ .closest("[role=button]")
+ .findByLabelText("chevrondown icon")
+ .should("be.visible");
+ });
+
+ // check that sorting persist after page refresh
+ cy.reload();
+ cy.wait("@getTableDataQuery");
+
+ cy.findByTestId("table-header").within(() => {
+ cy.findByText("Quantity")
+ .closest("[role=button]")
+ .findByLabelText("chevrondown icon")
+ .should("be.visible");
+
+ cy.findByText("Quantity").click();
+
+ cy.findAllByTestId("header-sort-indicator").should("have.length", 0);
+ });
+ });
+
+ it("should allow to view row details", () => {
+ openEditRowModal(2);
+ H.modal().within(() => {
+ cy.get("@rowId").then((rowId) => {
+ cy.findByTestId("ID-field-input").should("have.text", rowId);
+ });
+ });
+ });
+
+ it("should allow to edit a row using a modal", () => {
+ openEditRowModal(2);
+
+ H.modal().within(() => {
+ cy.findByTestId("Quantity-field-input")
+ .type("{backspace}{backspace}{backspace}123")
+ .blur();
+
+ cy.findByTestId("update-row-save-button").click();
+ });
+
+ cy.wait("@updateTableData").then(({ response }) => {
+ expect(response?.body.outputs[0].op).to.equal("updated");
+ expect(response?.body.outputs[0].row.QUANTITY).to.equal(123);
+ });
+
+ H.modal().should("not.exist");
+
+ H.undoToast().findByText("Successfully updated").should("be.visible");
+ H.expectUnstructuredSnowplowEvent({
+ event: "edit_data_record_modified",
+ event_detail: "update",
+ target_id: ORDERS_ID,
+ triggered_from: "modal",
+ result: "success",
+ });
+ });
+
+ describe("inline cell editing", () => {
+ // we use randomized values here to allow multiple runs in one session (since non-changed values are not updated)
+ const cases = [
+ {
+ tableId: PRODUCTS_ID,
+ dataType: "string",
+ column: "EAN",
+ value: Math.floor(Math.random() * 100000)
+ .toString()
+ .padStart(13, "0"),
+ },
+ {
+ tableId: ORDERS_ID,
+ dataType: "number",
+ column: "TAX",
+ value: Math.floor(Math.random() * 1000),
+ },
+ // TODO: fix flaky test due to timezone issues (runs locally but fails on CI)
+ // {
+ // tableId: PEOPLE_ID,
+ // dataType: "date",
+ // column: "BIRTH_DATE",
+ // value: dayjs(
+ // new Date(
+ // Date.now() -
+ // Math.floor(Math.random() * 1000 * 60 * 60 * 24 * 365),
+ // ),
+ // ).format("YYYY-MM-DD"),
+ // },
+ ];
+
+ cases.forEach(({ dataType, column, value, tableId }) => {
+ it(`should allow to edit a cell with type ${dataType}`, () => {
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${tableId}/edit`);
+
+ // Locate the table and the specific cell to edit
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1) // Select the second row (index 1)
+ .within(() => {
+ cy.get(`[data-column-id='${column}']`)
+ .as("targetCell")
+ .click({ scrollBehavior: "center" }); // Activate inline editing
+ });
+
+ // Edit the cell value
+ cy.get("@targetCell")
+ .find("input")
+ .first() // Assuming the cell becomes an input field
+ .type(`{selectAll}{backspace}${value}`) // Enter the new value
+ .blur(); // Trigger the save action by blurring the input
+
+ cy.wait("@updateTableData").then(({ response }) => {
+ expect(response?.body.outputs[0].op).to.equal("updated");
+ if (dataType === "date") {
+ // BE returns the date in the report timezone, so we should ignore the timezone offset
+ expect(response?.body.outputs[0].row[column]).to.satisfy(
+ (date: string) => date.startsWith(value.toString()),
+ );
+ } else {
+ expect(response?.body.outputs[0].row[column]).to.equal(value);
+ }
+ });
+
+ H.expectUnstructuredSnowplowEvent({
+ event: "edit_data_record_modified",
+ event_detail: "update",
+ target_id: tableId,
+ triggered_from: "inline",
+ result: "success",
+ });
+
+ H.undoToast().findByText("Successfully updated").should("be.visible");
+ });
+ });
+
+ it("should allow to edit a cell with datetime type", () => {
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+
+ // Locate the table and the specific cell to edit
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1) // Select the second row (index 1)
+ .within(() => {
+ cy.get("[data-column-id='CREATED_AT']").as("targetCell").click({
+ scrollBehavior: false,
+ }); // Activate inline editing
+ });
+
+ const day = Math.floor(Math.random() * 10) + 10; // 10-20
+ const hour = Math.floor(Math.random() * 12);
+ const minute = Math.floor(Math.random() * 60);
+
+ H.popover().within(() => {
+ cy.findByRole("button", { name: `${day} May 2024` }).click();
+ cy.findAllByRole("spinbutton").eq(0).type(hour.toString());
+ cy.findAllByRole("spinbutton").eq(1).type(minute.toString());
+ // It's safe to click the last button because we're in the popover
+ // eslint-disable-next-line no-unsafe-element-filtering
+ cy.findAllByRole("button").last().click();
+ });
+
+ cy.wait("@updateTableData").then(({ response }) => {
+ const targetDate = dayjs(
+ new Date(2024, 4, day, hour, minute, 0),
+ ).format("YYYY-MM-DDTHH:mm:ss");
+ const savedDate = response?.body.outputs[0].row.CREATED_AT;
+
+ expect(response?.body.outputs[0].op).to.equal("updated");
+ // Omit timezone offset
+ expect(savedDate.startsWith(targetDate)).to.be.true;
+ });
+
+ H.undoToast().findByText("Successfully updated").should("be.visible");
+ });
+
+ it("should allow to edit a cell with select type", () => {
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1)
+ .within(() => {
+ cy.get("[data-column-id='USER_ID']").as("targetCell").click({
+ scrollBehavior: false,
+ });
+ });
+
+ H.popover().within(() => {
+ const randomIndex = Math.floor(2 + Math.random() * 18);
+
+ cy.findAllByRole("option")
+ .should("have.length", 20)
+ .eq(randomIndex)
+ .click();
+
+ cy.wait("@updateTableData").then(({ response }) => {
+ expect(response?.body.outputs[0].op).to.equal("updated");
+ });
+ });
+
+ H.undoToast().findByText("Successfully updated").should("be.visible");
+ });
+
+ it("should not allow to edit PK cells", () => {
+ cy.visit(
+ `/browse/databases/${SAMPLE_DB_ID}/tables/${PRODUCTS_ID}/edit`,
+ );
+
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1)
+ .within(() => {
+ cy.get("[data-column-id='ID']")
+ .as("targetCell")
+ .click({
+ scrollBehavior: false,
+ })
+ .find("input")
+ .should("not.exist");
+ });
+ });
+
+ it("should handle errors", () => {
+ cy.visit(
+ `/browse/databases/${SAMPLE_DB_ID}/tables/${PRODUCTS_ID}/edit`,
+ );
+
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .eq(1)
+ .within(() => {
+ cy.get("[data-column-id='EAN']")
+ .as("targetCell")
+ .click({
+ scrollBehavior: false,
+ })
+ .find("input")
+ // Should be more than 13 characters to trigger the error
+ .type("{selectAll}{backspace}aaaaaaaaaaaaaaaaaaaaaaaaa", {
+ scrollBehavior: false,
+ }) // Enter the new value
+ .blur(); // Trigger the save action by blurring the input
+ });
+
+ H.undoToast()
+ .findByText("Couldn't save table changes")
+ .should("be.visible");
+ });
+ });
+ });
+
+ describe("create / delete row", () => {
+ beforeEach(() => {
+ resetSnowplow();
+
+ cy.intercept("GET", `/api/table/${ORDERS_ID}/query_metadata`).as(
+ "getOrdersTable",
+ );
+
+ cy.intercept("POST", "api/dataset").as("getTableDataQuery");
+ cy.intercept("POST", "api/ee/action-v2/execute-bulk").as("executeBulk");
+
+ cy.visit(`/browse/databases/${SAMPLE_DB_ID}/tables/${ORDERS_ID}/edit`);
+
+ cy.wait("@getOrdersTable");
+ });
+
+ it("should allow to create a row", () => {
+ cy.findByTestId("new-record-button").click();
+
+ H.modal().within(() => {
+ cy.findByText("Create a new record").should("be.visible");
+ cy.findByTestId("Tax-field-input").type("50");
+ cy.findByTestId("Total-field-input").type("100");
+ cy.findByTestId("Discount-field-input").type("10");
+ cy.findByTestId("create-row-form-submit-button").click();
+ });
+
+ cy.wait("@executeBulk").then(({ response, request }) => {
+ expect(request.body.action).to.equal("data-grid.row/create");
+ expect(response?.body.outputs[0].op).to.equal("created");
+ expect(response?.body.outputs[0].row.TAX).to.equal(50);
+ expect(response?.body.outputs[0].row.TOTAL).to.equal(100);
+ expect(response?.body.outputs[0].row.DISCOUNT).to.equal(10);
+ });
+
+ H.undoToast().within(() => {
+ cy.findByText("Record successfully created").should("be.visible");
+ cy.findByLabelText("close icon").click();
+ });
+
+ H.expectUnstructuredSnowplowEvent({
+ event: "edit_data_record_modified",
+ event_detail: "create",
+ target_id: ORDERS_ID,
+ triggered_from: "modal",
+ result: "success",
+ });
+ });
+
+ it("should allow to delete multiple rows (bulk)", () => {
+ cy.findAllByTestId("row-select-checkbox").eq(15).click();
+ cy.findAllByTestId("row-select-checkbox").eq(16).click();
+
+ cy.findByTestId("toast-card").findByText("Delete").click();
+
+ H.modal().within(() => {
+ cy.findByText("Delete 2 records?").should("be.visible");
+ cy.findByRole("button", { name: "Delete 2 records" }).click();
+ });
+
+ cy.wait("@executeBulk").then(({ response, request }) => {
+ expect(request.body.action).to.equal("data-grid.row/delete");
+ expect(response?.body.outputs[0].op).to.equal("deleted");
+ });
+
+ cy.findByTestId("toast-card").should("not.exist");
+
+ H.undoToast().findByText("Successfully deleted").should("be.visible");
+ });
+ });
+});
+
+function setTableEditingEnabledForDB(dbId: number, enabled = true) {
+ return cy.request("PUT", `/api/database/${dbId}`, {
+ settings: {
+ "database-enable-table-editing": enabled,
+ },
+ });
+}
+
+function openTableBrowser(databaseName: string = "Sample Database") {
+ cy.visit("/browse/databases");
+ cy.wait("@getDatabases");
+ cy.findByTestId("database-browser").findByText(databaseName).click();
+}
+
+function getTableEditIcon(tableName: string) {
+ return cy
+ .findByTestId("browse-schemas")
+ .contains(tableName)
+ .realHover()
+ .findByTestId("edit-table-icon");
+}
+
+function openTableEdit(tableName: string) {
+ getTableEditIcon(tableName).click();
+}
+
+function openEditRowModal(rowIndex: number) {
+ cy.findByTestId("table-root")
+ .findAllByRole("row")
+ .should("have.length.gte", 10)
+ .eq(rowIndex)
+ .within(() => {
+ cy.findAllByTestId("cell-data")
+ .first()
+ .invoke("text")
+ .then((text) => {
+ cy.wrap(text).as("rowId");
+ });
+
+ cy.findAllByTestId("cell-data").first().realHover({
+ scrollBehavior: false,
+ });
+
+ cy.findByTestId("row-edit-icon").click({
+ scrollBehavior: false,
+ });
+ });
+
+ H.modal().findByText("Edit record").should("be.visible");
+}
diff --git a/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj b/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
index 7ff6ee915e9..374f50b0e17 100644
--- a/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
+++ b/enterprise/backend/src/metabase_enterprise/action_v2/execute_form.clj
@@ -54,8 +54,22 @@
[:title :string]
[:parameters [:sequential ::describe-param]]])
+(defn- field-input-type-ignoring-semantics [create? field field-values]
+ (condp #(isa? %2 %1) (:base_type field)
+ :type/Boolean :input/boolean
+ :type/Integer :input/integer
+ :type/BigInteger :input/integer
+ :type/Float :input/float
+ :type/Decimal :input/float
+ :type/Date :input/date
+ :type/DateTime :input/datetime
+ :type/Time :input/time
+ (if (and (not create?) (#{:list :auto-list :search} (:type field-values)))
+ :input/dropdown
+ :input/text)))
+
(defn- field-input-type
- [field field-values]
+ [create? field field-values]
(condp #(isa? %2 %1) (:semantic_type field)
:type/Name :input/text
:type/Title :input/text
@@ -63,19 +77,12 @@
:type/Description :input/textarea
:type/Category :input/dropdown
:type/FK :input/dropdown
- :type/PK :input/dropdown
- (condp #(isa? %2 %1) (:base_type field)
- :type/Boolean :input/boolean
- :type/Integer :input/integer
- :type/BigInteger :input/integer
- :type/Float :input/float
- :type/Decimal :input/float
- :type/Date :input/date
- :type/DateTime :input/datetime
- :type/Time :input/time
- (if (#{:list :auto-list :search} (:type field-values))
- :input/dropdown
- :input/text))))
+ :type/PK (if create?
+ ;; Unless it's a compound PK, we need to provide a unique value for the column, so use the base_type
+ (field-input-type-ignoring-semantics create? field field-values)
+ ;; For updating and deleting, we must pick an existing row
+ :input/dropdown)
+ (field-input-type-ignoring-semantics create? field field-values)))
(defn- describe-table-action
[{:keys [action-kw
@@ -107,7 +114,8 @@
param-setting (get param-map (keyword (:name field)))
;; TODO get this from action configuration, when we add it, or inherit from table conf
column-settings nil
- auto-inc? (:database_is_auto_increment field pk?)]
+ auto-inc? (:database_is_auto_increment field pk?)
+ create? (contains? #{:table.row/create :data-grid.row/create} action-kw)]
:when (case action-kw
;; create does not take pk cols if auto increment, todo generated cols?
(:table.row/create :data-grid.row/create) (not auto-inc?)
@@ -128,7 +136,7 @@
:display_name (:display_name field)
:semantic_type (:semantic_type field)
;; TODO we are manually removing the namespace due to an issue with encoder/api not running
- :input_type (strip-namespace-hack (field-input-type field field-values))
+ :input_type (strip-namespace-hack (field-input-type create? field field-values))
:field_id (:id field)
:human_readable_field_id (-> field :dimensions first :human_readable_field_id)
:optional (not required)
diff --git a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
index 8c4c33c1f82..6b77e656048 100644
--- a/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/action_v2/api_test.clj
@@ -605,7 +605,7 @@
scope {:table-id table-id}]
(testing "create"
- (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "dropdown" :optional false :readonly false}
+ (is (=? {:parameters [{:id "id" :display_name "ID" :input_type "integer" :optional false :readonly false}
{:id "text" :display_name "Text" :input_type "text" :optional true :readonly false}
{:id "int" :display_name "Int" :input_type "integer" :optional true :readonly false}
{:id "bool" :display_name "Bool" :input_type "boolean" :optional true :readonly false}
diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.js b/enterprise/frontend/src/metabase-enterprise/plugins.js
index 32746519636..e084241b172 100644
--- a/enterprise/frontend/src/metabase-enterprise/plugins.js
+++ b/enterprise/frontend/src/metabase-enterprise/plugins.js
@@ -39,6 +39,7 @@ import "./user_provisioning";
import "./clean_up";
import "./metabot";
import "./database_replication";
+import "./table-editing";
import "./documents";
import "./semantic_search";
import "./transforms";
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.tsx
new file mode 100644
index 00000000000..fbb7cb4d197
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.tsx
@@ -0,0 +1,114 @@
+import { useState } from "react";
+import { t } from "ttag";
+
+import {
+ Description,
+ Error,
+ Label,
+} from "metabase/admin/databases/components/DatabaseFeatureComponents";
+import { DatabaseInfoSection } from "metabase/admin/databases/components/DatabaseInfoSection";
+import Toggle from "metabase/common/components/Toggle";
+import { trackSimpleEvent } from "metabase/lib/analytics";
+import { getResponseErrorMessage } from "metabase/lib/errors";
+import { Box, Flex } from "metabase/ui";
+import type {
+ Database,
+ DatabaseData,
+ DatabaseId,
+ DatabaseLocalSettingAvailability,
+} from "metabase-types/api";
+
+import {
+ DATABASE_TABLE_EDITING_SETTING,
+ isDatabaseTableEditingEnabled,
+} from "../settings";
+
+enum DisabledReasonKey {
+ MissingDriverFeature = "driver-feature-missing",
+ NoWriteableTable = "permissions/no-writable-table",
+ SyncInProgress = "database-metadata/sync-in-progress",
+ DatabaseEmtpy = "database-metadata/not-populated",
+}
+
+const VISIBLE_REASONS: string[] = [
+ DisabledReasonKey.NoWriteableTable,
+ DisabledReasonKey.SyncInProgress,
+ DisabledReasonKey.DatabaseEmtpy,
+];
+
+export function AdminDatabaseTableEditingSection({
+ database,
+ settingsAvailable,
+ updateDatabase,
+}: {
+ database: Database;
+ settingsAvailable?: Record<string, DatabaseLocalSettingAvailability>;
+ updateDatabase: (
+ database: { id: DatabaseId } & Partial<DatabaseData>,
+ ) => Promise<void>;
+}) {
+ const [error, setError] = useState<string | null>(null);
+
+ const handleToggle = async (enabled: boolean) => {
+ try {
+ setError(null);
+
+ trackSimpleEvent({
+ event: "edit_data_settings_toggled",
+ event_detail: enabled ? "on" : "off",
+ target_id: database.id,
+ triggered_from: "admin-settings-databases",
+ });
+
+ await updateDatabase({
+ id: database.id,
+ settings: { [DATABASE_TABLE_EDITING_SETTING]: enabled },
+ });
+ } catch (err) {
+ setError(getResponseErrorMessage(err) || t`An error occurred`);
+ }
+ };
+
+ const dataEditingSetting =
+ settingsAvailable?.[DATABASE_TABLE_EDITING_SETTING];
+
+ const isSettingDisabled =
+ !dataEditingSetting || dataEditingSetting.enabled === false;
+
+ const firstDisabledReason =
+ dataEditingSetting?.enabled === false
+ ? dataEditingSetting?.reasons?.[0]
+ : undefined;
+
+ const shouldShowSection =
+ !firstDisabledReason || VISIBLE_REASONS.includes(firstDisabledReason.key);
+
+ if (!dataEditingSetting || !shouldShowSection) {
+ return null;
+ }
+
+ return (
+ <DatabaseInfoSection
+ name={t`Editable table data`}
+ description={t`Allow the data within tables of this database to be edited by Admin users.`}
+ data-testid="database-table-editing-section"
+ >
+ <Flex align="center" justify="space-between" mb="xs">
+ <Label htmlFor="table-editing-toggle">{t`Editable tables`}</Label>
+ <Toggle
+ id="table-editing-toggle"
+ value={isDatabaseTableEditingEnabled(database)}
+ onChange={handleToggle}
+ disabled={isSettingDisabled}
+ />
+ </Flex>
+ <Box maw="22.5rem">
+ {error ? <Error>{error}</Error> : null}
+ <Description>
+ {firstDisabledReason?.message ??
+ t`Your database connection will need Write permissions.`}
+ </Description>
+ </Box>
+ </DatabaseInfoSection>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.unit.spec.tsx
new file mode 100644
index 00000000000..c825593e3a1
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/admin/AdminDatabaseTableEditingSection.unit.spec.tsx
@@ -0,0 +1,71 @@
+import { renderWithProviders, screen } from "__support__/ui";
+import type { DatabaseLocalSettingAvailability } from "metabase-types/api";
+import { createMockDatabase } from "metabase-types/api/mocks";
+
+import { DATABASE_TABLE_EDITING_SETTING } from "../settings";
+
+import { AdminDatabaseTableEditingSection } from "./AdminDatabaseTableEditingSection";
+
+const setup = (
+ setting: DatabaseLocalSettingAvailability = { enabled: true },
+) => {
+ const mockDatabase = createMockDatabase();
+
+ return renderWithProviders(
+ <AdminDatabaseTableEditingSection
+ database={mockDatabase}
+ settingsAvailable={{
+ [DATABASE_TABLE_EDITING_SETTING]: setting,
+ }}
+ updateDatabase={() => Promise.resolve()}
+ />,
+ );
+};
+
+describe("AdminDatabaseTableEditingSection", () => {
+ it("should render for supported engines", () => {
+ setup({
+ enabled: true,
+ });
+ expect(
+ screen.getByTestId("database-table-editing-section"),
+ ).toBeInTheDocument();
+ });
+
+ it("should render disabled toggle for specific disabled reasons", () => {
+ setup({
+ enabled: false,
+ reasons: [
+ {
+ key: "permissions/no-writable-table",
+ message: "Database connection is read-only",
+ },
+ ],
+ });
+
+ expect(
+ screen.getByTestId("database-table-editing-section"),
+ ).toBeInTheDocument();
+
+ expect(screen.getByRole("switch")).toBeDisabled();
+ expect(
+ screen.getByText("Database connection is read-only"),
+ ).toBeInTheDocument();
+ });
+
+ it("should hide section for other disabled reasons", () => {
+ setup({
+ enabled: false,
+ reasons: [
+ {
+ key: "driver-feature-missing",
+ message: "The driver does not support table editing",
+ },
+ ],
+ });
+
+ expect(
+ screen.queryByTestId("database-table-editing-section"),
+ ).not.toBeInTheDocument();
+ });
+});
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/api/index.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/api/index.ts
new file mode 100644
index 00000000000..22d6177c182
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/api/index.ts
@@ -0,0 +1,70 @@
+import { EnterpriseApi } from "metabase-enterprise/api/api";
+
+import {
+ type DescribeActionFormRequest,
+ type DescribeActionFormResponse,
+ TableActionId,
+ type TableDeleteRowsRequest,
+ type TableDeleteRowsResponse,
+ type TableInsertRowsRequest,
+ type TableInsertRowsResponse,
+ type TableUpdateRowsRequest,
+ type TableUpdateRowsResponse,
+} from "./types";
+
+export const tableEditingApi = EnterpriseApi.injectEndpoints({
+ endpoints: (builder) => ({
+ insertTableRows: builder.mutation<
+ TableInsertRowsResponse,
+ TableInsertRowsRequest
+ >({
+ query: ({ rows, scope }) => ({
+ method: "POST",
+ url: `/api/ee/action-v2/execute-bulk`,
+ body: { inputs: rows, scope, action: TableActionId.CreateRow },
+ }),
+ }),
+ updateTableRows: builder.mutation<
+ TableUpdateRowsResponse,
+ TableUpdateRowsRequest
+ >({
+ query: ({ inputs, params, scope }) => ({
+ method: "POST",
+ url: `/api/ee/action-v2/execute-bulk`,
+ body: { inputs, params, scope, action: TableActionId.UpdateRow },
+ }),
+ }),
+ deleteTableRows: builder.mutation<
+ TableDeleteRowsResponse,
+ TableDeleteRowsRequest
+ >({
+ query: ({ inputs, scope, params }) => ({
+ method: "POST",
+ url: `/api/ee/action-v2/execute-bulk`,
+ body: {
+ inputs,
+ scope,
+ action: TableActionId.DeleteRow,
+ ...(params && { params }),
+ },
+ }),
+ }),
+ describeActionForm: builder.mutation<
+ DescribeActionFormResponse,
+ DescribeActionFormRequest
+ >({
+ query: (body) => ({
+ method: "POST",
+ url: `/api/ee/action-v2/execute-form`,
+ body,
+ }),
+ }),
+ }),
+});
+
+export const {
+ useInsertTableRowsMutation,
+ useUpdateTableRowsMutation,
+ useDeleteTableRowsMutation,
+ useDescribeActionFormMutation,
+} = tableEditingApi;
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/api/types.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/api/types.ts
new file mode 100644
index 00000000000..a98fee54c25
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/api/types.ts
@@ -0,0 +1,85 @@
+import type { TableId } from "metabase-types/api";
+import type { DatasetColumn, RowValue } from "metabase-types/api/dataset";
+
+export enum TableActionId {
+ CreateRow = "data-grid.row/create",
+ UpdateRow = "data-grid.row/update",
+ DeleteRow = "data-grid.row/delete",
+ Undo = "data-editing/undo",
+ Redo = "data-editing/redo",
+}
+
+export type RowCellsWithPkValue = Record<DatasetColumn["name"], RowValue>;
+
+export type TableEditingActionScope = { "table-id": TableId };
+
+type ExecuteOutput<Op extends "created" | "updated" | "deleted"> = {
+ op: Op;
+ row: RowCellsWithPkValue;
+ "table-id": TableId;
+};
+
+export type TableInsertRowsRequest = {
+ rows: RowCellsWithPkValue[];
+ scope?: TableEditingActionScope;
+};
+
+export type TableInsertRowsResponse = {
+ outputs: ExecuteOutput<"created">[];
+};
+
+export type TableUpdateRowsRequest = {
+ inputs: RowCellsWithPkValue[];
+ params: RowCellsWithPkValue;
+ scope?: TableEditingActionScope;
+};
+
+export type TableUpdateRowsResponse = {
+ outputs: ExecuteOutput<"updated">[];
+};
+
+export type TableDeleteRowsRequest = {
+ inputs: RowCellsWithPkValue[];
+ scope?: TableEditingActionScope;
+ params?: { "delete-children"?: boolean };
+};
+
+export type TableDeleteRowsResponse = {
+ outputs: ExecuteOutput<"deleted">[];
+};
+
+export enum TableActionFormInputType {
+ Text = "text",
+ Textarea = "textarea",
+ Date = "date",
+ DateTime = "datetime",
+ Dropdown = "dropdown",
+ Boolean = "boolean",
+ Integer = "integer",
+ Float = "float",
+}
+
+export type TableActionFormParameter = {
+ id: string;
+ display_name: string;
+ input_type: TableActionFormInputType;
+ semantic_type?: string;
+ optional?: boolean;
+ nullable?: boolean;
+ readonly?: boolean;
+ field_id?: number;
+ human_readable_field_id?: number;
+ database_default?: string;
+ value?: RowValue;
+};
+
+export type DescribeActionFormRequest = {
+ action: TableActionId;
+ scope: TableEditingActionScope;
+ input?: RowCellsWithPkValue;
+};
+
+export type DescribeActionFormResponse = {
+ title: string;
+ parameters: TableActionFormParameter[];
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableBreadcrumbs.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableBreadcrumbs.tsx
new file mode 100644
index 00000000000..104167131fc
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableBreadcrumbs.tsx
@@ -0,0 +1,51 @@
+import { useMemo } from "react";
+import { t } from "ttag";
+
+import * as Urls from "metabase/lib/urls";
+import { HeadBreadcrumbs } from "metabase/query_builder/components/view/ViewHeader/components";
+import type { DataSourcePart } from "metabase/query_builder/components/view/ViewHeader/components/QuestionDataSource/utils";
+import { ViewHeading } from "metabase/query_builder/components/view/ViewSection";
+import type { Database, Table } from "metabase-types/api";
+
+type TableBreadcrumbsProps = {
+ database: Database;
+ table: Table;
+ showEditBreadcrumb?: boolean;
+};
+
+export const TableBreadcrumbs = ({
+ database,
+ table,
+ showEditBreadcrumb = false,
+}: TableBreadcrumbsProps) => {
+ const parts = useMemo(() => {
+ const parts: DataSourcePart[] = [
+ {
+ icon: "database" as const,
+ model: "database" as const,
+ name: database.name,
+ href: Urls.browseDatabase(database),
+ },
+ {
+ icon: "table" as const,
+ model: "table" as const,
+ name: table.display_name,
+ href: Urls.tableRowsQuery(database.id, table.id),
+ },
+ ];
+
+ if (showEditBreadcrumb) {
+ parts.push({
+ name: t`Edit`,
+ });
+ }
+
+ return parts;
+ }, [database, table, showEditBreadcrumb]);
+
+ return (
+ <ViewHeading color="medium">
+ <HeadBreadcrumbs parts={parts} variant="head" />
+ </ViewHeading>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableHeader.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableHeader.tsx
new file mode 100644
index 00000000000..f58898cbf44
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/common/TableHeader.tsx
@@ -0,0 +1,44 @@
+import type { PropsWithChildren } from "react";
+
+import { useGetDatabaseQuery, useGetTableQuery } from "metabase/api";
+import { Group, Skeleton, rem } from "metabase/ui";
+
+import { TableBreadcrumbs } from "./TableBreadcrumbs";
+
+type TableHeaderProps = PropsWithChildren<{
+ databaseId: number;
+ tableId: number;
+ showEditBreadcrumb?: boolean;
+}>;
+
+export function TableHeader({
+ databaseId,
+ tableId,
+ children,
+ showEditBreadcrumb = false,
+}: TableHeaderProps) {
+ const { data: table } = useGetTableQuery({ id: tableId });
+ const { data: database } = useGetDatabaseQuery({ id: databaseId });
+
+ return (
+ <Group
+ justify="space-between"
+ align="center"
+ p="0.5rem 1rem 0.5rem 2rem"
+ mih="4rem"
+ bg="var(--mb-color-background)"
+ >
+ {database && table ? (
+ <TableBreadcrumbs
+ database={database}
+ table={table}
+ showEditBreadcrumb={showEditBreadcrumb}
+ />
+ ) : (
+ <Skeleton height={rem(24)} width={rem(300)} />
+ )}
+
+ <Group gap="md">{children}</Group>
+ </Group>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/common/getRowCountMessage.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/common/getRowCountMessage.tsx
new file mode 100644
index 00000000000..b0b57ce200e
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/common/getRowCountMessage.tsx
@@ -0,0 +1,20 @@
+import { msgid, ngettext, t } from "ttag";
+
+import { formatNumber } from "metabase/lib/formatting/numbers";
+import { HARD_ROW_LIMIT } from "metabase-lib/v1/queries/utils/index";
+import type { Dataset } from "metabase-types/api";
+
+export const formatRowCount = (count: number) => {
+ const countString = formatNumber(count);
+ return ngettext(msgid`${countString} row`, `${countString} rows`, count);
+};
+
+export function getRowCountMessage(result: Dataset): string {
+ if (result.data.rows_truncated > 0) {
+ return t`Showing first ${formatRowCount(result.row_count)}`;
+ }
+ if (result.row_count === HARD_ROW_LIMIT) {
+ return t`Showing first ${HARD_ROW_LIMIT} rows`;
+ }
+ return t`Showing ${formatRowCount(result.row_count)}`;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/common/utils.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/common/utils.ts
new file mode 100644
index 00000000000..b98bd74ae69
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/common/utils.ts
@@ -0,0 +1,78 @@
+import { b64hash_to_utf8, utf8_to_b64url } from "metabase/lib/encoding";
+import { isPK } from "metabase-lib/v1/types/utils/isa";
+import type {
+ DatasetColumn,
+ Filter,
+ OrderBy,
+ RowValue,
+ RowValues,
+} from "metabase-types/api";
+
+import type { RowCellsWithPkValue } from "../api/types";
+
+export const serializeMbqlParam = (filterMbql: Array<any>): string => {
+ return utf8_to_b64url(JSON.stringify(filterMbql));
+};
+
+export const deserializeTableFilter = (filterParam: string): Filter | null => {
+ const maybeFilter = JSON.parse(b64hash_to_utf8(filterParam));
+
+ // simple and hacky way to test if param is a valid filter
+ return Array.isArray(maybeFilter) && typeof maybeFilter[0] === "string"
+ ? (maybeFilter as Filter)
+ : null;
+};
+
+export const deserializeTableSorting = (
+ sortingParam: string,
+): Array<OrderBy> | null => {
+ const maybeParam = JSON.parse(b64hash_to_utf8(sortingParam));
+
+ // simple and hacky way to test if param is valid
+ return Array.isArray(maybeParam) &&
+ Array.isArray(maybeParam[0]) &&
+ typeof maybeParam[0][0] === "string"
+ ? (maybeParam as Array<OrderBy>)
+ : null;
+};
+
+export const getPkColumns = (
+ columns: DatasetColumn[],
+): { indexes: number[]; names: string[] } => {
+ const pkColumnIndexes: number[] = [];
+ const pkColumnNames: string[] = [];
+
+ columns.forEach((col, index) => {
+ if (isPK(col)) {
+ pkColumnIndexes.push(index);
+ pkColumnNames.push(col.name);
+ }
+ });
+
+ return {
+ indexes: pkColumnIndexes,
+ names: pkColumnNames,
+ };
+};
+
+const getPkValuesKeyString = (pkValues: RowValue[]) => pkValues.join("-");
+
+export const getRowUniqueKeyByPkIndexes = (
+ pkColumnIndexes: number[],
+ rowData: RowValues,
+): string => {
+ const resultValues = pkColumnIndexes.map((pkIndex) => rowData[pkIndex]);
+
+ return getPkValuesKeyString(resultValues);
+};
+
+export const getRowObjectPkUniqueKeyByColumnNames = (
+ pkColumnNames: string[],
+ rowObject: RowCellsWithPkValue,
+): string => {
+ const pkValues: RowValue[] = pkColumnNames.map(
+ (colName) => rowObject[colName],
+ );
+
+ return getPkValuesKeyString(pkValues);
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/index.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/index.tsx
new file mode 100644
index 00000000000..9e5fdc4bacb
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/index.tsx
@@ -0,0 +1,17 @@
+import { PLUGIN_TABLE_EDITING } from "metabase/plugins";
+import { hasPremiumFeature } from "metabase-enterprise/settings";
+
+import { AdminDatabaseTableEditingSection } from "./admin/AdminDatabaseTableEditingSection";
+import { getRoutes } from "./routes";
+import { isDatabaseTableEditingEnabled } from "./settings";
+import { getTableEditUrl } from "./urls";
+
+if (hasPremiumFeature("table_data_editing")) {
+ PLUGIN_TABLE_EDITING.isEnabled = () => true;
+ PLUGIN_TABLE_EDITING.isDatabaseTableEditingEnabled =
+ isDatabaseTableEditingEnabled;
+ PLUGIN_TABLE_EDITING.getRoutes = getRoutes;
+ PLUGIN_TABLE_EDITING.getTableEditUrl = getTableEditUrl;
+ PLUGIN_TABLE_EDITING.AdminDatabaseTableEditingSection =
+ AdminDatabaseTableEditingSection;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx
new file mode 100644
index 00000000000..a5a4bea81de
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/ParameterActionInput.tsx
@@ -0,0 +1,85 @@
+import type { TableActionFormParameter } from "../api/types";
+import { TableActionFormInputType } from "../api/types";
+
+import {
+ TableActionInputBoolean,
+ type TableActionInputBooleanProps,
+} from "./TableActionInputBoolean";
+import {
+ TableActionInputDate,
+ type TableActionInputDateProps,
+} from "./TableActionInputDate";
+import {
+ TableActionInputDateTime,
+ type TableActionInputDateTimeProps,
+} from "./TableActionInputDateTime";
+import {
+ TableActionInputNumber,
+ type TableActionInputNumberProps,
+} from "./TableActionInputNumber";
+import {
+ TableActionInputSearchableSelect,
+ type TableActionInputSearchableSelectProps,
+} from "./TableActionInputSearchableSelect";
+import {
+ TableActionInputText,
+ type TableActionInputTextProps,
+} from "./TableActionInputText";
+import {
+ TableActionInputTextarea,
+ type TableActionInputTextareaProps,
+} from "./TableActionInputTextarea";
+
+type TableActionInputProps =
+ | TableActionInputDateProps
+ | TableActionInputDateTimeProps
+ | TableActionInputTextProps
+ | TableActionInputTextareaProps
+ | TableActionInputSearchableSelectProps
+ | TableActionInputBooleanProps
+ | TableActionInputNumberProps;
+
+export type ParameterActionInputProps = TableActionInputProps & {
+ parameter: TableActionFormParameter;
+};
+
+export function ParameterActionInput(props: ParameterActionInputProps) {
+ const { parameter, ...rest } = props;
+
+ switch (parameter.input_type) {
+ case TableActionFormInputType.Date:
+ return <TableActionInputDate {...rest} />;
+
+ case TableActionFormInputType.DateTime:
+ return <TableActionInputDateTime {...rest} />;
+
+ case TableActionFormInputType.Boolean:
+ return (
+ <TableActionInputBoolean {...rest} isNullable={parameter.nullable} />
+ );
+
+ case TableActionFormInputType.Integer:
+ return <TableActionInputNumber {...rest} allowDecimal={false} />;
+
+ case TableActionFormInputType.Float:
+ return <TableActionInputNumber {...rest} allowDecimal={true} />;
+
+ case TableActionFormInputType.Textarea:
+ return <TableActionInputTextarea {...rest} />;
+
+ case TableActionFormInputType.Dropdown:
+ if (parameter.field_id) {
+ return (
+ <TableActionInputSearchableSelect
+ {...rest}
+ fieldId={parameter.field_id}
+ searchFieldId={parameter.human_readable_field_id}
+ isNullable={parameter.nullable}
+ withCreateNew={!parameter.human_readable_field_id}
+ />
+ );
+ }
+ }
+
+ return <TableActionInputText {...rest} />;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputBoolean.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputBoolean.tsx
new file mode 100644
index 00000000000..33a9f2f9d41
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputBoolean.tsx
@@ -0,0 +1,116 @@
+import { useCallback, useMemo, useState } from "react";
+
+import { Combobox, Input, useCombobox } from "metabase/ui";
+import type { RowValue } from "metabase-types/api";
+
+import type { TableActionInputSharedProps } from "./types";
+
+export type TableActionInputBooleanProps = TableActionInputSharedProps & {
+ classNames?: {
+ wrapper?: string;
+ selectTextInputElement?: string;
+ };
+};
+
+const NULL_LABEL = "None";
+
+export const TableActionInputBoolean = ({
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ isNullable,
+ onEscape,
+ onBlur,
+ onChange,
+}: TableActionInputBooleanProps) => {
+ const [value, setValue] = useState(rowValueToOptionValue(initialValue));
+
+ const combobox = useCombobox({
+ defaultOpened: autoFocus,
+ });
+
+ const handleOptionSubmit = useCallback(
+ (value: string) => {
+ setValue(value);
+ onChange?.(optionValueToRowValue(value));
+ onBlur?.(optionValueToRowValue(value));
+ combobox.closeDropdown();
+ },
+ [setValue, onChange, onBlur, combobox],
+ );
+
+ const handleDismiss = useCallback(() => {
+ onEscape?.(optionValueToRowValue(value));
+ }, [onEscape, value]);
+
+ const options = useMemo(
+ () => [
+ { value: "true", label: "True" },
+ { value: "false", label: "False" },
+ ...(isNullable ? [{ value: "", label: NULL_LABEL }] : []),
+ ],
+ [isNullable],
+ );
+
+ return (
+ <Combobox
+ store={combobox}
+ position="bottom-start"
+ onOptionSubmit={handleOptionSubmit}
+ onDismiss={handleDismiss}
+ >
+ <Combobox.Target>
+ <Input
+ component="button"
+ type="button"
+ role="combobox"
+ pointer
+ onClick={() => combobox.openDropdown()}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.selectTextInputElement,
+ }}
+ style={{ overflow: "hidden" }} // for label truncation
+ {...inputProps}
+ >
+ {value ? (
+ value
+ ) : (
+ <Input.Placeholder c="text-light">{NULL_LABEL}</Input.Placeholder>
+ )}
+ </Input>
+ </Combobox.Target>
+
+ <Combobox.Dropdown mah="none" miw={150}>
+ <Combobox.Options p="0.5rem">
+ {options.map((item) => (
+ <Combobox.Option
+ selected={value === item.value}
+ value={item.value}
+ key={item.value}
+ >
+ {item.label}
+ </Combobox.Option>
+ ))}
+ </Combobox.Options>
+ </Combobox.Dropdown>
+ </Combobox>
+ );
+};
+
+function rowValueToOptionValue(value?: RowValue): string {
+ if (value === "true" || value === "false") {
+ return value;
+ }
+
+ return "";
+}
+
+function optionValueToRowValue(value?: string | null): string | null {
+ if (value === "true" || value === "false") {
+ return value;
+ }
+
+ return null;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDate.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDate.tsx
new file mode 100644
index 00000000000..429b1702327
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDate.tsx
@@ -0,0 +1,86 @@
+import { type KeyboardEvent, useCallback, useState } from "react";
+
+import { DEFAULT_DATE_STYLE } from "metabase/lib/formatting/datetime-utils";
+import { DateInput } from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+import {
+ DateValueTimezoneFormat,
+ useDateValueWithoutTimezone,
+} from "./use-date-value-without-timezone";
+
+export type TableActionInputDateProps = TableActionInputSharedProps & {
+ dateStyle?: string;
+ classNames?: {
+ wrapper?: string;
+ dateTextInputElement?: string;
+ };
+};
+
+export const TableActionInputDate = ({
+ dateStyle = DEFAULT_DATE_STYLE,
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ onChange,
+ onBlur,
+ onEscape,
+ onEnter,
+}: TableActionInputDateProps) => {
+ const { localDate: initialValueWithoutTimezone, restoreTimezone } =
+ useDateValueWithoutTimezone(initialValue ?? null, {
+ valueFormat: "YYYY-MM-DD",
+ timezoneFormat: DateValueTimezoneFormat.Never,
+ });
+
+ const [value, setValue] = useState(initialValueWithoutTimezone);
+ const [isFocused, setFocused] = useState(false);
+
+ const handleChange = useCallback(
+ (value: string | null) => {
+ setValue(value);
+ onChange?.(restoreTimezone(value));
+ },
+ [onChange, restoreTimezone],
+ );
+
+ const handleFocus = useCallback(() => {
+ setFocused(true);
+ }, [setFocused]);
+
+ const handleBlur = useCallback(() => {
+ setFocused(false);
+ onBlur?.(restoreTimezone(value));
+ }, [value, onBlur, restoreTimezone]);
+
+ const handleKeyUp = useCallback(
+ (event: KeyboardEvent) => {
+ if (event.key === "Escape") {
+ onEscape?.(restoreTimezone(value));
+ } else if (event.key === "Enter") {
+ onEnter?.(restoreTimezone(value));
+ }
+ },
+ [value, onEscape, onEnter, restoreTimezone],
+ );
+
+ return (
+ <DateInput
+ autoFocus={autoFocus}
+ value={value}
+ valueFormat={dateStyle}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.dateTextInputElement,
+ }}
+ // Keeps popover mounted when focused to improve time editing UX
+ popoverProps={{ opened: isFocused }}
+ onChange={handleChange}
+ onBlur={handleBlur}
+ onFocus={handleFocus}
+ onKeyUp={handleKeyUp}
+ {...inputProps}
+ />
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDateTime.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDateTime.tsx
new file mode 100644
index 00000000000..67e9b38a4ae
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputDateTime.tsx
@@ -0,0 +1,95 @@
+import { useDisclosure } from "@mantine/hooks";
+import { type KeyboardEvent, useCallback, useState } from "react";
+
+import {
+ DEFAULT_DATE_STYLE,
+ DEFAULT_TIME_STYLE,
+} from "metabase/lib/formatting/datetime-utils";
+import { DateTimePicker } from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+import { useDateValueWithoutTimezone } from "./use-date-value-without-timezone";
+
+export type TableActionInputDateTimeProps = TableActionInputSharedProps & {
+ dateTimeStyle?: string;
+ classNames?: {
+ wrapper?: string;
+ dateTextInputElement?: string;
+ };
+};
+
+const DEFAULT_DATETIME_STYLE = `${DEFAULT_DATE_STYLE}, ${DEFAULT_TIME_STYLE}`;
+
+export const TableActionInputDateTime = ({
+ dateTimeStyle = DEFAULT_DATETIME_STYLE,
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ onChange,
+ onEscape,
+ onEnter,
+ onBlur,
+}: TableActionInputDateTimeProps) => {
+ const { localDate: initialValueWithoutTimezone, restoreTimezone } =
+ useDateValueWithoutTimezone(initialValue ?? null);
+
+ const [value, setValue] = useState(initialValueWithoutTimezone ?? null);
+ const [dropdownOpened, dropdownHandlers] = useDisclosure(autoFocus);
+
+ const handleChange = useCallback(
+ (value: string | null) => {
+ setValue(value);
+ onChange?.(restoreTimezone(value));
+ },
+ [onChange, restoreTimezone],
+ );
+
+ const handleSubmit = useCallback(() => {
+ onBlur?.(restoreTimezone(value));
+ dropdownHandlers.close();
+ }, [value, onBlur, dropdownHandlers, restoreTimezone]);
+
+ const handleKeyUp = useCallback(
+ (event: KeyboardEvent) => {
+ if (event.key === "Enter") {
+ onEnter?.(restoreTimezone(value));
+ dropdownHandlers.close();
+ }
+ },
+ [value, onEnter, dropdownHandlers, restoreTimezone],
+ );
+
+ const handleDismiss = useCallback(() => {
+ onEscape?.(restoreTimezone(value));
+ dropdownHandlers.close();
+ }, [value, onEscape, dropdownHandlers, restoreTimezone]);
+
+ return (
+ <DateTimePicker
+ value={value}
+ valueFormat={dateTimeStyle}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.dateTextInputElement,
+ }}
+ onClick={dropdownHandlers.open}
+ submitButtonProps={{
+ variant: "light",
+ onClick: handleSubmit,
+ }}
+ popoverProps={{
+ opened: dropdownOpened,
+ onDismiss: handleDismiss,
+ styles: {
+ dropdown: {
+ "--popover-padding": "var(--mantine-spacing-md)",
+ },
+ },
+ }}
+ timePickerProps={{ format: "12h", onKeyUp: handleKeyUp }}
+ onChange={handleChange}
+ {...inputProps}
+ />
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputNumber.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputNumber.tsx
new file mode 100644
index 00000000000..eeac2334e5f
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputNumber.tsx
@@ -0,0 +1,82 @@
+import { useCallback, useState } from "react";
+
+import { NumberInput } from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+
+export type TableActionInputNumberProps = TableActionInputSharedProps & {
+ allowDecimal?: boolean;
+ hideControls?: boolean;
+ classNames?: {
+ wrapper?: string;
+ numberInputElement?: string;
+ };
+};
+
+export const TableActionInputNumber = ({
+ allowDecimal,
+ hideControls = true,
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ onEscape,
+ onEnter,
+ onBlur,
+ onChange,
+}: TableActionInputNumberProps) => {
+ const [value, setValue] = useState<string | number>(initialValue ?? "");
+
+ const handleChange = useCallback((value: string | number) => {
+ setValue(value);
+ }, []);
+
+ const handleChangeValue = useCallback(
+ ({ value }: { value: string }) => {
+ onChange?.(numberToRawValue(value));
+ },
+ [onChange],
+ );
+
+ const handleBlur = useCallback(() => {
+ onBlur?.(numberToRawValue(value));
+ }, [onBlur, value]);
+
+ const handleKeyUp = useCallback(
+ (event: React.KeyboardEvent<HTMLInputElement>) => {
+ if (event.key === "Escape") {
+ onEscape?.(numberToRawValue(value));
+ } else if (event.key === "Enter") {
+ onEnter?.(numberToRawValue(value));
+ }
+ },
+ [onEscape, onEnter, value],
+ );
+
+ return (
+ <NumberInput
+ hideControls={hideControls}
+ allowDecimal={allowDecimal}
+ defaultValue={(initialValue ?? "").toString()}
+ autoFocus={autoFocus}
+ onKeyUp={handleKeyUp}
+ // Somehow onChange triggers on the first blur, so we rely on onValueChange instead
+ onChange={handleChange}
+ onValueChange={handleChangeValue}
+ onBlur={handleBlur}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.numberInputElement,
+ }}
+ {...inputProps}
+ />
+ );
+};
+
+function numberToRawValue(value: string | number) {
+ if (value === "") {
+ return null;
+ }
+
+ return value.toString();
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputSearchableSelect.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputSearchableSelect.tsx
new file mode 100644
index 00000000000..6dcd1a5d0b9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputSearchableSelect.tsx
@@ -0,0 +1,216 @@
+import { useCallback, useMemo, useRef, useState } from "react";
+import { t } from "ttag";
+
+import {
+ Box,
+ Combobox,
+ Icon,
+ Input,
+ Loader,
+ Text,
+ TextInput,
+ useCombobox,
+} from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+import { useActionInputSearchableOptions } from "./use-action-input-searchable-options";
+
+export type TableActionInputSearchableSelectProps =
+ TableActionInputSharedProps & {
+ fieldId: number;
+ searchFieldId?: number;
+ withCreateNew?: boolean;
+ classNames?: {
+ wrapper?: string;
+ selectTextInputElement?: string;
+ selectLabel?: string;
+ };
+ };
+
+export const TableActionInputSearchableSelect = ({
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ isNullable,
+ fieldId,
+ searchFieldId,
+ withCreateNew,
+ onBlur,
+ onChange,
+ onEscape,
+}: TableActionInputSearchableSelectProps) => {
+ const searchInputRef = useRef<HTMLInputElement>(null);
+ const [value, setValue] = useState(initialValue?.toString() ?? "");
+ const [search, setSearch] = useState("");
+
+ const focusSearchInput = useCallback(() => {
+ setTimeout(() => searchInputRef.current?.focus(), 0);
+ }, []);
+
+ const combobox = useCombobox({
+ defaultOpened: autoFocus,
+ onDropdownOpen: focusSearchInput,
+ });
+
+ const { options, isLoading, isFetching } = useActionInputSearchableOptions({
+ initialValue,
+ search,
+ fieldId: fieldId,
+ searchFieldId: searchFieldId,
+ skipSearchQuery: !combobox.dropdownOpened,
+ });
+
+ const handleOptionSubmit = useCallback(
+ (value: string | null) => {
+ setValue(value ?? "");
+ onChange?.(value);
+ onBlur?.(value);
+ combobox.closeDropdown();
+ },
+ [setValue, onChange, onBlur, combobox],
+ );
+
+ const handleDismiss = useCallback(() => {
+ onEscape?.(value);
+ }, [onEscape, value]);
+
+ const inputLabel = useMemo(() => {
+ if (value) {
+ // Display remapped label if `searchFieldId` is provided
+ if (searchFieldId) {
+ const option = options.find((item) => item.value === value);
+ if (option) {
+ return (
+ <Text truncate="end" className={classNames?.selectLabel}>
+ {option.label}
+ </Text>
+ );
+ }
+ }
+
+ // Display the raw value if `searchFieldId` is not provided (e.g. for FKs)
+ else {
+ return (
+ <Text truncate="end" className={classNames?.selectLabel}>
+ {value}
+ </Text>
+ );
+ }
+ }
+
+ if (!value && inputProps?.placeholder) {
+ return (
+ <Input.Placeholder c="text-light" className={classNames?.selectLabel}>
+ {inputProps.placeholder}
+ </Input.Placeholder>
+ );
+ }
+
+ if (isLoading) {
+ return (
+ <Input.Placeholder c="text-light" className={classNames?.selectLabel}>
+ {t`Loading...`}
+ </Input.Placeholder>
+ );
+ }
+
+ // Fallback to the raw value instead of a label
+ if (value) {
+ return (
+ <Text truncate="end" className={classNames?.selectLabel}>
+ {value}
+ </Text>
+ );
+ }
+
+ return null;
+ }, [
+ isLoading,
+ value,
+ inputProps?.placeholder,
+ options,
+ searchFieldId,
+ classNames?.selectLabel,
+ ]);
+
+ const shouldDisplayClearButton =
+ isNullable && !!value && !inputProps?.disabled;
+
+ return (
+ <Combobox
+ store={combobox}
+ position="bottom-start"
+ onOptionSubmit={handleOptionSubmit}
+ onDismiss={handleDismiss}
+ >
+ <Combobox.Target>
+ <Input
+ component="button"
+ type="button"
+ role="combobox"
+ pointer
+ onClick={() => combobox.openDropdown()}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.selectTextInputElement,
+ }}
+ style={{ overflow: "hidden" }} // for label truncation
+ rightSectionPointerEvents="all"
+ rightSection={
+ shouldDisplayClearButton && (
+ <Icon
+ name="close"
+ color="var(--mb-color-text-light)"
+ onClick={() => handleOptionSubmit(null)}
+ onMouseDown={(event) => event.stopPropagation()}
+ />
+ )
+ }
+ {...inputProps}
+ >
+ {inputLabel}
+ </Input>
+ </Combobox.Target>
+
+ <Combobox.Dropdown mah="none" miw={250}>
+ <Box p="0.5rem" pb="0" bg="white" pos="sticky" top={0}>
+ <TextInput
+ value={search}
+ onChange={(event) => setSearch(event.currentTarget.value)}
+ placeholder={t`Search the list`}
+ leftSection={<Icon name="search" />}
+ rightSection={isFetching ? <Loader size="xs" /> : undefined}
+ // Auto focus works when dropdown is initially rendered as opened (e.g. inline editing)
+ // whereas `onDropdownOpen` is called only when dropdown is opened by clicking the button (e.g. modal editing)
+ autoFocus
+ ref={searchInputRef}
+ />
+ </Box>
+
+ <Combobox.Options p="0.5rem">
+ {options.length > 0 ? (
+ options.map((item) => (
+ <Combobox.Option
+ selected={value === item.value}
+ value={item.value}
+ key={item.value}
+ >
+ {item.label}
+ {searchFieldId ? ` [${item.value}]` : ""}
+ </Combobox.Option>
+ ))
+ ) : isLoading ? (
+ <Combobox.Empty>{t`Loading values...`}</Combobox.Empty>
+ ) : withCreateNew ? (
+ <Combobox.Option value={search}>
+ {t`Add option:`} <strong>{search}</strong>
+ </Combobox.Option>
+ ) : (
+ <Combobox.Empty>{t`Nothing found`}</Combobox.Empty>
+ )}
+ </Combobox.Options>
+ </Combobox.Dropdown>
+ </Combobox>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputText.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputText.tsx
new file mode 100644
index 00000000000..4fb9f68cceb
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputText.tsx
@@ -0,0 +1,46 @@
+import { Input } from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+
+export type TableActionInputTextProps = TableActionInputSharedProps & {
+ classNames?: {
+ wrapper?: string;
+ textInputElement?: string;
+ };
+};
+
+export const TableActionInputText = ({
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ onEscape,
+ onEnter,
+ onBlur,
+ onChange,
+}: TableActionInputTextProps) => {
+ return (
+ <Input
+ defaultValue={(initialValue ?? "").toString()}
+ autoFocus={autoFocus}
+ onKeyUp={(event) => {
+ if (event.key === "Escape") {
+ onEscape?.(event.currentTarget.value);
+ } else if (event.key === "Enter") {
+ onEnter?.(event.currentTarget.value);
+ }
+ }}
+ onChange={(event) => {
+ onChange?.(event.currentTarget.value);
+ }}
+ onBlur={(event) => {
+ onBlur?.(event.currentTarget.value);
+ }}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.textInputElement,
+ }}
+ {...inputProps}
+ />
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputTextarea.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputTextarea.tsx
new file mode 100644
index 00000000000..6afc176a376
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/TableActionInputTextarea.tsx
@@ -0,0 +1,58 @@
+import { Textarea } from "metabase/ui";
+
+import type { TableActionInputSharedProps } from "./types";
+
+const DEFAULT_MAX_ROWS = 8;
+const DEFAULT_MIN_ROWS = 1;
+
+export type TableActionInputTextareaProps = TableActionInputSharedProps & {
+ minRows?: number;
+ maxRows?: number;
+ classNames?: {
+ wrapper?: string;
+ textInputElement?: string;
+ };
+};
+
+export const TableActionInputTextarea = ({
+ minRows = DEFAULT_MIN_ROWS,
+ maxRows = DEFAULT_MAX_ROWS,
+ autoFocus,
+ inputProps,
+ initialValue,
+ classNames,
+ onBlur,
+ onEscape,
+ onChange,
+}: TableActionInputTextareaProps) => {
+ return (
+ <Textarea
+ defaultValue={(initialValue ?? "").toString()}
+ autoFocus={autoFocus}
+ onKeyUp={(event) => {
+ if (event.key === "Escape") {
+ onEscape?.(event.currentTarget.value);
+ }
+ // We skip handling Enter here because it might break the
+ // default behavior of the Textarea and we don't want to
+ // override it.
+ }}
+ onChange={(event) => {
+ onChange?.(event.currentTarget.value);
+ }}
+ onBlur={(event) => {
+ onBlur?.(event.currentTarget.value);
+ }}
+ classNames={{
+ wrapper: classNames?.wrapper,
+ input: classNames?.textInputElement,
+ }}
+ {...inputProps}
+ // Match the line height for single line text (should look like a regular input)
+ styles={{ input: { lineHeight: "165%" } }}
+ maxRows={maxRows}
+ minRows={minRows}
+ autosize
+ />
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/types.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/types.ts
new file mode 100644
index 00000000000..ee6f1c23ca2
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/types.ts
@@ -0,0 +1,26 @@
+import type { MantineSize, TextInputProps } from "metabase/ui";
+
+export type TableActionInputSharedProps = {
+ autoFocus?: boolean;
+ initialValue?: string;
+ inputProps?: InputProps;
+ isNullable?: boolean;
+ onChange?: (value: string | null) => unknown;
+ onBlur?: (value: string | null) => unknown;
+ onEscape?: (value: string | null) => unknown;
+ onEnter?: (value: string | null) => unknown;
+ onSelect?: (value: string | null) => unknown;
+};
+
+type InputProps = {
+ size?: MantineSize;
+ variant?: TextInputProps["variant"];
+ placeholder?: string;
+ disabled?: boolean;
+ error?: TextInputProps["error"];
+ leftSection?: TextInputProps["leftSection"];
+ leftSectionPointerEvents?: TextInputProps["leftSectionPointerEvents"];
+ rightSection?: TextInputProps["rightSection"];
+ rightSectionPointerEvents?: TextInputProps["rightSectionPointerEvents"];
+ "data-testid"?: string;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-action-input-searchable-options.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-action-input-searchable-options.tsx
new file mode 100644
index 00000000000..71588a44a47
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-action-input-searchable-options.tsx
@@ -0,0 +1,121 @@
+import { useEffect, useMemo, useState } from "react";
+
+import {
+ useGetRemappedFieldValueQuery,
+ useSearchFieldValuesQuery,
+} from "metabase/api/field";
+import { useDebouncedValue } from "metabase/common/hooks/use-debounced-value";
+import { getFieldOptions } from "metabase/querying/filters/components/FilterValuePicker/utils";
+import type { FieldValue, RowValue } from "metabase-types/api";
+
+const SEARCH_LIMIT_DEFAULT = 20;
+const SEARCH_DEBOUNCE = 500;
+
+type UseActionInputSearchableOptionsProps = {
+ initialValue?: RowValue;
+ search: string;
+ fieldId: number;
+ searchFieldId?: number;
+ limit?: number;
+ skipSearchQuery?: boolean;
+};
+
+export function useActionInputSearchableOptions({
+ initialValue,
+ search,
+ fieldId,
+ searchFieldId = fieldId,
+ limit = SEARCH_LIMIT_DEFAULT,
+ skipSearchQuery = false,
+}: UseActionInputSearchableOptionsProps) {
+ const debouncedSearch = useDebouncedValue(search, SEARCH_DEBOUNCE);
+ const [searchValueToFetch, setSearchValueToFetch] = useState(debouncedSearch);
+
+ // Values that are fetched without search value (all values with some limit)
+ const [initialFieldValues, setInitialFieldValues] = useState<
+ FieldValue[] | undefined
+ >(undefined);
+
+ // Local search is performed if there are less than `limit` initial values
+ const shouldPerformLocalSearch =
+ initialFieldValues && initialFieldValues.length < limit;
+
+ // Update the search value to fetch on every debounced search value change
+ // if the local search is disabled
+ useEffect(() => {
+ if (!shouldPerformLocalSearch) {
+ setSearchValueToFetch(debouncedSearch);
+ }
+ }, [debouncedSearch, shouldPerformLocalSearch]);
+
+ const {
+ data: fieldValues,
+ isLoading,
+ isFetching,
+ } = useSearchFieldValuesQuery(
+ {
+ fieldId: fieldId,
+ searchFieldId: searchFieldId,
+ // empty string is invalid from the API perspective
+ value: searchValueToFetch === "" ? undefined : searchValueToFetch,
+ limit: SEARCH_LIMIT_DEFAULT,
+ },
+ { skip: skipSearchQuery },
+ );
+
+ const { data: initialFieldValue } = useGetRemappedFieldValueQuery(
+ {
+ fieldId,
+ remappedFieldId: searchFieldId,
+ value: initialValue?.toString() ?? "",
+ },
+ { skip: !initialValue || fieldId === searchFieldId },
+ );
+
+ useEffect(() => {
+ if (!initialFieldValues && fieldValues) {
+ setInitialFieldValues(fieldValues);
+ }
+ }, [initialFieldValues, fieldValues]);
+
+ const options = useMemo(() => {
+ if (fieldValues) {
+ const mergedFieldValues = fieldValues.slice();
+ if (initialFieldValue) {
+ const hasInitialValueInFieldValues = !!mergedFieldValues.find(
+ ([value]) => value === initialFieldValue[0],
+ );
+
+ if (!hasInitialValueInFieldValues) {
+ mergedFieldValues.push(initialFieldValue);
+ }
+ }
+
+ const options = getFieldOptions(mergedFieldValues);
+
+ if (shouldPerformLocalSearch) {
+ const searchValue = search.toLowerCase().trim();
+ return options.filter(
+ (item) =>
+ item.label.toLowerCase().includes(searchValue) ||
+ item.value.toLowerCase().includes(searchValue),
+ );
+ }
+
+ return options;
+ }
+
+ if (initialFieldValue) {
+ return getFieldOptions([initialFieldValue]);
+ }
+
+ return [];
+ }, [shouldPerformLocalSearch, fieldValues, initialFieldValue, search]);
+
+ return {
+ options,
+ isLoading,
+ isFetching:
+ (isFetching || search !== debouncedSearch) && !shouldPerformLocalSearch,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-date-value-without-timezone.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-date-value-without-timezone.ts
new file mode 100644
index 00000000000..8971933560f
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/inputs/use-date-value-without-timezone.ts
@@ -0,0 +1,56 @@
+import dayjs from "dayjs";
+import { useCallback, useMemo } from "react";
+
+export enum DateValueTimezoneFormat {
+ Always,
+ Never,
+ NonZero,
+}
+
+type UseDateValueWithoutTimezoneOptions = {
+ valueFormat?: string;
+ timezoneFormat?: DateValueTimezoneFormat;
+};
+
+export const useDateValueWithoutTimezone = (
+ value: string | null,
+ {
+ valueFormat = "YYYY-MM-DDTHH:mm:ss",
+ timezoneFormat = DateValueTimezoneFormat.NonZero,
+ }: UseDateValueWithoutTimezoneOptions = {},
+) => {
+ const { timezoneOffsetString, timezoneOffset, localDate } = useMemo(() => {
+ // If no value, we fall back to the browser timezone
+ const parsedDate = dayjs.parseZone(value ?? new Date());
+
+ const timezoneOffset = parsedDate.utcOffset();
+ const timezoneOffsetString = parsedDate.format("Z");
+
+ return {
+ timezoneOffset,
+ timezoneOffsetString,
+ localDate: value ? dayjs.parseZone(value).format(valueFormat) : null,
+ };
+ }, [value, valueFormat]);
+
+ const restoreTimezone = useCallback(
+ (value: string | null) => {
+ if (!value) {
+ return null;
+ }
+
+ if (
+ timezoneFormat === DateValueTimezoneFormat.Never ||
+ (timezoneFormat === DateValueTimezoneFormat.NonZero &&
+ timezoneOffset === 0)
+ ) {
+ return dayjs(value).format(valueFormat);
+ }
+
+ return dayjs(value).format(valueFormat) + timezoneOffsetString;
+ },
+ [valueFormat, timezoneOffsetString, timezoneOffset, timezoneFormat],
+ );
+
+ return { localDate, timezoneOffsetString, restoreTimezone };
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/routes.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/routes.tsx
new file mode 100644
index 00000000000..91c1d8b0aff
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/routes.tsx
@@ -0,0 +1,12 @@
+import { Route } from "react-router";
+
+import { EditTableDataContainer } from "./table-edit/EditTableDataContainer";
+
+export function getRoutes() {
+ return (
+ <Route
+ path="databases/:dbId/tables/:tableId/edit(/:objectId)"
+ component={EditTableDataContainer}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/settings.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/settings.tsx
new file mode 100644
index 00000000000..06b161f8e6d
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/settings.tsx
@@ -0,0 +1,7 @@
+import type { Database } from "metabase-types/api";
+
+export const DATABASE_TABLE_EDITING_SETTING = "database-enable-table-editing";
+
+export function isDatabaseTableEditingEnabled(database: Database) {
+ return database.settings?.[DATABASE_TABLE_EDITING_SETTING] ?? false;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.module.css
new file mode 100644
index 00000000000..3cf02c3cf05
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.module.css
@@ -0,0 +1,27 @@
+.container {
+ height: 100%;
+ width: 100%;
+ overflow: hidden;
+}
+
+.gridWrapper {
+ overflow: hidden;
+ flex-grow: 1;
+ background-color: var(--mb-color-background);
+}
+
+.gridFooter {
+ border: 1px solid var(--mb-color-border);
+ background-color: var(--mb-color-background);
+ color: var(--mb-color-text-secondary);
+ padding: 0.5rem 1.5rem;
+ padding-left: 0.5rem;
+ height: 2.5rem;
+ justify-content: flex-end;
+ align-items: center;
+}
+
+.filtersSection {
+ background-color: var(--mb-color-background);
+ border-top: 1px solid var(--mb-color-border);
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.tsx
new file mode 100644
index 00000000000..ad91335d297
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataContainer.tsx
@@ -0,0 +1,295 @@
+import { useDisclosure } from "@mantine/hooks";
+import type { Location } from "history";
+import { useCallback, useMemo } from "react";
+import { msgid, ngettext, t } from "ttag";
+
+import { useGetDatabaseQuery, useGetTableQuery } from "metabase/api";
+import {
+ BulkActionBar,
+ BulkActionButton,
+} from "metabase/common/components/BulkActionBar";
+import { GenericError } from "metabase/common/components/ErrorPages";
+import { useCloseNavbarOnMount } from "metabase/common/hooks/use-close-navbar-on-mount";
+import { useSelector } from "metabase/lib/redux";
+import { getUserIsAdmin } from "metabase/selectors/user";
+import { Box, Flex, Stack, Text } from "metabase/ui";
+import { extractRemappedColumns } from "metabase/visualizations";
+
+import type { TableEditingActionScope } from "../api/types";
+import { TableHeader } from "../common/TableHeader";
+import { getRowCountMessage } from "../common/getRowCountMessage";
+import { isDatabaseTableEditingEnabled } from "../settings";
+
+import S from "./EditTableDataContainer.module.css";
+import { EditTableDataHeader } from "./EditTableDataHeader";
+import { EditTableDataOverlay } from "./EditTableDataOverlay";
+import { EditTableDataGrid } from "./data-grid/EditTableDataGrid";
+import { CreateRowActionFormModal } from "./modals/CreateRowActionFormModal";
+import { DeleteBulkRowConfirmationModal } from "./modals/DeleteBulkRowConfirmationModal";
+import { UpdateRowActionFormModal } from "./modals/UpdateRowActionFormModal";
+import { useEditTableData } from "./use-edit-table-data";
+import { useEditTableLoadingOverlay } from "./use-edit-table-loading-overlay";
+import { useTableCreateRow } from "./use-table-create-row";
+import { useTableCRUD } from "./use-table-crud";
+import {
+ getRowInputAndParamsFromRow,
+ useTableExpandedUpdateRow,
+} from "./use-table-expanded-update-row";
+import { useEditingTableRowSelection } from "./use-table-row-selection";
+import { useTableEditingStateAdHocQueryUpdateStrategy } from "./use-table-state-adhoc-query-update-strategy";
+
+type EditTableDataContainerProps = {
+ params: {
+ dbId: string;
+ tableId: string;
+ objectId?: string;
+ };
+ location: Location<{ filter?: string }>;
+};
+
+export const EditTableDataContainer = ({
+ params: { dbId: dbIdParam, tableId: tableIdParam },
+ location,
+}: EditTableDataContainerProps) => {
+ useCloseNavbarOnMount();
+
+ const databaseId = parseInt(dbIdParam, 10);
+ const tableId = parseInt(tableIdParam, 10);
+
+ const { data: database } = useGetDatabaseQuery({ id: databaseId });
+ const { data: table } = useGetTableQuery({ id: tableId });
+
+ const {
+ data: rawDataset,
+ isLoading,
+ isFetching,
+ tableQuestion,
+ tableQuery,
+ getColumnSortDirection,
+ handleTableQuestionChange,
+ handleChangeColumnSort,
+ } = useEditTableData({
+ tableId,
+ databaseId,
+ location,
+ });
+
+ const datasetData = useMemo(() => {
+ return rawDataset?.data
+ ? extractRemappedColumns(rawDataset.data)
+ : undefined;
+ }, [rawDataset]);
+
+ const stateUpdateStrategy =
+ useTableEditingStateAdHocQueryUpdateStrategy(tableQuery);
+
+ const scope = useMemo<TableEditingActionScope>(
+ () => ({ "table-id": tableId }),
+ [tableId],
+ );
+
+ const {
+ isInserting,
+ isUpdating,
+ isDeleting,
+ handleRowCreate,
+ handleRowUpdate,
+ handleRowDelete,
+ handleRowDeleteBulk,
+ } = useTableCRUD({
+ scope,
+ datasetData,
+ stateUpdateStrategy,
+ });
+
+ const loadingOverlayProps = useEditTableLoadingOverlay({
+ isDatasetLoading: isLoading,
+ isDatasetFetching: isFetching,
+ });
+
+ const {
+ isCreateRowModalOpen,
+ openCreateRowModal,
+ closeCreateRowModal,
+ formDescription: createFromDescription,
+ } = useTableCreateRow({ scope });
+
+ const {
+ expandedRow,
+ handleExpandRow,
+ handleExpandedRowUpdate,
+ handleExpandedRowDelete,
+ closeExpandedRow,
+ formDescription: updateFormDescription,
+ } = useTableExpandedUpdateRow({
+ scope,
+ datasetData,
+ handleRowUpdate,
+ handleRowDelete,
+ });
+
+ const [deleteBulkRequested, deleteBulkModalController] = useDisclosure(false);
+ const { rowSelection, selectedRowIndices, setRowSelection } =
+ useEditingTableRowSelection();
+
+ const handleDeleteBulkSubmit = useCallback(async () => {
+ if (!datasetData) {
+ return;
+ }
+
+ const { cols, rows } = datasetData;
+ const inputs = selectedRowIndices.map(
+ (index) => getRowInputAndParamsFromRow(cols, rows[index]).input,
+ );
+
+ const success = await handleRowDeleteBulk(inputs);
+ if (success) {
+ setRowSelection({});
+ deleteBulkModalController.close();
+ }
+ }, [
+ datasetData,
+ setRowSelection,
+ selectedRowIndices,
+ handleRowDeleteBulk,
+ deleteBulkModalController,
+ ]);
+
+ const isAdmin = useSelector(getUserIsAdmin);
+ if (!isAdmin) {
+ return (
+ <EditTablePermissionError
+ databaseId={databaseId}
+ tableId={tableId}
+ title={t`You are not allowed to edit this table`}
+ message={t`Only admin users are authorized to edit tables`}
+ />
+ );
+ }
+
+ if (database && !isDatabaseTableEditingEnabled(database)) {
+ return (
+ <EditTablePermissionError
+ databaseId={databaseId}
+ tableId={tableId}
+ title={t`Table editing is not enabled for this database`}
+ message={t`Please ask your admin to enable table editing`}
+ />
+ );
+ }
+
+ if (table && !table.is_writable) {
+ return (
+ <EditTablePermissionError
+ databaseId={databaseId}
+ tableId={tableId}
+ title={t`This table is read-only`}
+ message={t`Please ask your admin to configure the connection to allow editing`}
+ />
+ );
+ }
+
+ return (
+ <Stack gap={0} data-testid="edit-table-data-root" className={S.container}>
+ <EditTableDataHeader
+ databaseId={databaseId}
+ tableId={tableId}
+ question={tableQuestion}
+ onQuestionChange={handleTableQuestionChange}
+ onCreate={openCreateRowModal}
+ />
+ <Box pos="relative" className={S.gridWrapper}>
+ <EditTableDataOverlay {...loadingOverlayProps} />
+ {datasetData && (
+ <EditTableDataGrid
+ handleRowUpdate={handleRowUpdate}
+ updateFormDescription={updateFormDescription}
+ data={datasetData}
+ getColumnSortDirection={getColumnSortDirection}
+ onColumnSort={handleChangeColumnSort}
+ onRowExpandClick={handleExpandRow}
+ rowSelection={rowSelection}
+ onRowSelectionChange={setRowSelection}
+ />
+ )}
+ </Box>
+
+ {rawDataset && (
+ <Flex className={S.gridFooter}>
+ <Text fw="bold" size="md" c="inherit" component="span">
+ {getRowCountMessage(rawDataset)}
+ </Text>
+ </Flex>
+ )}
+
+ <BulkActionBar
+ opened={selectedRowIndices.length > 0 && !deleteBulkRequested}
+ message={ngettext(
+ msgid`Selected ${selectedRowIndices.length} row`,
+ `Selected ${selectedRowIndices.length} rows`,
+ selectedRowIndices.length,
+ )}
+ >
+ <BulkActionButton onClick={deleteBulkModalController.open}>
+ {t`Delete`}
+ </BulkActionButton>
+ </BulkActionBar>
+
+ <CreateRowActionFormModal
+ opened={isCreateRowModalOpen}
+ description={createFromDescription}
+ onClose={closeCreateRowModal}
+ onSubmit={handleRowCreate}
+ isLoading={isInserting}
+ />
+
+ <UpdateRowActionFormModal
+ opened={!!expandedRow}
+ initialValues={expandedRow?.params}
+ description={updateFormDescription}
+ onClose={closeExpandedRow}
+ onSubmit={handleExpandedRowUpdate}
+ onDelete={handleExpandedRowDelete}
+ isLoading={isUpdating}
+ isDeleting={isDeleting}
+ />
+
+ <DeleteBulkRowConfirmationModal
+ opened={deleteBulkRequested}
+ rowCount={selectedRowIndices.length}
+ onConfirm={handleDeleteBulkSubmit}
+ isLoading={isDeleting}
+ onClose={deleteBulkModalController.close}
+ />
+ </Stack>
+ );
+};
+
+type EditTablePermissionErrorProps = {
+ databaseId: number;
+ tableId: number;
+ title: string;
+ message: string;
+};
+
+function EditTablePermissionError({
+ databaseId,
+ tableId,
+ title,
+ message,
+}: EditTablePermissionErrorProps) {
+ return (
+ <Stack
+ gap={0}
+ data-testid="edit-table-data-restricted"
+ className={S.container}
+ >
+ <TableHeader
+ databaseId={databaseId}
+ tableId={tableId}
+ showEditBreadcrumb
+ />
+ <GenericError title={title} message={message} details={undefined} />
+ </Stack>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.module.css
new file mode 100644
index 00000000000..5c22010b64a
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.module.css
@@ -0,0 +1,40 @@
+/* copied from metabase/query_builder/components/view/ViewHeader/ViewTitleHeader.module.css */
+
+.FilterButton {
+ &[data-variant="default"] {
+ transition: background 300ms linear;
+
+ &:hover {
+ color: var(--mb-color-filter);
+ background-color: color-mix(in srgb, var(--mb-color-filter) 10%, white);
+ }
+ }
+}
+
+.FilterButtonAttachment {
+ &[data-variant="default"] {
+ padding: 0.5rem;
+ transition: background 300ms linear;
+
+ &:hover {
+ background-color: color-mix(in srgb, var(--mb-color-filter) 10%, white);
+ }
+
+ &:hover,
+ &[data-expanded="true"] {
+ .FilterCountChip {
+ color: var(--mb-color-text-selected);
+ background-color: var(--mb-color-filter);
+ }
+ }
+ }
+}
+
+.FilterCountChip {
+ font-size: 0.6875rem;
+ border-radius: 10px;
+ line-height: 1rem;
+ padding-inline: 0.5rem;
+ color: var(--mb-color-text-primary);
+ background-color: color-mix(in srgb, var(--mb-color-filter) 15%, white);
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.tsx
new file mode 100644
index 00000000000..f6d626630cd
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataFilterButton.tsx
@@ -0,0 +1,83 @@
+import { useDisclosure } from "@mantine/hooks";
+import cx from "classnames";
+import { useMemo } from "react";
+import { t } from "ttag";
+
+import { getFilterItems } from "metabase/querying/filters/components/FilterPanel/utils";
+import { MultiStageFilterPicker } from "metabase/querying/filters/components/FilterPicker/MultiStageFilterPicker";
+import { Button, Icon, Popover, Tooltip } from "metabase/ui";
+import type * as Lib from "metabase-lib";
+import type Question from "metabase-lib/v1/Question";
+
+import ViewTitleHeaderS from "./EditTableDataFilterButton.module.css";
+
+interface EditTableDataFilterButtonProps {
+ className?: string;
+ question: Question;
+ isExpanded: boolean;
+ onExpand: () => void;
+ onCollapse: () => void;
+ onQuestionChange: (newQuestion: Question) => void;
+}
+
+// copied from metabase/query_builder/components/view/ViewHeader/components/FilterHeaderButton.tsx
+export function EditTableDataFilterButton({
+ className,
+ question,
+ isExpanded,
+ onExpand,
+ onCollapse,
+ onQuestionChange,
+}: EditTableDataFilterButtonProps) {
+ const [isOpened, { close, toggle }] = useDisclosure();
+ const query = question.query();
+ const items = useMemo(() => (query ? getFilterItems(query) : []), [query]);
+ const hasFilters = items.length > 0;
+ const label = isExpanded ? t`Hide filters` : t`Show filters`;
+
+ const handleQueryChange = (newQuery: Lib.Query) => {
+ const newQuestion = question.setQuery(newQuery);
+ onQuestionChange(newQuestion);
+ };
+
+ return (
+ <Button.Group>
+ <Popover opened={isOpened} position="bottom-start" onDismiss={close}>
+ <Popover.Target>
+ <Button
+ className={cx(className, ViewTitleHeaderS.FilterButton)}
+ leftSection={<Icon name={hasFilters ? "filter_plus" : "filter"} />}
+ onClick={toggle}
+ data-testid="table-edit-filter-header"
+ >
+ {t`Filter`}
+ </Button>
+ </Popover.Target>
+ <Popover.Dropdown>
+ <MultiStageFilterPicker
+ query={query}
+ canAppendStage={false}
+ onChange={handleQueryChange}
+ onClose={close}
+ />
+ </Popover.Dropdown>
+ </Popover>
+ {hasFilters && (
+ <Tooltip label={label}>
+ <Button
+ aria-label={label}
+ className={ViewTitleHeaderS.FilterButtonAttachment}
+ onClick={isExpanded ? onCollapse : onExpand}
+ data-testid="filters-visibility-control"
+ data-expanded={isExpanded}
+ style={{ borderLeft: "none" }} // mantine puts a double border between buttons in groups
+ >
+ <div className={ViewTitleHeaderS.FilterCountChip}>
+ {items?.length}
+ </div>
+ </Button>
+ </Tooltip>
+ )}
+ </Button.Group>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataHeader.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataHeader.tsx
new file mode 100644
index 00000000000..27fac1b1c22
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataHeader.tsx
@@ -0,0 +1,64 @@
+import { useDisclosure } from "@mantine/hooks";
+import { t } from "ttag";
+
+import { QuestionFiltersHeader } from "metabase/query_builder/components/view/ViewHeader/components";
+import { Box, Button, Icon, Stack } from "metabase/ui";
+import type Question from "metabase-lib/v1/Question";
+
+import { TableHeader } from "../common/TableHeader";
+
+import S from "./EditTableDataContainer.module.css";
+import { EditTableDataFilterButton } from "./EditTableDataFilterButton";
+
+interface EditTableDataHeaderProps {
+ databaseId: number;
+ tableId: number;
+ question?: Question;
+ onQuestionChange: (newQuestion: Question) => void;
+ onCreate: () => void;
+}
+
+export const EditTableDataHeader = ({
+ databaseId,
+ tableId,
+ question,
+ onQuestionChange,
+ onCreate,
+}: EditTableDataHeaderProps) => {
+ const [
+ areFiltersExpanded,
+ { open: onExpandFilters, close: onCollapseFilters },
+ ] = useDisclosure(true);
+
+ return (
+ <Stack gap={0}>
+ <TableHeader databaseId={databaseId} tableId={tableId} showEditBreadcrumb>
+ {question && (
+ <EditTableDataFilterButton
+ question={question}
+ isExpanded={areFiltersExpanded}
+ onExpand={onExpandFilters}
+ onCollapse={onCollapseFilters}
+ onQuestionChange={onQuestionChange}
+ />
+ )}
+
+ <Button
+ leftSection={<Icon name="add" />}
+ variant="filled"
+ onClick={onCreate}
+ data-testid="new-record-button"
+ >{t`New record`}</Button>
+ </TableHeader>
+ {question && (
+ <Box className={S.filtersSection}>
+ <QuestionFiltersHeader
+ expanded={areFiltersExpanded}
+ question={question}
+ updateQuestion={onQuestionChange}
+ />
+ </Box>
+ )}
+ </Stack>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.module.css
new file mode 100644
index 00000000000..65070e35c02
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.module.css
@@ -0,0 +1,29 @@
+.overlay {
+ position: absolute;
+ top: 0;
+ left: 0;
+ right: 0;
+ bottom: 0;
+ /* +1 for DataGrid `headerContainer` (which has z-index: 2) */
+ z-index: 3;
+ display: flex;
+ flex-direction: column;
+ justify-content: center;
+ align-items: center;
+ gap: 1.5rem;
+ color: var(--mb-color-brand);
+ background-color: color-mix(
+ in srgb,
+ var(--mb-color-bg-white) 75%,
+ transparent
+ );
+ transition:
+ opacity 0.5s,
+ background-color 0.5s;
+}
+
+.message {
+ font-size: 1.25rem;
+ font-weight: 700;
+ color: var(--mb-color-brand);
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.tsx
new file mode 100644
index 00000000000..5f07567b078
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/EditTableDataOverlay.tsx
@@ -0,0 +1,25 @@
+import LoadingSpinner from "metabase/common/components/LoadingSpinner";
+import { Flex, Text } from "metabase/ui";
+
+import S from "./EditTableDataOverlay.module.css";
+
+type EditTableDataOverlayProps = {
+ message?: string;
+ show?: boolean;
+};
+
+export function EditTableDataOverlay({
+ show = true,
+ message,
+}: EditTableDataOverlayProps) {
+ if (!show) {
+ return null;
+ }
+
+ return (
+ <Flex className={S.overlay}>
+ <LoadingSpinner />
+ {message && <Text className={S.message}>{message}</Text>}
+ </Flex>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.module.css
new file mode 100644
index 00000000000..94cdeeaf1c4
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.module.css
@@ -0,0 +1,68 @@
+/** DataGrid overrides */
+
+.tableRow {
+ &:hover .tableBodyCell {
+ background-color: var(--mb-base-color-blue-5);
+ }
+
+ &:hover .tableHeaderCell {
+ /* Draggable hover highlight for `SortableHeader` */
+ box-shadow: none;
+ }
+
+ &[data-row-selected="true"] .tableBodyCell {
+ background-color: var(--mb-base-color-blue-5);
+ }
+}
+
+.tableHeaderContainer {
+ --cell-bg-color: var(--mb-base-color-orion-5);
+ --cell-hover-bg-color: var(--mb-base-color-orion-5);
+
+ background-color: var(--mb-base-color-orion-5);
+
+ & .tableRow {
+ background-color: transparent;
+ }
+}
+
+.tableHeaderCell {
+ border-right: 1px solid var(--mb-color-border);
+ font-weight: 700;
+
+ &:hover {
+ color: var(--mb-color-brand);
+ }
+}
+
+.tableBodyContainer {
+ background-color: var(--mb-color-bg-white);
+}
+
+.tableBodyCell {
+ background-color: var(--mb-color-bg-white);
+ border-right: 1px solid var(--mb-color-border);
+}
+
+.headerCellContainer {
+ background-color: var(--cell-bg-color);
+ height: 100%;
+ display: flex;
+ align-items: center;
+ padding: 0 0.75rem;
+}
+
+.sortIndicator {
+ flex-shrink: 0;
+ margin-left: 0.25rem;
+}
+
+.pencilIcon {
+ cursor: pointer;
+ color: var(--mb-color-brand);
+ visibility: hidden;
+}
+
+.tableRow:hover .pencilIcon {
+ visibility: visible;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.tsx
new file mode 100644
index 00000000000..e597c656136
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/EditTableDataGrid.tsx
@@ -0,0 +1,148 @@
+import type { OnChangeFn, RowSelectionState } from "@tanstack/react-table";
+import { useCallback, useMemo } from "react";
+
+import { DataGrid, useDataGridInstance } from "metabase/data-grid";
+import type { DatasetColumn, DatasetData } from "metabase-types/api";
+
+import type { DescribeActionFormResponse } from "../../api/types";
+import type { TableDataGetColumnSortDirection } from "../use-edit-table-data";
+import type { TableRowUpdateHandler } from "../use-table-crud";
+
+import S from "./EditTableDataGrid.module.css";
+import { useEditDataDridColumnOptions } from "./use-edit-datagrid-column-options";
+import {
+ ROW_SELECT_COLUMN_ID,
+ useTableColumnRowSelect,
+} from "./use-table-column-row-select";
+import { useTableEditingCell } from "./use-table-editing-cell";
+
+type EditTableDataGridProps = {
+ data: DatasetData;
+ updateFormDescription?: DescribeActionFormResponse;
+ rowSelection?: RowSelectionState;
+ onRowSelectionChange?: OnChangeFn<RowSelectionState>;
+ handleRowUpdate: TableRowUpdateHandler;
+ onRowExpandClick: (rowIndex: number) => void;
+ getColumnSortDirection?: TableDataGetColumnSortDirection;
+ onColumnSort?: (field: DatasetColumn) => void;
+};
+
+export const EditTableDataGrid = ({
+ data,
+ updateFormDescription,
+ rowSelection,
+ onRowSelectionChange,
+ onRowExpandClick,
+ getColumnSortDirection,
+ onColumnSort,
+ handleRowUpdate,
+}: EditTableDataGridProps) => {
+ const { cols, rows } = data;
+
+ const {
+ editingCell,
+ getCellState,
+ handleSelectEditingCell,
+ handleCancelEditing,
+ handleUpdateEditingCell,
+ } = useTableEditingCell({
+ data,
+ handleRowUpdate,
+ });
+
+ const columnsOptions = useEditDataDridColumnOptions({
+ cols,
+ editingCell,
+ updateFormDescription,
+ getColumnSortDirection,
+ onColumnSort,
+ onCancelEditing: handleCancelEditing,
+ onValueUpdated: handleUpdateEditingCell,
+ getCellState,
+ });
+
+ const columnRowSelectOptions = useTableColumnRowSelect({
+ onRowEditClick: onRowExpandClick,
+ });
+
+ const handleBodyCellClick = useCallback(
+ (
+ _event: React.MouseEvent<HTMLDivElement>,
+ rowIndex: number,
+ columnId: string,
+ ) => {
+ if (
+ editingCell?.rowIndex === rowIndex &&
+ editingCell?.columnId === columnId
+ ) {
+ return;
+ }
+
+ const parameter = updateFormDescription?.parameters.find(
+ ({ id }) => id === columnId,
+ );
+ if (parameter?.readonly) {
+ return;
+ }
+
+ handleSelectEditingCell(rowIndex, columnId);
+ },
+ [handleSelectEditingCell, editingCell, updateFormDescription],
+ );
+
+ const columnSizingMap = useMemo(() => {
+ return {
+ [ROW_SELECT_COLUMN_ID]: 74,
+ };
+ }, []);
+
+ const columnOrder = useMemo(
+ () => [ROW_SELECT_COLUMN_ID, ...cols.map(({ name }) => name)],
+ [cols],
+ );
+
+ const tableProps = useDataGridInstance({
+ data: rows,
+ columnsOptions,
+ columnOrder,
+ columnSizingMap,
+ columnPinning: { left: [ROW_SELECT_COLUMN_ID] },
+ enableRowSelection: true,
+ rowSelection,
+ onRowSelectionChange,
+ columnRowSelectOptions,
+ });
+
+ const stylingProps = useMemo(
+ () => ({
+ classNames: {
+ headerContainer: S.tableHeaderContainer,
+ headerCell: S.tableHeaderCell,
+ bodyContainer: S.tableBodyContainer,
+ bodyCell: S.tableBodyCell,
+ row: S.tableRow,
+ },
+
+ // Overrides theme constants and default bg
+ styles: {
+ bodyCell: {
+ backgroundColor: undefined,
+ },
+ },
+ theme: {
+ cell: {
+ backgroundColor: "",
+ },
+ },
+ }),
+ [],
+ );
+
+ return (
+ <DataGrid
+ onBodyCellClick={handleBodyCellClick}
+ {...tableProps}
+ {...stylingProps}
+ />
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/TableEditingCell.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/TableEditingCell.module.css
new file mode 100644
index 00000000000..4313d244fe8
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/TableEditingCell.module.css
@@ -0,0 +1,68 @@
+.inlineEditingTextInput {
+ transition: none;
+ outline: 2px solid var(--mb-base-color-blue-50);
+ outline-offset: -2px;
+ border-color: transparent;
+ box-sizing: border-box;
+ background-color: var(--mb-base-color-white);
+ border-radius: 0;
+ text-overflow: ellipsis;
+ white-space: nowrap;
+ overflow: hidden;
+
+ /* Should be in sync with DEFAULT_FONT_SIZE */
+ font-size: 12.5px;
+
+ /* Properly alignment with BaseCell, to avoid text flickering (BaseCell has only border-bottom 1px) */
+ border-top-width: 0;
+
+ /* Keep outline consistent for inline editing, since focused and non focused state is semantically the same */
+ &:focus {
+ outline: 2px solid var(--mb-base-color-blue-50);
+ }
+}
+
+.selectLabel {
+ /* Should be in sync with DEFAULT_FONT_SIZE */
+ font-size: 12.5px;
+}
+
+.cell {
+ font-weight: normal;
+ border-bottom-color: var(--mb-color-border);
+}
+
+.readonlyCell {
+ font-weight: normal;
+ color: var(--mb-color-text-disabled);
+ cursor: not-allowed;
+ border-bottom-color: var(--mb-color-border);
+}
+
+.errorCell {
+ background-color: var(--mb-base-color-dubloon-5);
+ outline: 2px solid var(--mb-base-color-dubloon-30);
+ outline-offset: -2px;
+ border-radius: 0.25rem;
+
+ &:hover {
+ background-color: var(--mb-base-color-dubloon-5);
+ }
+}
+
+.updatingCell {
+ background: linear-gradient(
+ 110deg,
+ var(--mb-color-bg-light) 8%,
+ var(--mb-color-bg-white) 18%,
+ var(--mb-color-bg-light) 33%
+ );
+ background-size: 200% 100%;
+ animation: 1s shine linear infinite;
+}
+
+@keyframes shine {
+ to {
+ background-position-x: -200%;
+ }
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getEditingCellTemplate.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getEditingCellTemplate.tsx
new file mode 100644
index 00000000000..36c3125b404
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getEditingCellTemplate.tsx
@@ -0,0 +1,43 @@
+import type { CellContext } from "@tanstack/react-table";
+
+import type { TableActionFormParameter } from "metabase-enterprise/table-editing/api/types";
+import { ParameterActionInput } from "metabase-enterprise/table-editing/inputs/ParameterActionInput";
+import type { RowValue, RowValues } from "metabase-types/api";
+
+import S from "./TableEditingCell.module.css";
+
+type Props = {
+ parameterDescription: TableActionFormParameter;
+ onValueUpdated: (value: string | null) => void;
+ onCancelEditing: () => void;
+};
+
+export function getEditingCellTemplate({
+ parameterDescription,
+ onValueUpdated,
+ onCancelEditing,
+}: Props) {
+ return function EditingCell(props: CellContext<RowValues, RowValue>) {
+ return (
+ <ParameterActionInput
+ parameter={parameterDescription}
+ autoFocus
+ initialValue={props.getValue()?.toString()}
+ inputProps={{
+ variant: "unstyled",
+ size: "sm",
+ }}
+ classNames={{
+ textInputElement: S.inlineEditingTextInput,
+ numberInputElement: S.inlineEditingTextInput,
+ selectTextInputElement: S.inlineEditingTextInput,
+ dateTextInputElement: S.inlineEditingTextInput,
+ selectLabel: S.selectLabel,
+ }}
+ onEscape={onCancelEditing}
+ onEnter={onValueUpdated}
+ onBlur={onValueUpdated}
+ />
+ );
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getHeaderTemplate.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getHeaderTemplate.tsx
new file mode 100644
index 00000000000..7762128f558
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/getHeaderTemplate.tsx
@@ -0,0 +1,39 @@
+import { Ellipsified } from "metabase/common/components/Ellipsified";
+import { Box, Icon } from "metabase/ui";
+import type { OrderByDirection } from "metabase-lib";
+import type { DatasetColumn } from "metabase-types/api";
+
+import S from "./EditTableDataGrid.module.css";
+
+type Props = {
+ sortDirection?: OrderByDirection;
+ column: DatasetColumn;
+ onColumnSort?: (column: DatasetColumn) => void;
+};
+
+export function getTableEditingHeaderTemplate({
+ sortDirection,
+ column,
+ onColumnSort,
+}: Props) {
+ return function TableEditingHeader() {
+ return (
+ <Box
+ className={S.headerCellContainer}
+ onClick={() => {
+ onColumnSort?.(column);
+ }}
+ >
+ <Ellipsified>{column.display_name}</Ellipsified>
+ {sortDirection != null ? (
+ <Icon
+ className={S.sortIndicator}
+ data-testid="header-sort-indicator"
+ name={sortDirection === "asc" ? "chevronup" : "chevrondown"}
+ size={10}
+ />
+ ) : null}
+ </Box>
+ );
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-cell-state.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-cell-state.ts
new file mode 100644
index 00000000000..7fde50da76a
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-cell-state.ts
@@ -0,0 +1,50 @@
+import { useCallback, useState } from "react";
+
+export enum EditDataGridCellState {
+ Updating,
+ Error,
+}
+
+export function useEditDataGridCellState() {
+ const [cellState, _setCellState] = useState<
+ Record<string, EditDataGridCellState | undefined>
+ >({});
+
+ const getCellState = useCallback(
+ (columnId: string, rowIndex: number) => {
+ return cellState[getCellKey(columnId, rowIndex)];
+ },
+ [cellState],
+ );
+
+ const setCellState = useCallback(
+ (columnId: string, rowIndex: number, state: EditDataGridCellState) => {
+ _setCellState((prev) => ({
+ ...prev,
+ [getCellKey(columnId, rowIndex)]: state,
+ }));
+ },
+ [_setCellState],
+ );
+
+ const resetCellState = useCallback(
+ (columnId: string, rowIndex: number) => {
+ _setCellState((prev) => ({
+ ...prev,
+ [getCellKey(columnId, rowIndex)]: undefined,
+ }));
+ },
+ [_setCellState],
+ );
+
+ return {
+ cellState,
+ getCellState,
+ setCellState,
+ resetCellState,
+ };
+}
+
+function getCellKey(columnId: string, rowIndex: number) {
+ return `${columnId}_${rowIndex}`;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-column-options.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-column-options.tsx
new file mode 100644
index 00000000000..50728e5a18b
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-edit-datagrid-column-options.tsx
@@ -0,0 +1,111 @@
+import cx from "classnames";
+import { useCallback, useMemo } from "react";
+
+import type { ColumnOptions } from "metabase/data-grid";
+import { formatValue } from "metabase/lib/formatting";
+import type { DescribeActionFormResponse } from "metabase-enterprise/table-editing/api/types";
+import type { DatasetColumn, RowValue, RowValues } from "metabase-types/api";
+
+import type { TableDataGetColumnSortDirection } from "../use-edit-table-data";
+
+import S from "./TableEditingCell.module.css";
+import { getEditingCellTemplate } from "./getEditingCellTemplate";
+import { getTableEditingHeaderTemplate } from "./getHeaderTemplate";
+import { EditDataGridCellState } from "./use-edit-datagrid-cell-state";
+import type { TableEditingCell } from "./use-table-editing-cell";
+
+type UseEditDataGridColumnOptions = {
+ cols: DatasetColumn[];
+ editingCell: TableEditingCell | null;
+ updateFormDescription?: DescribeActionFormResponse;
+ getColumnSortDirection?: TableDataGetColumnSortDirection;
+ onColumnSort?: (column: DatasetColumn) => void;
+ onCancelEditing: () => void;
+ onValueUpdated: (value: string | null) => void;
+ getCellState: (
+ columnId: string,
+ rowIndex: number,
+ ) => EditDataGridCellState | undefined;
+};
+
+export function useEditDataDridColumnOptions({
+ cols,
+ editingCell,
+ updateFormDescription,
+ getColumnSortDirection,
+ onColumnSort,
+ onCancelEditing,
+ onValueUpdated,
+ getCellState,
+}: UseEditDataGridColumnOptions) {
+ const getIsEditing = useCallback(
+ (columnId: string, rowIndex: number) => {
+ return (
+ editingCell?.columnId === columnId && editingCell?.rowIndex === rowIndex
+ );
+ },
+ [editingCell],
+ );
+
+ const getCellClassName = useCallback(
+ (_value: RowValue, rowIndex: number, columnId: string) => {
+ const cellState = getCellState(columnId, rowIndex);
+ return cx(S.cell, {
+ [S.updatingCell]: cellState === EditDataGridCellState.Updating,
+ [S.errorCell]: cellState === EditDataGridCellState.Error,
+ });
+ },
+ [getCellState],
+ );
+
+ return useMemo<ColumnOptions<RowValues, RowValue>[]>(
+ () =>
+ cols.map((column, columnIndex) => {
+ const sortDirection = getColumnSortDirection?.(column);
+
+ const options: ColumnOptions<RowValues, RowValue> = {
+ id: column.name,
+ name: column.display_name,
+ accessorFn: (row) => row[columnIndex],
+ formatter: (value) => formatValue(value, { column }),
+ wrap: false,
+ sortDirection,
+ getCellClassName,
+ header: getTableEditingHeaderTemplate({
+ column,
+ onColumnSort,
+ sortDirection,
+ }),
+ };
+
+ const parameterDescription = updateFormDescription?.parameters.find(
+ (parameter) => parameter.id === column.name,
+ );
+
+ if (parameterDescription) {
+ options.getIsEditing = getIsEditing;
+ options.editingCell = getEditingCellTemplate({
+ parameterDescription,
+ onCancelEditing,
+ onValueUpdated,
+ });
+
+ if (parameterDescription.readonly) {
+ options.getCellClassName = () => S.readonlyCell;
+ }
+ }
+
+ return options;
+ }),
+ [
+ cols,
+ getColumnSortDirection,
+ onColumnSort,
+ getIsEditing,
+ getCellClassName,
+ onCancelEditing,
+ onValueUpdated,
+ updateFormDescription,
+ ],
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-column-row-select.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-column-row-select.tsx
new file mode 100644
index 00000000000..06671aa1a3c
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-column-row-select.tsx
@@ -0,0 +1,84 @@
+import type { Row, Table } from "@tanstack/react-table";
+import { useMemo } from "react";
+
+import { BaseCell, type ColumnOptions } from "metabase/data-grid";
+import { Checkbox, Flex, Group, Icon, Tooltip, rem } from "metabase/ui";
+import type { RowValue, RowValues } from "metabase-types/api";
+
+import S from "./EditTableDataGrid.module.css";
+import CellS from "./TableEditingCell.module.css";
+
+export const ROW_SELECT_COLUMN_ID = "__MB_ROW_SELECT";
+
+type UseTableColumnRowSelectProps = {
+ onRowEditClick: (rowIndex: number) => void;
+};
+
+export function useTableColumnRowSelect({
+ onRowEditClick,
+}: UseTableColumnRowSelectProps) {
+ return useMemo<ColumnOptions<RowValues, RowValue> | undefined>(
+ () => getRowSelectColumn({ onRowEditClick }),
+ [onRowEditClick],
+ );
+}
+
+export function getRowSelectColumn({
+ onRowEditClick,
+}: UseTableColumnRowSelectProps) {
+ return {
+ id: ROW_SELECT_COLUMN_ID,
+ name: "",
+ accessorFn: () => null,
+ header: ({ table }: { table: Table<RowValues> }) => (
+ <Flex
+ p="0.75rem"
+ h="100%"
+ align="center"
+ bg="var(--cell-bg-color)"
+ className={S.tableHeaderCell}
+ >
+ <Checkbox
+ size={rem(16)}
+ checked={table.getIsAllRowsSelected()}
+ indeterminate={table.getIsSomeRowsSelected()}
+ onChange={table.getToggleAllRowsSelectedHandler()}
+ data-testid="row-select-all-checkbox"
+ styles={{
+ input: {
+ borderColor: "var(--mb-color-border)",
+ },
+ }}
+ />
+ </Flex>
+ ),
+ cell: ({ row }: { row: Row<RowValues> }) => (
+ <BaseCell className={CellS.cell}>
+ <Group align="center" justify="flex-start" h="100%">
+ <Checkbox
+ size={rem(16)}
+ checked={row.getIsSelected()}
+ disabled={!row.getCanSelect()}
+ indeterminate={row.getIsSomeSelected()}
+ onChange={row.getToggleSelectedHandler()}
+ data-testid="row-select-checkbox"
+ styles={{
+ input: {
+ borderColor: "var(--mb-color-border)",
+ },
+ }}
+ />
+
+ <Tooltip label="Edit record">
+ <Icon
+ name="pencil"
+ data-testid="row-edit-icon"
+ className={S.pencilIcon}
+ onClick={() => onRowEditClick(row.index)}
+ />
+ </Tooltip>
+ </Group>
+ </BaseCell>
+ ),
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-editing-cell.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-editing-cell.tsx
new file mode 100644
index 00000000000..2a8a6dd4659
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/data-grid/use-table-editing-cell.tsx
@@ -0,0 +1,111 @@
+import { useCallback, useState } from "react";
+
+import { isPK } from "metabase-lib/v1/types/utils/isa";
+import type { DatasetData, RowValue } from "metabase-types/api";
+
+import type { RowCellsWithPkValue } from "../../api/types";
+import type { TableRowUpdateHandler } from "../use-table-crud";
+
+import {
+ EditDataGridCellState,
+ useEditDataGridCellState,
+} from "./use-edit-datagrid-cell-state";
+
+type UseTableEditingCellProps = {
+ data: DatasetData;
+ handleRowUpdate: TableRowUpdateHandler;
+};
+
+export type TableEditingCell = {
+ rowIndex: number;
+ columnId: string;
+ value: RowValue;
+ actionInput: RowCellsWithPkValue;
+};
+
+export function useTableEditingCell({
+ data,
+ handleRowUpdate,
+}: UseTableEditingCellProps) {
+ const { getCellState, setCellState, resetCellState } =
+ useEditDataGridCellState();
+ const [editingCell, setEditingCell] = useState<TableEditingCell | null>(null);
+
+ const handleSelectEditingCell = useCallback(
+ (rowIndex: number, columnId: string) => {
+ if (!data) {
+ return;
+ }
+
+ const { rows, cols } = data;
+ const row = rows[rowIndex];
+
+ const actionInput = cols.reduce((acc, col, index) => {
+ if (isPK(col)) {
+ acc[col.name] = rows[rowIndex][index];
+ }
+
+ return acc;
+ }, {} as RowCellsWithPkValue);
+
+ const columnIndex = cols.findIndex((col) => col.name === columnId);
+ const value = row[columnIndex];
+
+ setEditingCell({ rowIndex, columnId, value, actionInput });
+ },
+ [data],
+ );
+
+ const handleCancelEditing = useCallback(() => {
+ setEditingCell(null);
+ }, []);
+
+ const handleUpdateEditingCell = useCallback(
+ async (value: string | null) => {
+ if (!editingCell) {
+ return;
+ }
+
+ handleCancelEditing();
+
+ if (editingCell.value?.toString() !== value?.toString()) {
+ setCellState(
+ editingCell.columnId,
+ editingCell.rowIndex,
+ EditDataGridCellState.Updating,
+ );
+
+ const result = await handleRowUpdate({
+ input: editingCell.actionInput,
+ params: { [editingCell.columnId]: value },
+ shouldPerformOptimisticUpdate: true,
+ });
+
+ if (!result) {
+ setCellState(
+ editingCell.columnId,
+ editingCell.rowIndex,
+ EditDataGridCellState.Error,
+ );
+ } else {
+ resetCellState(editingCell.columnId, editingCell.rowIndex);
+ }
+ }
+ },
+ [
+ editingCell,
+ handleCancelEditing,
+ handleRowUpdate,
+ resetCellState,
+ setCellState,
+ ],
+ );
+
+ return {
+ editingCell,
+ getCellState,
+ handleSelectEditingCell,
+ handleUpdateEditingCell,
+ handleCancelEditing,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/CreateRowActionFormModal.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/CreateRowActionFormModal.tsx
new file mode 100644
index 00000000000..d380429c60e
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/CreateRowActionFormModal.tsx
@@ -0,0 +1,192 @@
+import cx from "classnames";
+import { useFormik } from "formik";
+import { useCallback, useEffect } from "react";
+import { t } from "ttag";
+
+import { LeaveConfirmModal } from "metabase/common/components/LeaveConfirmModal";
+import Animation from "metabase/css/core/animation.module.css";
+import { Box, Button, Center, Flex, Loader, Modal } from "metabase/ui";
+import type { RowValue } from "metabase-types/api";
+
+import type {
+ DescribeActionFormResponse,
+ RowCellsWithPkValue,
+ TableActionFormParameter,
+} from "../../api/types";
+
+import { ModalParameterActionInput } from "./ModalParameterActionInput";
+import S from "./TableActionFormModal.module.css";
+import { TableActionFormModalParameter } from "./TableActionFormModalParameter";
+import { useActionFormUnsavedLeaveConfirmation } from "./use-action-form-unsaved-leave-confirmation";
+
+interface CreateRowActionFormModalProps {
+ opened: boolean;
+ description?: DescribeActionFormResponse;
+ isLoading: boolean;
+ initialValues?: RowCellsWithPkValue;
+ onClose: () => void;
+ onSubmit: (data: RowCellsWithPkValue) => Promise<boolean>;
+}
+
+export function CreateRowActionFormModal({
+ opened,
+ description,
+ isLoading,
+ initialValues,
+ onClose,
+ onSubmit,
+}: CreateRowActionFormModalProps) {
+ const validateForm = useCallback(
+ (values: RowCellsWithPkValue) => {
+ const errors: Record<string, string> = {};
+
+ description?.parameters.forEach((parameter) => {
+ const isRequired = !parameter.optional;
+ if (isRequired && !values[parameter.id]) {
+ errors[parameter.id] = t`This column is required`;
+ }
+ });
+
+ return errors;
+ },
+ [description?.parameters],
+ );
+
+ const handleFormikSubmit = useCallback(
+ async (values: RowCellsWithPkValue) => {
+ const success = await onSubmit(values);
+ if (success) {
+ onClose();
+ }
+ },
+ [onClose, onSubmit],
+ );
+
+ const {
+ isValid,
+ resetForm,
+ setFieldValue,
+ handleSubmit,
+ values,
+ validateForm: revalidateForm,
+ } = useFormik({
+ initialValues: initialValues ?? ({} as Record<string, RowValue>),
+ onSubmit: handleFormikSubmit,
+ validate: validateForm,
+ validateOnMount: true,
+ });
+
+ // Reset form when modal is opened
+ useEffect(() => {
+ if (opened) {
+ resetForm({ values: initialValues });
+ revalidateForm();
+ }
+ }, [opened, resetForm, revalidateForm, initialValues]);
+
+ const shouldShowLeaveConfirmation = useCallback(() => {
+ return Object.keys(values).length > 0;
+ }, [values]);
+
+ const {
+ showLeaveConfirmation,
+ handleClose,
+ handleContinue,
+ handleLeaveConfirmation,
+ } = useActionFormUnsavedLeaveConfirmation({
+ shouldShowLeaveConfirmation,
+ onClose,
+ });
+
+ return (
+ <>
+ <LeaveConfirmModal
+ opened={showLeaveConfirmation}
+ onConfirm={handleLeaveConfirmation}
+ onClose={handleContinue}
+ />
+ <Modal.Root opened={opened} onClose={handleClose}>
+ <Modal.Overlay />
+ <Modal.Content
+ transitionProps={{ transition: "slide-left" }}
+ classNames={{
+ content: cx(S.modalContent, Animation.slideLeft),
+ }}
+ >
+ <form onSubmit={handleSubmit}>
+ <Modal.Header px="xl" pb="0" className={S.modalHeader}>
+ <Modal.Title>{t`Create a new record`}</Modal.Title>
+ </Modal.Header>
+ <Modal.Body px="xl" py="lg" className={cx(S.modalBody)}>
+ <Box className={S.modalBodyGrid}>
+ {!description ? (
+ <Center className={S.modalBodyLoader}>
+ <Loader />
+ </Center>
+ ) : (
+ description.parameters.map((parameter) => {
+ return (
+ <TableActionFormModalParameter
+ key={parameter.id}
+ parameter={parameter}
+ >
+ <ModalFormInput
+ initialValue={
+ values[parameter.id] ??
+ initialValues?.[parameter.id]
+ }
+ parameter={parameter}
+ onChange={setFieldValue}
+ />
+ </TableActionFormModalParameter>
+ );
+ })
+ )}
+ </Box>
+ </Modal.Body>
+ <Flex px="xl" className={S.modalFooter} gap="lg" justify="flex-end">
+ <Button variant="subtle" onClick={handleClose}>
+ {t`Cancel`}
+ </Button>
+ <Button
+ disabled={isLoading || !isValid}
+ variant="filled"
+ type="submit"
+ data-testid="create-row-form-submit-button"
+ >
+ {t`Create`}
+ </Button>
+ </Flex>
+ </form>
+ </Modal.Content>
+ </Modal.Root>
+ </>
+ );
+}
+
+type ModalFormInputProps = {
+ initialValue?: RowValue;
+ parameter: TableActionFormParameter;
+ onChange: (field: string, value: RowValue) => void;
+};
+
+export function ModalFormInput({
+ initialValue,
+ parameter,
+ onChange,
+}: ModalFormInputProps) {
+ const handleChange = useCallback(
+ (value: RowValue) => {
+ onChange(parameter.id, value);
+ },
+ [parameter.id, onChange],
+ );
+
+ return (
+ <ModalParameterActionInput
+ initialValue={initialValue?.toString()}
+ parameter={parameter}
+ onChange={handleChange}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteBulkRowConfirmationModal.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteBulkRowConfirmationModal.tsx
new file mode 100644
index 00000000000..7095c79572a
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteBulkRowConfirmationModal.tsx
@@ -0,0 +1,49 @@
+import { msgid, ngettext, t } from "ttag";
+
+import { Button, Group, Modal } from "metabase/ui";
+
+type DeleteBulkRowConfirmationModalProps = {
+ opened: boolean;
+ rowCount: number;
+ isLoading?: boolean;
+ onConfirm: () => void;
+ onClose: () => void;
+};
+export function DeleteBulkRowConfirmationModal({
+ opened,
+ rowCount,
+ isLoading,
+ onConfirm,
+ onClose,
+}: DeleteBulkRowConfirmationModalProps) {
+ return (
+ <Modal
+ size="md"
+ title={ngettext(
+ msgid`Delete ${rowCount} record?`,
+ `Delete ${rowCount} records?`,
+ rowCount,
+ )}
+ opened={opened}
+ onClose={onClose}
+ >
+ <Group justify="flex-end" mt="xl">
+ <Button variant="subtle" onClick={onClose}>
+ {t`Cancel`}
+ </Button>
+ <Button
+ variant="filled"
+ color="danger"
+ onClick={onConfirm}
+ loading={isLoading}
+ >
+ {ngettext(
+ msgid`Delete ${rowCount} record`,
+ `Delete ${rowCount} records`,
+ rowCount,
+ )}
+ </Button>
+ </Group>
+ </Modal>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteRowConfirmationModal.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteRowConfirmationModal.tsx
new file mode 100644
index 00000000000..c7281a2f1e7
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/DeleteRowConfirmationModal.tsx
@@ -0,0 +1,32 @@
+import { t } from "ttag";
+
+import { Button, Group, Modal } from "metabase/ui";
+
+type DeleteRowConfirmationModalProps = {
+ isLoading: boolean;
+ onConfirm: () => void;
+ onCancel: () => void;
+};
+export function DeleteRowConfirmationModal({
+ onConfirm,
+ onCancel,
+ isLoading,
+}: DeleteRowConfirmationModalProps) {
+ return (
+ <Modal size="md" title={t`Delete this record?`} opened onClose={onCancel}>
+ <Group justify="flex-end" mt="xl">
+ <Button variant="subtle" onClick={onCancel}>
+ {t`Cancel`}
+ </Button>
+ <Button
+ variant="filled"
+ color="danger"
+ onClick={onConfirm}
+ loading={isLoading}
+ >
+ {t`Delete record`}
+ </Button>
+ </Group>
+ </Modal>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.module.css
new file mode 100644
index 00000000000..84131f83ed7
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.module.css
@@ -0,0 +1,19 @@
+.inputWrapper,
+.inputWrapperText {
+ width: 100%;
+}
+
+.inputWrapperDate {
+ width: 15rem;
+}
+
+.inputWrapperBoolean,
+.inputWrapperNumber {
+ width: 10rem;
+}
+
+.inputWrapperDropdown {
+ width: max-content;
+ min-width: 10rem;
+ max-width: 20rem;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.tsx
new file mode 100644
index 00000000000..b389fc1c449
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/ModalParameterActionInput.tsx
@@ -0,0 +1,73 @@
+import { t } from "ttag";
+
+import { Combobox, Icon } from "metabase/ui";
+import { TableActionFormInputType } from "metabase-enterprise/table-editing/api/types";
+
+import {
+ ParameterActionInput,
+ type ParameterActionInputProps,
+} from "../../inputs/ParameterActionInput";
+
+import S from "./ModalParameterActionInput.module.css";
+
+export function ModalParameterActionInput(props: ParameterActionInputProps) {
+ const { parameter, ...rest } = props;
+
+ const placeholder = parameter.database_default
+ ? t`Auto populated`
+ : parameter.optional
+ ? t`Optional`
+ : undefined;
+
+ const disabled = parameter.readonly;
+
+ const inputProps = {
+ ...rest.inputProps,
+ placeholder,
+ disabled,
+ "data-testid": `${parameter.display_name}-field-input`,
+ };
+
+ let wrapperClassName = S.inputWrapper;
+ let hideControls;
+
+ switch (parameter.input_type) {
+ case TableActionFormInputType.Text:
+ wrapperClassName = S.inputWrapperText;
+ break;
+
+ case TableActionFormInputType.Integer:
+ hideControls = false;
+ wrapperClassName = S.inputWrapperNumber;
+ break;
+
+ case TableActionFormInputType.Float:
+ wrapperClassName = S.inputWrapperNumber;
+ break;
+
+ case TableActionFormInputType.Date:
+ case TableActionFormInputType.DateTime:
+ wrapperClassName = S.inputWrapperDate;
+ inputProps.leftSection = <Icon name="calendar" />;
+ inputProps.leftSectionPointerEvents = "none";
+ break;
+
+ case TableActionFormInputType.Boolean:
+ case TableActionFormInputType.Dropdown:
+ inputProps.rightSection = <Combobox.Chevron />;
+ inputProps.rightSectionPointerEvents = "none";
+ wrapperClassName = S.inputWrapperDropdown;
+ }
+
+ return (
+ <ParameterActionInput
+ {...rest}
+ inputProps={inputProps}
+ parameter={parameter}
+ hideControls={hideControls}
+ classNames={{
+ wrapper: wrapperClassName,
+ }}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModal.module.css b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModal.module.css
new file mode 100644
index 00000000000..c2b5026a7a7
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModal.module.css
@@ -0,0 +1,44 @@
+.modalContent {
+ position: fixed;
+ width: 45rem;
+ height: 100dvh;
+ max-height: 100dvh;
+ inset-inline-end: 0;
+ border-radius: 0;
+}
+
+.modalHeader {
+ background-color: var(--mb-color-background);
+ border-bottom: 1px solid var(--mb-color-border);
+ height: rem(80);
+ padding-top: 0;
+ padding-bottom: 0;
+ align-items: center;
+}
+
+.modalBody {
+ height: calc(100dvh - rem(80) - rem(72) /* header + footer */);
+ overflow-y: scroll;
+}
+
+.modalBodyGrid {
+ display: grid;
+ grid-template-columns: 35% auto;
+ row-gap: rem(24);
+ column-gap: rem(16);
+}
+
+.modalBodyLoader {
+ grid-column: 1 / -1;
+}
+
+.modalBodyColumn {
+ align-self: center;
+}
+
+.modalFooter {
+ background-color: var(--mb-color-background);
+ border-top: 1px solid var(--mb-color-border);
+ height: rem(72);
+ align-items: center;
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModalParameter.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModalParameter.tsx
new file mode 100644
index 00000000000..8a7173715bd
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/TableActionFormModalParameter.tsx
@@ -0,0 +1,52 @@
+import type { PropsWithChildren } from "react";
+
+import { Box, Group, Stack, Text } from "metabase/ui";
+
+import {
+ TableActionFormInputType,
+ type TableActionFormParameter,
+} from "../../api/types";
+
+import S from "./TableActionFormModal.module.css";
+
+type ActionFormModalParameterProps = PropsWithChildren<{
+ parameter: TableActionFormParameter;
+}>;
+
+const INPUT_TYPE_TO_DISPLAY_TYPE_MAP: Record<TableActionFormInputType, string> =
+ {
+ [TableActionFormInputType.Text]: "text",
+ [TableActionFormInputType.Textarea]: "description",
+ [TableActionFormInputType.Date]: "date",
+ [TableActionFormInputType.DateTime]: "date",
+ [TableActionFormInputType.Dropdown]: "category",
+ [TableActionFormInputType.Boolean]: "boolean",
+ [TableActionFormInputType.Integer]: "integer",
+ [TableActionFormInputType.Float]: "float",
+ };
+
+export function TableActionFormModalParameter({
+ parameter,
+ children,
+}: ActionFormModalParameterProps) {
+ return (
+ <>
+ <Group h="2.5rem" align="center">
+ <Stack gap="0" className={S.modalBodyColumn} maw="100%">
+ <Text truncate maw="100%">
+ {parameter.display_name}
+ {!parameter.optional && (
+ <Text component="span" c="error">
+ *
+ </Text>
+ )}
+ </Text>
+ <Text fz="sm" c="text-secondary" lh={1.1}>
+ {INPUT_TYPE_TO_DISPLAY_TYPE_MAP[parameter.input_type] ?? "text"}
+ </Text>
+ </Stack>
+ </Group>
+ <Box>{children}</Box>
+ </>
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/UpdateRowActionFormModal.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/UpdateRowActionFormModal.tsx
new file mode 100644
index 00000000000..442bd943531
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/UpdateRowActionFormModal.tsx
@@ -0,0 +1,201 @@
+import cx from "classnames";
+import { useFormik } from "formik";
+import { useCallback, useEffect } from "react";
+import { t } from "ttag";
+
+import { LeaveConfirmModal } from "metabase/common/components/LeaveConfirmModal";
+import Animation from "metabase/css/core/animation.module.css";
+import { Box, Button, Center, Flex, Loader, Modal } from "metabase/ui";
+import type { RowValue } from "metabase-types/api";
+
+import type {
+ DescribeActionFormResponse,
+ RowCellsWithPkValue,
+ TableActionFormParameter,
+} from "../../api/types";
+
+import { ModalParameterActionInput } from "./ModalParameterActionInput";
+import S from "./TableActionFormModal.module.css";
+import { TableActionFormModalParameter } from "./TableActionFormModalParameter";
+import { useActionFormUnsavedLeaveConfirmation } from "./use-action-form-unsaved-leave-confirmation";
+
+interface UpdateRowActionFormModalProps {
+ opened: boolean;
+ description?: DescribeActionFormResponse;
+ isLoading: boolean;
+ isDeleting: boolean;
+ initialValues?: RowCellsWithPkValue;
+ onClose: () => void;
+ onSubmit: (data: RowCellsWithPkValue) => Promise<boolean>;
+ onDelete: () => Promise<boolean>;
+}
+
+export function UpdateRowActionFormModal({
+ opened,
+ description,
+ isLoading,
+ initialValues,
+ onClose,
+ onSubmit,
+}: UpdateRowActionFormModalProps) {
+ const validateForm = useCallback(
+ (values: RowCellsWithPkValue) => {
+ const errors: Record<string, string> = {};
+
+ const updatedKeys = Object.keys(values);
+ if (updatedKeys.length === 0) {
+ errors.all = t`No changes to save`;
+ }
+
+ for (const key of updatedKeys) {
+ const parameter = description?.parameters.find((p) => p.id === key);
+ const isRequired = !parameter?.optional;
+ if (isRequired && !values[key]) {
+ errors[key] = t`This column is required`;
+ }
+ }
+
+ return errors;
+ },
+ [description?.parameters],
+ );
+
+ const handleFormikSubmit = useCallback(
+ async (values: RowCellsWithPkValue) => {
+ const success = await onSubmit(values);
+ if (success) {
+ onClose();
+ }
+ },
+ [onClose, onSubmit],
+ );
+
+ const {
+ isValid,
+ values,
+ resetForm,
+ setFieldValue,
+ handleSubmit,
+ validateForm: revalidateForm,
+ } = useFormik({
+ // We want to track only changed values, not all values
+ initialValues: {} as Record<string, RowValue>,
+ onSubmit: handleFormikSubmit,
+ validate: validateForm,
+ validateOnMount: true,
+ });
+
+ // Reset form when modal is opened
+ useEffect(() => {
+ if (opened) {
+ resetForm();
+ revalidateForm({});
+ }
+ }, [opened, resetForm, revalidateForm]);
+
+ const shouldShowLeaveConfirmation = useCallback(() => {
+ return Object.keys(values).length > 0;
+ }, [values]);
+
+ const {
+ showLeaveConfirmation,
+ handleClose,
+ handleContinue,
+ handleLeaveConfirmation,
+ } = useActionFormUnsavedLeaveConfirmation({
+ shouldShowLeaveConfirmation,
+ onClose,
+ });
+
+ return (
+ <>
+ <LeaveConfirmModal
+ opened={showLeaveConfirmation}
+ onConfirm={handleLeaveConfirmation}
+ onClose={handleContinue}
+ />
+ <Modal.Root opened={opened} onClose={handleClose}>
+ <Modal.Overlay />
+ <Modal.Content
+ transitionProps={{ transition: "slide-left" }}
+ classNames={{
+ content: cx(S.modalContent, Animation.slideLeft),
+ }}
+ >
+ <form onSubmit={handleSubmit}>
+ <Modal.Header px="xl" pb="0" className={S.modalHeader}>
+ <Modal.Title>{t`Edit record`}</Modal.Title>
+ </Modal.Header>
+ <Modal.Body px="xl" py="lg" className={cx(S.modalBody)}>
+ <Box className={S.modalBodyGrid}>
+ {!description ? (
+ <Center className={S.modalBodyLoader}>
+ <Loader />
+ </Center>
+ ) : (
+ description.parameters.map((parameter) => {
+ return (
+ <TableActionFormModalParameter
+ key={parameter.id}
+ parameter={parameter}
+ >
+ <ModalFormInput
+ initialValue={
+ values[parameter.id] ??
+ initialValues?.[parameter.id]
+ }
+ parameter={parameter}
+ onChange={setFieldValue}
+ />
+ </TableActionFormModalParameter>
+ );
+ })
+ )}
+ </Box>
+ </Modal.Body>
+ <Flex px="xl" className={S.modalFooter} gap="lg" justify="flex-end">
+ <Button variant="subtle" onClick={handleClose}>
+ {t`Cancel`}
+ </Button>
+ <Button
+ disabled={isLoading || !isValid}
+ variant="filled"
+ type="submit"
+ data-testid="update-row-save-button"
+ >
+ {t`Save changes`}
+ </Button>
+ </Flex>
+ </form>
+ </Modal.Content>
+ </Modal.Root>
+ </>
+ );
+}
+
+type ModalFormInputProps = {
+ initialValue?: RowValue;
+ parameter: TableActionFormParameter;
+ onChange: (field: string, value: RowValue) => void;
+};
+
+export function ModalFormInput({
+ initialValue,
+ parameter,
+ onChange,
+}: ModalFormInputProps) {
+ const handleChange = useCallback(
+ (value: RowValue) => {
+ onChange(parameter.id, value);
+ },
+ [parameter.id, onChange],
+ );
+
+ return (
+ <ModalParameterActionInput
+ initialValue={initialValue?.toString()}
+ parameter={parameter}
+ onChange={handleChange}
+ />
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/use-action-form-unsaved-leave-confirmation.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/use-action-form-unsaved-leave-confirmation.tsx
new file mode 100644
index 00000000000..4022de0a112
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/modals/use-action-form-unsaved-leave-confirmation.tsx
@@ -0,0 +1,36 @@
+import { useDisclosure } from "@mantine/hooks";
+import { useCallback } from "react";
+
+type UseActionFormUnsavedLeaveConfirmationProps = {
+ shouldShowLeaveConfirmation: () => boolean;
+ onClose: () => void;
+};
+
+export function useActionFormUnsavedLeaveConfirmation({
+ shouldShowLeaveConfirmation,
+ onClose,
+}: UseActionFormUnsavedLeaveConfirmationProps) {
+ const [showLeaveConfirmation, leaveConfirmationHandlers] =
+ useDisclosure(false);
+
+ const handleClose = useCallback(() => {
+ if (shouldShowLeaveConfirmation()) {
+ leaveConfirmationHandlers.open();
+ return;
+ }
+
+ onClose();
+ }, [leaveConfirmationHandlers, onClose, shouldShowLeaveConfirmation]);
+
+ const handleLeaveConfirmation = useCallback(() => {
+ leaveConfirmationHandlers.close();
+ onClose();
+ }, [leaveConfirmationHandlers, onClose]);
+
+ return {
+ showLeaveConfirmation,
+ handleContinue: leaveConfirmationHandlers.close,
+ handleClose,
+ handleLeaveConfirmation,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/ErrorUpdateToast.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/ErrorUpdateToast.tsx
new file mode 100644
index 00000000000..358a7cc09f9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/ErrorUpdateToast.tsx
@@ -0,0 +1,45 @@
+import { useState } from "react";
+import { t } from "ttag";
+
+import { Button, Group, Icon, Stack, Text } from "metabase/ui";
+
+import { getUpdateApiErrorMessage } from "./getUpdateErrorMessage";
+
+type ErrorUpdateToastProps = {
+ error: unknown;
+};
+
+export const ErrorUpdateToast = ({ error }: ErrorUpdateToastProps) => {
+ const [showDetails, setShowDetails] = useState(false);
+
+ const errorMessage = getUpdateApiErrorMessage(error);
+
+ if (showDetails) {
+ return (
+ <Stack gap="0.5rem" w="30rem" maw="100%">
+ <Text c="text-white">{t`Couldn't save table changes:`}</Text>
+ <Text c="text-white" style={{ fontFamily: "monospace" }}>
+ {errorMessage}
+ </Text>
+ </Stack>
+ );
+ }
+
+ return (
+ <Group gap="2.5rem" w="20rem">
+ <Group gap="0.5rem">
+ <Icon name="warning" c="danger" size={12} />
+ <Text c="text-white" fw={700}>{t`Couldn't save table changes`}</Text>
+ </Group>
+
+ <Button
+ size="compact-lg"
+ color="var(--mb-base-color-orion-80)"
+ variant="filled"
+ autoContrast
+ radius="0.5rem"
+ onClick={() => setShowDetails(true)}
+ >{t`More info`}</Button>
+ </Group>
+ );
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/getUpdateErrorMessage.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/getUpdateErrorMessage.tsx
new file mode 100644
index 00000000000..37a06b73438
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/getUpdateErrorMessage.tsx
@@ -0,0 +1,34 @@
+import { t } from "ttag";
+
+import type { GenericErrorResponse } from "metabase/lib/errors";
+
+export const getUpdateApiErrorMessage = (
+ error: GenericErrorResponse | unknown,
+): string => {
+ const maybeError = error as GenericErrorResponse;
+
+ if (typeof maybeError.data === "string") {
+ return maybeError.data;
+ }
+
+ if (
+ Array.isArray(maybeError.data?.errors) &&
+ ("message" in maybeError.data?.errors[0] ||
+ "error" in maybeError.data?.errors[0])
+ ) {
+ return maybeError.data.errors[0].message ?? maybeError.data.errors[0].error;
+ }
+
+ if (
+ Array.isArray(maybeError.errors) &&
+ ("message" in maybeError.errors[0] || "error" in maybeError.errors[0])
+ ) {
+ return maybeError.errors[0].message ?? maybeError.errors[0].error;
+ }
+
+ if (typeof maybeError.data?.message === "string") {
+ return maybeError.data.message;
+ }
+
+ return t`Unknown error`;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/use-table-editing-toast-controller.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/use-table-editing-toast-controller.tsx
new file mode 100644
index 00000000000..628c4e9bb96
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/toasts/use-table-editing-toast-controller.tsx
@@ -0,0 +1,39 @@
+import { useMemo } from "react";
+
+import { useDispatch } from "metabase/lib/redux";
+import { addUndo } from "metabase/redux/undo";
+
+import { ErrorUpdateToast } from "./ErrorUpdateToast";
+
+export type TableEditingToastControler = {
+ showSuccessToast: (message: string) => void;
+};
+
+export function useTableEditingToastController() {
+ const dispatch = useDispatch();
+
+ return useMemo(
+ () => ({
+ showSuccessToast(message: string) {
+ dispatch(
+ addUndo({
+ message,
+ }),
+ );
+ },
+ showErrorToast(error: unknown, onDismiss?: () => void) {
+ dispatch(
+ addUndo({
+ toastColor: "bg-black",
+ icon: null,
+ renderChildren: () => <ErrorUpdateToast error={error} />,
+ timeout: null, // removes automatic toast hide
+ undo: false,
+ onDismiss,
+ }),
+ );
+ },
+ }),
+ [dispatch],
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-adhoc-table-query.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-adhoc-table-query.ts
new file mode 100644
index 00000000000..9ee1b643360
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-adhoc-table-query.ts
@@ -0,0 +1,124 @@
+import type { Location } from "history";
+import { useCallback, useEffect, useMemo } from "react";
+import { push } from "react-router-redux";
+
+import { useDispatch, useSelector } from "metabase/lib/redux";
+import { loadMetadataForTable } from "metabase/questions/actions";
+import { getMetadata } from "metabase/selectors/metadata";
+import * as Lib from "metabase-lib";
+import Question from "metabase-lib/v1/Question";
+import type { StructuredDatasetQuery } from "metabase-types/api";
+
+import {
+ deserializeTableFilter,
+ deserializeTableSorting,
+ serializeMbqlParam,
+} from "../common/utils";
+
+type UseAdHocTableQueryProps = {
+ tableId: number;
+ databaseId: number;
+ location: Location<{ filter?: string; sorting?: string }>;
+};
+
+export const useAdHocTableQuery = ({
+ tableId,
+ databaseId,
+ location,
+}: UseAdHocTableQueryProps) => {
+ const filterQueryParam = useMemo(
+ () =>
+ location.query?.filter
+ ? deserializeTableFilter(location.query.filter)
+ : null,
+ [location.query.filter],
+ );
+
+ const sortingQueryParam = useMemo(
+ () =>
+ location.query?.sorting
+ ? deserializeTableSorting(location.query.sorting)
+ : null,
+ [location.query.sorting],
+ );
+
+ const metadata = useSelector(getMetadata);
+ const dispatch = useDispatch();
+
+ const table = metadata.table(tableId);
+
+ useEffect(() => {
+ dispatch(loadMetadataForTable(tableId));
+ }, [dispatch, tableId]);
+
+ const tableQuestion = useMemo(() => {
+ if (table) {
+ let question = Question.create({ databaseId, tableId, metadata });
+
+ if (filterQueryParam || sortingQueryParam) {
+ const legacyQuery = Lib.toLegacyQuery(
+ question.query(),
+ ) as StructuredDatasetQuery;
+
+ if (filterQueryParam) {
+ legacyQuery.query.filter = filterQueryParam;
+ }
+
+ if (sortingQueryParam) {
+ legacyQuery.query["order-by"] = sortingQueryParam;
+ }
+
+ question = question.setDatasetQuery(legacyQuery);
+ }
+
+ return question;
+ }
+ }, [
+ table,
+ databaseId,
+ tableId,
+ metadata,
+ filterQueryParam,
+ sortingQueryParam,
+ ]);
+
+ const handleTableQuestionChange = useCallback(
+ (newQuestion: Question) => {
+ const legacyQuery = Lib.toLegacyQuery(
+ newQuestion.query(),
+ ) as StructuredDatasetQuery;
+ const newFilterMbql = legacyQuery.query.filter;
+ const newSortingMbql = legacyQuery.query["order-by"];
+
+ if (newFilterMbql || newSortingMbql) {
+ const searchParams = new URLSearchParams();
+ if (newFilterMbql) {
+ searchParams.append("filter", serializeMbqlParam(newFilterMbql));
+ }
+ if (newSortingMbql) {
+ searchParams.append("sorting", serializeMbqlParam(newSortingMbql));
+ }
+
+ // don't set filter param if it is empty
+ dispatch(
+ push(`${window.location.pathname}?${searchParams.toString()}`),
+ );
+ } else {
+ dispatch(push(window.location.pathname));
+ }
+ },
+ [dispatch],
+ );
+
+ const tableQuery = useMemo(() => {
+ if (tableQuestion) {
+ return Lib.toLegacyQuery(tableQuestion.query());
+ }
+ }, [tableQuestion]);
+
+ return {
+ tableQuestion,
+ tableQuery,
+ handleTableQuestionChange,
+ };
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-data.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-data.ts
new file mode 100644
index 00000000000..bb8a7e16071
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-data.ts
@@ -0,0 +1,128 @@
+import type { Location } from "history";
+import { useCallback } from "react";
+
+import { skipToken, useGetAdhocQueryQuery } from "metabase/api";
+import type { OrderByDirection, Query } from "metabase-lib";
+import {
+ changeDirection,
+ displayInfo,
+ findMatchingColumn,
+ fromLegacyColumn,
+ orderBy,
+ orderBys,
+ orderableColumns,
+ removeOrderBys,
+} from "metabase-lib";
+import type Question from "metabase-lib/v1/Question";
+import type { DatasetColumn } from "metabase-types/api";
+
+import { useAdHocTableQuery } from "./use-adhoc-table-query";
+
+type Props = {
+ tableId: number;
+ databaseId: number;
+ location: Location<{ filter?: string }>;
+};
+
+export type TableDataGetColumnSortDirection = (
+ column: DatasetColumn,
+) => OrderByDirection | undefined;
+
+export function useEditTableData({ tableId, databaseId, location }: Props) {
+ const { tableQuestion, tableQuery, handleTableQuestionChange } =
+ useAdHocTableQuery({ tableId, databaseId, location });
+
+ const getColumnSortDirection = useCallback(
+ (columnOrField: DatasetColumn) => {
+ if (!tableQuestion || !columnOrField) {
+ return;
+ }
+
+ const { query, stageIndex, column } = getQueryColumn(
+ tableQuestion,
+ columnOrField,
+ );
+
+ if (column != null) {
+ const columnInfo = displayInfo(query, stageIndex, column);
+ if (columnInfo.orderByPosition != null) {
+ const orderBysMap = orderBys(query, stageIndex);
+ const orderBy = orderBysMap[columnInfo.orderByPosition];
+ const orderByInfo = displayInfo(query, stageIndex, orderBy);
+ return orderByInfo.direction;
+ }
+ }
+ },
+ [tableQuestion],
+ );
+
+ const handleChangeColumnSort = useCallback(
+ (columnOrField: DatasetColumn) => {
+ if (!tableQuestion) {
+ return;
+ }
+
+ const { query, stageIndex, column } = getQueryColumn(
+ tableQuestion,
+ columnOrField,
+ );
+
+ if (column) {
+ const sortDirection = getColumnSortDirection(columnOrField);
+
+ let newQuery: Query;
+ if (sortDirection === "asc") {
+ // have existing sorting by asc, then make it desc
+ const clauses = orderBys(query, stageIndex);
+ newQuery = changeDirection(query, clauses[0]);
+ } else if (sortDirection === "desc") {
+ // have existing sorting by desc, then remove sorting - WRK-446
+ newQuery = removeOrderBys(query, stageIndex);
+ } else {
+ // no existing sorting on this column - apply new sorting (default - by asc)
+ newQuery = orderBy(
+ removeOrderBys(query, stageIndex),
+ stageIndex,
+ column,
+ );
+ }
+
+ const newQuestion = tableQuestion.setQuery(newQuery);
+ handleTableQuestionChange?.(newQuestion);
+ }
+ },
+ [tableQuestion, getColumnSortDirection, handleTableQuestionChange],
+ );
+
+ const { data, isLoading, isFetching } = useGetAdhocQueryQuery(
+ tableQuery || skipToken,
+ );
+
+ return {
+ data,
+ isLoading,
+ isFetching,
+ tableQuestion,
+ tableQuery,
+ getColumnSortDirection,
+ handleTableQuestionChange,
+ handleChangeColumnSort,
+ };
+}
+
+const getQueryColumn = (question: Question, columnOrField: DatasetColumn) => {
+ const query = question.query();
+ const stageIndex = -1;
+ const column = findMatchingColumn(
+ query,
+ stageIndex,
+ fromLegacyColumn(query, stageIndex, columnOrField),
+ orderableColumns(query, stageIndex),
+ );
+
+ return {
+ query,
+ stageIndex,
+ column,
+ };
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-loading-overlay.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-loading-overlay.ts
new file mode 100644
index 00000000000..7e80276fc4d
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-edit-table-loading-overlay.ts
@@ -0,0 +1,32 @@
+import { useMemo } from "react";
+import { t } from "ttag";
+
+type UseEditTableLoadingOverlayProps = {
+ isDatasetLoading: boolean;
+ isDatasetFetching: boolean;
+};
+
+export function useEditTableLoadingOverlay({
+ isDatasetLoading,
+ isDatasetFetching,
+}: UseEditTableLoadingOverlayProps) {
+ return useMemo(() => {
+ if (isDatasetLoading) {
+ return {
+ show: true,
+ message: t`Loading data...`,
+ };
+ }
+
+ if (isDatasetFetching) {
+ return {
+ show: true,
+ message: t`Updating...`,
+ };
+ }
+
+ return {
+ show: false,
+ };
+ }, [isDatasetLoading, isDatasetFetching]);
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-action-form-description.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-action-form-description.ts
new file mode 100644
index 00000000000..32927628bac
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-action-form-description.ts
@@ -0,0 +1,38 @@
+import { useCallback, useEffect } from "react";
+
+import { useDescribeActionFormMutation } from "../api";
+import type { TableActionId, TableEditingActionScope } from "../api/types";
+
+type UseTableDescribeTmpModalProps = {
+ actionId: TableActionId;
+ scope: TableEditingActionScope;
+ fetchOnMount?: boolean;
+};
+
+export const useActionFormDescription = ({
+ actionId,
+ scope,
+ fetchOnMount = true,
+}: UseTableDescribeTmpModalProps) => {
+ const [fetchModalDescription, { data, isLoading }] =
+ useDescribeActionFormMutation();
+
+ const refetch = useCallback(() => {
+ fetchModalDescription({
+ action: actionId,
+ scope,
+ });
+ }, [actionId, scope, fetchModalDescription]);
+
+ useEffect(() => {
+ if (fetchOnMount) {
+ refetch();
+ }
+ }, [fetchOnMount, refetch]);
+
+ return {
+ refetch,
+ data,
+ isLoading,
+ };
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-create-row.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-create-row.tsx
new file mode 100644
index 00000000000..b523423e926
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-create-row.tsx
@@ -0,0 +1,28 @@
+import { useDisclosure } from "@mantine/hooks";
+
+import { TableActionId, type TableEditingActionScope } from "../api/types";
+
+import { useActionFormDescription } from "./use-table-action-form-description";
+
+type UseTableCreateRowProps = {
+ scope: TableEditingActionScope;
+};
+
+export function useTableCreateRow({ scope }: UseTableCreateRowProps) {
+ const [
+ isCreateRowModalOpen,
+ { open: openCreateRowModal, close: closeCreateRowModal },
+ ] = useDisclosure(false);
+
+ const { data: formDescription } = useActionFormDescription({
+ actionId: TableActionId.CreateRow,
+ scope,
+ });
+
+ return {
+ isCreateRowModalOpen,
+ openCreateRowModal,
+ closeCreateRowModal,
+ formDescription,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-crud.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-crud.tsx
new file mode 100644
index 00000000000..6195e998ea4
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-crud.tsx
@@ -0,0 +1,259 @@
+import { useCallback } from "react";
+import { t } from "ttag";
+
+import { trackSimpleEvent } from "metabase/lib/analytics";
+import type { DatasetData } from "metabase-types/api";
+
+import {
+ useDeleteTableRowsMutation,
+ useInsertTableRowsMutation,
+ useUpdateTableRowsMutation,
+} from "../api";
+import type {
+ RowCellsWithPkValue,
+ TableEditingActionScope,
+} from "../api/types";
+
+import { useTableEditingToastController } from "./toasts/use-table-editing-toast-controller";
+import type { TableEditingStateUpdateStrategy } from "./use-table-state-update-strategy";
+
+type UseTableCRUDProps = {
+ scope: TableEditingActionScope;
+ datasetData: DatasetData | null | undefined;
+ stateUpdateStrategy: TableEditingStateUpdateStrategy;
+};
+
+export type TableRowUpdateHandler = (props: {
+ input: RowCellsWithPkValue;
+ params: RowCellsWithPkValue;
+ shouldPerformOptimisticUpdate?: boolean;
+}) => Promise<boolean>;
+
+export type TableRowDeleteHandler = (
+ input: RowCellsWithPkValue,
+ cascadeDelete?: boolean,
+) => Promise<boolean>;
+
+export const useTableCRUD = ({
+ scope,
+ datasetData,
+ stateUpdateStrategy,
+}: UseTableCRUDProps) => {
+ const toastController = useTableEditingToastController();
+
+ const [deleteTableRows, { isLoading: isDeleting }] =
+ useDeleteTableRowsMutation();
+ const [updateTableRows, { isLoading: isUpdating }] =
+ useUpdateTableRowsMutation();
+ const [insertTableRows, { isLoading: isInserting }] =
+ useInsertTableRowsMutation();
+
+ const handleRowUpdateBulk = useCallback(
+ async ({
+ inputs,
+ params,
+ shouldPerformOptimisticUpdate,
+ }: {
+ inputs: RowCellsWithPkValue[];
+ params: RowCellsWithPkValue;
+ shouldPerformOptimisticUpdate?: boolean;
+ }) => {
+ if (!datasetData) {
+ console.warn(
+ "Failed to update table data - no data is loaded for a table",
+ );
+ return false;
+ }
+
+ if (shouldPerformOptimisticUpdate) {
+ const updatedRows = inputs.map((input) => ({
+ ...input,
+ ...params,
+ }));
+
+ stateUpdateStrategy.onRowsUpdated(updatedRows);
+ }
+
+ try {
+ const response = await updateTableRows({
+ inputs,
+ params,
+ scope,
+ });
+
+ if (!response.error && response.data) {
+ stateUpdateStrategy.onRowsUpdated(
+ response.data.outputs.map((output) => output.row),
+ );
+
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "update",
+ target_id: Number(scope["table-id"]),
+ triggered_from: shouldPerformOptimisticUpdate ? "inline" : "modal",
+ result: "success",
+ });
+
+ toastController.showSuccessToast(t`Successfully updated`);
+ } else {
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "update",
+ target_id: Number(scope["table-id"]),
+ triggered_from: shouldPerformOptimisticUpdate ? "inline" : "modal",
+ result: "error",
+ });
+
+ toastController.showErrorToast(response.error);
+ }
+
+ return !response.error;
+ } catch (error) {
+ toastController.showErrorToast(error);
+
+ throw error;
+ }
+ },
+ [datasetData, updateTableRows, scope, stateUpdateStrategy, toastController],
+ );
+
+ const handleRowUpdate = useCallback<TableRowUpdateHandler>(
+ async ({ input, params, shouldPerformOptimisticUpdate }) => {
+ return handleRowUpdateBulk({
+ inputs: [input],
+ params,
+ shouldPerformOptimisticUpdate,
+ });
+ },
+ [handleRowUpdateBulk],
+ );
+
+ const handleRowCreate = useCallback(
+ async (data: RowCellsWithPkValue): Promise<boolean> => {
+ const response = await insertTableRows({
+ rows: [data],
+ scope,
+ });
+
+ if (!response.error && response.data) {
+ stateUpdateStrategy.onRowsCreated(
+ response.data.outputs.map((output) => output.row),
+ );
+
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "create",
+ target_id: Number(scope["table-id"]),
+ triggered_from: "modal",
+ result: "success",
+ });
+
+ toastController.showSuccessToast(t`Record successfully created`);
+ } else {
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "create",
+ target_id: Number(scope["table-id"]),
+ triggered_from: "modal",
+ result: "error",
+ });
+
+ toastController.showErrorToast(response.error);
+ }
+
+ return !response.error;
+ },
+ [insertTableRows, scope, stateUpdateStrategy, toastController],
+ );
+
+ const handleRowDeleteBulk = useCallback(
+ async (inputs: RowCellsWithPkValue[], cascadeDelete = false) => {
+ if (!datasetData) {
+ console.warn(
+ "Failed to update table data - no data is loaded for a table",
+ );
+ return false;
+ }
+
+ const requestParams = cascadeDelete
+ ? { params: { "delete-children": true } }
+ : {};
+
+ const response = await deleteTableRows({
+ inputs,
+ scope,
+ ...requestParams,
+ });
+
+ if (response.data?.outputs) {
+ stateUpdateStrategy.onRowsDeleted(
+ response.data.outputs.map((output) => output.row),
+ );
+
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "delete",
+ target_id: Number(scope["table-id"]),
+ triggered_from: "modal",
+ result: "success",
+ });
+
+ toastController.showSuccessToast(t`Successfully deleted`);
+ }
+
+ if (response.error) {
+ trackSimpleEvent({
+ event: "edit_data_record_modified",
+ event_detail: "delete",
+ target_id: Number(scope["table-id"]),
+ triggered_from: "modal",
+ result: "error",
+ });
+
+ toastController.showErrorToast(response.error);
+ }
+
+ return !response.error;
+ },
+ [datasetData, deleteTableRows, scope, stateUpdateStrategy, toastController],
+ );
+
+ const handleRowDelete = useCallback(
+ async (input: RowCellsWithPkValue, cascadeDelete = false) => {
+ return handleRowDeleteBulk([input], cascadeDelete);
+ },
+ [handleRowDeleteBulk],
+ );
+
+ const handleRowDeleteWithCascade = useCallback(
+ async (inputs: RowCellsWithPkValue[]) => {
+ return handleRowDeleteBulk(inputs, true);
+ },
+ [handleRowDeleteBulk],
+ );
+
+ return {
+ isInserting,
+ isDeleting,
+ isUpdating,
+
+ handleRowCreate,
+ handleRowUpdate,
+ handleRowUpdateBulk,
+ handleRowDelete,
+ handleRowDeleteBulk,
+ handleRowDeleteWithCascade,
+ };
+};
+
+export const isForeignKeyConstraintErrorResponse = (error: any): boolean => {
+ if (!error?.data?.errors) {
+ return false;
+ }
+
+ return isForeignKeyConstraintError(error.data.errors);
+};
+
+export function isForeignKeyConstraintError(error: any): boolean {
+ return error?.type === "metabase.actions.error/children-exist";
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-expanded-update-row.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-expanded-update-row.tsx
new file mode 100644
index 00000000000..35ffceba64a
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-expanded-update-row.tsx
@@ -0,0 +1,113 @@
+import { useCallback, useState } from "react";
+
+import { isPK } from "metabase-lib/v1/types/utils/isa";
+import type { DatasetColumn, DatasetData, RowValues } from "metabase-types/api";
+
+import {
+ type RowCellsWithPkValue,
+ TableActionId,
+ type TableEditingActionScope,
+} from "../api/types";
+
+import { useActionFormDescription } from "./use-table-action-form-description";
+import type {
+ TableRowDeleteHandler,
+ TableRowUpdateHandler,
+} from "./use-table-crud";
+
+type UseTableExpandedUpdateRowProps = {
+ datasetData?: DatasetData;
+ scope: TableEditingActionScope;
+ handleRowUpdate: TableRowUpdateHandler;
+ handleRowDelete: TableRowDeleteHandler;
+};
+
+export type ExpandedRow = {
+ input: RowCellsWithPkValue;
+ params: RowCellsWithPkValue;
+};
+
+export function useTableExpandedUpdateRow({
+ datasetData,
+ scope,
+ handleRowUpdate,
+ handleRowDelete,
+}: UseTableExpandedUpdateRowProps) {
+ const [expandedRow, setExpandedRow] = useState<ExpandedRow | null>(null);
+
+ const handleExpandRow = useCallback(
+ (rowIndex: number) => {
+ if (!datasetData) {
+ return;
+ }
+
+ const { rows, cols } = datasetData;
+ const { input, params } = getRowInputAndParamsFromRow(
+ cols,
+ rows[rowIndex],
+ );
+
+ setExpandedRow({ input, params });
+ },
+ [datasetData],
+ );
+
+ const closeExpandedRow = useCallback(() => {
+ setExpandedRow(null);
+ }, []);
+
+ const handleExpandedRowUpdate = useCallback(
+ (params: RowCellsWithPkValue) => {
+ if (!expandedRow) {
+ // Type safety
+ return Promise.resolve(false);
+ }
+
+ return handleRowUpdate({
+ input: expandedRow.input,
+ params,
+ });
+ },
+ [expandedRow, handleRowUpdate],
+ );
+
+ const handleExpandedRowDelete = useCallback(() => {
+ if (!expandedRow) {
+ return Promise.resolve(false);
+ }
+
+ return handleRowDelete(expandedRow.input);
+ }, [expandedRow, handleRowDelete]);
+
+ const { data: formDescription } = useActionFormDescription({
+ actionId: TableActionId.UpdateRow,
+ scope,
+ });
+
+ return {
+ expandedRow,
+ handleExpandRow,
+ handleExpandedRowUpdate,
+ handleExpandedRowDelete,
+ closeExpandedRow,
+ formDescription,
+ };
+}
+
+export function getRowInputAndParamsFromRow(
+ cols: DatasetColumn[],
+ row: RowValues,
+) {
+ const input: RowCellsWithPkValue = {};
+ const params: RowCellsWithPkValue = {};
+
+ cols.forEach((col, index) => {
+ if (isPK(col)) {
+ input[col.name] = row[index];
+ }
+
+ params[col.name] = row[index];
+ });
+
+ return { input, params };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-row-selection.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-row-selection.ts
new file mode 100644
index 00000000000..e79302bfec3
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-row-selection.ts
@@ -0,0 +1,16 @@
+import type { RowSelectionState } from "@tanstack/react-table";
+import { useMemo, useState } from "react";
+
+export function useEditingTableRowSelection() {
+ const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
+ const selectedRowIndices = useMemo(
+ () => Object.keys(rowSelection).map(Number),
+ [rowSelection],
+ );
+
+ return {
+ rowSelection,
+ selectedRowIndices,
+ setRowSelection,
+ };
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-adhoc-query-update-strategy.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-adhoc-query-update-strategy.ts
new file mode 100644
index 00000000000..9cfb54f3c04
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-adhoc-query-update-strategy.ts
@@ -0,0 +1,129 @@
+import type { ThunkDispatch, UnknownAction } from "@reduxjs/toolkit";
+import { useCallback, useMemo } from "react";
+
+import { datasetApi } from "metabase/api";
+import { useDispatch } from "metabase/lib/redux";
+import { checkNotNull } from "metabase/lib/types";
+import type { DatasetQuery } from "metabase-types/api";
+
+import type { RowCellsWithPkValue } from "../api/types";
+import {
+ getPkColumns,
+ getRowObjectPkUniqueKeyByColumnNames,
+ getRowUniqueKeyByPkIndexes,
+} from "../common/utils";
+
+import type { TableEditingStateUpdateStrategy } from "./use-table-state-update-strategy";
+import {
+ createPrimaryKeyToUpdatedRowObjectMap,
+ mapDataEditingRowObjectsToRowValues,
+} from "./use-table-state-update-strategy";
+
+export function useTableEditingStateAdHocQueryUpdateStrategy(
+ nullableAdhocQuery: DatasetQuery | undefined,
+): TableEditingStateUpdateStrategy {
+ // "metabase-api" state is not typed, so we need to use the `any` type
+ const dispatch = useDispatch() as ThunkDispatch<any, any, UnknownAction>;
+
+ const onRowsCreated = useCallback(
+ (rows: RowCellsWithPkValue[]) => {
+ const adhocQuery = checkNotNull(nullableAdhocQuery);
+
+ dispatch(
+ datasetApi.util.updateQueryData(
+ "getAdhocQuery",
+ adhocQuery,
+ ({ data }) => {
+ data.rows.push(
+ ...mapDataEditingRowObjectsToRowValues(rows, data.cols),
+ );
+ },
+ ),
+ );
+ },
+ [dispatch, nullableAdhocQuery],
+ );
+
+ const onRowsUpdated = useCallback(
+ (rows: RowCellsWithPkValue[]) => {
+ const adhocQuery = checkNotNull(nullableAdhocQuery);
+
+ const patchResult = dispatch(
+ datasetApi.util.updateQueryData(
+ "getAdhocQuery",
+ adhocQuery,
+ ({ data }) => {
+ const { indexes: pkIndexes, names: pkNames } = getPkColumns(
+ data.cols,
+ );
+
+ const primaryKeyToUpdatedRowObjectMap =
+ createPrimaryKeyToUpdatedRowObjectMap(pkNames, rows);
+
+ for (const rowArray of data.rows) {
+ const pkUniqueKey = getRowUniqueKeyByPkIndexes(
+ pkIndexes,
+ rowArray,
+ );
+ const updatedRowObject =
+ primaryKeyToUpdatedRowObjectMap.get(pkUniqueKey);
+
+ if (updatedRowObject) {
+ for (let i = 0; i < data.cols.length; i++) {
+ const columnName = data.cols[i].name;
+ if (columnName in updatedRowObject) {
+ rowArray[i] = updatedRowObject[columnName];
+ }
+ }
+ }
+ }
+ },
+ ),
+ );
+
+ return {
+ revert: patchResult.undo,
+ };
+ },
+ [dispatch, nullableAdhocQuery],
+ );
+
+ const onRowsDeleted = useCallback(
+ (rows: RowCellsWithPkValue[]) => {
+ const adhocQuery = checkNotNull(nullableAdhocQuery);
+
+ dispatch(
+ datasetApi.util.updateQueryData(
+ "getAdhocQuery",
+ adhocQuery,
+ ({ data }) => {
+ const { indexes: pkIndexes, names: pkNames } = getPkColumns(
+ data.cols,
+ );
+
+ const deletedPKsSet = new Set(
+ rows.map((row) =>
+ getRowObjectPkUniqueKeyByColumnNames(pkNames, row),
+ ),
+ );
+
+ data.rows = data.rows.filter(
+ (row) =>
+ !deletedPKsSet.has(getRowUniqueKeyByPkIndexes(pkIndexes, row)),
+ );
+ },
+ ),
+ );
+ },
+ [dispatch, nullableAdhocQuery],
+ );
+
+ return useMemo(
+ () => ({
+ onRowsCreated,
+ onRowsUpdated,
+ onRowsDeleted,
+ }),
+ [onRowsCreated, onRowsUpdated, onRowsDeleted],
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-update-strategy.ts b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-update-strategy.ts
new file mode 100644
index 00000000000..78168d7c624
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/table-edit/use-table-state-update-strategy.ts
@@ -0,0 +1,38 @@
+import type { DatasetColumn } from "metabase-types/api";
+
+import type { RowCellsWithPkValue } from "../api/types";
+import { getRowObjectPkUniqueKeyByColumnNames } from "../common/utils";
+
+export type OptimisticUpdatePatchResult = {
+ revert: () => void;
+};
+
+export interface TableEditingStateUpdateStrategy {
+ onRowsCreated: (rows: RowCellsWithPkValue[]) => void;
+ onRowsUpdated: (
+ rows: RowCellsWithPkValue[],
+ ) => OptimisticUpdatePatchResult | void;
+ onRowsDeleted: (rows: RowCellsWithPkValue[]) => void;
+}
+
+export function mapDataEditingRowObjectsToRowValues(
+ rows: RowCellsWithPkValue[],
+ columns: DatasetColumn[],
+) {
+ return rows.map((row) => columns.map((column) => row[column.name]));
+}
+
+export function createPrimaryKeyToUpdatedRowObjectMap(
+ pkColumnNames: string[],
+ rows: RowCellsWithPkValue[],
+) {
+ return new Map(
+ rows.map((row) => {
+ const pkUniqueKey = getRowObjectPkUniqueKeyByColumnNames(
+ pkColumnNames,
+ row,
+ );
+ return [pkUniqueKey, row];
+ }),
+ );
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/table-editing/urls.tsx b/enterprise/frontend/src/metabase-enterprise/table-editing/urls.tsx
new file mode 100644
index 00000000000..40b8fc6dcb2
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/table-editing/urls.tsx
@@ -0,0 +1,5 @@
+import type { DatabaseId, TableId } from "metabase-types/api";
+
+export function getTableEditUrl(tableId: TableId, databaseId: DatabaseId) {
+ return `/browse/databases/${databaseId}/tables/${tableId}/edit`;
+}
diff --git a/frontend/src/metabase-types/analytics/event.ts b/frontend/src/metabase-types/analytics/event.ts
index 9b5b6b39f85..0c217f4644c 100644
--- a/frontend/src/metabase-types/analytics/event.ts
+++ b/frontend/src/metabase-types/analytics/event.ts
@@ -247,6 +247,27 @@ export type EmbedWizardCodeCopiedEvent = ValidateEvent<{
event: "embed_wizard_code_copied";
}>;
+export type TableEditingSettingsToggledEvent = ValidateEvent<{
+ event: "edit_data_settings_toggled";
+ event_detail: "on" | "off";
+ target_id: number;
+ triggered_from: "admin-settings-databases";
+}>;
+
+export type TableEditButtonClickedEvent = ValidateEvent<{
+ event: "edit_data_button_clicked";
+ target_id: number;
+ triggered_from: "table-browser";
+}>;
+
+export type TableEditingRecordModifiedEvent = ValidateEvent<{
+ event: "edit_data_record_modified";
+ event_detail: "create" | "update" | "delete";
+ target_id: number;
+ triggered_from: "inline" | "modal";
+ result: "success" | "error";
+}>;
+
export type ConnectionStringParsedSuccessEvent = ValidateEvent<{
event: "connection_string_parsed_success";
triggered_from: "admin" | "setup" | "embedding_setup";
@@ -322,6 +343,11 @@ export type EmbedWizardEvent =
| EmbedWizardAuthSelectedEvent
| EmbedWizardCodeCopiedEvent;
+export type TableEditingEvent =
+ | TableEditingSettingsToggledEvent
+ | TableEditButtonClickedEvent
+ | TableEditingRecordModifiedEvent;
+
export type SimpleEvent =
| CustomSMTPSetupClickedEvent
| CustomSMTPSetupSuccessEvent
@@ -352,6 +378,7 @@ export type SimpleEvent =
| DashboardFilterCreatedEvent
| DashboardFilterMovedEvent
| EmbedWizardEvent
+ | TableEditingEvent
| ConnectionStringParsedSuccessEvent
| ConnectionStringParsedFailedEvent
| TransformTriggerManualRunEvent
diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.ts
index 80f1103519e..35d3414f13e 100644
--- a/frontend/src/metabase-types/api/database.ts
+++ b/frontend/src/metabase-types/api/database.ts
@@ -14,6 +14,7 @@ export type DatabaseSettings = {
export type DatabaseFeature =
| "actions"
+ | "actions/data-editing"
| "basic-aggregations"
| "binning"
| "case-sensitivity-string-filter-options"
@@ -118,6 +119,19 @@ export interface GetDatabaseRequest {
exclude_uneditable_details?: boolean;
}
+export interface GetDatabaseSettingsAvailableResponse {
+ settings: Record<string, DatabaseLocalSettingAvailability>;
+}
+
+export type DatabaseLocalSettingDisableReason = {
+ key: string;
+ message: string;
+};
+
+export type DatabaseLocalSettingAvailability =
+ | { enabled: true }
+ | { enabled: false; reasons: DatabaseLocalSettingDisableReason[] };
+
export type GetDatabaseHealthResponse =
| { status: "ok" }
| { status: "error"; message: string; errors: unknown };
diff --git a/frontend/src/metabase-types/api/field.ts b/frontend/src/metabase-types/api/field.ts
index b1738a34fb0..d1c7f03ee51 100644
--- a/frontend/src/metabase-types/api/field.ts
+++ b/frontend/src/metabase-types/api/field.ts
@@ -148,7 +148,7 @@ export interface GetFieldValuesResponse {
export interface SearchFieldValuesRequest {
fieldId: FieldId;
searchFieldId: FieldId;
- value: string;
+ value?: string;
limit: number;
}
diff --git a/frontend/src/metabase-types/api/mocks/settings.ts b/frontend/src/metabase-types/api/mocks/settings.ts
index a33b5036ea5..4770615ae88 100644
--- a/frontend/src/metabase-types/api/mocks/settings.ts
+++ b/frontend/src/metabase-types/api/mocks/settings.ts
@@ -135,6 +135,7 @@ export const createMockTokenFeatures = (
offer_metabase_ai: false,
database_routing: false,
development_mode: false,
+ table_data_editing: false,
documents: false,
semantic_search: false,
transforms: false,
diff --git a/frontend/src/metabase-types/api/settings.ts b/frontend/src/metabase-types/api/settings.ts
index 650c4cca4bb..cdeedae89e9 100644
--- a/frontend/src/metabase-types/api/settings.ts
+++ b/frontend/src/metabase-types/api/settings.ts
@@ -293,6 +293,7 @@ export const tokenFeatures = [
"development_mode",
"etl_connections",
"etl_connections_pg",
+ "table_data_editing",
"documents",
"semantic_search",
"transforms",
diff --git a/frontend/src/metabase-types/api/table.ts b/frontend/src/metabase-types/api/table.ts
index e55722580d6..6218ab8e6f6 100644
--- a/frontend/src/metabase-types/api/table.ts
+++ b/frontend/src/metabase-types/api/table.ts
@@ -1,5 +1,6 @@
import type { Card, CardType } from "./card";
import type { Database, DatabaseId, InitialSyncStatus } from "./database";
+import type { DatasetData } from "./dataset";
import type { Field, FieldId } from "./field";
import type { Segment } from "./segment";
@@ -43,6 +44,7 @@ export type Table = {
visibility_type: TableVisibilityType;
initial_sync_status: InitialSyncStatus;
is_upload: boolean;
+ is_writable?: boolean;
caveats?: string;
points_of_interest?: string;
created_at: string;
@@ -129,3 +131,28 @@ export interface DeleteUploadTableRequest {
tableId: TableId;
"archive-cards"?: boolean;
}
+
+export interface GetTableDataRequest {
+ tableId: TableId;
+}
+
+export type TableData = {
+ data: DatasetData;
+ database_id: DatabaseId;
+ table_id: TableId;
+ row_count: number;
+ running_time: number;
+ error?:
+ | string
+ | {
+ status: number; // HTTP status code
+ data?: string;
+ };
+ error_type?: string;
+ error_is_curated?: boolean;
+ status?: string;
+ /** In milliseconds */
+ average_execution_time?: number;
+ /** A date in ISO 8601 format */
+ started_at?: string;
+};
diff --git a/frontend/src/metabase-types/store/undo.ts b/frontend/src/metabase-types/store/undo.ts
index 203615355c3..29a8ef9bfdf 100644
--- a/frontend/src/metabase-types/store/undo.ts
+++ b/frontend/src/metabase-types/store/undo.ts
@@ -34,6 +34,8 @@ export interface Undo {
action: () => void;
};
ref?: RefObject<HTMLDivElement>;
+ renderChildren?: (undo: Undo) => ReactNode;
+ onDismiss?: (undoId: string | number) => void;
}
export type UndoState = Undo[];
diff --git a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx
index 5917c7e0ab4..91d344f67d7 100644
--- a/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx
+++ b/frontend/src/metabase/admin/databases/containers/DatabaseEditApp.tsx
@@ -4,7 +4,10 @@ import { t } from "ttag";
import _ from "underscore";
import ErrorBoundary from "metabase/ErrorBoundary";
-import { useGetDatabaseQuery } from "metabase/api";
+import {
+ useGetDatabaseQuery,
+ useGetDatabaseSettingsAvailableQuery,
+} from "metabase/api";
import Breadcrumbs from "metabase/common/components/Breadcrumbs";
import { GenericError } from "metabase/common/components/ErrorPages";
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
@@ -15,6 +18,7 @@ import { connect, useSelector } from "metabase/lib/redux";
import {
PLUGIN_DATABASE_REPLICATION,
PLUGIN_DB_ROUTING,
+ PLUGIN_TABLE_EDITING,
} from "metabase/plugins";
import { getUserIsAdmin } from "metabase/selectors/user";
import { Box, Divider, Flex } from "metabase/ui";
@@ -62,6 +66,9 @@ function DatabaseEditAppInner({
error,
} = useGetDatabaseQuery({ id: databaseId }, { pollingInterval });
+ const { data: settingsAvailable } =
+ useGetDatabaseSettingsAvailableQuery(databaseId);
+
useEffect(
function pollDatabaseWhileSyncing() {
const isSyncing = database?.initial_sync_status === "incomplete";
@@ -107,6 +114,12 @@ function DatabaseEditAppInner({
database={database}
/>
+ <PLUGIN_TABLE_EDITING.AdminDatabaseTableEditingSection
+ database={database}
+ settingsAvailable={settingsAvailable?.settings}
+ updateDatabase={updateDatabase}
+ />
+
<PLUGIN_DB_ROUTING.DatabaseRoutingSection
database={database}
/>
diff --git a/frontend/src/metabase/api/database.ts b/frontend/src/metabase/api/database.ts
index b5df972881f..56eb772d3c8 100644
--- a/frontend/src/metabase/api/database.ts
+++ b/frontend/src/metabase/api/database.ts
@@ -10,6 +10,7 @@ import type {
GetDatabaseHealthResponse,
GetDatabaseMetadataRequest,
GetDatabaseRequest,
+ GetDatabaseSettingsAvailableResponse,
ListDatabaseIdFieldsRequest,
ListDatabaseSchemaTablesRequest,
ListDatabaseSchemasRequest,
@@ -72,6 +73,15 @@ export const databaseApi = Api.injectEndpoints({
providesTags: (database) =>
database ? provideDatabaseTags(database) : [],
}),
+ getDatabaseSettingsAvailable: builder.query<
+ GetDatabaseSettingsAvailableResponse,
+ DatabaseId
+ >({
+ query: (id) => ({
+ method: "GET",
+ url: `/api/database/${id}/settings-available`,
+ }),
+ }),
listDatabaseSchemas: builder.query<
SchemaName[],
ListDatabaseSchemasRequest
@@ -268,6 +278,7 @@ export const {
useGetDatabaseQuery,
useGetDatabaseHealthQuery,
useGetDatabaseMetadataQuery,
+ useGetDatabaseSettingsAvailableQuery,
useListDatabaseSchemasQuery,
useLazyListDatabaseSchemasQuery,
usePrefetch: useDatabasePrefetch,
diff --git a/frontend/src/metabase/api/table.ts b/frontend/src/metabase/api/table.ts
index 059e98dde43..18e13699e77 100644
--- a/frontend/src/metabase/api/table.ts
+++ b/frontend/src/metabase/api/table.ts
@@ -1,8 +1,10 @@
import type {
ForeignKey,
+ GetTableDataRequest,
GetTableQueryMetadataRequest,
GetTableRequest,
Table,
+ TableData,
TableId,
TableListQuery,
UpdateTableFieldsOrderRequest,
@@ -45,6 +47,12 @@ export const tableApi = Api.injectEndpoints({
}),
providesTags: (table) => (table ? provideTableTags(table) : []),
}),
+ getTableData: builder.query<TableData, GetTableDataRequest>({
+ query: ({ tableId }) => ({
+ method: "GET",
+ url: `/api/table/${tableId}/data`,
+ }),
+ }),
listTableForeignKeys: builder.query<ForeignKey[], TableId>({
query: (id) => ({
method: "GET",
@@ -128,6 +136,7 @@ export const {
useListTablesQuery,
useGetTableQuery,
useGetTableQueryMetadataQuery,
+ useGetTableDataQuery,
useListTableForeignKeysQuery,
useLazyListTableForeignKeysQuery,
useUpdateTableMutation,
diff --git a/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.jsx b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.jsx
index a86c031460f..edd155c24e9 100644
--- a/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.jsx
+++ b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.jsx
@@ -7,18 +7,22 @@ import { BrowseGrid } from "metabase/browse/components/BrowseGrid";
import { BrowserCrumbs } from "metabase/common/components/BrowserCrumbs";
import Link from "metabase/common/components/Link";
import CS from "metabase/css/core/index.css";
-import { color } from "metabase/lib/colors";
+import { trackSimpleEvent } from "metabase/lib/analytics";
+import { useSelector } from "metabase/lib/redux";
import { isSyncInProgress } from "metabase/lib/syncing";
-import { Box, Group, Icon, Loader } from "metabase/ui";
+import { PLUGIN_TABLE_EDITING } from "metabase/plugins";
+import { getDatabases } from "metabase/reference/selectors";
+import { getUserIsAdmin } from "metabase/selectors/user";
+import { ActionIcon, Group, Icon, Loader, Paper } from "metabase/ui";
import { isVirtualCardId } from "metabase-lib/v1/metadata/utils/saved-questions";
import { BrowseHeaderContent } from "../../components/BrowseHeader.styled";
import { trackTableClick } from "../analytics";
+import S from "./TableBrowser.module.css";
import { useDatabaseCrumb } from "./useDatabaseCrumb";
const propTypes = {
- database: PropTypes.object,
tables: PropTypes.array.isRequired,
getTableUrl: PropTypes.func.isRequired,
metadata: PropTypes.object,
@@ -37,7 +41,14 @@ export const TableBrowser = ({
xraysEnabled,
showSchemaInHeader = true,
}) => {
+ const databases = useSelector(getDatabases);
+ const database = databases[dbId];
+ const isAdmin = useSelector(getUserIsAdmin);
const databaseCrumb = useDatabaseCrumb(dbId);
+ const canEditTables =
+ database &&
+ isAdmin &&
+ PLUGIN_TABLE_EDITING.isDatabaseTableEditingEnabled(database);
return (
<>
@@ -59,6 +70,7 @@ export const TableBrowser = ({
getTableUrl={getTableUrl}
xraysEnabled={xraysEnabled}
metadata={metadata}
+ canEditTables={canEditTables}
/>
))}
</BrowseGrid>
@@ -74,6 +86,7 @@ const itemPropTypes = {
xraysEnabled: PropTypes.bool,
metadata: PropTypes.object,
getTableUrl: PropTypes.func.isRequired,
+ canEditTables: PropTypes.bool,
};
const TableBrowserItem = ({
@@ -82,9 +95,11 @@ const TableBrowserItem = ({
xraysEnabled,
metadata,
getTableUrl,
+ canEditTables,
}) => {
const isVirtual = isVirtualCardId(table.id);
const isLoading = isSyncInProgress(table);
+ const isTableWritable = table.is_writable;
return (
<BrowseCard
@@ -100,6 +115,7 @@ const TableBrowserItem = ({
tableId={table.id}
dbId={dbId}
xraysEnabled={xraysEnabled}
+ canEditTables={canEditTables && isTableWritable}
/>
)}
</>
@@ -113,30 +129,64 @@ const itemButtonsPropTypes = {
tableId: PropTypes.number,
dbId: PropTypes.number,
xraysEnabled: PropTypes.bool,
+ canEditTables: PropTypes.bool,
};
-const TableBrowserItemButtons = ({ tableId, dbId, xraysEnabled }) => {
+const TableBrowserItemButtons = ({
+ tableId,
+ dbId,
+ xraysEnabled,
+ canEditTables,
+}) => {
+ const handleEditTableClicked = () => {
+ trackSimpleEvent({
+ event: "edit_data_button_clicked",
+ target_id: tableId,
+ triggered_from: "table-browser",
+ });
+ };
+
return (
- <Box className={cx(CS.hoverChild)}>
- <Group gap="md">
+ <Paper p="sm" className={cx(CS.hoverChild, S.tableBrowserItemButtons)}>
+ <Group gap="sm">
{xraysEnabled && (
- <Link to={`/auto/dashboard/table/${tableId}`}>
- <Icon
- name="bolt_filled"
- tooltip={t`X-ray this table`}
- color={color("warning")}
- />
- </Link>
+ <ActionIcon
+ component={Link}
+ to={`/auto/dashboard/table/${tableId}`}
+ size="sm"
+ tooltip={t`X-ray this table`}
+ color="warning"
+ aria-label={t`X-ray this table`}
+ >
+ <Icon name="bolt" />
+ </ActionIcon>
)}
- <Link to={`/reference/databases/${dbId}/tables/${tableId}`}>
- <Icon
- name="reference"
- tooltip={t`Learn about this table`}
- color={color("text-medium")}
- />
- </Link>
+ {canEditTables && (
+ <ActionIcon
+ component={Link}
+ to={PLUGIN_TABLE_EDITING.getTableEditUrl(tableId, dbId)}
+ onClick={handleEditTableClicked}
+ size="sm"
+ tooltip={t`Edit this table`}
+ color="text-medium"
+ aria-label={t`Edit this table`}
+ data-testid="edit-table-icon"
+ >
+ <Icon name="pencil" />
+ </ActionIcon>
+ )}
+ <ActionIcon
+ component={Link}
+ to={`/reference/databases/${dbId}/tables/${tableId}`}
+ size="sm"
+ tooltip={t`Learn about this table`}
+ color="text-medium"
+ aria-label={t`Learn about this table`}
+ >
+ <Icon name="reference" />
+ </ActionIcon>
</Group>
- </Box>
+ </Paper>
);
};
diff --git a/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.module.css b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.module.css
new file mode 100644
index 00000000000..ad465bd287a
--- /dev/null
+++ b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.module.css
@@ -0,0 +1,6 @@
+.tableBrowserItemButtons {
+ position: absolute;
+ top: 50%;
+ transform: translateY(-50%);
+ right: 0.5rem;
+}
diff --git a/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.unit.spec.js b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.unit.spec.js
index f52ea0a8372..63711a0fa88 100644
--- a/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.unit.spec.js
+++ b/frontend/src/metabase/browse/tables/TableBrowser/TableBrowser.unit.spec.js
@@ -21,7 +21,7 @@ describe("TableBrowser", () => {
setup({ id: 1, name: "Orders", initial_sync_status: "complete" });
expect(screen.getByText("Orders")).toBeInTheDocument();
- expect(screen.getByLabelText("bolt_filled icon")).toBeInTheDocument();
+ expect(screen.getByLabelText("bolt icon")).toBeInTheDocument();
expect(screen.queryByTestId("loading-indicator")).not.toBeInTheDocument();
});
@@ -29,7 +29,7 @@ describe("TableBrowser", () => {
setup({ id: 1, name: "Orders", initial_sync_status: "incomplete" });
expect(screen.getByText("Orders")).toBeInTheDocument();
- expect(screen.queryByLabelText("bolt_filled icon")).not.toBeInTheDocument();
+ expect(screen.queryByLabelText("bolt icon")).not.toBeInTheDocument();
expect(screen.getByTestId("loading-indicator")).toBeInTheDocument();
});
@@ -37,7 +37,7 @@ describe("TableBrowser", () => {
setup({ id: 1, name: "Orders", initial_sync_status: "aborted" });
expect(screen.getByText("Orders")).toBeInTheDocument();
- expect(screen.getByLabelText("bolt_filled icon")).toBeInTheDocument();
+ expect(screen.getByLabelText("bolt icon")).toBeInTheDocument();
expect(screen.queryByTestId("loading-indicator")).not.toBeInTheDocument();
});
});
diff --git a/frontend/src/metabase/common/components/UndoListing.tsx b/frontend/src/metabase/common/components/UndoListing.tsx
index 2bb75e66dad..4c8c25b7a51 100644
--- a/frontend/src/metabase/common/components/UndoListing.tsx
+++ b/frontend/src/metabase/common/components/UndoListing.tsx
@@ -128,7 +128,11 @@ function UndoToast({
color={undo.iconColor ?? "var(--mb-color-text-secondary-inverse)"}
/>
)}
- <Ellipsified showTooltip={false}>{renderMessage(undo)}</Ellipsified>
+ {undo.renderChildren ? (
+ undo.renderChildren(undo)
+ ) : (
+ <Ellipsified showTooltip={false}>{renderMessage(undo)}</Ellipsified>
+ )}
</CardContentSide>
<ControlsCardContent>
{undo.actions && undo.actions.length > 0 && (
@@ -174,7 +178,10 @@ export function UndoListing() {
<UndoListOverlay
undos={undos}
onUndo={(undo) => dispatch(performUndo(undo.id))}
- onDismiss={(undo) => dispatch(dismissUndo({ undoId: undo.id }))}
+ onDismiss={(undo) => {
+ undo.onDismiss?.(undo.id);
+ dispatch(dismissUndo({ undoId: undo.id }));
+ }}
/>
);
}
diff --git a/frontend/src/metabase/common/hooks/use-close-navbar-on-mount.tsx b/frontend/src/metabase/common/hooks/use-close-navbar-on-mount.tsx
new file mode 100644
index 00000000000..655a27a779b
--- /dev/null
+++ b/frontend/src/metabase/common/hooks/use-close-navbar-on-mount.tsx
@@ -0,0 +1,12 @@
+import { useMount } from "react-use";
+
+import { useDispatch } from "metabase/lib/redux";
+import { closeNavbar } from "metabase/redux/app";
+
+export function useCloseNavbarOnMount() {
+ const dispatch = useDispatch();
+
+ useMount(() => {
+ dispatch(closeNavbar());
+ });
+}
diff --git a/frontend/src/metabase/data-grid/types.ts b/frontend/src/metabase/data-grid/types.ts
index 70b1f67f091..09d7750b1e1 100644
--- a/frontend/src/metabase/data-grid/types.ts
+++ b/frontend/src/metabase/data-grid/types.ts
@@ -71,6 +71,9 @@ export interface ColumnOptions<TRow extends RowData, TValue = unknown> {
/** Custom cell render template */
cell?: ColumnDefTemplate<CellContext<TRow, TValue>>;
+ /** Custom cell render template for cells in editing state */
+ editingCell?: (props: CellContext<TRow, TValue>) => React.JSX.Element;
+
/** Custom header render template */
header?: ColumnDefTemplate<HeaderContext<TRow, TValue>>;
@@ -78,10 +81,18 @@ export interface ColumnOptions<TRow extends RowData, TValue = unknown> {
cellVariant?: BodyCellVariant;
/** Function to determine CSS class names for cells */
- getCellClassName?: (value: TValue, rowIndex: number) => string;
+ getCellClassName?: (
+ value: TValue,
+ rowIndex: number,
+ columnId: string,
+ ) => string;
/** Function to determine CSS styles for cells */
- getCellStyle?: (value: TValue, rowIndex: number) => React.CSSProperties;
+ getCellStyle?: (
+ value: TValue,
+ rowIndex: number,
+ columnId: string,
+ ) => React.CSSProperties;
/** Visual style of the header cell */
headerVariant?: HeaderCellVariant;
@@ -109,6 +120,9 @@ export interface ColumnOptions<TRow extends RowData, TValue = unknown> {
/** Function to format cell values when copying to clipboard */
clipboardFormatter?: PlainCellFormatter<TValue>;
+
+ /** Function to determine if a cell is in editing state */
+ getIsEditing?: (columnId: string, rowIndex: number) => boolean;
}
/**
diff --git a/frontend/src/metabase/data-grid/utils/columns/data-column.tsx b/frontend/src/metabase/data-grid/utils/columns/data-column.tsx
index b0610746e8d..18bdcacb223 100644
--- a/frontend/src/metabase/data-grid/utils/columns/data-column.tsx
+++ b/frontend/src/metabase/data-grid/utils/columns/data-column.tsx
@@ -24,17 +24,23 @@ const getDefaultCellTemplate = <TRow, TValue>(
wrap,
getCellClassName,
getCellStyle,
+ getIsEditing,
+ editingCell: EditingCellComponent,
}: ColumnOptions<TRow, TValue>,
isTruncated: boolean,
onExpand: (columnName: string, content: React.ReactNode) => void,
) => {
- return function Cell({
- getValue,
- row,
- isSelected,
- }: CellContext<TRow, TValue> & { isSelected?: boolean }) {
+ return function Cell(
+ props: CellContext<TRow, TValue> & { isSelected?: boolean },
+ ) {
+ const { getValue, row, isSelected } = props;
const value = getValue();
const backgroundColor = getBackgroundColor?.(value, row?.index);
+ const isEditing = getIsEditing?.(id, row.index);
+
+ if (isEditing && EditingCellComponent) {
+ return <EditingCellComponent {...props} />;
+ }
return (
<BodyCell
@@ -49,8 +55,8 @@ const getDefaultCellTemplate = <TRow, TValue>(
onExpand={onExpand}
variant={cellVariant}
wrap={wrap}
- className={getCellClassName?.(value, row.index)}
- style={getCellStyle?.(value, row.index)}
+ className={getCellClassName?.(value, row.index, id)}
+ style={getCellStyle?.(value, row.index, id)}
/>
);
};
diff --git a/frontend/src/metabase/lib/core.ts b/frontend/src/metabase/lib/core.ts
index f6d97366fba..bf918811510 100644
--- a/frontend/src/metabase/lib/core.ts
+++ b/frontend/src/metabase/lib/core.ts
@@ -574,7 +574,7 @@ export const FIELD_SEMANTIC_TYPES: FieldSemanticType[] = [
export const FIELD_SEMANTIC_TYPES_MAP = FIELD_SEMANTIC_TYPES.reduce(
(map, type) => Object.assign({}, map, { [type.id]: type }),
- {},
+ {} as Record<FieldSemanticType["id"], FieldSemanticType>,
);
export const HAS_FIELD_VALUES_OPTIONS = [
diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.ts
index 83d4690084e..5256dec3f74 100644
--- a/frontend/src/metabase/plugins/index.ts
+++ b/frontend/src/metabase/plugins/index.ts
@@ -67,6 +67,9 @@ import type {
DashCardId,
Dashboard,
DashboardId,
+ DatabaseData,
+ DatabaseId,
+ DatabaseLocalSettingAvailability,
Database as DatabaseType,
Dataset,
Document,
@@ -808,6 +811,20 @@ export const PLUGIN_SMTP_OVERRIDE: {
SMTPOverrideConnectionForm: PluginPlaceholder,
};
+export const PLUGIN_TABLE_EDITING = {
+ isEnabled: () => false,
+ isDatabaseTableEditingEnabled: (_database: DatabaseType): boolean => false,
+ getRoutes: () => null as React.ReactElement | null,
+ getTableEditUrl: (_tableId: TableId, _databaseId: DatabaseId): string => "/",
+ AdminDatabaseTableEditingSection: PluginPlaceholder as ComponentType<{
+ database: DatabaseType;
+ settingsAvailable?: Record<string, DatabaseLocalSettingAvailability>;
+ updateDatabase: (
+ database: { id: DatabaseId } & Partial<DatabaseData>,
+ ) => Promise<void>;
+ }>,
+};
+
export const PLUGIN_DOCUMENTS = {
getRoutes: () => null as React.ReactElement | null,
shouldShowDocumentInNewItemMenu: () => false,
diff --git a/frontend/src/metabase/routes.jsx b/frontend/src/metabase/routes.jsx
index a9a3f09cb56..0361a9e8842 100644
--- a/frontend/src/metabase/routes.jsx
+++ b/frontend/src/metabase/routes.jsx
@@ -44,6 +44,7 @@ import {
PLUGIN_EMBEDDING_IFRAME_SDK_SETUP,
PLUGIN_LANDING_PAGE,
PLUGIN_METABOT,
+ PLUGIN_TABLE_EDITING,
} from "metabase/plugins";
import { QueryBuilder } from "metabase/query_builder/containers/QueryBuilder";
import { loadCurrentUser } from "metabase/redux/user";
@@ -310,6 +311,8 @@ export const getRoutes = (store) => {
component={BrowseTables}
/>
+ {PLUGIN_TABLE_EDITING.getRoutes()}
+
{/* These two Redirects support legacy paths in v48 and earlier */}
<Redirect from=":dbId-:slug" to="databases/:dbId-:slug" />
<Redirect
diff --git a/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.config.tsx b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.config.tsx
new file mode 100644
index 00000000000..3f4409258aa
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.config.tsx
@@ -0,0 +1,47 @@
+import { DateTimePicker } from "@mantine/dates";
+
+import CalendarStyles from "../Calendar/Calendar.module.css";
+
+import Styles from "./DateTimePicker.module.css";
+
+export const dateTimePickerOverrides = {
+ DateTimePicker: DateTimePicker.extend({
+ defaultProps: {
+ size: "md",
+ popoverProps: {
+ styles: {
+ dropdown: {
+ /* Padding workaround since popover padding is overridden to zero */
+ "--popover-padding": "var(--mantine-spacing-md)",
+ },
+ },
+ },
+ submitButtonProps: {
+ variant: "light",
+ },
+ },
+ classNames: {
+ levelsGroup: Styles.levelsGroup,
+ timeWrapper: Styles.timeWrapper,
+ day: CalendarStyles.day,
+ weekday: CalendarStyles.weekday,
+ month: CalendarStyles.month,
+ monthRow: CalendarStyles.row,
+ monthCell: CalendarStyles.cell,
+ monthsList: CalendarStyles.monthsList,
+ monthsListRow: CalendarStyles.row,
+ monthsListCell: CalendarStyles.cell,
+ yearsList: CalendarStyles.yearsList,
+ yearsListRow: CalendarStyles.row,
+ yearsListCell: CalendarStyles.cell,
+ calendarHeader: CalendarStyles.calendarHeader,
+ calendarHeaderLevel: CalendarStyles.calendarHeaderLevel,
+ calendarHeaderControl: CalendarStyles.calendarHeaderControl,
+ },
+ styles: {
+ weekdaysRow: {
+ boxSizing: "border-box",
+ },
+ },
+ }),
+};
diff --git a/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.mdx b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.mdx
new file mode 100644
index 00000000000..04983c6a5f9
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.mdx
@@ -0,0 +1,25 @@
+import { Canvas, Story, Meta } from "@storybook/blocks";
+import { DatePicker, Box } from "metabase/ui";
+import * as DateTimePickerStories from "./DateTimePicker.stories";
+
+<Meta of={DateTimePickerStories} />
+
+# DateTimePicker
+
+Our themed wrapper around [Mantine DateTimePicker](https://mantine.dev/dates/date-time-picker/).
+
+## Docs
+
+- [Mantine DatePicker Docs](https://mantine.dev/dates/date-time-picker/)
+
+## Examples
+
+<Canvas of={DateTimePickerStories.Default} />
+
+### With Seconds
+
+<Canvas of={DateTimePickerStories.WithSeconds} />
+
+### Value Format
+
+<Canvas of={DateTimePickerStories.ValueFormat} />
diff --git a/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.module.css b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.module.css
new file mode 100644
index 00000000000..bd10e2044f7
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.module.css
@@ -0,0 +1,11 @@
+.levelsGroup {
+ /* Padding workaround since popover padding is overridden to zero */
+ padding: var(--popover-padding);
+ padding-bottom: 0;
+}
+
+.timeWrapper {
+ /* Padding workaround since popover padding is overridden to zero */
+ padding: var(--popover-padding);
+ padding-top: 0;
+}
diff --git a/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.stories.tsx b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.stories.tsx
new file mode 100644
index 00000000000..9c5bdf14a98
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/DateTimePicker/DateTimePicker.stories.tsx
@@ -0,0 +1,38 @@
+import { DateTimePicker } from "metabase/ui";
+
+const args = {};
+
+const sampleArgs = {
+ date1: new Date(2023, 9, 8, 9, 1, 2),
+ date2: new Date(2023, 9, 24, 10, 2, 3),
+ date3: new Date(2023, 9, 16, 23, 54, 0),
+};
+
+const argTypes = {};
+
+export default {
+ title: "Components/Inputs/DateTimePicker",
+ component: DateTimePicker,
+ args,
+ argTypes,
+};
+
+export const Default = {
+ args: {
+ defaultValue: sampleArgs.date1,
+ },
+};
+
+export const WithSeconds = {
+ args: {
+ defaultValue: sampleArgs.date2,
+ withSeconds: true,
+ },
+};
+
+export const ValueFormat = {
+ args: {
+ defaultValue: sampleArgs.date3,
+ valueFormat: "HH:mm on YYYY-MM-DD",
+ },
+};
diff --git a/frontend/src/metabase/ui/components/inputs/DateTimePicker/index.ts b/frontend/src/metabase/ui/components/inputs/DateTimePicker/index.ts
new file mode 100644
index 00000000000..e3286aef6b7
--- /dev/null
+++ b/frontend/src/metabase/ui/components/inputs/DateTimePicker/index.ts
@@ -0,0 +1,3 @@
+export { DateTimePicker } from "@mantine/dates";
+export type { DateTimePickerProps } from "@mantine/dates";
+export { dateTimePickerOverrides } from "./DateTimePicker.config";
diff --git a/frontend/src/metabase/ui/components/inputs/index.ts b/frontend/src/metabase/ui/components/inputs/index.ts
index ef8f84e56f5..fcdd21bdc4f 100644
--- a/frontend/src/metabase/ui/components/inputs/index.ts
+++ b/frontend/src/metabase/ui/components/inputs/index.ts
@@ -5,6 +5,7 @@ export * from "./Chip";
export * from "./Combobox";
export * from "./DateInput";
export * from "./DatePicker";
+export * from "./DateTimePicker";
export * from "./FileInput";
export * from "./Input";
export * from "./MonthPicker";
diff --git a/frontend/src/metabase/ui/theme.ts b/frontend/src/metabase/ui/theme.ts
index 1466cd8c02b..3614d078b32 100644
--- a/frontend/src/metabase/ui/theme.ts
+++ b/frontend/src/metabase/ui/theme.ts
@@ -20,6 +20,7 @@ import {
comboboxOverrides,
dateInputOverrides,
datePickerOverrides,
+ dateTimePickerOverrides,
dividerOverrides,
fileInputOverrides,
hoverCardOverrides,
@@ -149,6 +150,7 @@ export const getThemeOverrides = (): MantineThemeOverride => ({
...codeOverrides,
...dateInputOverrides,
...datePickerOverrides,
+ ...dateTimePickerOverrides,
...dividerOverrides,
...fileInputOverrides,
...inputOverrides,