diff --git a/src/app/components/ItemTooltip.tsx b/src/app/components/ItemTooltip.tsx index f64df6a3..926aed45 100644 --- a/src/app/components/ItemTooltip.tsx +++ b/src/app/components/ItemTooltip.tsx @@ -1,10 +1,9 @@ import type { ItemStack } from 'deepslate/core' import { AttributeModifierOperation, Enchantment, Identifier, MobEffectInstance, Potion } from 'deepslate/core' import { NbtList, NbtType } from 'deepslate/nbt' -import { useMemo } from 'preact/hooks' import { useVersion } from '../contexts/Version.jsx' import { useAsync } from '../hooks/useAsync.js' -import { getTranslation } from '../services/Resources.js' +import { getLanguage, getTranslation } from '../services/Resources.js' import { TextComponent } from './TextComponent.jsx' interface Props { @@ -14,19 +13,33 @@ interface Props { export function ItemTooltip({ item, advanced }: Props) { const { version } = useVersion() + const { value: language } = useAsync(() => getLanguage(version), [version]) + const isPotion = item.is('potion') || item.is('splash_potion') || item.is('lingering_potion') - const descriptionId = useMemo(() => { - const d = `${item.id.namespace}.${item.id.path}` - if (isPotion) { - return `${d}.effect.${Potion.fromNbt(item).name}` + let displayName = item.tag.getCompound('display').getString('Name') + let name: string | undefined + if (displayName) { + try { + name = JSON.parse(displayName) + } catch (e) { + displayName = '' } - return d - }, [item]) - const { value: translatedName } = useAsync(() => { - return getTranslation(version, `item.${descriptionId}`) ?? getTranslation(version, `block.${descriptionId}`) - }, [version, descriptionId]) - const displayName = item.tag.getCompound('display').getString('Name') - const name = displayName ? JSON.parse(displayName) : (translatedName ?? fakeTranslation(item.id.path)) + } + if (name === undefined) { + if (language) { + let descriptionId = `${item.id.namespace}.${item.id.path}` + if (isPotion) { + descriptionId = `${descriptionId}.effect.${Potion.fromNbt(item).name}` + } + name = getTranslation(language, `item.${descriptionId}`) + name ??= getTranslation(language, `block.${descriptionId}`) + } + name ??= item.id.path + .replace(/[_\/]/g, ' ') + .split(' ') + .map(word => word.charAt(0).toUpperCase() + word.slice(1)) + .join(' ') + } const durability = item.getItem().durability const enchantments = (item.is('enchanted_book') ? item.tag.getList('StoredEnchantments', NbtType.Compound) : item.tag.getList('Enchantments', NbtType.Compound)) ?? NbtList.create() @@ -95,14 +108,6 @@ export function ItemTooltip({ item, advanced }: Props) { } -function fakeTranslation(str: string) { - return str - .replace(/[_\/]/g, ' ') - .split(' ') - .map(word => word.charAt(0).toUpperCase() + word.slice(1)) - .join(' ') -} - const TooltipMasks = { enchantments: 1, modifiers: 2, diff --git a/src/app/services/Resources.ts b/src/app/services/Resources.ts index 8caf0c64..a94ef67a 100644 --- a/src/app/services/Resources.ts +++ b/src/app/services/Resources.ts @@ -163,7 +163,9 @@ export class ResourceWrapper implements Resources { } } -const Languages: Record | Promise>> = {} +export type Language = Record + +const Languages: Record> = {} export async function getLanguage(version: VersionId) { if (!Languages[version]) { @@ -181,10 +183,9 @@ export async function getLanguage(version: VersionId) { return Languages[version] } -export async function getTranslation(version: VersionId, key: string, params?: string[]) { - const lang = await getLanguage(version) +export function getTranslation(lang: Language, key: string, params?: string[]) { const str = lang[key] - if (!str) return null + if (!str) return undefined return replaceTranslation(str, params) }