fix: map language code in updates
All checks were successful
Deploy / Deploy (push) Successful in 1m24s

This commit is contained in:
CDN 2025-02-03 23:09:11 +08:00
parent 01e69e4f5b
commit 48b65d804a
Signed by: CDN
GPG key ID: 0C656827F9F80080
2 changed files with 23 additions and 9 deletions

View file

@ -216,7 +216,8 @@ const Timeline = () => {
</> </>
) : ( ) : (
<div className="text-center py-20"> <div className="text-center py-20">
<p>{t('updates.notFound.title')}</p> <p className="text-gray-900 dark:text-gray-100">{t('updates.notFound.title')}</p>
<p className="text-gray-600 dark:text-gray-400 mt-2">{t('updates.notFound.description')}</p>
</div> </div>
)} )}
</div> </div>

View file

@ -18,19 +18,28 @@ interface UpdatesIndex {
years: string[]; years: string[];
} }
// Map i18n language codes to data directory paths
const LANGUAGE_CODE_MAP: Record<string, string> = {
'zh-CN': 'zh-Hans',
'zh-TW': 'zh-Hant',
'en': 'en',
'en-US': 'en'
};
// 获取更新年份列表 // 获取更新年份列表
const getUpdateYears = async (language: string): Promise<string[]> => { const getUpdateYears = async (language: string): Promise<string[]> => {
try { 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) { if (!response.ok) {
throw new Error(`Failed to load updates index, status: ${response.status}`); throw new Error(`Failed to load updates index, status: ${response.status}`);
} }
const index: UpdatesIndex = await response.json(); const index: UpdatesIndex = await response.json();
return index.years || []; return index.years || [];
} catch (error) { } 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 []; return [];
} }
}; };
@ -38,16 +47,17 @@ const getUpdateYears = async (language: string): Promise<string[]> => {
// 动态获取指定的年度更新 // 动态获取指定的年度更新
const getYearUpdates = async (yearFile: string, language: string): Promise<YearUpdates | null> => { const getYearUpdates = async (yearFile: string, language: string): Promise<YearUpdates | null> => {
try { 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) { if (!response.ok) {
throw new Error(`Failed to load updates from ${yearFile}, status: ${response.status}`); throw new Error(`Failed to load updates from ${yearFile}, status: ${response.status}`);
} }
const data: YearUpdates = await response.json(); const data: YearUpdates = await response.json();
return data; return data;
} catch (error) { } 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; return null;
} }
}; };
@ -85,6 +95,9 @@ export const useUpdates = (page: number = 1, pageSize: number = 10) => {
const { data: allUpdates = [], isLoading, error } = useQuery({ const { data: allUpdates = [], isLoading, error } = useQuery({
queryKey: ['updates', i18n.language], queryKey: ['updates', i18n.language],
queryFn: () => getAllUpdates(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[] = []) => { const getPaginatedUpdates = (selectedTags: string[] = []) => {