Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: assign unique names to new data sets #1706

Merged
merged 1 commit into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
} 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 @@
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 @@

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 @@
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
Loading