This commit is contained in:
北枳 2025-09-08 23:00:17 +08:00
parent 264e6d92b2
commit 58d0017e98
2 changed files with 35 additions and 15 deletions

View File

@ -1,5 +1,5 @@
import { notification, Progress } from 'antd';
import { useEffect, useRef, useState, useMemo } from 'react';
import { useEffect, useRef, useState, useMemo, useCallback } from 'react';
import { Scissors } from 'lucide-react';
import { motion } from 'framer-motion';
@ -52,6 +52,10 @@ interface EditingNotificationProps {
timeout?: number;
/** 是否显示关闭按钮默认false */
showCloseIcon?: boolean;
/** 获取当前进度值的回调 */
onGetProgress?: (progress: number) => void;
/** 设置进度值的回调 */
onSetProgress?: (setProgressFn: (value: number) => void) => void;
}
/**
@ -69,15 +73,31 @@ export const showEditingNotification = (props: EditingNotificationProps) => {
onFail,
timeout = 8 * 60 * 1000, // 默认8分钟
showCloseIcon = false,
onGetProgress,
onSetProgress,
} = props;
const NotificationContent = () => {
const [progress, setProgress] = useState(0);
const [progress, setProgressInternal] = useState(0);
const [status, setStatus] = useState<'active' | 'success' | 'exception'>('active');
const [currentDescription, setCurrentDescription] = useState(description);
const timerRef = useRef<NodeJS.Timeout>();
const startTimeRef = useRef(Date.now());
// 包装 setProgress 以支持外部回调
const setProgress = useCallback((value: number | ((prev: number) => number)) => {
const newValue = typeof value === 'function' ? value(progress) : value;
setProgressInternal(newValue);
onGetProgress?.(newValue);
}, [progress]); // 添加 progress 作为依赖
// 监听外部设置进度值
useEffect(() => {
if (onSetProgress) {
onSetProgress(setProgress);
}
}, [setProgress]); // 只依赖 setProgress
// 重置进度条
const resetProgress = () => {
setProgress(0);
@ -115,7 +135,7 @@ export const showEditingNotification = (props: EditingNotificationProps) => {
if (isCompleted) {
// 如果完成了快速增加到100%
setProgress(prev => {
setProgress((prev: number) => {
const next = prev + (100 - prev) / 10;
if (next >= 99.9) {
setStatus('success');
@ -127,15 +147,18 @@ export const showEditingNotification = (props: EditingNotificationProps) => {
return next;
});
} else if (elapsed >= timeLimit) {
// 超时失败
// 超时失败但继续更新进度
setStatus('exception');
setCurrentDescription(timeoutDescription);
onFail?.();
clearInterval(timerRef.current);
return;
// 不清除定时器,继续更新进度
setProgress((prev: number) => {
const next = Math.min(prev + 0.2, 90); // 失败后继续缓慢增加但限制在90%以内
return next;
});
} else {
// 正常进度缓慢增加到90%
setProgress(prev => {
setProgress((prev: number) => {
const targetProgress = (elapsed / timeLimit) * 90;
const next = Math.min(prev + 0.5, targetProgress);
return next;
@ -145,7 +168,7 @@ export const showEditingNotification = (props: EditingNotificationProps) => {
timerRef.current = setInterval(updateProgress, 100);
return () => clearInterval(timerRef.current);
}, [isCompleted]);
}, [isCompleted, setProgress, successDescription, timeoutDescription, timeout]);
return (
<div data-alt="editing-notification" style={{ minWidth: '300px' }}>

View File

@ -154,7 +154,7 @@ export function useWorkflowData({ onEditPlanGenerated }: UseWorkflowDataProps =
description: `Generating intelligent editing plan... ${retryCount ? 'Retry Time: ' + retryCount : ''}`,
successDescription: 'Editing plan generated successfully.',
timeoutDescription: 'Editing plan generation failed. Please refresh and try again.',
timeout: 3 * 60 * 1000
timeout: 8 * 60 * 1000
});
// 先停止轮询
await new Promise(resolve => {
@ -191,17 +191,14 @@ export function useWorkflowData({ onEditPlanGenerated }: UseWorkflowDataProps =
setNeedStreamData(true);
setIsGenerateEditPlan(false);
// 显示失败通知3秒
// 显示失败通知,但保持进度条继续更新
showEditingNotification({
key: notificationKey,
description: `Generating intelligent editing plan... ${retryCount ? 'Retry Time: ' + retryCount : ''}`,
timeoutDescription: 'Editing plan generation failed. Retrying later.',
timeout: 3000
timeout: 8 * 60 * 1000 // 延长超时时间到8分钟给重试留出足够时间
});
setTimeout(() => {
notification.destroy(notificationKey);
setIsLoadingGenerateEditPlan(false);
}, 8000);
setIsLoadingGenerateEditPlan(false);
}
}, [episodeId, onEditPlanGenerated, notificationKey]);