|
| 1 | +import { |
| 2 | + Modal, |
| 3 | + ModalOverlay, |
| 4 | + ModalContent, |
| 5 | + ModalHeader, |
| 6 | + ModalBody, |
| 7 | + ModalFooter, |
| 8 | + Button, |
| 9 | + Input, |
| 10 | + Text, |
| 11 | + VStack, |
| 12 | + Alert, |
| 13 | + AlertIcon, |
| 14 | + Link, |
| 15 | + Code, |
| 16 | + useToast, |
| 17 | +} from "@chakra-ui/react"; |
| 18 | +import { useState } from "react"; |
| 19 | +import { Icon } from "@iconify/react"; |
| 20 | + |
| 21 | +export function PatInputModal({ isOpen, onClose, onTokenSet }) { |
| 22 | + const [token, setToken] = useState(""); |
| 23 | + const [isLoading, setIsLoading] = useState(false); |
| 24 | + const toast = useToast(); |
| 25 | + |
| 26 | + const handleSubmit = async () => { |
| 27 | + if (!token.trim()) { |
| 28 | + toast({ |
| 29 | + title: "请输入 Personal Access Token", |
| 30 | + status: "warning", |
| 31 | + duration: 3000, |
| 32 | + isClosable: true, |
| 33 | + }); |
| 34 | + return; |
| 35 | + } |
| 36 | + |
| 37 | + // 验证 token 格式 |
| 38 | + if (!token.startsWith("ghp_") && !token.startsWith("github_pat_")) { |
| 39 | + toast({ |
| 40 | + title: "Token 格式错误", |
| 41 | + description: "Personal Access Token 应该以 'ghp_' 或 'github_pat_' 开头", |
| 42 | + status: "error", |
| 43 | + duration: 3000, |
| 44 | + isClosable: true, |
| 45 | + }); |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + setIsLoading(true); |
| 50 | + |
| 51 | + try { |
| 52 | + // 测试 token 是否有效 |
| 53 | + const response = await fetch("https://api.github.com/user", { |
| 54 | + headers: { |
| 55 | + Authorization: `token ${token}`, |
| 56 | + Accept: "application/vnd.github.v3+json", |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + if (response.ok) { |
| 61 | + onTokenSet(token); |
| 62 | + toast({ |
| 63 | + title: "登录成功", |
| 64 | + description: "Personal Access Token 验证成功", |
| 65 | + status: "success", |
| 66 | + duration: 3000, |
| 67 | + isClosable: true, |
| 68 | + }); |
| 69 | + onClose(); |
| 70 | + setToken(""); |
| 71 | + } else { |
| 72 | + throw new Error("Token 验证失败"); |
| 73 | + } |
| 74 | + } catch (error) { |
| 75 | + toast({ |
| 76 | + title: "Token 验证失败", |
| 77 | + description: "请检查 Token 是否正确或是否有足够的权限", |
| 78 | + status: "error", |
| 79 | + duration: 3000, |
| 80 | + isClosable: true, |
| 81 | + }); |
| 82 | + } finally { |
| 83 | + setIsLoading(false); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + const handleClose = () => { |
| 88 | + setToken(""); |
| 89 | + onClose(); |
| 90 | + }; |
| 91 | + |
| 92 | + return ( |
| 93 | + <Modal isOpen={isOpen} onClose={handleClose} size="lg"> |
| 94 | + <ModalOverlay /> |
| 95 | + <ModalContent> |
| 96 | + <ModalHeader> |
| 97 | + <Icon icon="mingcute:key-line" style={{ display: "inline", marginRight: "8px" }} /> |
| 98 | + 设置 Personal Access Token |
| 99 | + </ModalHeader> |
| 100 | + <ModalBody> |
| 101 | + <VStack spacing={4} align="stretch"> |
| 102 | + <Alert status="info"> |
| 103 | + <AlertIcon /> |
| 104 | + <Text fontSize="sm"> |
| 105 | + 由于这是纯前端项目,需要您提供 GitHub Personal Access Token 来完成认证。 |
| 106 | + </Text> |
| 107 | + </Alert> |
| 108 | + |
| 109 | + <VStack spacing={3} align="stretch"> |
| 110 | + <Text fontSize="sm" fontWeight="medium"> |
| 111 | + 如何获取 Personal Access Token: |
| 112 | + </Text> |
| 113 | + <VStack spacing={2} align="stretch" pl={4}> |
| 114 | + <Text fontSize="xs"> |
| 115 | + 1. 前往{" "} |
| 116 | + <Link |
| 117 | + href="https://github.com/settings/tokens/new" |
| 118 | + color="blue.500" |
| 119 | + isExternal |
| 120 | + > |
| 121 | + GitHub Token 设置页面 |
| 122 | + </Link> |
| 123 | + </Text> |
| 124 | + <Text fontSize="xs"> |
| 125 | + 2. 填写 Token 名称(如:ProfilePage Auth) |
| 126 | + </Text> |
| 127 | + <Text fontSize="xs"> |
| 128 | + 3. 选择权限:<Code fontSize="xs">repo</Code>(仓库权限) |
| 129 | + </Text> |
| 130 | + <Text fontSize="xs"> |
| 131 | + 4. 点击 "Generate token" 生成 |
| 132 | + </Text> |
| 133 | + <Text fontSize="xs"> |
| 134 | + 5. 复制生成的 Token 并粘贴到下方输入框 |
| 135 | + </Text> |
| 136 | + </VStack> |
| 137 | + </VStack> |
| 138 | + |
| 139 | + <Input |
| 140 | + placeholder="粘贴您的 Personal Access Token(ghp_...)" |
| 141 | + value={token} |
| 142 | + onChange={(e) => setToken(e.target.value)} |
| 143 | + type="password" |
| 144 | + size="md" |
| 145 | + /> |
| 146 | + |
| 147 | + <Alert status="warning" fontSize="xs"> |
| 148 | + <AlertIcon /> |
| 149 | + Token 将仅保存在您的浏览器本地,不会上传到任何服务器。 |
| 150 | + </Alert> |
| 151 | + </VStack> |
| 152 | + </ModalBody> |
| 153 | + |
| 154 | + <ModalFooter> |
| 155 | + <Button variant="ghost" mr={3} onClick={handleClose}> |
| 156 | + 取消 |
| 157 | + </Button> |
| 158 | + <Button |
| 159 | + colorScheme="blue" |
| 160 | + onClick={handleSubmit} |
| 161 | + isLoading={isLoading} |
| 162 | + loadingText="验证中..." |
| 163 | + leftIcon={<Icon icon="mingcute:check-line" />} |
| 164 | + > |
| 165 | + 验证并登录 |
| 166 | + </Button> |
| 167 | + </ModalFooter> |
| 168 | + </ModalContent> |
| 169 | + </Modal> |
| 170 | + ); |
| 171 | +} |
0 commit comments