Skip to content

Commit

Permalink
fix: grouping not updating token metadata correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
dayvar14 committed Oct 16, 2024
1 parent cbf3f2c commit 9d45251
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/components/InitiativeList/InitiativeSubListSubItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ const InitiativeSubListSubItem: React.FC<{
if (newNameCopy !== tokenCopy.name) {
if (!newNameCopy) newNameCopy = token.name
tokenCopy.plainTextName = newNameCopy
Token.updateToken(tokenCopy)
Token.updateTokens([tokenCopy])
setNewName(newNameCopy)
}
setIsEditingName(false)
Expand Down
2 changes: 1 addition & 1 deletion src/components/OptionsList/GroupOptionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const GroupOptionsList: React.FC<{
const MAX_TURN_COUNT = 5

useEffect(() => {
setSplitCount(tokens.length >= 5 ? 5 : 1)
setSplitCount(tokens.length > 5 ? 5 : 1)
}, [subGroup])

const handleNumberChange = (
Expand Down
5 changes: 5 additions & 0 deletions src/obr/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,9 @@ export module Group {
groupMetadata.groupsByType[subGroup.groupType].subGroupsById[subGroupId] =
splitSubgroupsById[subGroupId]
})

await updateGroupMetadata(groupMetadata)
await Token.updateTokens(Object.values(subGroup.tokensById))
}
}

Expand Down Expand Up @@ -319,6 +321,7 @@ function proximityBasedSplittingAlgorithm(
}

group.forEach(token => {
token.tokenMetadata.subGroupId = subGroupId
newSubGroup.tokensById[token.id] = token
})

Expand Down Expand Up @@ -484,6 +487,7 @@ function hierarchicalClusteringSplittingAlgorithm(
}

cluster.tokens.forEach(token => {
token.tokenMetadata.subGroupId = subGroupId
newSubGroup.tokensById[token.id] = token
})

Expand Down Expand Up @@ -545,6 +549,7 @@ function randomSplittingAlgorithm(
}

group.forEach(token => {
token.tokenMetadata.subGroupId = subGroupId
newSubGroup.tokensById[token.id] = token
})

Expand Down
22 changes: 21 additions & 1 deletion src/obr/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,27 @@ export namespace Token {
})
}

export const updateToken = (token: Token): void => {
export const updateTokens = (token: Token[]): void => {
const tokenMap = new Map<string, Token>()
for (const t of token) {
tokenMap.set(t.id, t)
}

OBR.scene.items.updateItems(
token.map(token => token.id),
items => {
if (items.length === 0) return
for (const item of items) {
const token = tokenMap.get(item.id)
if (!token) return
;(item as Image).text.plainText = token.plainTextName
;(item as Image).metadata[TOKEN_METADATA_ID] = token.tokenMetadata
}
},
)
}

export const updateTokenMetadata = (token: Token): void => {
OBR.scene.items.updateItems([token.id], items => {
if (items.length === 0) return
;(items[0] as Image).text.plainText = token.plainTextName
Expand Down

0 comments on commit 9d45251

Please sign in to comment.