返回 AiToEarn
AICreateTitle.tsx
1 import React, { ForwardedRef, forwardRef, memo, useState } from 'react';
2 import styles from './aICreateTitle.module.scss';
3 import { Button, message, Tooltip } from 'antd';
4 import { ExclamationCircleOutlined } from '@ant-design/icons';
5 import { IVideoFile } from '../../../../components/Choose/VideoChoose';
6 import { useShallow } from 'zustand/react/shallow';
7 import { useAICreateTitleStore } from './useAICreateTitle';
8 import { toolsApi } from '../../../../api/tools';
9 import { AiCreateType } from '../../../../api/types/tools';
10
11 const VITE_APP_FILE_HOST = import.meta.env.VITE_APP_FILE_HOST;
12
13 export interface IAICreateTitleRef {}
14
15 export interface IAICreateTitleProps {
16 type: AiCreateType;
17 // 提示信息
18 tips?: string;
19 // 用于生成文字的视频
20 videoFile?: IVideoFile;
21 onAiCreateFinish: (text: string) => void;
22 // 字数限制
23 max: number;
24 }
25
26 /**
27 * AI生成标题和描述
28 */
29 const AICreateTitle = memo(
30 forwardRef(
31 (
32 { type, tips, videoFile, onAiCreateFinish, max }: IAICreateTitleProps,
33 ref: ForwardedRef<IAICreateTitleRef>,
34 ) => {
35 const [loading, setLoading] = useState(false);
36 const { getVideo, setVideo } = useAICreateTitleStore(
37 useShallow((state) => ({
38 getVideo: state.getVideo,
39 setVideo: state.setVideo,
40 })),
41 );
42
43 return (
44 <div className={styles.aICreateTitle}>
45 <Button
46 loading={loading}
47 type="link"
48 onClick={async () => {
49 if (!videoFile) return message.warning('您必须上传一个视频!');
50 if (videoFile.duration > 40)
51 return message.warning('视频超过40秒,不支持生成!');
52 if (loading) return;
53 setLoading(true);
54 const videoId = videoFile.size + videoFile.filename;
55 let videoOssUrl = '';
56 videoOssUrl = getVideo(videoId);
57 // 上传
58 if (!videoOssUrl) {
59 const { name } = await toolsApi.uploadFileTemp(videoFile.file);
60 if (name) {
61 videoOssUrl = VITE_APP_FILE_HOST + name;
62 setVideo(videoId, videoOssUrl);
63 } else {
64 setLoading(false);
65 return message.error('网络繁忙,请稍后重试!');
66 }
67 }
68 const res = await toolsApi.apiVideoAiTitle(
69 videoOssUrl,
70 type,
71 max,
72 );
73 onAiCreateFinish(res);
74 setLoading(false);
75 }}
76 >
77 {type === AiCreateType.TITLE ? 'AI生成标题' : 'AI生成描述'}
78 {tips && (
79 <Tooltip title={tips}>
80 <ExclamationCircleOutlined />
81 </Tooltip>
82 )}
83 </Button>
84 </div>
85 );
86 },
87 ),
88 );
89 AICreateTitle.displayName = 'AICreateTitle';
90
91 export default AICreateTitle;
92
92 lines Plain Text