Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: github sync #68

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
"@babel/preset-react": "^7.24.1",
"@babel/preset-typescript": "^7.24.1",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.13",
"@types/chrome": "^0.0.267",
"@types/chrome": "^0.0.268",
"@types/md5": "^2.3.5",
"@types/react": "^18.3.1",
"@types/react": "^18.3.2",
"@types/react-beautiful-dnd": "^13.1.8",
"@types/react-dom": "^18.3.0",
"@types/uuid": "^9.0.8",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"babel-loader": "^9.1.3",
"clean-webpack-plugin": "^4.0.0",
"copy-webpack-plugin": "12.0.2",
Expand Down Expand Up @@ -96,9 +96,10 @@
"he": "^1.2.0",
"i18next": "^23.11.4",
"libmuse": "git+https://github.com/lovegaoshi/muse.git#apm-release",
"material-ui-confirm": "^3.0.12",
"material-ui-confirm": "^3.0.14",
"md5": "^2.3.0",
"notistack": "^3.0.1",
"octokit": "^4.0.2",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"prop-types": "^15.8.1",
Expand Down
51 changes: 24 additions & 27 deletions src/components/lyric/Lyric.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -111,33 +111,30 @@ export default withStyles(styles)((props: Props) => {
/>
</Grid>
<Grid sx={mStyles.lrcInputGrid} item xs={12} container spacing={0}>
<Grid sx={mStyles.lrcOffsetGrid} item xs={3}>
<TextField
type='number'
variant='outlined'
label='歌词补偿(毫秒)'
InputProps={{
className: classes.inputOffset,
}}
value={lyricOffset}
onChange={onLrcOffsetChange}
/>
</Grid>
<Grid sx={mStyles.lrcSearchGrid} item xs={9}>
<TextField
variant='outlined'
label='歌词搜索'
InputProps={{
className: classes.inputLrc,
}}
InputLabelProps={{
shrink: true,
}}
placeholder={songTitle}
value={songTitle}
onChange={(e) => setSongTitle(e.target.value)}
/>
</Grid>
<TextField
sx={mStyles.lrcOffsetGrid}
type='number'
variant='outlined'
label='歌词补偿(毫秒)'
InputProps={{
className: classes.inputOffset,
}}
value={lyricOffset}
onChange={onLrcOffsetChange}
/>
<TextField
variant='outlined'
label='歌词搜索'
InputProps={{
className: classes.inputLrc,
}}
InputLabelProps={{
shrink: true,
}}
placeholder={songTitle}
value={songTitle}
onChange={(e) => setSongTitle(e.target.value)}
/>
</Grid>

<Grid sx={mStyles.lrcSearchBarGrid} item xs={12}>
Expand Down
14 changes: 12 additions & 2 deletions src/components/setting/SyncSetting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { ExportFavButton, ImportFavButton } from './sync/LocalSyncButton';
import DropboxSyncButton from './sync/DropboxAuth';
import GiteeSyncButton from './sync/GiteeAuth';
import GithubSyncButton from './sync/GithubAuth';

function SyncSetttingButtons() {
const playerSettings = useStore(
Expand Down Expand Up @@ -47,7 +48,7 @@ function SyncSetttingButtons() {
<DropboxSyncButton
sx={AddFavIcon}
restoreFromUint8Array={async (v) => {
await initializeFromSync(v);
initializeFromSync(v);
}}
/>
);
Expand All @@ -56,7 +57,16 @@ function SyncSetttingButtons() {
<GiteeSyncButton
sx={AddFavIcon}
restoreFromUint8Array={async (v) => {
await initializeFromSync(v);
initializeFromSync(v);
}}
/>
);
case SyncOptions.GITHUB:
return (
<GithubSyncButton
sx={AddFavIcon}
restoreFromUint8Array={async (v) => {
initializeFromSync(v);
}}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/setting/sync/GenericSyncButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function ExportSyncFavButton({ noxBackup, login, sx }: ExportPropsR) {
const cloudUpload = async () => {
const exportedDict = await exportStorageRaw();
const response = await noxBackup(exportedDict);
if (response.status === 200) {
if (response.status === 200 || response.status === 201) {
enqueueSnackbar('歌单上传到云端成功!', {
variant: 'success',
autoHideDuration: 4000,
Expand Down
69 changes: 69 additions & 0 deletions src/components/setting/sync/GithubAuth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import {
checkAuthentication,
noxBackup,
noxRestore,
} from '@APM/utils/sync/Github';
import { logger } from '@utils/Logger';
import GenericSyncButton, { GenericPropsR } from './GenericSyncButton';

let authToken = '';
const clientId = process.env.GITHUB_KEY;
const clientSecret = process.env.GITHUB_SECRET;
const redirectURI = chrome.identity.getRedirectURL();

export const getAuth = async (
callback = (_v?: string) => checkAuthentication(authToken).then(console.log),
errorHandling = logger.error,
) =>
chrome.identity.launchWebAuthFlow(
{
url: `https://github.com/login/oauth/authorize?client_id=${clientId}&redirect_uri=${redirectURI}&scope=repo,user,administration:write`,
interactive: true,
},
async (responseUrl) => {
if (responseUrl === undefined) {
errorHandling('no response url returned. auth aborted by user.');
} else {
const authCode = new URL(responseUrl).searchParams.get('code');
const res = await fetch(
`https://github.com/login/oauth/access_token?code=${authCode}&client_id=${clientId}&redirect_uri=${redirectURI}&client_secret=${clientSecret}
`,
{ method: 'POST' },
);
authToken = new URL(
`https://foo.com/bar?${await res.text()}`,
).searchParams.get('access_token')!;
callback(responseUrl);
}
},
);

const login = async (
callback: () => Promise<void> = async () => undefined,
errorCallback = logger.error,
) => {
try {
if (!(await checkAuthentication())) {
logger.debug('github token expired, need to log in');
await getAuth(callback, errorCallback);
} else {
callback();
}
return true;
} catch (e) {
logger.warn('github fail');
errorCallback(e);
return false;
}
};

const GiteeSyncButton = ({ restoreFromUint8Array, sx }: GenericPropsR) =>
GenericSyncButton({
restoreFromUint8Array,
noxBackup: (v) => noxBackup(v, authToken),
noxRestore: () => noxRestore(authToken),
login,
sx,
});

export default GiteeSyncButton;
Loading