Skip to content

Commit

Permalink
solved all the problems
Browse files Browse the repository at this point in the history
Signed-off-by: wblx <904653630@qq.com>
  • Loading branch information
wblxxx committed Jul 29, 2024
1 parent c27d31d commit b4a25ed
Show file tree
Hide file tree
Showing 7 changed files with 177 additions and 109 deletions.
5 changes: 2 additions & 3 deletions src/api/githubApi.ts → src/helpers/githubApi.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// src/api/githubApi.ts
let githubToken = '';

export const saveToken = (token: string) => {
githubToken = token;
localStorage.setItem('github_token', token); // 保存 token 到 localStorage
localStorage.setItem('github_token', token);
};

export const getToken = () => {
Expand Down Expand Up @@ -32,4 +31,4 @@ export const githubRequest = async (endpoint: string, options: RequestInit = {})
}

return response.json();
};
};
26 changes: 25 additions & 1 deletion src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,29 @@
"activity_icon": "activity",
"openrank_icon": "openrank",
"contributors_participants_icon": "contributors and participants",
"merged_lines_icon": "code lines change"
"merged_lines_icon": "code lines change",
"github_token_configuration": "GitHub Token Configuration",
"github_token_tooltip": "Enter your GitHub Token here for authentication.",
"github_token_description": "Providing a GitHub Token ensures that HyperCRX can securely and effectively access and operate on your GitHub data for personalized analysis.",
"github_token_how_to_generate": "How to generate a GitHub Token?",
"github_token_step1": "1. Log in to GitHub and go to Settings.",
"github_token_step2": "2. Select Developer settings.",
"github_token_step3": "3. Choose Personal access tokens, click Tokens(classic), then Generate a personal access token.",
"github_token_step4": "4. Set token details:",
"github_token_note": "Note",
"github_token_note_description": "A descriptive name, e.g., \"HyperCrx Token\".",
"github_token_expiration": "Expiration",
"github_token_expiration_description": "Choose the validity period.",
"github_token_scopes": "Scopes",
"github_token_scopes_description": "Select permission scopes, such as repo and workflow.",
"github_token_step5": "5. Click the Generate token button.",
"github_token_step6": "6. Copy the generated token and paste it into the input box below.",
"github_token_placeholder": "GitHub Token",
"github_token_save": "Save",
"github_token_edit": "Edit",
"github_token_test": "Test Token",
"github_token_error_empty": "Token cannot be empty",
"github_token_success_save": "Token saved successfully",
"github_token_success_valid": "Token is valid. Username: {{username}}",
"github_token_error_invalid": "Invalid token or request failed"
}
26 changes: 25 additions & 1 deletion src/locales/zh_CN/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,29 @@
"activity_icon": "activity数",
"openrank_icon": "openrank值",
"contributors_participants_icon": "contributors和participants数",
"merged_lines_icon": "代码变化量"
"merged_lines_icon": "代码变化量",
"github_token_configuration": "GitHub 令牌配置",
"github_token_tooltip": "在此输入您的 GitHub 令牌以进行身份验证。",
"github_token_description": "提供 GitHub 令牌可确保 HyperCRX 能够安全有效地访问和操作您的 GitHub 数据以进行个性化分析。",
"github_token_how_to_generate": "如何生成 GitHub 令牌?",
"github_token_step1": "1. 登录 GitHub 并进入设置。",
"github_token_step2": "2. 选择开发者设置。",
"github_token_step3": "3. 选择个人访问令牌,Tokens(classic),然后生成个人访问令牌。",
"github_token_step4": "4. 设置令牌详细信息:",
"github_token_note": "注意",
"github_token_note_description": "描述性名称,例如“HyperCrx Token",
"github_token_expiration": "过期时间",
"github_token_expiration_description": "选择有效期。",
"github_token_scopes": "权限范围",
"github_token_scopes_description": "选择权限范围,例如 repo 和 workflow。",
"github_token_step5": "5. 点击生成令牌按钮。",
"github_token_step6": "6. 复制生成的令牌并将其粘贴到下面的输入框中。",
"github_token_placeholder": "GitHub 令牌",
"github_token_save": "保存",
"github_token_edit": "编辑",
"github_token_test": "测试令牌",
"github_token_error_empty": "令牌不能为空",
"github_token_success_save": "令牌保存成功",
"github_token_success_valid": "令牌有效。用户名:{{username}}",
"github_token_error_invalid": "令牌无效或请求失败"
}
97 changes: 0 additions & 97 deletions src/pages/Options/GitHubToken.tsx

This file was deleted.

