refactor(PostCard, PostCardGrid, HomeScreen): enhance PostCard component and remove PostCardGrid
- Introduced a new PostCardRelativeTime component for dynamic time display, improving user experience with real-time updates. - Refactored PostCard to utilize memoization for performance optimization and prevent unnecessary re-renders. - Removed the PostCardGrid component to streamline the codebase, consolidating functionality within the PostCard component. - Updated HomeScreen to leverage the new PostCard features, ensuring consistent post rendering and interaction handling.
This commit is contained in:
@@ -248,12 +248,18 @@ export const getPostTotalEngagement = (post: Post): number => {
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化帖子创建时间为相对时间描述
|
||||
* 根据创建时间 ISO 字符串格式化为列表用相对时间(与历史 formatPostTime 行为一致)
|
||||
*/
|
||||
export const formatPostTime = (post: Post): string => {
|
||||
const createdAt = new Date(post.createdAt);
|
||||
export const formatPostCreatedAtString = (createdAt: string | undefined | null): string => {
|
||||
if (!createdAt) return '';
|
||||
const createdAtDate = new Date(createdAt);
|
||||
if (Number.isNaN(createdAtDate.getTime())) return '';
|
||||
|
||||
const now = new Date();
|
||||
const diffMs = now.getTime() - createdAt.getTime();
|
||||
const diffMs = now.getTime() - createdAtDate.getTime();
|
||||
if (diffMs < 0) {
|
||||
return createdAtDate.toLocaleDateString('zh-CN');
|
||||
}
|
||||
const diffSeconds = Math.floor(diffMs / 1000);
|
||||
const diffMinutes = Math.floor(diffSeconds / 60);
|
||||
const diffHours = Math.floor(diffMinutes / 60);
|
||||
@@ -271,5 +277,12 @@ export const formatPostTime = (post: Post): string => {
|
||||
if (diffDays < 7) {
|
||||
return `${diffDays}天前`;
|
||||
}
|
||||
return createdAt.toLocaleDateString('zh-CN');
|
||||
return createdAtDate.toLocaleDateString('zh-CN');
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化帖子创建时间为相对时间描述
|
||||
*/
|
||||
export const formatPostTime = (post: Post): string => {
|
||||
return formatPostCreatedAtString(post.createdAt);
|
||||
};
|
||||
Reference in New Issue
Block a user