-
Notifications
You must be signed in to change notification settings - Fork 826
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(project): support bind github (#1576)
* feat(project): support bind github * update binding github callback uri * update styles
- Loading branch information
Showing
13 changed files
with
237 additions
and
24 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
93 changes: 93 additions & 0 deletions
93
desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/binding/GitHub.tsx
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,93 @@ | ||
import { shell } from "electron"; | ||
import { v4 } from "uuid"; | ||
import classNames from "classnames"; | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import { message, Modal } from "antd"; | ||
import { useTranslation } from "react-i18next"; | ||
import { GithubFilled } from "@ant-design/icons"; | ||
|
||
import { GlobalStore } from "../../../../stores/global-store"; | ||
import { useSafePromise } from "../../../../utils/hooks/lifecycle"; | ||
import { | ||
bindingProcess, | ||
LoginPlatform, | ||
removeBinding, | ||
setBindingAuthUUID, | ||
} from "../../../../api-middleware/flatServer"; | ||
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants"; | ||
import { getGithubURL } from "../../../LoginPage/githubLogin"; | ||
import { errorTips } from "../../../../components/Tips/ErrorTips"; | ||
|
||
export interface BindGitHubProps { | ||
isBind: boolean; | ||
onRefresh: () => void; | ||
globalStore: GlobalStore; | ||
} | ||
|
||
export const BindGitHub: React.FC<BindGitHubProps> = ({ isBind, onRefresh, globalStore }) => { | ||
const sp = useSafePromise(); | ||
const { t } = useTranslation(); | ||
const [authUUID, setAuthUUID] = useState(""); | ||
|
||
const cancel = useCallback((): void => { | ||
setAuthUUID(""); | ||
onRefresh(); | ||
}, [onRefresh]); | ||
|
||
useEffect(() => { | ||
let timer = NaN; | ||
async function waitUntilBindFinish(): Promise<void> { | ||
const result = await sp(bindingProcess(authUUID)); | ||
if (result.processing) { | ||
timer = window.setTimeout(waitUntilBindFinish, 2000); | ||
} else { | ||
if (!result.status) { | ||
message.info(t("bind-wechat-failed")); | ||
} | ||
cancel(); | ||
} | ||
} | ||
if (authUUID) { | ||
waitUntilBindFinish(); | ||
return () => { | ||
Number.isNaN(timer) || window.clearTimeout(timer); | ||
}; | ||
} | ||
return; | ||
}, [authUUID, cancel, onRefresh, sp, t]); | ||
|
||
const bindGitHub = async (): Promise<void> => { | ||
const authUUID = v4(); | ||
setAuthUUID(authUUID); | ||
await sp(setBindingAuthUUID(authUUID)); | ||
void shell.openExternal(getGithubURL(authUUID, FLAT_SERVER_USER_BINDING.GITHUB_CALLBACK)); | ||
}; | ||
|
||
const unbind = (): void => { | ||
Modal.confirm({ | ||
content: t("unbind-confirm"), | ||
onOk: async () => { | ||
try { | ||
const { token } = await sp(removeBinding(LoginPlatform.Github)); | ||
globalStore.updateUserToken(token); | ||
onRefresh(); | ||
message.info(t("unbind-success")); | ||
} catch (err) { | ||
errorTips(err); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<span | ||
className={classNames("binding-github", { | ||
"is-bind": isBind, | ||
})} | ||
title={isBind ? t("is-bind") : t("not-bind")} | ||
onClick={isBind ? unbind : bindGitHub} | ||
> | ||
<GithubFilled style={{ color: "#fff" }} /> | ||
</span> | ||
); | ||
}; |
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
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
92 changes: 92 additions & 0 deletions
92
web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/binding/GitHub.tsx
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,92 @@ | ||
import { v4 } from "uuid"; | ||
import classNames from "classnames"; | ||
import React, { useCallback, useEffect, useState } from "react"; | ||
import { message, Modal } from "antd"; | ||
import { useTranslation } from "react-i18next"; | ||
import { GithubFilled } from "@ant-design/icons"; | ||
|
||
import { GlobalStore } from "../../../../stores/GlobalStore"; | ||
import { useSafePromise } from "../../../../utils/hooks/lifecycle"; | ||
import { | ||
bindingProcess, | ||
LoginPlatform, | ||
removeBinding, | ||
setBindingAuthUUID, | ||
} from "../../../../api-middleware/flatServer"; | ||
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants"; | ||
import { getGithubURL } from "../../../LoginPage/githubLogin"; | ||
import { errorTips } from "../../../../components/Tips/ErrorTips"; | ||
|
||
export interface BindGitHubProps { | ||
isBind: boolean; | ||
onRefresh: () => void; | ||
globalStore: GlobalStore; | ||
} | ||
|
||
export const BindGitHub: React.FC<BindGitHubProps> = ({ isBind, onRefresh, globalStore }) => { | ||
const sp = useSafePromise(); | ||
const { t } = useTranslation(); | ||
const [authUUID, setAuthUUID] = useState(""); | ||
|
||
const cancel = useCallback((): void => { | ||
setAuthUUID(""); | ||
onRefresh(); | ||
}, [onRefresh]); | ||
|
||
useEffect(() => { | ||
let timer = NaN; | ||
async function waitUntilBindFinish(): Promise<void> { | ||
const result = await sp(bindingProcess(authUUID)); | ||
if (result.processing) { | ||
timer = window.setTimeout(waitUntilBindFinish, 2000); | ||
} else { | ||
if (!result.status) { | ||
message.info(t("bind-wechat-failed")); | ||
} | ||
cancel(); | ||
} | ||
} | ||
if (authUUID) { | ||
waitUntilBindFinish(); | ||
return () => { | ||
Number.isNaN(timer) || window.clearTimeout(timer); | ||
}; | ||
} | ||
return; | ||
}, [authUUID, cancel, onRefresh, sp, t]); | ||
|
||
const bindGitHub = async (): Promise<void> => { | ||
const authUUID = v4(); | ||
setAuthUUID(authUUID); | ||
await sp(setBindingAuthUUID(authUUID)); | ||
void window.open(getGithubURL(authUUID, FLAT_SERVER_USER_BINDING.GITHUB_CALLBACK)); | ||
}; | ||
|
||
const unbind = (): void => { | ||
Modal.confirm({ | ||
content: t("unbind-confirm"), | ||
onOk: async () => { | ||
try { | ||
const { token } = await sp(removeBinding(LoginPlatform.Github)); | ||
globalStore.updateUserToken(token); | ||
onRefresh(); | ||
message.info(t("unbind-success")); | ||
} catch (err) { | ||
errorTips(err); | ||
} | ||
}, | ||
}); | ||
}; | ||
|
||
return ( | ||
<span | ||
className={classNames("binding-github", { | ||
"is-bind": isBind, | ||
})} | ||
title={isBind ? t("is-bind") : t("not-bind")} | ||
onClick={isBind ? unbind : bindGitHub} | ||
> | ||
<GithubFilled style={{ color: "#fff" }} /> | ||
</span> | ||
); | ||
}; |
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
Oops, something went wrong.