From 819fc17faf3093d35313ac848cf12c040946798e Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 12 Nov 2019 23:36:17 +0800 Subject: [PATCH] refactor: access token and intelligent toggle --- src/components/SettingsBar.tsx | 91 ++++++++++++---------------------- src/components/SideBar.tsx | 52 +++++++++++++++---- src/driver/core/SideBar.ts | 9 +++- 3 files changed, 82 insertions(+), 70 deletions(-) diff --git a/src/components/SettingsBar.tsx b/src/components/SettingsBar.tsx index 98ac7e1..3d96cd6 100644 --- a/src/components/SettingsBar.tsx +++ b/src/components/SettingsBar.tsx @@ -1,9 +1,8 @@ -import { raiseError } from 'analytics' import { Icon } from 'components/Icon' import { useConfigs } from 'containers/ConfigsContext' import { oauth, VERSION } from 'env' import * as React from 'react' -import { friendlyFormatShortcut, JSONRequest, parseURLSearch } from 'utils/general' +import { friendlyFormatShortcut } from 'utils/general' import { useStates } from 'utils/hooks' import * as keyHelper from 'utils/keyHelper' import { SimpleField, SimpleFieldInput } from './MoreOption' @@ -60,19 +59,21 @@ function SettingsBarContent() { const useToggleShowSideBarShortcut = useStates(configContext.val.shortcut) const useReloadHint = useStates('') - React.useEffect(() => { - if (!configContext.val.access_token) { - trySetUpAccessTokenWithCode().then(accessToken => { - useAccessToken.set(accessToken) - saveToken('') - }) - } - }, []) + const { val: accessTokenHint } = useAccessTokenHint + const { val: toggleShowSideBarShortcut } = useToggleShowSideBarShortcut + const { val: shortcutHint } = useShortcutHint + const { val: accessToken } = useAccessToken + const { val: reloadHint } = useReloadHint React.useEffect(() => { useToggleShowSideBarShortcut.set(configContext.val.shortcut) }, [configContext.val.shortcut]) + React.useEffect(() => { + // clear input when access token updates + useAccessToken.set('') + }, [configContext.val.access_token]) + const onInputAccessToken = React.useCallback( ({ currentTarget: { value } }: React.FormEvent) => { useAccessToken.set(value) @@ -87,28 +88,25 @@ function SettingsBarContent() { if (key === 'Enter') saveToken() }, []) - const saveToken = React.useCallback(async (hint?: typeof useAccessTokenHint.val) => { - const { val: accessToken } = useAccessToken - if (accessToken) { - configContext.set({ access_token: accessToken }) - useAccessToken.set('') - useAccessTokenHint.set( - hint || ( - - window.location.reload()}> - Reload - {' '} - to activate! - - ), - ) - } - }, []) - - const clearToken = React.useCallback(async () => { - configContext.set({ access_token: '' }) - useAccessToken.set('') - }, []) + const saveToken = React.useCallback( + async (hint?: typeof useAccessTokenHint.val) => { + if (accessToken) { + configContext.set({ access_token: accessToken }) + useAccessToken.set('') + useAccessTokenHint.set( + hint || ( + + window.location.reload()}> + Reload + {' '} + to activate! + + ), + ) + } + }, + [accessToken], + ) const saveShortcut = React.useCallback(async () => { const { val: toggleShowSideBarShortcut } = useToggleShowSideBarShortcut @@ -140,12 +138,6 @@ function SettingsBarContent() { [], ) - const { val: accessTokenHint } = useAccessTokenHint - const { val: toggleShowSideBarShortcut } = useToggleShowSideBarShortcut - const { val: shortcutHint } = useShortcutHint - const { val: accessToken } = useAccessToken - const { val: reloadHint } = useReloadHint - return ( <>

Settings

@@ -182,7 +174,7 @@ function SettingsBarContent() { onKeyPress={onPressAccessToken} /> {hasAccessToken && !accessToken ? ( - ) : ( @@ -264,24 +256,3 @@ export function SettingsBar(props: Props) { ) } - -async function trySetUpAccessTokenWithCode() { - try { - const search = parseURLSearch() - if ('code' in search) { - const res = await JSONRequest('https://github.com/login/oauth/access_token', { - code: search.code, - client_id: oauth.clientId, - client_secret: oauth.clientSecret, - }) - const { access_token: accessToken, scope } = res - if (scope !== 'repo' || !accessToken) { - throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`) - } - window.history.pushState({}, 'removed code', window.location.pathname.replace(/#.*$/, '')) - return accessToken - } - } catch (err) { - raiseError(err) - } -} diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 3a303c5..e4eae82 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -1,30 +1,43 @@ +import { raiseError } from 'analytics' import { FileExplorer } from 'components/FileExplorer' import { MetaBar } from 'components/MetaBar' import { Portal } from 'components/Portal' import { Resizable } from 'components/Resizable' import { SettingsBar } from 'components/SettingsBar' import { ToggleShowButton } from 'components/ToggleShowButton' +import { useConfigs } from 'containers/ConfigsContext' import { connect } from 'driver/connect' import { SideBarCore } from 'driver/core' import { ConnectorState, Props } from 'driver/core/SideBar' +import { oauth } from 'env' import * as React from 'react' import { cx } from 'utils/cx' +import { JSONRequest, parseURLSearch } from 'utils/general' const RawGitako: React.FC = function RawGitako(props) { + const configContext = useConfigs() + const accessToken = props.configContext.val.access_token + React.useEffect(() => { - const { init, useListeners } = props - init() + const { init } = props + ;(async function() { + if (!accessToken) { + const accessToken = await trySetUpAccessTokenWithCode() + configContext.set({ access_token: accessToken }) + } + init() + })() + }, []) + + React.useEffect(() => { + const { useListeners } = props useListeners(true) return () => useListeners(false) }, []) - const accessToken = props.configContext.val.access_token - React.useEffect(() => { - if (accessToken) { - // reload when setting new accessToken - if (accessToken) props.init() - } - }, [accessToken, props.init]) + // reload when setting new accessToken + // special way to implement didUpdate + React.useEffect(() => () => props.init(), [accessToken]) const { errorDueToAuth, @@ -99,3 +112,24 @@ function renderAccessDeniedError() { ) } + +async function trySetUpAccessTokenWithCode() { + try { + const search = parseURLSearch() + if ('code' in search) { + const res = await JSONRequest('https://github.com/login/oauth/access_token', { + code: search.code, + client_id: oauth.clientId, + client_secret: oauth.clientSecret, + }) + const { access_token: accessToken, scope } = res + if (scope !== 'repo' || !accessToken) { + throw new Error(`Cannot resolve token response: '${JSON.stringify(res)}'`) + } + window.history.pushState({}, 'removed code', window.location.pathname.replace(/#.*$/, '')) + return accessToken + } + } catch (err) { + raiseError(err) + } +} diff --git a/src/driver/core/SideBar.ts b/src/driver/core/SideBar.ts index 6de60da..2a594d1 100644 --- a/src/driver/core/SideBar.ts +++ b/src/driver/core/SideBar.ts @@ -194,8 +194,15 @@ export const onKeyDown: BoundMethodCreator<[KeyboardEvent]> = dispatch => e => { } export const toggleShowSideBar: BoundMethodCreator = dispatch => () => { - const [{ shouldShow }] = dispatch.get() + const [{ shouldShow }, { configContext }] = dispatch.get() dispatch.call(setShouldShow, !shouldShow) + + const { + val: { intelligentToggle }, + } = configContext + if (intelligentToggle !== null) { + configContext.set({ intelligentToggle: !shouldShow }) + } } export const setShouldShow: BoundMethodCreator<