From 6d821af680d87754622f74649fcfc1ff327a7346 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio <1120791+LautaroPetaccio@users.noreply.github.com> Date: Mon, 15 Jul 2024 16:08:07 -0300 Subject: [PATCH] feat: New default item URN suffix (#3140) --- .../CreateAndEditMultipleItemsModal.tsx | 3 ++- .../CreateSingleItemModal.tsx | 3 ++- src/lib/urn.spec.ts | 17 ++++++++++++++++- src/lib/urn.ts | 6 ++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/components/Modals/CreateAndEditMultipleItemsModal/CreateAndEditMultipleItemsModal.tsx b/src/components/Modals/CreateAndEditMultipleItemsModal/CreateAndEditMultipleItemsModal.tsx index 38c0ff844..0797f4901 100644 --- a/src/components/Modals/CreateAndEditMultipleItemsModal/CreateAndEditMultipleItemsModal.tsx +++ b/src/components/Modals/CreateAndEditMultipleItemsModal/CreateAndEditMultipleItemsModal.tsx @@ -24,6 +24,7 @@ import { config } from 'config' import { EngineType, getModelData } from 'lib/getModelData' import { getExtension, toMB } from 'lib/file' import { + getDefaultThirdPartyItemUrnSuffix, buildThirdPartyURN, buildThirdPartyV2URN, decodedCollectionsUrnAreEqual, @@ -207,7 +208,7 @@ export default class CreateAndEditMultipleItemsModal extends React.PureComponent decodedCollectionUrn.thirdPartyLinkedCollectionName, decodedCollectionUrn.linkedCollectionNetwork, decodedCollectionUrn.linkedCollectionAddress, - thirdPartyTokenId ?? uuid.v4() + getDefaultThirdPartyItemUrnSuffix(loadedFile.wearable.name) ) ) } diff --git a/src/components/Modals/CreateSingleItemModal/CreateSingleItemModal.tsx b/src/components/Modals/CreateSingleItemModal/CreateSingleItemModal.tsx index d083765c0..7ee85bd2f 100644 --- a/src/components/Modals/CreateSingleItemModal/CreateSingleItemModal.tsx +++ b/src/components/Modals/CreateSingleItemModal/CreateSingleItemModal.tsx @@ -70,6 +70,7 @@ import { import { EngineType, getItemData, getModelData } from 'lib/getModelData' import { getExtension, toMB } from 'lib/file' import { + getDefaultThirdPartyItemUrnSuffix, buildThirdPartyURN, buildThirdPartyV2URN, DecodedURN, @@ -274,7 +275,7 @@ export default class CreateSingleItemModal extends React.PureComponent { }) }) }) + +describe('when getting a default third party item URN suffix', () => { + describe('and the item name is empty', () => { + it('should return a string with the "default" word plus 4 random hex characters', () => { + expect(getDefaultThirdPartyItemUrnSuffix('')).toMatch(/^default-[0-9a-f]{4}$/) + }) + }) + + describe('and the item name is not empty', () => { + it('should return a string with the sluggled item name plus 4 random hex characters', () => { + expect(getDefaultThirdPartyItemUrnSuffix('a wonderful item: name')).toMatch(/^a-wonderful-item-name-[0-9a-f]{4}$/) + }) + }) +}) diff --git a/src/lib/urn.ts b/src/lib/urn.ts index bd109da06..f2667723e 100644 --- a/src/lib/urn.ts +++ b/src/lib/urn.ts @@ -1,4 +1,5 @@ import { getURNProtocol, Network } from '@dcl/schemas' +import slug from 'slug' import { getChainIdByNetwork } from 'decentraland-dapps/dist/lib/eth' /** @@ -277,3 +278,8 @@ export const isThirdPartyV2CollectionDecodedUrn = ( !!urn.thirdPartyLinkedCollectionName && !!urn.linkedCollectionNetwork && !!urn.linkedCollectionContractAddress + +export const getDefaultThirdPartyItemUrnSuffix = (itemName: string) => { + const randHex = Array.from({ length: 4 }, () => Math.floor(Math.random() * 16).toString(16)).join('') + return `${slug(itemName.length > 0 ? itemName : 'default')}-${randHex}` +}