Skip to content

Commit

Permalink
fix: fix critical ui crash in 1.8.8 for chat
Browse files Browse the repository at this point in the history
  • Loading branch information
zardoy committed Mar 18, 2024
1 parent 826fed5 commit 4d4732d
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/react/MessageFormatted.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import { ComponentProps } from 'react'
import { render } from '@xmcl/text-component'
import { noCase } from 'change-case'
import mojangson from 'mojangson'
import { MessageFormatPart } from '../botUtils'
import { openURL } from '../menus/components/common'
import { chatInputValueGlobal } from './ChatContainer'

const hoverItemToText = (hoverEvent: MessageFormatPart['hoverEvent']) => {
if (!hoverEvent) return undefined
const contents = hoverEvent['contents'] ?? hoverEvent.value
if (typeof contents === 'string') return contents
// if (hoverEvent.action === 'show_text') {
// return contents
// }
if (hoverEvent.action === 'show_item') {
return contents.id
}
if (hoverEvent.action === 'show_entity') {
let str = noCase(contents.type.replace('minecraft:', ''))
if (contents.name) str += `: ${contents.name.text}`
return str
try {
if (!hoverEvent) return undefined
const contents = hoverEvent['contents'] ?? hoverEvent.value
if (typeof contents.text === 'string' && contents.text.startsWith('{')) {
Object.assign(contents, mojangson.simplify(mojangson.parse(contents.text)))
}
if (typeof contents === 'string') return contents
// if (hoverEvent.action === 'show_text') {
// return contents
// }
if (hoverEvent.action === 'show_item') {
return contents.id
}
if (hoverEvent.action === 'show_entity') {
let str = noCase(contents.type.replace('minecraft:', ''))
if (contents.name) str += `: ${contents.name.text}`
return str
}
} catch (err) {
// todo report critical error
console.error('Failed to parse message hover', err)
return undefined
}
}

Expand All @@ -45,7 +55,7 @@ const clickEventToProps = (clickEvent: MessageFormatPart['clickEvent']) => {
if (clickEvent.action === 'copy_to_clipboard') {
return {
onClick () {
navigator.clipboard.writeText(clickEvent.value)
void navigator.clipboard.writeText(clickEvent.value)
}
}
}
Expand Down

0 comments on commit 4d4732d

Please sign in to comment.