From 61782a77b36c4aa3f99699b547a20e5885490519 Mon Sep 17 00:00:00 2001 From: root Date: Fri, 11 Jul 2025 01:45:19 +0800 Subject: [PATCH] fix --- src/pages/HomePage.tsx | 159 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 141 insertions(+), 18 deletions(-) diff --git a/src/pages/HomePage.tsx b/src/pages/HomePage.tsx index 7b30ebd..c1482ac 100644 --- a/src/pages/HomePage.tsx +++ b/src/pages/HomePage.tsx @@ -1,13 +1,101 @@ -import React from 'react' +import React, { useState, useEffect } from 'react' import { Link } from 'react-router-dom' import { Plus, FolderOpen, Video, Music, Zap, Sparkles, Database } from 'lucide-react' +import { invoke } from '@tauri-apps/api/core' +import { ProjectService, Project } from '../services/projectService' const HomePage: React.FC = () => { - const recentProjects = [ - { id: 1, name: '旅行视频剪辑', lastModified: '2 小时前', thumbnail: '/placeholder-video.jpg' }, - { id: 2, name: '产品宣传片', lastModified: '1 天前', thumbnail: '/placeholder-video.jpg' }, - { id: 3, name: '音乐MV制作', lastModified: '3 天前', thumbnail: '/placeholder-video.jpg' }, - ] + const [recentProjects, setRecentProjects] = useState([]) + const [loading, setLoading] = useState(true) + + // 加载最近的项目 + useEffect(() => { + const loadRecentProjects = async () => { + try { + const response = await ProjectService.getAllProjects() + if (response.status && response.data) { + // 按更新时间排序,取最近的3个项目 + const sortedProjects = response.data + .sort((a, b) => new Date(b.updated_at).getTime() - new Date(a.updated_at).getTime()) + .slice(0, 3) + setRecentProjects(sortedProjects) + } + } catch (error) { + console.error('Failed to load recent projects:', error) + } finally { + setLoading(false) + } + } + + loadRecentProjects() + }, []) + + // 简化的图片组件 + const ProjectImage: React.FC<{ imagePath: string; alt: string; className: string }> = ({ + imagePath, alt, className + }) => { + const [imageSrc, setImageSrc] = useState('') + const [imageLoading, setImageLoading] = useState(true) + + useEffect(() => { + const loadImage = async () => { + if (!imagePath) { + setImageLoading(false) + return + } + + if (imagePath.startsWith('http') || imagePath.startsWith('data:')) { + setImageSrc(imagePath) + setImageLoading(false) + return + } + + try { + const dataUrl = await invoke('read_image_as_data_url', { filePath: imagePath }) + setImageSrc(dataUrl) + } catch (error) { + console.error('Failed to read image:', error) + } finally { + setImageLoading(false) + } + } + + loadImage() + }, [imagePath]) + + if (imageLoading) { + return
+ } + + if (!imageSrc) { + return ( +
+
+ ) + } + + return {alt} + } + + // 格式化时间显示 + const formatTimeAgo = (dateString: string): string => { + const now = new Date() + const date = new Date(dateString) + const diffInMs = now.getTime() - date.getTime() + const diffInHours = Math.floor(diffInMs / (1000 * 60 * 60)) + const diffInDays = Math.floor(diffInHours / 24) + + if (diffInHours < 1) { + return '刚刚' + } else if (diffInHours < 24) { + return `${diffInHours} 小时前` + } else if (diffInDays < 7) { + return `${diffInDays} 天前` + } else { + return date.toLocaleDateString('zh-CN') + } + } const quickActions = [ { icon: Sparkles, label: 'AI 视频生成', description: '使用 AI 将图片转换为动态视频', path: '/ai-video' }, @@ -73,20 +161,55 @@ const HomePage: React.FC = () => {
- {recentProjects.map((project) => ( -
-
-
-
-

{project.name}

-

{project.lastModified}

+ {loading ? ( + // 加载状态 + Array.from({ length: 3 }).map((_, index) => ( +
+
+
+
+
+
+ )) + ) : recentProjects.length > 0 ? ( + // 显示真实项目 + recentProjects.map((project) => ( + +
+ +
+
+

{project.name}

+

{formatTimeAgo(project.updated_at)}

+ {project.product_name && ( +

{project.product_name}

+ )} +
+ + )) + ) : ( + // 空状态 +
+
- ))} + )}