The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Fix the Glossary deduplication UX (#68243)
Gabriel Moreno3a490774cc7d
diff --git a/frontend/src/metabase/reference/glossary/Glossary.module.css b/frontend/src/metabase/reference/glossary/Glossary.module.cssindex 233b8e01781..b2c1d27c253 100644--- a/frontend/src/metabase/reference/glossary/Glossary.module.css+++ b/frontend/src/metabase/reference/glossary/Glossary.module.css@@ -52,3 +52,9 @@border: none;padding-inline: 0 !important;}++.inputError {+ border: none;+ padding-inline: 0 !important;+ color: var(--mb-color-error) !important;+}diff --git a/frontend/src/metabase/reference/glossary/GlossaryRowEditor.tsx b/frontend/src/metabase/reference/glossary/GlossaryRowEditor.tsxindex 52196f9e140..d193ded53b4 100644--- a/frontend/src/metabase/reference/glossary/GlossaryRowEditor.tsx+++ b/frontend/src/metabase/reference/glossary/GlossaryRowEditor.tsx@@ -1,5 +1,5 @@import { useHotkeys } from "@mantine/hooks";-import { useCallback, useEffect, useRef, useState } from "react";+import { useCallback, useEffect, useMemo, useRef, useState } from "react";import { t } from "ttag";import type { GlossaryItem } from "metabase/api";@@ -8,6 +8,7 @@ import {Box,Group,Icon,+ Text,TextInput,Textarea,Tooltip,@@ -20,6 +21,7 @@ type GlossaryRowEditorProps = {onSave: (term: string, definition: string) => void | Promise<void>;onCancel: () => void;autoFocusField?: "term" | "definition";+ existingTerms?: string[];};export function GlossaryRowEditor({@@ -27,11 +29,24 @@ export function GlossaryRowEditor({onSave,onCancel,autoFocusField = "term",+ existingTerms = [],}: GlossaryRowEditorProps) {const [term, setTerm] = useState(item.term);const [definition, setDefinition] = useState(item.definition);- const canSave = term.trim() !== "" && definition.trim() !== "";+ const isDuplicate = useMemo(() => {+ const trimmedTerm = term.trim().toLowerCase();+ if (!trimmedTerm) {+ return false;+ }++ return existingTerms.some(+ (existingTerm) => existingTerm.toLowerCase() === trimmedTerm,+ );+ }, [term, existingTerms]);++ const canSave =+ term.trim() !== "" && definition.trim() !== "" && !isDuplicate;const save = useCallback(() => {void onSave(term.trim(), definition.trim());@@ -53,7 +68,7 @@ export function GlossaryRowEditor({<Box component="td" valign="top"><TextInputclassNames={{- input: S.input,+ input: isDuplicate ? S.inputError : S.input,}}fw="bold"variant="unstyled"@@ -66,27 +81,37 @@ export function GlossaryRowEditor({/></Box><Box component="td" valign="top" pr="0">- <Textarea- classNames={{- input: S.input,- }}- variant="unstyled"- placeholder={t`A warm-blooded, egg-laying vertebrate of the class Aves.`}- value={definition}- onChange={(e) => setDefinition(e.currentTarget.value)}- autosize- minRows={1}- maxRows={6}- autoFocus={autoFocusField === "definition"}- styles={{- input: {- paddingTop: "0.75rem",- paddingBottom: "0.25rem",- lineHeight: 1.2,- },- }}- ref={definitionRef}- />+ {isDuplicate ? (+ <Group gap="xs" pt="sm" wrap="nowrap">+ <Icon c="text-secondary" name="info" />+ <Text+ c="text-secondary"+ fw="normal"+ >{t`This term already exists in the Glossary`}</Text>+ </Group>+ ) : (+ <Textarea+ classNames={{+ input: S.input,+ }}+ variant="unstyled"+ placeholder={t`A warm-blooded, egg-laying vertebrate of the class Aves.`}+ value={definition}+ onChange={(e) => setDefinition(e.currentTarget.value)}+ autosize+ minRows={1}+ maxRows={6}+ autoFocus={autoFocusField === "definition"}+ styles={{+ input: {+ paddingTop: "0.75rem",+ paddingBottom: "0.25rem",+ lineHeight: 1.2,+ },+ }}+ ref={definitionRef}+ />+ )}</Box><Box component="td" valign="top" align="center" px="md" pt="sm" pb={0}><Group justify="flex-end" gap="xs" wrap="nowrap">@@ -99,16 +124,18 @@ export function GlossaryRowEditor({<Icon name="close" /></ActionIcon></Tooltip>- <Tooltip label={t`Save`}>- <ActionIcon- aria-label={t`Save`}- variant={canSave ? "filled" : "subtle"}- disabled={!canSave}- onClick={save}- >- <Icon name="check" />- </ActionIcon>- </Tooltip>+ {!isDuplicate && (+ <Tooltip label={t`Save`}>+ <ActionIcon+ aria-label={t`Save`}+ variant={canSave ? "filled" : "subtle"}+ disabled={!canSave}+ onClick={save}+ >+ <Icon name="check" />+ </ActionIcon>+ </Tooltip>+ )}</Group></Box></>diff --git a/frontend/src/metabase/reference/glossary/GlossaryTable.tsx b/frontend/src/metabase/reference/glossary/GlossaryTable.tsxindex 826b88e686b..6a9571ffc83 100644--- a/frontend/src/metabase/reference/glossary/GlossaryTable.tsx+++ b/frontend/src/metabase/reference/glossary/GlossaryTable.tsx@@ -131,10 +131,12 @@ export function GlossaryTable({rowRenderer={(row) => {// Create rowif ("kind" in row && row.kind === "create") {+ const existingTerms = glossary.map((g) => g.term);return (<tr className={cx(S.row, S.rowEditor)}><GlossaryRowEditoritem={{ term: "", definition: "" }}+ existingTerms={existingTerms}onCancel={() => setIsCreating(false)}onSave={async (term, definition) => {await onCreate(term, definition);@@ -154,6 +156,9 @@ export function GlossaryTable({<GlossaryRowEditoritem={item}autoFocusField={editingField ?? "term"}+ existingTerms={glossary+ .filter((g) => g.id !== item.id)+ .map((g) => g.term)}onCancel={() => setEditingId(null)}onSave={async (newTerm, newDefinition) => {await onEdit(item.id, newTerm, newDefinition);diff --git a/frontend/src/metabase/reference/glossary/GlossaryTable.unit.spec.tsx b/frontend/src/metabase/reference/glossary/GlossaryTable.unit.spec.tsxindex 36a9118d714..34ce866aeb4 100644--- a/frontend/src/metabase/reference/glossary/GlossaryTable.unit.spec.tsx+++ b/frontend/src/metabase/reference/glossary/GlossaryTable.unit.spec.tsx@@ -246,4 +246,128 @@ describe("GlossaryTable", () => {);expect(saveBtn).toBeEnabled();});++ it("shows duplicate warning when term matches existing term", async () => {+ const user = userEvent.setup();+ const existingItem = makeItem({+ id: 1,+ term: "Alpha",+ definition: "First",+ });++ renderWithProviders(+ <GlossaryTable+ glossary={[existingItem]}+ onCreate={jest.fn()}+ onEdit={jest.fn()}+ onDelete={jest.fn()}+ />,+ );++ await user.click(screen.getByRole("button", { name: /new term/i }));++ const termInput = screen.getByPlaceholderText(/bird/i);+ await user.type(termInput, "Alpha");++ // Should show duplicate warning message+ expect(+ screen.getByText(/this term already exists in the glossary/i),+ ).toBeInTheDocument();++ // Save button should be hidden+ expect(+ screen.queryByRole("button", { name: /save/i }),+ ).not.toBeInTheDocument();+ });++ it("detects duplicates case-insensitively", async () => {+ const user = userEvent.setup();+ const existingItem = makeItem({+ id: 1,+ term: "Alpha",+ definition: "First",+ });++ renderWithProviders(+ <GlossaryTable+ glossary={[existingItem]}+ onCreate={jest.fn()}+ onEdit={jest.fn()}+ onDelete={jest.fn()}+ />,+ );++ await user.click(screen.getByRole("button", { name: /new term/i }));++ const termInput = screen.getByPlaceholderText(/bird/i);+ await user.type(termInput, "ALPHA");++ // Should show duplicate warning (case insensitive)+ expect(+ screen.getByText(/this term already exists in the glossary/i),+ ).toBeInTheDocument();+ });++ it("does not show duplicate warning when editing the same term", async () => {+ const user = userEvent.setup();+ const item = makeItem({ id: 1, term: "Alpha", definition: "First" });++ renderWithProviders(+ <GlossaryTable+ glossary={[item]}+ onCreate={jest.fn()}+ onEdit={jest.fn()}+ onDelete={jest.fn()}+ />,+ );++ // Click on the term to edit it+ await user.click(screen.getByText("Alpha"));++ // The term input should have "Alpha" and no duplicate warning+ const termInput = screen.getByPlaceholderText(/bird/i);+ expect(termInput).toHaveValue("Alpha");++ // Should NOT show duplicate warning when editing the same term+ expect(+ screen.queryByText(/this term already exists in the glossary/i),+ ).not.toBeInTheDocument();++ // Save button should be visible+ expect(screen.getByRole("button", { name: /save/i })).toBeInTheDocument();+ });++ it("shows duplicate warning when editing term to match another existing term", async () => {+ const user = userEvent.setup();+ const items: GlossaryItem[] = [+ makeItem({ id: 1, term: "Alpha", definition: "First" }),+ makeItem({ id: 2, term: "Beta", definition: "Second" }),+ ];++ renderWithProviders(+ <GlossaryTable+ glossary={items}+ onCreate={jest.fn()}+ onEdit={jest.fn()}+ onDelete={jest.fn()}+ />,+ );++ // Click on "Alpha" to edit it+ await user.click(screen.getByText("Alpha"));++ const termInput = screen.getByPlaceholderText(/bird/i);+ await user.clear(termInput);+ await user.type(termInput, "Beta");++ // Should show duplicate warning because "Beta" already exists+ expect(+ screen.getByText(/this term already exists in the glossary/i),+ ).toBeInTheDocument();++ // Save button should be hidden+ expect(+ screen.queryByRole("button", { name: /save/i }),+ ).not.toBeInTheDocument();+ });});