/* Copyright (C) 2025 QuantumNous This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . For commercial licensing, please contact support@quantumnous.com */ import { useEffectEvent } from '../../../hooks/common/useEffectEvent'; import React, { useState, useEffect, useRef } from 'react'; import { useTranslation } from 'react-i18next'; import { Typography, Card, Empty, Spin, Toast } from '@/components/ui'; import { IconPlayCircle, IconClock, IconFile } from '@/components/ui/icons'; import { API } from '../../../helpers'; import './VideoTutorial.css'; const { Title, Text, Paragraph } = Typography; const VideoTutorial = () => { const { t } = useTranslation(); const [videos, setVideos] = useState([]); const [currentVideo, setCurrentVideo] = useState(null); const [loading, setLoading] = useState(true); const [videoError, setVideoError] = useState(false); const videoRef = useRef(null); // 获取视频列表 const onMount = useEffectEvent(() => { loadVideos(); }); useEffect(() => onMount(), [onMount]); const loadVideos = async () => { setLoading(true); try { const res = await API.get('/api/tutorial/videos'); if (res.data.success && res.data.data) { setVideos(res.data.data); if (res.data.data.length > 0) { setCurrentVideo(res.data.data[0]); } } } catch (error) { console.error('Failed to load tutorial videos:', error); Toast.error(t('加载视频失败')); } finally { setLoading(false); } }; // 格式化时长 const formatDuration = (seconds) => { if (!seconds) return '--:--'; const mins = Math.floor(seconds / 60); const secs = seconds % 60; return `${mins}:${secs.toString().padStart(2, '0')}`; }; // 格式化文件大小 const formatFileSize = (bytes) => { if (!bytes) return '--'; if (bytes < 1024 * 1024) { return `${(bytes / 1024).toFixed(1)} KB`; } return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; }; // 切换视频 const handleSelectVideo = (video) => { setCurrentVideo(video); setVideoError(false); // 重置视频播放 if (videoRef.current) { videoRef.current.load(); } }; // 视频加载错误处理 const handleVideoError = () => { setVideoError(true); Toast.error(t('视频加载失败,请刷新页面重试')); }; // 刷新视频URL const refreshVideoUrl = async () => { setVideoError(false); await loadVideos(); }; if (loading) { return (
{t('加载视频中...')}
); } if (videos.length === 0) { return (
} title={t('暂无视频教程')} description={t('管理员还没有上传视频教程,请查看图文教程')} />
); } return (
{/* 视频播放器区域 */}
{currentVideo && ( <>
{videoError ? (
{t('视频加载失败')}
) : ( )}
{currentVideo.title} {currentVideo.description && ( {currentVideo.description} )}
{currentVideo.duration > 0 && ( {formatDuration(currentVideo.duration)} )} {currentVideo.file_size > 0 && ( {formatFileSize(currentVideo.file_size)} )}
)}
{/* 视频列表 */} {videos.length > 1 && (
{t('视频列表')}
{videos.map((video, index) => (
handleSelectVideo(video)} >
{video.thumbnail_url ? ( {video.title} ) : (
)} {index + 1} {video.duration > 0 && ( {formatDuration(video.duration)} )}
{video.title} {video.description && ( {video.description} )}
))}
)} {/* 提示信息 */}
💡
{t('观看提示')} {t( '建议在 WiFi 环境下观看视频教程。如遇视频加载缓慢,请刷新页面重试。', )}
); }; export default VideoTutorial;