Skip to content

Commit

Permalink
Improve fallbacks when item data does not exist for a language (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
karashiiro authored Oct 7, 2024
1 parent 60d22d2 commit 29232bb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
29 changes: 26 additions & 3 deletions data/game/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,34 @@ export function getItemKind(id: number, lang: Language): ItemKind | undefined {
return (itemKinds[lang] ?? itemKinds['en'])[id];
}

export function getItem(id: number, lang: Language): Item | undefined {
return (items[lang] ?? items['en'])[id];
export function getItem(id: number, lang: Language): Item {
const itemLang = getItemCore(id, lang);
if (itemLang) {
return itemLang;
}

console.warn(`Missing data for item ${id} in language ${lang}, falling back to English`);

const itemEn = getItemCore(id, 'en');
if (itemEn) {
return itemEn;
}

console.warn(`Missing data for item ${id} in English, falling back to placeholder data`);

return getFallbackItem(id);
}

function getItemCore(id: number, lang: Language): Item | undefined {
const langItems = items[lang];
if (!langItems) {
return undefined;
}

return langItems[id];
}

export function getFallbackItem(id: number): Item {
function getFallbackItem(id: number): Item {
return {
id: id,
name: 'Failed to retrieve',
Expand Down
4 changes: 2 additions & 2 deletions pages/account/alerts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, { useState } from 'react';
import { useSWRConfig } from 'swr';
import AccountLayout from '../../components/AccountLayout/AccountLayout';
import GameIcon from '../../components/GameIcon/GameIcon';
import { getFallbackItem, getItem, getItemKind, getItemSearchCategory } from '../../data/game';
import { getItem, getItemKind, getItemSearchCategory } from '../../data/game';
import useAlerts from '../../hooks/useAlerts';
import useSettings from '../../hooks/useSettings';
import useWorlds from '../../hooks/useWorlds';
Expand Down Expand Up @@ -224,7 +224,7 @@ const Alerts: NextPage = () => {
</h1>
<div className="account-panel">
{[...alertGroups.entries()].map(([itemId, alertGroup]) => {
const item = getItem(itemId, lang) ?? getFallbackItem(itemId);
const item = getItem(itemId, lang);
return (
<div key={itemId}>
<div className={styles.alertPanel}>
Expand Down
9 changes: 2 additions & 7 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ import { UserList } from '../types/universalis/user';
import HomeUserList from '../components/Home/HomeUserList/HomeUserList';
import { useState } from 'react';
import { Trans } from '@lingui/macro';
import { getFallbackItem, getItem } from '../data/game';
import { getItem } from '../data/game';
import useSettings from '../hooks/useSettings';
import { getServers } from '../service/servers';
import { TaxRates } from '../types/universalis/TaxRates';
import HomeLeastRecentlyUpdated from '../components/Home/HomeLeastRecentlyUpdated/HomeLeastRecentlyUpdated';
import { FETCH_OPTIONS, getBaseUrl } from '../service/universalis';
import useSWR from 'swr';
import useSWRImmutable from 'swr/immutable';
import { useSession } from 'next-auth/react';
import useDataCenters from '../hooks/useDataCenters';
import ErrorBoundary from '../components/ErrorBoundary/ErrorBoundary';
import HomeGuides from '../components/Home/HomeGuides/HomeGuides';

function sum(arr: number[], start: number, end: number) {
Expand Down Expand Up @@ -187,9 +184,7 @@ const Home: NextPage = () => {
<h4>
<Trans>Recent Updates</Trans>
</h4>
<RecentUpdatesPanel
items={(recent ?? []).map((itemId) => getItem(itemId, lang) ?? getFallbackItem(itemId))}
/>
<RecentUpdatesPanel items={(recent ?? []).map((itemId) => getItem(itemId, lang))} />
<TaxRatesPanel data={taxes ?? zeroTaxRates()} world={world} />
<WorldUploadCountsPanel data={worldUploads ?? []} world={world} />
<UploadCountPanel
Expand Down

0 comments on commit 29232bb

Please sign in to comment.