Skip to content

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

Optimize field metadata list renders (#74043)

Andersonede230a98666
diff --git a/frontend/src/metabase/metadata/components/FieldList/FieldItem/FieldItem.tsx b/frontend/src/metabase/metadata/components/FieldList/FieldItem/FieldItem.tsx
index 94c773b028a..ff8c7dc5a95 100644
--- a/frontend/src/metabase/metadata/components/FieldList/FieldItem/FieldItem.tsx
+++ b/frontend/src/metabase/metadata/components/FieldList/FieldItem/FieldItem.tsx
@@ -1,5 +1,6 @@
import cx from "classnames";
import type { MouseEvent } from "react";
+import { memo, useMemo } from "react";
import { Link } from "react-router";
import { t } from "ttag";
@@ -17,12 +18,12 @@ type FieldItemProps = {
active?: boolean;
parent?: Field;
readOnly?: boolean;
- onSelect?: () => void;
- onNameChange: (newName: string) => void;
- onDescriptionChange: (newDescription: string | null) => void;
+ onSelect?: (field: Field) => void;
+ onNameChange: (field: Field, newName: string) => void;
+ onDescriptionChange: (field: Field, newDescription: string | null) => void;
};
-export function FieldItem({
+const FieldItemBase = ({
active,
field,
href,
@@ -31,13 +32,15 @@ export function FieldItem({
onSelect,
onNameChange,
onDescriptionChange,
-}: FieldItemProps) {
- const icon = getColumnIcon(Lib.legacyColumnTypeInfo(field));
+}: FieldItemProps) => {
+ const icon = useMemo(() => {
+ return getColumnIcon(Lib.legacyColumnTypeInfo(field));
+ }, [field]);
const handleNameChange = async (newValue: string) => {
const newName = newValue.trim();
if (field.display_name !== newName) {
- onNameChange(newName);
+ onNameChange(field, newName);
}
};
@@ -46,7 +49,7 @@ export function FieldItem({
const newDescription = trimmedValue.length === 0 ? null : trimmedValue;
if (field.description !== newDescription) {
- onDescriptionChange(newDescription);
+ onDescriptionChange(field, newDescription);
}
};
@@ -69,7 +72,7 @@ export function FieldItem({
) {
event.preventDefault();
} else {
- onSelect?.();
+ onSelect?.(field);
}
};
@@ -171,4 +174,41 @@ export function FieldItem({
</Flex>
</Card>
);
+};
+
+export function areFieldItemPropsEqual(
+ prevProps: FieldItemProps,
+ nextProps: FieldItemProps,
+) {
+ return (
+ prevProps.active === nextProps.active &&
+ prevProps.href === nextProps.href &&
+ prevProps.readOnly === nextProps.readOnly &&
+ prevProps.onSelect === nextProps.onSelect &&
+ prevProps.onNameChange === nextProps.onNameChange &&
+ prevProps.onDescriptionChange === nextProps.onDescriptionChange &&
+ areFieldsEqual(prevProps.field, nextProps.field) &&
+ areParentFieldsEqual(prevProps.parent, nextProps.parent)
+ );
}
+
+function areFieldsEqual(prevField: Field, nextField: Field) {
+ return (
+ prevField.id === nextField.id &&
+ prevField.display_name === nextField.display_name &&
+ prevField.description === nextField.description &&
+ prevField.base_type === nextField.base_type &&
+ prevField.effective_type === nextField.effective_type &&
+ prevField.semantic_type === nextField.semantic_type &&
+ prevField.fk_target_field_id === nextField.fk_target_field_id
+ );
+}
+
+function areParentFieldsEqual(prevField?: Field, nextField?: Field) {
+ return (
+ prevField?.id === nextField?.id &&
+ prevField?.display_name === nextField?.display_name
+ );
+}
+
+export const FieldItem = memo(FieldItemBase, areFieldItemPropsEqual);
diff --git a/frontend/src/metabase/metadata/components/FieldList/FieldList.tsx b/frontend/src/metabase/metadata/components/FieldList/FieldList.tsx
index bf538dc7a27..0807b0668c6 100644
--- a/frontend/src/metabase/metadata/components/FieldList/FieldList.tsx
+++ b/frontend/src/metabase/metadata/components/FieldList/FieldList.tsx
@@ -46,11 +46,9 @@ export function FieldList<T extends number | string>({
parent={parent}
href={getFieldHref?.(field) ?? ""}
readOnly={readOnly}
- onSelect={() => onSelect?.(field)}
- onNameChange={(newName) => onNameChange(field, newName)}
- onDescriptionChange={(newDescription) =>
- onDescriptionChange(field, newDescription)
- }
+ onSelect={onSelect}
+ onNameChange={onNameChange}
+ onDescriptionChange={onDescriptionChange}
/>
);
})}
diff --git a/frontend/src/metabase/metadata/components/FieldList/FieldList.unit.spec.tsx b/frontend/src/metabase/metadata/components/FieldList/FieldList.unit.spec.tsx
new file mode 100644
index 00000000000..898c496bad2
--- /dev/null
+++ b/frontend/src/metabase/metadata/components/FieldList/FieldList.unit.spec.tsx
@@ -0,0 +1,62 @@
+import { createMockField } from "metabase-types/api/mocks";
+
+import { areFieldItemPropsEqual } from "./FieldItem";
+
+describe("areFieldItemPropsEqual", () => {
+ const onNameChange = jest.fn();
+ const onDescriptionChange = jest.fn();
+
+ it("keeps field items memoized when rendered field data did not change", () => {
+ const field = createMockField({
+ id: 1,
+ name: "field_1",
+ display_name: "Field 1",
+ position: 1,
+ });
+ const nextField = { ...field };
+
+ expect(
+ areFieldItemPropsEqual(
+ {
+ field,
+ href: "/field/1",
+ onNameChange,
+ onDescriptionChange,
+ },
+ {
+ field: nextField,
+ href: "/field/1",
+ onNameChange,
+ onDescriptionChange,
+ },
+ ),
+ ).toBe(true);
+ });
+
+ it("rerenders field items when rendered field data changes", () => {
+ const field = createMockField({
+ id: 1,
+ name: "field_1",
+ display_name: "Field 1",
+ position: 1,
+ });
+ const nextField = { ...field, semantic_type: "type/Category" };
+
+ expect(
+ areFieldItemPropsEqual(
+ {
+ field,
+ href: "/field/1",
+ onNameChange,
+ onDescriptionChange,
+ },
+ {
+ field: nextField,
+ href: "/field/1",
+ onNameChange,
+ onDescriptionChange,
+ },
+ ),
+ ).toBe(false);
+ });
+});
diff --git a/frontend/src/metabase/metadata/components/PreviewSection/PreviewSection.tsx b/frontend/src/metabase/metadata/components/PreviewSection/PreviewSection.tsx
index 36508dc99a1..e210bc76a52 100644
--- a/frontend/src/metabase/metadata/components/PreviewSection/PreviewSection.tsx
+++ b/frontend/src/metabase/metadata/components/PreviewSection/PreviewSection.tsx
@@ -104,12 +104,7 @@ const PreviewSectionBase = ({
databaseId={databaseId}
field={field}
fieldId={fieldId}
- /**
- * Make sure internal component state is reset when changing any field settings.
- * This is because use***Filter hooks cache some parts of state internally on mount
- * and do not account for all prop changes during their lifecycle.
- */
- key={JSON.stringify(field)}
+ key={getFilteringPreviewKey(field)}
table={table}
/>
)}
@@ -118,4 +113,16 @@ const PreviewSectionBase = ({
);
};
+function getFilteringPreviewKey(field: Field) {
+ return [
+ field.id,
+ field.base_type,
+ field.effective_type,
+ field.semantic_type,
+ field.fk_target_field_id,
+ field.visibility_type,
+ field.has_field_values,
+ ].join(":");
+}
+
export const PreviewSection = memo(PreviewSectionBase);
diff --git a/frontend/src/metabase/metadata/components/TableFieldList/TableFieldList.tsx b/frontend/src/metabase/metadata/components/TableFieldList/TableFieldList.tsx
index 48565c81df6..66e32506b7a 100644
--- a/frontend/src/metabase/metadata/components/TableFieldList/TableFieldList.tsx
+++ b/frontend/src/metabase/metadata/components/TableFieldList/TableFieldList.tsx
@@ -1,4 +1,4 @@
-import { useMemo } from "react";
+import { useCallback, useMemo } from "react";
import { t } from "ttag";
import _ from "underscore";
@@ -27,52 +27,62 @@ export const TableFieldList = ({
return _.sortBy(table.fields ?? [], (item) => item.position);
}, [table.fields]);
- const handleNameChange = async (field: Field, name: string) => {
- const id = getRawTableFieldId(field);
- const { error } = await updateField({ id, display_name: name });
-
- if (error) {
- sendErrorToast(t`Failed to update name of ${field.display_name}`);
- } else {
- sendSuccessToast(t`Name of ${field.display_name} updated`, async () => {
- const { error } = await updateField({
- id,
- display_name: field.display_name,
- });
- sendUndoToast(error);
- });
- }
- };
+ const handleGetFieldHref = useCallback(
+ (field: Field) => getFieldHref(getRawTableFieldId(field)),
+ [getFieldHref],
+ );
- const handleDescriptionChange = async (
- field: Field,
- description: string | null,
- ) => {
- const id = getRawTableFieldId(field);
- const { error } = await updateField({ id, description });
+ const handleNameChange = useCallback(
+ async (field: Field, name: string) => {
+ const id = getRawTableFieldId(field);
+ const { error } = await updateField({ id, display_name: name });
- if (error) {
- sendErrorToast(t`Failed to update description of ${field.display_name}`);
- } else {
- sendSuccessToast(
- t`Description of ${field.display_name} updated`,
- async () => {
+ if (error) {
+ sendErrorToast(t`Failed to update name of ${field.display_name}`);
+ } else {
+ sendSuccessToast(t`Name of ${field.display_name} updated`, async () => {
const { error } = await updateField({
id,
- description: field.description ?? "",
+ display_name: field.display_name,
});
sendUndoToast(error);
- },
- );
- }
- };
+ });
+ }
+ },
+ [sendErrorToast, sendSuccessToast, sendUndoToast, updateField],
+ );
+
+ const handleDescriptionChange = useCallback(
+ async (field: Field, description: string | null) => {
+ const id = getRawTableFieldId(field);
+ const { error } = await updateField({ id, description });
+
+ if (error) {
+ sendErrorToast(
+ t`Failed to update description of ${field.display_name}`,
+ );
+ } else {
+ sendSuccessToast(
+ t`Description of ${field.display_name} updated`,
+ async () => {
+ const { error } = await updateField({
+ id,
+ description: field.description ?? "",
+ });
+ sendUndoToast(error);
+ },
+ );
+ }
+ },
+ [sendErrorToast, sendSuccessToast, sendUndoToast, updateField],
+ );
return (
<FieldList
fields={fields}
activeFieldKey={activeFieldId}
getFieldKey={getRawTableFieldId}
- getFieldHref={(field) => getFieldHref(getRawTableFieldId(field))}
+ getFieldHref={handleGetFieldHref}
onNameChange={handleNameChange}
onDescriptionChange={handleDescriptionChange}
/>