feat: cache update index and contents
This commit is contained in:
parent
2daa1b9879
commit
be80282695
6 changed files with 217 additions and 158 deletions
|
@ -11,6 +11,7 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"@ant-design/icons": "^5.6.0",
|
||||
"@tanstack/react-query": "^5.66.0",
|
||||
"antd": "^5.23.3",
|
||||
"clsx": "^2.1.1",
|
||||
"i18next": "^23.16.8",
|
||||
|
|
18
pnpm-lock.yaml
generated
18
pnpm-lock.yaml
generated
|
@ -11,6 +11,9 @@ importers:
|
|||
'@ant-design/icons':
|
||||
specifier: ^5.6.0
|
||||
version: 5.6.0(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||
'@tanstack/react-query':
|
||||
specifier: ^5.66.0
|
||||
version: 5.66.0(react@18.3.1)
|
||||
antd:
|
||||
specifier: ^5.23.3
|
||||
version: 5.23.3(react-dom@18.3.1(react@18.3.1))(react@18.3.1)
|
||||
|
@ -772,6 +775,14 @@ packages:
|
|||
peerDependencies:
|
||||
tailwindcss: '>=3.0.0 || insiders || >=4.0.0-alpha.20 || >=4.0.0-beta.1'
|
||||
|
||||
'@tanstack/query-core@5.66.0':
|
||||
resolution: {integrity: sha512-J+JeBtthiKxrpzUu7rfIPDzhscXF2p5zE/hVdrqkACBP8Yu0M96mwJ5m/8cPPYQE9aRNvXztXHlNwIh4FEeMZw==}
|
||||
|
||||
'@tanstack/react-query@5.66.0':
|
||||
resolution: {integrity: sha512-z3sYixFQJe8hndFnXgWu7C79ctL+pI0KAelYyW+khaNJ1m22lWrhJU2QrsTcRKMuVPtoZvfBYrTStIdKo+x0Xw==}
|
||||
peerDependencies:
|
||||
react: ^18 || ^19
|
||||
|
||||
'@types/babel__core@7.20.5':
|
||||
resolution: {integrity: sha512-qoQprZvz5wQFJwMDqeseRXWv3rqMvhgpbXFfVyWhbx9X47POIA6i/+dXefEmZKoAgOaTdaIgNSMqMIU61yRyzA==}
|
||||
|
||||
|
@ -2589,6 +2600,13 @@ snapshots:
|
|||
postcss-selector-parser: 6.0.10
|
||||
tailwindcss: 3.4.17
|
||||
|
||||
'@tanstack/query-core@5.66.0': {}
|
||||
|
||||
'@tanstack/react-query@5.66.0(react@18.3.1)':
|
||||
dependencies:
|
||||
'@tanstack/query-core': 5.66.0
|
||||
react: 18.3.1
|
||||
|
||||
'@types/babel__core@7.20.5':
|
||||
dependencies:
|
||||
'@babel/parser': 7.26.7
|
||||
|
|
14
src/App.tsx
14
src/App.tsx
|
@ -10,11 +10,25 @@ import UpdateDetailPage from './pages/UpdateDetailPage';
|
|||
import ContributorsPage from './pages/ContributorsPage';
|
||||
import AboutPage from './pages/AboutPage';
|
||||
import iconMap from './utils/iconMap';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
|
||||
function App() {
|
||||
const { t } = useTranslation();
|
||||
const queryClient = useQueryClient();
|
||||
const socialLinks = t('social.links', { returnObjects: true });
|
||||
|
||||
// 在窗口关闭时清除缓存
|
||||
React.useEffect(() => {
|
||||
const handleBeforeUnload = () => {
|
||||
queryClient.clear();
|
||||
};
|
||||
|
||||
window.addEventListener('beforeunload', handleBeforeUnload);
|
||||
return () => {
|
||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||
};
|
||||
}, [queryClient]);
|
||||
|
||||
return (
|
||||
<ThemeProvider>
|
||||
<Router>
|
||||
|
|
|
@ -61,52 +61,31 @@ const Pagination: React.FC<PaginationProps> = ({
|
|||
const Timeline = () => {
|
||||
const { t, i18n } = useTranslation();
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [updates, setUpdates] = useState<Update[]>([]);
|
||||
const [pagination, setPagination] = useState({
|
||||
currentPage: 1,
|
||||
totalPages: 1,
|
||||
totalItems: 0,
|
||||
hasNextPage: false,
|
||||
hasPrevPage: false
|
||||
});
|
||||
|
||||
// 获取当前页码和标签筛选
|
||||
const currentPage = Math.max(1, parseInt(searchParams.get('page') || '1', 10));
|
||||
const selectedTags = searchParams.get('tags')?.split(',').filter(Boolean) || [];
|
||||
const { getPaginatedUpdates } = useUpdates(currentPage);
|
||||
|
||||
// 使用 React Query hook
|
||||
const { getPaginatedUpdates, isLoading, error } = useUpdates(currentPage);
|
||||
|
||||
// 获取分页数据
|
||||
const { updates, pagination } = getPaginatedUpdates(selectedTags);
|
||||
|
||||
// 获取所有可用的标签
|
||||
const availableTags = Object.keys(t('updates.tags', { returnObjects: true }));
|
||||
|
||||
// 当页码超出范围时自动调整
|
||||
useEffect(() => {
|
||||
const fetchUpdates = async () => {
|
||||
try {
|
||||
const result = await getPaginatedUpdates(selectedTags);
|
||||
setUpdates(result.updates);
|
||||
setPagination(result.pagination);
|
||||
|
||||
// 如果当前页码超出范围,自动调整到最后一页
|
||||
if (currentPage > result.pagination.totalPages) {
|
||||
setSearchParams((prev) => {
|
||||
prev.set('page', result.pagination.totalPages.toString());
|
||||
return prev;
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Failed to fetch updates:', error);
|
||||
setUpdates([]);
|
||||
setPagination({
|
||||
currentPage: 1,
|
||||
totalPages: 1,
|
||||
totalItems: 0,
|
||||
hasNextPage: false,
|
||||
hasPrevPage: false
|
||||
});
|
||||
}
|
||||
};
|
||||
fetchUpdates();
|
||||
}, [currentPage, selectedTags, i18n.language, getPaginatedUpdates, setSearchParams]);
|
||||
if (pagination.totalPages > 0 && currentPage > pagination.totalPages) {
|
||||
setSearchParams((prev) => {
|
||||
prev.set('page', pagination.totalPages.toString());
|
||||
return prev;
|
||||
});
|
||||
}
|
||||
}, [pagination.totalPages, currentPage, setSearchParams]);
|
||||
|
||||
// 处理页码变化
|
||||
const handlePageChange = (page: number) => {
|
||||
setSearchParams((prev) => {
|
||||
prev.set('page', page.toString());
|
||||
|
@ -114,7 +93,8 @@ const Timeline = () => {
|
|||
});
|
||||
};
|
||||
|
||||
const handleTagsChange = (tags: string[]) => {
|
||||
// 处理标签变化
|
||||
const handleTagChange = (tags: string[]) => {
|
||||
setSearchParams((prev) => {
|
||||
if (tags.length > 0) {
|
||||
prev.set('tags', tags.join(','));
|
||||
|
@ -126,6 +106,14 @@ const Timeline = () => {
|
|||
});
|
||||
};
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="text-center py-20">
|
||||
<p className="text-red-500">{t('updates.error')}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container mx-auto px-8 md:px-20 lg:px-32 xl:px-48 2xl:px-64">
|
||||
<div className="max-w-5xl">
|
||||
|
@ -137,7 +125,7 @@ const Timeline = () => {
|
|||
<TagFilter
|
||||
availableTags={availableTags}
|
||||
selectedTags={selectedTags}
|
||||
onTagsChange={handleTagsChange}
|
||||
onChange={handleTagChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
@ -145,57 +133,69 @@ const Timeline = () => {
|
|||
{/* Timeline line */}
|
||||
<div className="absolute left-6 top-0 h-full w-0.5 bg-blue-200 dark:bg-blue-900"></div>
|
||||
|
||||
<div className="space-y-8">
|
||||
{updates.map((update) => (
|
||||
<div key={update.id} className="relative flex items-start group">
|
||||
{/* Timeline dot */}
|
||||
<div className="absolute left-6 transform -translate-x-1/2 w-2.5 h-2.5 bg-blue-600 dark:bg-blue-500 rounded-full mt-2 group-hover:scale-125 transition-transform"></div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="ml-16 w-full">
|
||||
<span className="text-sm text-blue-600 dark:text-blue-400 font-medium block mb-1">{update.date}</span>
|
||||
<Link
|
||||
to={getUpdateUrl(update)}
|
||||
target={update.link ? "_blank" : undefined}
|
||||
className="group/link"
|
||||
>
|
||||
<h3 className="text-xl font-semibold text-gray-900 dark:text-white mb-2 hover:text-blue-600 dark:hover:text-blue-400 inline-flex items-center">
|
||||
{update.title}
|
||||
{update.link && (
|
||||
<ExternalLink className="w-4 h-4 ml-1 opacity-0 group-hover/link:opacity-100 transition-opacity" />
|
||||
)}
|
||||
</h3>
|
||||
</Link>
|
||||
<p className="text-gray-600 dark:text-gray-300">{update.summary}</p>
|
||||
<div className="mt-4 flex items-center space-x-2">
|
||||
{update.tags.map((tag) => (
|
||||
<button
|
||||
key={tag}
|
||||
onClick={() => handleTagsChange([...selectedTags, tag])}
|
||||
className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium transition-colors ${
|
||||
selectedTags.includes(tag)
|
||||
? 'bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200'
|
||||
: 'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700'
|
||||
}`}
|
||||
{isLoading ? (
|
||||
<div className="text-center py-20">
|
||||
<p>{t('updates.loading')}</p>
|
||||
</div>
|
||||
) : updates.length > 0 ? (
|
||||
<>
|
||||
<div className="space-y-8">
|
||||
{updates.map((update) => (
|
||||
<div key={update.id} className="relative flex items-start group">
|
||||
{/* Timeline dot */}
|
||||
<div className="absolute left-6 transform -translate-x-1/2 w-2.5 h-2.5 bg-blue-600 dark:bg-blue-500 rounded-full mt-2 group-hover:scale-125 transition-transform"></div>
|
||||
|
||||
{/* Content */}
|
||||
<div className="ml-16 w-full">
|
||||
<span className="text-sm text-blue-600 dark:text-blue-400 font-medium block mb-1">{update.date}</span>
|
||||
<Link
|
||||
to={getUpdateUrl(update)}
|
||||
target={update.link ? "_blank" : undefined}
|
||||
className="group/link"
|
||||
>
|
||||
{t(`updates.tags.${tag}`)}
|
||||
</button>
|
||||
))}
|
||||
<h3 className="text-xl font-semibold text-gray-900 dark:text-white mb-2 hover:text-blue-600 dark:hover:text-blue-400 inline-flex items-center">
|
||||
{update.title}
|
||||
{update.link && (
|
||||
<ExternalLink className="w-4 h-4 ml-1 opacity-0 group-hover/link:opacity-100 transition-opacity" />
|
||||
)}
|
||||
</h3>
|
||||
</Link>
|
||||
<p className="text-gray-600 dark:text-gray-300">{update.summary}</p>
|
||||
<div className="mt-4 flex items-center space-x-2">
|
||||
{update.tags.map((tag) => (
|
||||
<button
|
||||
key={tag}
|
||||
onClick={() => handleTagChange([...selectedTags, tag])}
|
||||
className={`inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium transition-colors ${
|
||||
selectedTags.includes(tag)
|
||||
? 'bg-blue-100 dark:bg-blue-900 text-blue-800 dark:text-blue-200'
|
||||
: 'bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 hover:bg-gray-200 dark:hover:bg-gray-700'
|
||||
}`}
|
||||
>
|
||||
{t(`updates.tags.${tag}`)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<div className="mt-4 border-b border-gray-100 dark:border-gray-800"></div>
|
||||
</div>
|
||||
</div>
|
||||
<div className="mt-4 border-b border-gray-100 dark:border-gray-800"></div>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{pagination.totalPages > 0 && (
|
||||
<Pagination
|
||||
currentPage={pagination.currentPage}
|
||||
totalPages={pagination.totalPages}
|
||||
onPageChange={handlePageChange}
|
||||
hasNextPage={pagination.hasNextPage}
|
||||
hasPrevPage={pagination.hasPrevPage}
|
||||
/>
|
||||
{pagination.totalPages > 1 && (
|
||||
<Pagination
|
||||
currentPage={pagination.currentPage}
|
||||
totalPages={pagination.totalPages}
|
||||
onPageChange={handlePageChange}
|
||||
hasNextPage={pagination.hasNextPage}
|
||||
hasPrevPage={pagination.hasPrevPage}
|
||||
/>
|
||||
)}
|
||||
</>
|
||||
) : (
|
||||
<div className="text-center py-20">
|
||||
<p>{t('updates.no_results')}</p>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
|
13
src/main.tsx
13
src/main.tsx
|
@ -3,9 +3,20 @@ import { createRoot } from 'react-dom/client';
|
|||
import App from './App.tsx';
|
||||
import './i18n';
|
||||
import './index.css';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
staleTime: Infinity, // 数据永不过期,除非手动使其失效
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<App />
|
||||
</QueryClientProvider>
|
||||
</StrictMode>
|
||||
);
|
|
@ -1,4 +1,5 @@
|
|||
import { useTranslation } from 'react-i18next';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export interface Update {
|
||||
id: string;
|
||||
|
@ -17,73 +18,84 @@ interface UpdatesIndex {
|
|||
years: string[];
|
||||
}
|
||||
|
||||
// 获取更新年份列表
|
||||
const getUpdateYears = async (language: string): Promise<string[]> => {
|
||||
try {
|
||||
const response = await fetch(`/data/${language}/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);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
};
|
||||
|
||||
// 动态获取指定的年度更新
|
||||
const getYearUpdates = async (yearFile: string, language: string): Promise<YearUpdates | null> => {
|
||||
try {
|
||||
const response = await fetch(`/data/${language}/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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
// 获取所有更新并按日期排序
|
||||
const getAllUpdates = async (language: string, selectedTags: string[] = []): Promise<Update[]> => {
|
||||
const allUpdates: Update[] = [];
|
||||
|
||||
// 从索引文件获取年份列表
|
||||
const yearFiles = await getUpdateYears(language);
|
||||
|
||||
// 加载所有年份的更新
|
||||
for (const yearFile of yearFiles) {
|
||||
const yearData = await getYearUpdates(yearFile, language);
|
||||
if (yearData?.updates?.length) {
|
||||
// 如果指定了标签,只添加包含所有选定标签的更新
|
||||
const filteredUpdates = selectedTags.length > 0
|
||||
? yearData.updates.filter(update =>
|
||||
selectedTags.every(tag => update.tags.includes(tag))
|
||||
)
|
||||
: yearData.updates;
|
||||
|
||||
allUpdates.push(...filteredUpdates);
|
||||
}
|
||||
}
|
||||
|
||||
// 按日期降序排序
|
||||
return allUpdates.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
};
|
||||
|
||||
// 获取分页的更新列表
|
||||
export const useUpdates = (page: number = 1, pageSize: number = 10) => {
|
||||
const { i18n } = useTranslation();
|
||||
|
||||
// 获取更新年份列表
|
||||
const getUpdateYears = async (): Promise<string[]> => {
|
||||
try {
|
||||
const response = await fetch(`/data/${i18n.language}/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);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
};
|
||||
const { data: allUpdates = [], isLoading, error } = useQuery({
|
||||
queryKey: ['updates', i18n.language],
|
||||
queryFn: () => getAllUpdates(i18n.language),
|
||||
});
|
||||
|
||||
// 动态获取指定的年度更新
|
||||
const getYearUpdates = async (yearFile: string): Promise<YearUpdates | null> => {
|
||||
try {
|
||||
const response = await fetch(`/data/${i18n.language}/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);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
};
|
||||
const getPaginatedUpdates = (selectedTags: string[] = []) => {
|
||||
// 过滤标签
|
||||
const filteredUpdates = selectedTags.length > 0
|
||||
? allUpdates.filter(update =>
|
||||
selectedTags.every(tag => update.tags.includes(tag))
|
||||
)
|
||||
: allUpdates;
|
||||
|
||||
// 获取所有更新并按日期排序
|
||||
const getAllUpdates = async (selectedTags: string[] = []): Promise<Update[]> => {
|
||||
const allUpdates: Update[] = [];
|
||||
|
||||
// 从索引文件获取年份列表
|
||||
const yearFiles = await getUpdateYears();
|
||||
|
||||
// 加载所有年份的更新
|
||||
for (const yearFile of yearFiles) {
|
||||
const yearData = await getYearUpdates(yearFile);
|
||||
if (yearData?.updates?.length) {
|
||||
// 如果指定了标签,只添加包含所有选定标签的更新
|
||||
const filteredUpdates = selectedTags.length > 0
|
||||
? yearData.updates.filter(update =>
|
||||
selectedTags.every(tag => update.tags.includes(tag))
|
||||
)
|
||||
: yearData.updates;
|
||||
|
||||
allUpdates.push(...filteredUpdates);
|
||||
}
|
||||
}
|
||||
|
||||
// 按日期降序排序
|
||||
return allUpdates.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
};
|
||||
|
||||
// 获取分页的更新列表
|
||||
const getPaginatedUpdates = async (selectedTags: string[] = []) => {
|
||||
const allUpdates = await getAllUpdates(selectedTags);
|
||||
const totalItems = allUpdates.length;
|
||||
const totalItems = filteredUpdates.length;
|
||||
|
||||
// 如果没有更新,返回空结果
|
||||
if (totalItems === 0) {
|
||||
|
@ -99,26 +111,29 @@ export const useUpdates = (page: number = 1, pageSize: number = 10) => {
|
|||
};
|
||||
}
|
||||
|
||||
// 计算分页信息
|
||||
const totalPages = Math.ceil(totalItems / pageSize);
|
||||
const safeCurrentPage = Math.min(Math.max(1, page), totalPages);
|
||||
|
||||
const start = (safeCurrentPage - 1) * pageSize;
|
||||
const end = Math.min(start + pageSize, totalItems);
|
||||
const updates = allUpdates.slice(start, end);
|
||||
const normalizedPage = Math.min(Math.max(1, page), totalPages);
|
||||
const startIndex = (normalizedPage - 1) * pageSize;
|
||||
const endIndex = Math.min(startIndex + pageSize, totalItems);
|
||||
|
||||
return {
|
||||
updates,
|
||||
updates: filteredUpdates.slice(startIndex, endIndex),
|
||||
pagination: {
|
||||
currentPage: safeCurrentPage,
|
||||
currentPage: normalizedPage,
|
||||
totalPages,
|
||||
totalItems,
|
||||
hasNextPage: safeCurrentPage < totalPages,
|
||||
hasPrevPage: safeCurrentPage > 1
|
||||
hasNextPage: normalizedPage < totalPages,
|
||||
hasPrevPage: normalizedPage > 1
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
return { getPaginatedUpdates, getAllUpdates };
|
||||
return {
|
||||
getPaginatedUpdates,
|
||||
isLoading,
|
||||
error
|
||||
};
|
||||
};
|
||||
|
||||
export const getUpdateUrl = (update: Update): string => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue