|
1 | 1 | import * as React from 'react'; |
2 | | -import { Button, Modal } from 'antd'; |
3 | | -import { CopyOutlined } from '@ant-design/icons'; |
| 2 | +import { Button, Modal, Space, message } from 'antd'; |
| 3 | +import { CopyOutlined, GithubOutlined, BugOutlined } from '@ant-design/icons'; |
4 | 4 | import { Light as SyntaxHighlighter } from 'react-syntax-highlighter'; |
5 | 5 | import yaml from 'react-syntax-highlighter/dist/esm/languages/hljs/yaml'; |
6 | 6 | import { vs } from 'react-syntax-highlighter/dist/esm/styles/hljs'; |
7 | 7 |
|
8 | 8 | // 注册YAML语言 |
9 | 9 | SyntaxHighlighter.registerLanguage('yaml', yaml); |
10 | 10 |
|
| 11 | +// GitHub仓库信息 |
| 12 | +const GITHUB_REPO = 'JSREP/crawler-leetcode'; |
| 13 | +const GITHUB_BASE_URL = `https://github.com/${GITHUB_REPO}`; |
| 14 | + |
11 | 15 | // 自定义高亮主题 |
12 | 16 | const highlightTheme = { |
13 | 17 | ...vs, |
@@ -48,16 +52,98 @@ const YamlPreviewSection: React.FC<YamlPreviewSectionProps> = ({ |
48 | 52 | setIsModalVisible(false); |
49 | 53 | }; |
50 | 54 |
|
| 55 | + // 提取挑战名称,用于PR和Issue标题 |
| 56 | + const extractChallengeName = (): string => { |
| 57 | + try { |
| 58 | + // 尝试从YAML中提取name字段 |
| 59 | + const match = yamlOutput.match(/name:\s*(.+)/); |
| 60 | + if (match && match[1]) { |
| 61 | + return match[1].trim(); |
| 62 | + } |
| 63 | + } catch (e) { |
| 64 | + console.error('无法提取挑战名称', e); |
| 65 | + } |
| 66 | + return '新挑战'; |
| 67 | + }; |
| 68 | + |
| 69 | + // 创建Pull Request |
| 70 | + const createPullRequest = () => { |
| 71 | + if (!yamlOutput) { |
| 72 | + message.error('请先预览YAML生成内容'); |
| 73 | + return; |
| 74 | + } |
| 75 | + |
| 76 | + const challengeName = extractChallengeName(); |
| 77 | + |
| 78 | + // 准备PR内容 |
| 79 | + const title = encodeURIComponent(`新增题目: ${challengeName}`); |
| 80 | + const body = encodeURIComponent( |
| 81 | + `## 新增题目\n\n` + |
| 82 | + `提交一个新的挑战题目。\n\n` + |
| 83 | + `### YAML代码\n\n` + |
| 84 | + `\`\`\`yaml\n${yamlOutput}\n\`\`\`` |
| 85 | + ); |
| 86 | + |
| 87 | + // 构建PR创建URL |
| 88 | + const prUrl = `${GITHUB_BASE_URL}/compare/main...?quick_pull=1&title=${title}&body=${body}`; |
| 89 | + |
| 90 | + // 在新标签页中打开 |
| 91 | + window.open(prUrl, '_blank'); |
| 92 | + }; |
| 93 | + |
| 94 | + // 创建Issue |
| 95 | + const createIssue = () => { |
| 96 | + if (!yamlOutput) { |
| 97 | + message.error('请先预览YAML生成内容'); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + const challengeName = extractChallengeName(); |
| 102 | + |
| 103 | + // 准备Issue内容 |
| 104 | + const title = encodeURIComponent(`题目请求: ${challengeName}`); |
| 105 | + const body = encodeURIComponent( |
| 106 | + `## 题目请求\n\n` + |
| 107 | + `请求添加以下挑战题目。\n\n` + |
| 108 | + `### YAML代码\n\n` + |
| 109 | + `\`\`\`yaml\n${yamlOutput}\n\`\`\`` |
| 110 | + ); |
| 111 | + |
| 112 | + // 构建Issue创建URL |
| 113 | + const issueUrl = `${GITHUB_BASE_URL}/issues/new?title=${title}&body=${body}`; |
| 114 | + |
| 115 | + // 在新标签页中打开 |
| 116 | + window.open(issueUrl, '_blank'); |
| 117 | + }; |
| 118 | + |
51 | 119 | return ( |
52 | 120 | <> |
53 | 121 | <div style={{ marginBottom: 16, marginTop: 24 }}> |
54 | | - <Button |
55 | | - type="primary" |
56 | | - onClick={showModal} |
57 | | - icon={<CopyOutlined />} |
58 | | - > |
59 | | - 预览YAML |
60 | | - </Button> |
| 122 | + <Space size="middle"> |
| 123 | + <Button |
| 124 | + type="primary" |
| 125 | + onClick={showModal} |
| 126 | + icon={<CopyOutlined />} |
| 127 | + > |
| 128 | + 预览YAML |
| 129 | + </Button> |
| 130 | + |
| 131 | + <Button |
| 132 | + type="default" |
| 133 | + onClick={createPullRequest} |
| 134 | + icon={<GithubOutlined />} |
| 135 | + > |
| 136 | + Pull Request |
| 137 | + </Button> |
| 138 | + |
| 139 | + <Button |
| 140 | + type="default" |
| 141 | + onClick={createIssue} |
| 142 | + icon={<BugOutlined />} |
| 143 | + > |
| 144 | + New Issue |
| 145 | + </Button> |
| 146 | + </Space> |
61 | 147 | </div> |
62 | 148 |
|
63 | 149 | <Modal |
|
0 commit comments