From 8072e385f8fbbf848ce39da9748d7ea3da91da13 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 23 Aug 2022 01:42:07 +0800 Subject: [PATCH] enhance: OAuth workflow --- .../settings/AccessTokenSettings.tsx | 85 +++++++++++++++++-- 1 file changed, 79 insertions(+), 6 deletions(-) diff --git a/src/components/settings/AccessTokenSettings.tsx b/src/components/settings/AccessTokenSettings.tsx index 896157c..d683eb3 100644 --- a/src/components/settings/AccessTokenSettings.tsx +++ b/src/components/settings/AccessTokenSettings.tsx @@ -1,10 +1,13 @@ -import { Button, Text, TextInput } from '@primer/react' +import { Box, Button, Spinner, Text, TextInput } from '@primer/react' import { wikiLinks } from 'components/settings/SettingsBar' import { useConfigs } from 'containers/ConfigsContext' +import { SideBarStateContext } from 'containers/SideBarState' import { platform } from 'platforms' import { Gitea } from 'platforms/Gitea' import { Gitee } from 'platforms/Gitee' import * as React from 'react' +import { IIFC } from 'react-iifc' +import { useLoadedContext } from 'utils/hooks/useLoadedContext' import { useStateIO } from 'utils/hooks/useStateIO' import { SettingsSection } from './SettingsSection' @@ -17,6 +20,7 @@ export function AccessTokenSettings() { const [accessTokenInputValue, setAccessTokenInputValue] = React.useState('') const useAccessTokenHint = useStateIO('') const focusInput = useStateIO(false) + const sidebarState = useLoadedContext(SideBarStateContext).value const { value: accessTokenHint } = useAccessTokenHint @@ -78,11 +82,56 @@ export function AccessTokenSettings() { } > - {hasAccessToken ? ( -
- Your token has been saved. - -
+ {sidebarState === 'getting-access-token' ? ( + + + Getting access token + + ) : hasAccessToken ? ( + + {() => { + const [showConfirmButton, setShowConfirmButton] = React.useState(false) + return ( + + {showConfirmButton ? ( + + {() => { + const [allowClear, setAllowClear] = React.useState(false) + const waitForSeconds = 3 + const timePast = useTimePast(1000, waitForSeconds * 1000) + React.useEffect(() => { + const timeout = setTimeout(() => setAllowClear(true), waitForSeconds * 1000) + return () => clearTimeout(timeout) + }, []) + const countDown = waitForSeconds - timePast + + return ( + + Are you sure to clear the token? + + + + + + ) + }} + + ) : ( + + Your token has been saved. + + + )} + + ) + }} + ) : (
{platform === Gitea ? ( @@ -127,3 +176,27 @@ export function AccessTokenSettings() { ) } + +function useTimePast(unit = 1000, max?: number) { + const [timePast, setTimePast] = React.useState(0) + React.useEffect(() => { + const checkInterval = (unit / 10) >> 0 // 10x check times for better accuracy + const start = Date.now() + let memoLastValue = 0 + const interval = setInterval(() => { + const now = Date.now() + const pastInMilliseconds = now - start + const pastInSeconds = (pastInMilliseconds / 1000) >> 0 + if (pastInSeconds !== memoLastValue) { + setTimePast(pastInSeconds) + memoLastValue = pastInSeconds + + if (max && pastInMilliseconds >= max) clearInterval(interval) + } + }, checkInterval) + + return () => clearInterval(interval) + }, [unit, max]) + + return timePast +}