/*
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 { I18N_TECHNICAL_TEXT } from '../../../i18n/technicalText.js';
import { useEffectEvent } from '../../../hooks/common/useEffectEvent';
import React, { useState, useEffect, useRef } from 'react';
import { useTranslation } from 'react-i18next';
import { Typography, Card, Spin, Toast } from '@/components/ui';
import { Button, Empty } from 'antd';
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 (