Skip to content

Commit f7d4bc4

Browse files
committed
feat: 改进YAML提交流程,优化Pull Request和Issue创建逻辑
1 parent b1238ad commit f7d4bc4

File tree

1 file changed

+102
-11
lines changed

1 file changed

+102
-11
lines changed

src/components/ChallengeContributePage/components/YamlPreviewSection.tsx

Lines changed: 102 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { Button, Modal, Space, message } from 'antd';
3-
import { CopyOutlined, GithubOutlined, BugOutlined } from '@ant-design/icons';
3+
import { CopyOutlined, GithubOutlined } from '@ant-design/icons';
44
import { Light as SyntaxHighlighter } from 'react-syntax-highlighter';
55
import yaml from 'react-syntax-highlighter/dist/esm/languages/hljs/yaml';
66
import { vs } from 'react-syntax-highlighter/dist/esm/styles/hljs';
@@ -12,6 +12,9 @@ SyntaxHighlighter.registerLanguage('yaml', yaml);
1212
const GITHUB_REPO = 'JSREP/crawler-leetcode';
1313
const GITHUB_BASE_URL = `https://github.com/${GITHUB_REPO}`;
1414

15+
// YAML长度限制(超过此长度需要手动提交)
16+
const YAML_LENGTH_LIMIT = 1000;
17+
1518
// 自定义高亮主题
1619
const highlightTheme = {
1720
...vs,
@@ -36,6 +39,8 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
3639
onCopyYaml
3740
}) => {
3841
const [isModalVisible, setIsModalVisible] = React.useState<boolean>(false);
42+
const [isLengthWarningVisible, setIsLengthWarningVisible] = React.useState<boolean>(false);
43+
const [currentAction, setCurrentAction] = React.useState<'PR' | 'Issue' | null>(null);
3944

4045
const showModal = () => {
4146
onGenerateYaml(); // 先生成YAML
@@ -46,6 +51,11 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
4651
setIsModalVisible(false);
4752
};
4853

54+
const handleLengthWarningCancel = () => {
55+
setIsLengthWarningVisible(false);
56+
setCurrentAction(null);
57+
};
58+
4959
// 复制并关闭Modal
5060
const handleCopyAndClose = () => {
5161
onCopyYaml();
@@ -66,13 +76,59 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
6676
return '新挑战';
6777
};
6878

69-
// 创建Pull Request
70-
const createPullRequest = () => {
79+
// 检查YAML长度并决定是否显示警告
80+
const checkYamlLengthAndProceed = (action: 'PR' | 'Issue') => {
81+
// 如果还没有YAML,先生成
7182
if (!yamlOutput) {
72-
message.error('请先预览YAML生成内容');
83+
onGenerateYaml();
84+
}
85+
86+
// 检查生成后的YAML长度
87+
if (yamlOutput.length > YAML_LENGTH_LIMIT) {
88+
// 设置当前操作类型
89+
setCurrentAction(action);
90+
// 显示长度警告弹窗
91+
setIsLengthWarningVisible(true);
7392
return;
7493
}
7594

95+
// 长度在限制内,继续正常流程
96+
if (action === 'PR') {
97+
proceedToPullRequest();
98+
} else {
99+
proceedToIssue();
100+
}
101+
};
102+
103+
// 手动复制YAML,用于超长情况
104+
const handleManualCopyYaml = () => {
105+
navigator.clipboard.writeText(yamlOutput)
106+
.then(() => {
107+
message.success('YAML已复制到剪贴板,请在GitHub中粘贴');
108+
109+
// 打开相应的GitHub页面
110+
if (currentAction === 'PR') {
111+
const prUrl = `${GITHUB_BASE_URL}/compare/main...`;
112+
window.open(prUrl, '_blank');
113+
} else {
114+
const issueUrl = `${GITHUB_BASE_URL}/issues/new`;
115+
window.open(issueUrl, '_blank');
116+
}
117+
118+
// 关闭警告弹窗
119+
setIsLengthWarningVisible(false);
120+
setCurrentAction(null);
121+
})
122+
.catch((error) => {
123+
console.error('复制YAML失败:', error);
124+
message.error('复制YAML失败,请手动复制后提交');
125+
});
126+
};
127+
128+
// 创建Pull Request的实际实现
129+
const proceedToPullRequest = () => {
130+
if (!yamlOutput) return;
131+
76132
const challengeName = extractChallengeName();
77133

78134
// 使用navigator.clipboard API复制YAML到剪贴板
@@ -101,12 +157,9 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
101157
});
102158
};
103159

104-
// 创建Issue
105-
const createIssue = () => {
106-
if (!yamlOutput) {
107-
message.error('请先预览YAML生成内容');
108-
return;
109-
}
160+
// 创建Issue的实际实现
161+
const proceedToIssue = () => {
162+
if (!yamlOutput) return;
110163

111164
const challengeName = extractChallengeName();
112165

@@ -136,6 +189,16 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
136189
});
137190
};
138191

192+
// 创建Pull Request的触发函数
193+
const createPullRequest = () => {
194+
checkYamlLengthAndProceed('PR');
195+
};
196+
197+
// 创建Issue的触发函数
198+
const createIssue = () => {
199+
checkYamlLengthAndProceed('Issue');
200+
};
201+
139202
return (
140203
<>
141204
<div style={{ marginBottom: 16, marginTop: 24 }}>
@@ -159,13 +222,14 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
159222
<Button
160223
type="default"
161224
onClick={createIssue}
162-
icon={<BugOutlined />}
225+
icon={<GithubOutlined />}
163226
>
164227
New Issue
165228
</Button>
166229
</Space>
167230
</div>
168231

232+
{/* YAML预览对话框 */}
169233
<Modal
170234
title="预览YAML代码"
171235
open={isModalVisible}
@@ -200,6 +264,33 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({
200264
</SyntaxHighlighter>
201265
</div>
202266
</Modal>
267+
268+
{/* YAML长度警告对话框 */}
269+
<Modal
270+
title="YAML内容过长"
271+
open={isLengthWarningVisible}
272+
onCancel={handleLengthWarningCancel}
273+
footer={[
274+
<Button key="cancel" onClick={handleLengthWarningCancel}>
275+
取消
276+
</Button>,
277+
<Button key="copy" type="primary" icon={<CopyOutlined />} onClick={handleManualCopyYaml}>
278+
复制YAML并继续
279+
</Button>
280+
]}
281+
>
282+
<div style={{ padding: '16px 0' }}>
283+
<p>生成的YAML内容超过了{YAML_LENGTH_LIMIT}个字符,无法通过URL参数直接传递。</p>
284+
<p>请使用以下步骤手动提交:</p>
285+
<ol>
286+
<li>点击下方的"复制YAML并继续"按钮将YAML内容复制到剪贴板</li>
287+
<li>然后会自动跳转到GitHub {currentAction === 'PR' ? 'Pull Request' : 'Issue'} 创建页面</li>
288+
<li>在GitHub页面上,填写标题和描述</li>
289+
<li>将YAML内容粘贴到描述区域中的合适位置</li>
290+
<li>提交{currentAction === 'PR' ? 'Pull Request' : 'Issue'}</li>
291+
</ol>
292+
</div>
293+
</Modal>
203294
</>
204295
);
205296
};

0 commit comments

Comments
 (0)