3 changes: 1 addition & 2 deletions src/pages/Options/Options.css
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* src/pages/Options/Options.css */
.container {
width: 98vw;
min-height: calc(100vh - 100px);
Expand Down Expand Up @@ -112,4 +111,4 @@ a {

.github-token-options button:hover {
background-color: #0056b3;
}
}
12 changes: 7 additions & 5 deletions src/pages/Options/Options.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// src/pages/Options/index.tsx
import React, { useState, useEffect } from 'react';
import { Checkbox, Radio, Space, Row, Col } from 'antd';
import { importedFeatures } from '../../../README.md';
Expand All @@ -8,7 +7,7 @@ import TooltipTrigger from '../../components/TooltipTrigger';
import './Options.css';
import { useTranslation } from 'react-i18next';
import '../../helpers/i18n';
import GitHubToken from './GitHubToken'; // 引入 GitHubToken 组件
import GitHubToken from './components/GitHubToken';

const stacksStyleOptions = {
headerStack: {
Expand All @@ -20,6 +19,9 @@ const stacksStyleOptions = {
settingStack: {
margin: '10px 25px',
},
tokenStack: {
margin: '10px 25px',
},
};

const Options = (): JSX.Element => {
Expand Down Expand Up @@ -70,7 +72,7 @@ const Options = (): JSX.Element => {
<Row
justify="center"
style={stacksStyleOptions.mainStack}
gutter={[30, 30]} // 设置间距
gutter={[30, 30]}
>
<Col
span={24}
Expand Down Expand Up @@ -155,9 +157,10 @@ const Options = (): JSX.Element => {
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
margin: stacksStyleOptions.tokenStack.margin,
}}
>
<GitHubToken /> {/* 添加 GitHubToken 组件 */}
<GitHubToken /> {/* Add GitHubToken component */}
</Col>
</Row>
</Space>
Expand All @@ -166,4 +169,3 @@ const Options = (): JSX.Element => {
};

export default Options;

117 changes: 117 additions & 0 deletions src/pages/Options/components/GitHubToken.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import React, { useState, useEffect } from 'react';
import TooltipTrigger from '../../../components/TooltipTrigger';
import { saveToken, getToken, githubRequest } from '../../../helpers/githubApi';
import { message } from 'antd';
import { useTranslation } from 'react-i18next';

message.config({
top: 1130,
duration: 2,
maxCount: 3,
});

const GitHubToken = () => {
const [token, setToken] = useState('');
const [isCollapsed, setIsCollapsed] = useState(true);
const [isEditing, setIsEditing] = useState(false);
const { t } = useTranslation();

useEffect(() => {
const storedToken = getToken();
if (storedToken) {
setToken(storedToken);
}
}, []);

const handleSave = () => {
if (!token.trim()) {
message.error(t('github_token_error_empty'));
return;
}
saveToken(token);
message.success(t('github_token_success_save'));
setIsEditing(false);
};

const handleEdit = () => {
setIsEditing(true);
};

const handleTestToken = async () => {
try {
const userData = await githubRequest('/user', {
headers: { Authorization: `Bearer ${token}` }
});
message.success(t('github_token_success_valid', { username: userData.login }));
} catch (error) {
console.error('Token test failed:', error);
message.error(t('github_token_error_invalid'));
}
};

const obfuscateToken = (token: string) => {
if (token.length <= 4) return token;
return `${token[0]}${'*'.repeat(token.length - 2)}${token[token.length - 1]}`;
};

return (
<div className="github-token-options Box">
<div className="Box-header">
<h2 className="Box-title">{t('github_token_configuration')}</h2>
<TooltipTrigger content={t('github_token_tooltip')} />
</div>
<p>{t('github_token_description')}</p>
<div className="collapsible-section">
<div
className="collapsible-header"
onClick={() => setIsCollapsed(!isCollapsed)}
style={{ cursor: 'pointer', display: 'flex', alignItems: 'center' }}
>
<p>{t('github_token_how_to_generate')}</p>
<span style={{ marginLeft: '5px' }}>{isCollapsed ? '▶' : '▼'}</span>
</div>
{!isCollapsed && (
<div className="instructions Box-body">
<ol>
<li>{t('github_token_step1')}</li>
<li>{t('github_token_step2')}</li>
<li>{t('github_token_step3')}</li>
<li>
{t('github_token_step4')}
<ul>
<li><strong>{t('github_token_note')}</strong>: {t('github_token_note_description')}</li>
<li><strong>{t('github_token_expiration')}</strong>: {t('github_token_expiration_description')}</li>
<li><strong>{t('github_token_scopes')}</strong>: {t('github_token_scopes_description')}</li>
</ul>
</li>
<li>{t('github_token_step5')}</li>
<li>{t('github_token_step6')}</li>
</ol>
</div>
)}
</div>
<div style={{ marginBottom: '10px' }} id="message-container"></div> {/* Container for displaying messages */}
<div style={{ display: 'flex', alignItems: 'center' }}>
<input
type="text"
value={isEditing ? token : obfuscateToken(token)}
onChange={(e) => setToken(e.target.value)}
placeholder={t('github_token_placeholder')}
style={{ marginRight: '10px', flex: 1 }}
disabled={!isEditing}
/>
{isEditing ? (
<button onClick={handleSave} style={{ marginRight: '10px', marginTop: '17px' }}>{t('github_token_save')}</button>
) : (
<button onClick={handleEdit} style={{ marginRight: '10px', marginTop: '17px' }}>{t('github_token_edit')}</button>
)}
<button onClick={handleTestToken} style={{ marginTop: '17px' }}>{t('github_token_test')}</button>
</div>
</div>
);
};

export default GitHubToken;



0 comments on commit b4a25ed

Please sign in to comment.