Skip to content

Commit

Permalink
feat: New default item URN suffix (#3140)
Browse files Browse the repository at this point in the history
  • Loading branch information
LautaroPetaccio authored Jul 15, 2024
1 parent bcd6063 commit 6d821af
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -207,7 +208,7 @@ export default class CreateAndEditMultipleItemsModal extends React.PureComponent
decodedCollectionUrn.thirdPartyLinkedCollectionName,
decodedCollectionUrn.linkedCollectionNetwork,
decodedCollectionUrn.linkedCollectionAddress,
thirdPartyTokenId ?? uuid.v4()
getDefaultThirdPartyItemUrnSuffix(loadedFile.wearable.name)
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ import {
import { EngineType, getItemData, getModelData } from 'lib/getModelData'
import { getExtension, toMB } from 'lib/file'
import {
getDefaultThirdPartyItemUrnSuffix,
buildThirdPartyURN,
buildThirdPartyV2URN,
DecodedURN,
Expand Down Expand Up @@ -274,7 +275,7 @@ export default class CreateSingleItemModal extends React.PureComponent<Props, St
decodedCollectionUrn.thirdPartyLinkedCollectionName,
decodedCollectionUrn.linkedCollectionNetwork,
decodedCollectionUrn.linkedCollectionContractAddress,
uuid.v4()
getDefaultThirdPartyItemUrnSuffix(name)
)
}

Expand Down
17 changes: 16 additions & 1 deletion src/lib/urn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
DecodedURN,
isThirdPartyV2CollectionDecodedUrn,
isThirdPartyCollectionDecodedUrn,
decodedCollectionsUrnAreEqual
decodedCollectionsUrnAreEqual,
getDefaultThirdPartyItemUrnSuffix
} from './urn'

jest.mock('decentraland-dapps/dist/lib/eth')
Expand Down Expand Up @@ -566,3 +567,17 @@ describe('when checking if two decoded collection URNs are equal', () => {
})
})
})

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}$/)
})
})
})
6 changes: 6 additions & 0 deletions src/lib/urn.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getURNProtocol, Network } from '@dcl/schemas'
import slug from 'slug'
import { getChainIdByNetwork } from 'decentraland-dapps/dist/lib/eth'

/**
Expand Down Expand Up @@ -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}`
}

0 comments on commit 6d821af

Please sign in to comment.