|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { motion, AnimatePresence } from "framer-motion"; |
| 5 | +import { useTranslations } from "./LocaleProvider"; |
| 6 | +import { IoChevronDown, IoChevronUp } from "react-icons/io5"; |
| 7 | +import { |
| 8 | + FiGithub, |
| 9 | + FiDownload, |
| 10 | + FiAlertCircle, |
| 11 | + FiCheckCircle, |
| 12 | +} from "react-icons/fi"; |
| 13 | + |
| 14 | +export default function GitHubDownloader() { |
| 15 | + const { t } = useTranslations(); |
| 16 | + const [isExpanded, setIsExpanded] = useState(false); |
| 17 | + const [username, setUsername] = useState(""); |
| 18 | + const [repository, setRepository] = useState(""); |
| 19 | + const [branch, setBranch] = useState("main"); |
| 20 | + const [error, setError] = useState<string | null>(null); |
| 21 | + const [success, setSuccess] = useState(false); |
| 22 | + |
| 23 | + // 处理展开/折叠 |
| 24 | + const toggleExpand = () => { |
| 25 | + setIsExpanded(!isExpanded); |
| 26 | + // 重置状态 |
| 27 | + if (!isExpanded) { |
| 28 | + setError(null); |
| 29 | + setSuccess(false); |
| 30 | + } |
| 31 | + }; |
| 32 | + |
| 33 | + // 验证表单 |
| 34 | + const validateForm = (): boolean => { |
| 35 | + if (!username.trim()) { |
| 36 | + setError(t("github.invalidRepo")); |
| 37 | + return false; |
| 38 | + } |
| 39 | + if (!repository.trim()) { |
| 40 | + setError(t("github.invalidRepo")); |
| 41 | + return false; |
| 42 | + } |
| 43 | + return true; |
| 44 | + }; |
| 45 | + |
| 46 | + // 下载仓库 |
| 47 | + const downloadRepository = () => { |
| 48 | + if (!validateForm()) return; |
| 49 | + |
| 50 | + try { |
| 51 | + const downloadUrl = `https://api.github.com/${username}/${repository}/zip/${branch}`; |
| 52 | + |
| 53 | + // 打开新窗口进行下载 |
| 54 | + window.open(downloadUrl, "_blank"); |
| 55 | + |
| 56 | + // 显示成功消息 |
| 57 | + setSuccess(true); |
| 58 | + |
| 59 | + // 清除表单 |
| 60 | + setTimeout(() => { |
| 61 | + setSuccess(false); |
| 62 | + }, 3000); |
| 63 | + } catch (err) { |
| 64 | + console.error("打开下载链接出错:", err); |
| 65 | + setError(t("github.networkError")); |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + // 渲染标题栏 |
| 70 | + const renderHeader = () => ( |
| 71 | + <div |
| 72 | + className="flex items-center justify-between p-4 bg-white dark:bg-gray-800 border-b-2 border-blue-500 dark:border-blue-600 rounded-t-lg cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-750 transition-colors shadow-sm" |
| 73 | + onClick={toggleExpand} |
| 74 | + > |
| 75 | + <h2 className="text-xl font-bold text-gray-800 dark:text-gray-100 flex items-center"> |
| 76 | + <FiGithub className="h-6 w-6 mr-2 text-blue-600 dark:text-blue-500" /> |
| 77 | + <span>{t("github.title")}</span> |
| 78 | + </h2> |
| 79 | + <div className="flex items-center"> |
| 80 | + {isExpanded ? ( |
| 81 | + <IoChevronUp className="h-5 w-5 text-gray-600 dark:text-gray-300" /> |
| 82 | + ) : ( |
| 83 | + <IoChevronDown className="h-5 w-5 text-gray-600 dark:text-gray-300" /> |
| 84 | + )} |
| 85 | + </div> |
| 86 | + </div> |
| 87 | + ); |
| 88 | + |
| 89 | + return ( |
| 90 | + <div className="w-full mt-6"> |
| 91 | + {renderHeader()} |
| 92 | + |
| 93 | + <AnimatePresence> |
| 94 | + {isExpanded && ( |
| 95 | + <motion.div |
| 96 | + initial={{ opacity: 0, height: 0 }} |
| 97 | + animate={{ opacity: 1, height: "auto" }} |
| 98 | + exit={{ opacity: 0, height: 0 }} |
| 99 | + transition={{ duration: 0.3 }} |
| 100 | + className="overflow-hidden bg-white dark:bg-gray-800 rounded-b-lg shadow-sm border border-t-0 border-gray-200 dark:border-gray-700" |
| 101 | + > |
| 102 | + <div className="p-4"> |
| 103 | + <p className="text-sm text-gray-600 dark:text-gray-400 mb-4"> |
| 104 | + {t("github.description")} |
| 105 | + </p> |
| 106 | + |
| 107 | + <div className="space-y-4"> |
| 108 | + {/* 用户名/组织名输入 */} |
| 109 | + <div> |
| 110 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> |
| 111 | + {t("github.username")} |
| 112 | + </label> |
| 113 | + <input |
| 114 | + type="text" |
| 115 | + value={username} |
| 116 | + onChange={(e) => setUsername(e.target.value)} |
| 117 | + placeholder={t("github.placeholder.username")} |
| 118 | + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" |
| 119 | + /> |
| 120 | + </div> |
| 121 | + |
| 122 | + {/* 仓库名输入 */} |
| 123 | + <div> |
| 124 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> |
| 125 | + {t("github.repository")} |
| 126 | + </label> |
| 127 | + <input |
| 128 | + type="text" |
| 129 | + value={repository} |
| 130 | + onChange={(e) => setRepository(e.target.value)} |
| 131 | + placeholder={t("github.placeholder.repository")} |
| 132 | + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" |
| 133 | + /> |
| 134 | + </div> |
| 135 | + |
| 136 | + {/* 分支名输入 */} |
| 137 | + <div> |
| 138 | + <label className="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1"> |
| 139 | + {t("github.branch")} |
| 140 | + <span className="ml-2 text-xs text-gray-500 dark:text-gray-400"> |
| 141 | + {t("github.defaultBranch")} |
| 142 | + </span> |
| 143 | + </label> |
| 144 | + <input |
| 145 | + type="text" |
| 146 | + value={branch} |
| 147 | + onChange={(e) => setBranch(e.target.value)} |
| 148 | + placeholder={t("github.placeholder.branch")} |
| 149 | + className="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md focus:outline-none focus:ring-1 focus:ring-blue-500 focus:border-blue-500 bg-white dark:bg-gray-700 text-gray-900 dark:text-gray-100" |
| 150 | + /> |
| 151 | + </div> |
| 152 | + |
| 153 | + {/* 下载按钮 */} |
| 154 | + <div className="flex justify-end"> |
| 155 | + <button |
| 156 | + onClick={downloadRepository} |
| 157 | + className="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-md text-sm font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 flex items-center" |
| 158 | + > |
| 159 | + <FiDownload className="mr-2" /> |
| 160 | + {t("github.download")} |
| 161 | + </button> |
| 162 | + </div> |
| 163 | + |
| 164 | + {/* 错误信息 */} |
| 165 | + {error && ( |
| 166 | + <div className="mt-4 bg-red-50 dark:bg-red-900/20 border-l-4 border-red-500 dark:border-red-600 p-4 rounded"> |
| 167 | + <div className="flex"> |
| 168 | + <FiAlertCircle className="h-5 w-5 text-red-500 dark:text-red-400 mr-2" /> |
| 169 | + <span className="text-red-700 dark:text-red-400"> |
| 170 | + {error} |
| 171 | + </span> |
| 172 | + </div> |
| 173 | + </div> |
| 174 | + )} |
| 175 | + |
| 176 | + {/* 成功信息 */} |
| 177 | + {success && ( |
| 178 | + <div className="mt-4 bg-green-50 dark:bg-green-900/20 border-l-4 border-green-500 dark:border-green-600 p-4 rounded"> |
| 179 | + <div className="flex"> |
| 180 | + <FiCheckCircle className="h-5 w-5 text-green-500 dark:text-green-400 mr-2" /> |
| 181 | + <span className="text-green-700 dark:text-green-400"> |
| 182 | + {t("github.success")} |
| 183 | + </span> |
| 184 | + </div> |
| 185 | + </div> |
| 186 | + )} |
| 187 | + </div> |
| 188 | + </div> |
| 189 | + </motion.div> |
| 190 | + )} |
| 191 | + </AnimatePresence> |
| 192 | + </div> |
| 193 | + ); |
| 194 | +} |
0 commit comments