diff --git a/v3/src/components/case-table/case-table-tool-shelf-button.tsx b/v3/src/components/case-table/case-table-tool-shelf-button.tsx index 0d70e960a..67f206762 100644 --- a/v3/src/components/case-table/case-table-tool-shelf-button.tsx +++ b/v3/src/components/case-table/case-table-tool-shelf-button.tsx @@ -13,6 +13,7 @@ import { } from "../../models/data/data-set-notifications" import { kSharedDataSetType, SharedDataSet } from "../../models/shared/shared-data-set" import { getFormulaManager, getSharedModelManager } from "../../models/tiles/tile-environment" +import { uniqueName } from "../../utilities/js-utils" import { t } from "../../utilities/translation/translate" import { createOrShowTableOrCardForDataset, createTableOrCardForDataset @@ -31,7 +32,8 @@ export const CaseTableToolShelfMenuList = observer(function CaseTableToolShelfMe const document = appState.document const content = document.content const manager = getSharedModelManager(document) - const datasets = manager?.getSharedModelsByType(kSharedDataSetType) + const datasets = manager?.getSharedModelsByType(kSharedDataSetType) ?? [] + const datasetNames = datasets.map(data => data.dataSet.name) const { isOpen, onOpen, onClose } = useDisclosure() const [modalOpen, setModalOpen] = useState(false) const [dataSetIdToDeleteId, setDataSetIdToDelete] = useState("") @@ -40,7 +42,9 @@ export const CaseTableToolShelfMenuList = observer(function CaseTableToolShelfMe const handleCreateNewCaseTable = () => { document.applyModelChange(() => { - const ds = DataSet.create({ name: t("DG.AppController.createDataSet.name")}) + const baseName = t("DG.AppController.createDataSet.name") + const newName = uniqueName(baseName, name => !datasetNames.includes(name), " ") + const ds = DataSet.create({ name: newName }) ds.addAttribute({ name: t("DG.AppController.createDataSet.initialAttribute") }) const options: INewTileOptions = { animateCreation: true } const tile = createDefaultTileOfType(kCaseTableTileType, options) @@ -65,7 +69,7 @@ export const CaseTableToolShelfMenuList = observer(function CaseTableToolShelfMe return ( <> - {datasets?.map((dataset) => { + {datasets.map((dataset) => { // case table title reflects DataSet title const tileTitle = dataset.dataSet.title return ( diff --git a/v3/src/utilities/js-utils.ts b/v3/src/utilities/js-utils.ts index b6aaf2f27..0ec067e5f 100644 --- a/v3/src/utilities/js-utils.ts +++ b/v3/src/utilities/js-utils.ts @@ -84,10 +84,10 @@ export function uniqueId(idLength = 16): string { * * returns a unique name from a given base name, adding a numeric suffix if necessary */ -export function uniqueName(base: string, isValid: (name: string) => boolean) { +export function uniqueName(base: string, isValid: (name: string) => boolean, space = "") { if (isValid(base)) return base let name: string - for (let i = 2; !isValid(name = `${base}${i}`); ++i) { + for (let i = 2; !isValid(name = `${base}${space}${i}`); ++i) { // nothing to do } return name