forked from hypertrons/hypertrons-crx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:Added functionality to retrieve and save GitHub token (hypertron…
…s#839) * feat:Added functionality to retrieve and save GitHub token (hypertrons#812) Signed-off-by: wblx <904653630@qq.com> * solved all the problems Signed-off-by: wblx <904653630@qq.com> * sync * Resolve inconsistent margins Signed-off-by: wblx <904653630@qq.com> * response problem Signed-off-by: wblx <904653630@qq.com> * duplicated options Signed-off-by: wblx <904653630@qq.com> * chore: run prettier to format Signed-off-by: Harry <xiaohanwu12@gmail.com> --------- Signed-off-by: wblx <904653630@qq.com> Signed-off-by: Harry <xiaohanwu12@gmail.com>
- Loading branch information
1 parent
6ae48d6
commit d40c396
Showing
9 changed files
with
320 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getToken, saveToken } from '../helpers/github-token'; | ||
|
||
export const githubRequest = async (endpoint: string, options: RequestInit = {}): Promise<any | null> => { | ||
const token = getToken(); | ||
if (!token) { | ||
return null; | ||
} | ||
|
||
try { | ||
const response = await fetch(`https://api.github.com${endpoint}`, { | ||
...options, | ||
}); | ||
return response.json(); | ||
} catch (error) { | ||
return null; | ||
} | ||
}; | ||
|
||
export { saveToken, getToken }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const saveToken = (token: string) => { | ||
localStorage.setItem('github_token', token); | ||
}; | ||
|
||
export const getToken = (): string => { | ||
return localStorage.getItem('github_token') || ''; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import React, { useState, useEffect, useRef } from 'react'; | ||
import TooltipTrigger from '../../../components/TooltipTrigger'; | ||
import { saveToken, getToken, githubRequest } from '../../../api/githubApi'; | ||
import { message } from 'antd'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
const GitHubToken = () => { | ||
const [token, setToken] = useState(''); | ||
const [isCollapsed, setIsCollapsed] = useState(true); | ||
const [isEditing, setIsEditing] = useState(false); | ||
const { t } = useTranslation(); | ||
const inputRef = useRef<HTMLInputElement>(null); | ||
|
||
useEffect(() => { | ||
const storedToken = getToken(); | ||
if (storedToken) { | ||
setToken(storedToken); | ||
} | ||
}, []); | ||
|
||
const handleSave = () => { | ||
if (!token.trim()) { | ||
showMessage(t('github_token_error_empty'), 'error'); | ||
return; | ||
} | ||
saveToken(token); | ||
showMessage(t('github_token_success_save'), 'success'); | ||
setIsEditing(false); | ||
}; | ||
|
||
const handleEdit = () => { | ||
setIsEditing(true); | ||
}; | ||
|
||
const handleTestToken = async () => { | ||
const userData = await githubRequest('/user', { | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}); | ||
|
||
if (userData === null || userData.message) { | ||
showMessage(t('github_token_error_invalid'), 'error'); | ||
} else { | ||
showMessage(t('github_token_success_valid', { username: userData.login }), 'success'); | ||
} | ||
}; | ||
|
||
const obfuscateToken = (token: string): string => { | ||
if (token.length <= 4) return token; | ||
return `${token[0]}${'*'.repeat(token.length - 2)}${token[token.length - 1]}`; | ||
}; | ||
|
||
const showMessage = (content: string, type: 'success' | 'error') => { | ||
if (inputRef.current) { | ||
const rect = inputRef.current.getBoundingClientRect(); | ||
message.config({ | ||
top: rect.top - 50, | ||
duration: 2, | ||
maxCount: 3, | ||
}); | ||
message[type](content); | ||
} | ||
}; | ||
|
||
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> | ||
<div style={{ display: 'flex', alignItems: 'center' }}> | ||
<input | ||
type="text" | ||
ref={inputRef} | ||
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; |
Oops, something went wrong.