From 48b65d804a9f108efde44d1af29f7614e7ba9267 Mon Sep 17 00:00:00 2001 From: cdn0x12 Date: Mon, 3 Feb 2025 23:09:11 +0800 Subject: [PATCH] fix: map language code in updates --- src/components/Timeline.tsx | 3 ++- src/utils/updates.ts | 29 +++++++++++++++++++++-------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/src/components/Timeline.tsx b/src/components/Timeline.tsx index 582f08c..dbf51fa 100644 --- a/src/components/Timeline.tsx +++ b/src/components/Timeline.tsx @@ -216,7 +216,8 @@ const Timeline = () => { ) : (
-

{t('updates.notFound.title')}

+

{t('updates.notFound.title')}

+

{t('updates.notFound.description')}

)} diff --git a/src/utils/updates.ts b/src/utils/updates.ts index e65cbcf..0a115f2 100644 --- a/src/utils/updates.ts +++ b/src/utils/updates.ts @@ -18,19 +18,28 @@ interface UpdatesIndex { years: string[]; } +// Map i18n language codes to data directory paths +const LANGUAGE_CODE_MAP: Record = { + 'zh-CN': 'zh-Hans', + 'zh-TW': 'zh-Hant', + 'en': 'en', + 'en-US': 'en' +}; + // 获取更新年份列表 const getUpdateYears = async (language: string): Promise => { try { - const response = await fetch(`/data/${language}/updates.json`); + // Map the language code to the correct data directory path + const dataLanguage = LANGUAGE_CODE_MAP[language] || language; + + const response = await fetch(`/data/${dataLanguage}/updates.json`); if (!response.ok) { throw new Error(`Failed to load updates index, status: ${response.status}`); } const index: UpdatesIndex = await response.json(); return index.years || []; } catch (error) { - if (process.env.NODE_ENV === 'development') { - console.warn(`Failed to load updates index:`, error); - } + console.warn(`Failed to load updates index:`, error); return []; } }; @@ -38,16 +47,17 @@ const getUpdateYears = async (language: string): Promise => { // 动态获取指定的年度更新 const getYearUpdates = async (yearFile: string, language: string): Promise => { try { - const response = await fetch(`/data/${language}/updates/${yearFile}`); + // Map the language code to the correct data directory path + const dataLanguage = LANGUAGE_CODE_MAP[language] || language; + + const response = await fetch(`/data/${dataLanguage}/updates/${yearFile}`); if (!response.ok) { throw new Error(`Failed to load updates from ${yearFile}, status: ${response.status}`); } const data: YearUpdates = await response.json(); return data; } catch (error) { - if (process.env.NODE_ENV === 'development') { - console.warn(`Failed to load updates from ${yearFile}:`, error); - } + console.warn(`Failed to load updates from ${yearFile}:`, error); return null; } }; @@ -85,6 +95,9 @@ export const useUpdates = (page: number = 1, pageSize: number = 10) => { const { data: allUpdates = [], isLoading, error } = useQuery({ queryKey: ['updates', i18n.language], queryFn: () => getAllUpdates(i18n.language), + staleTime: 5 * 60 * 1000, // Cache for 5 minutes + retry: 3, // Retry failed requests up to 3 times + retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 10000), // Exponential backoff }); const getPaginatedUpdates = (selectedTags: string[] = []) => {