Skip to content

Commit

Permalink
fix: assign unique names to new data sets
Browse files Browse the repository at this point in the history
  • Loading branch information
kswenson committed Dec 24, 2024
1 parent 02af839 commit 256ed60
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 7 additions & 3 deletions v3/src/components/case-table/case-table-tool-shelf-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<typeof SharedDataSet>(kSharedDataSetType)
const datasets = manager?.getSharedModelsByType<typeof SharedDataSet>(kSharedDataSetType) ?? []
const datasetNames = datasets.map(data => data.dataSet.name)

Check warning on line 36 in v3/src/components/case-table/case-table-tool-shelf-button.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-table/case-table-tool-shelf-button.tsx#L36

Added line #L36 was not covered by tests
const { isOpen, onOpen, onClose } = useDisclosure()
const [modalOpen, setModalOpen] = useState(false)
const [dataSetIdToDeleteId, setDataSetIdToDelete] = useState("")
Expand All @@ -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 })

Check warning on line 47 in v3/src/components/case-table/case-table-tool-shelf-button.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-table/case-table-tool-shelf-button.tsx#L45-L47

Added lines #L45 - L47 were not covered by tests
ds.addAttribute({ name: t("DG.AppController.createDataSet.initialAttribute") })
const options: INewTileOptions = { animateCreation: true }
const tile = createDefaultTileOfType(kCaseTableTileType, options)
Expand All @@ -65,7 +69,7 @@ export const CaseTableToolShelfMenuList = observer(function CaseTableToolShelfMe
return (
<>
<MenuList>
{datasets?.map((dataset) => {
{datasets.map((dataset) => {

Check warning on line 72 in v3/src/components/case-table/case-table-tool-shelf-button.tsx

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-table/case-table-tool-shelf-button.tsx#L72

Added line #L72 was not covered by tests
// case table title reflects DataSet title
const tileTitle = dataset.dataSet.title
return (
Expand Down
4 changes: 2 additions & 2 deletions v3/src/utilities/js-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 256ed60

Please sign in to comment.