From 5e5b54132e24ed1ad382b0bd66c5a325823e2af2 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Sun, 29 Mar 2020 15:48:23 +0800 Subject: [PATCH] feat: platform getOAuthLink --- .../settings/AccessTokenSettings.tsx | 10 ++-- src/components/settings/FileTreeSettings.tsx | 2 +- src/components/{ => settings}/SettingsBar.tsx | 60 +++++++++++-------- src/platforms/GitHub/index.ts | 6 ++ src/platforms/Gitee/index.ts | 8 ++- src/platforms/platform.d.ts | 3 +- 6 files changed, 54 insertions(+), 35 deletions(-) rename src/components/{ => settings}/SettingsBar.tsx (63%) diff --git a/src/components/settings/AccessTokenSettings.tsx b/src/components/settings/AccessTokenSettings.tsx index 9846326..6986f0f 100644 --- a/src/components/settings/AccessTokenSettings.tsx +++ b/src/components/settings/AccessTokenSettings.tsx @@ -1,7 +1,7 @@ import { Button, TextInput } from '@primer/components' -import { wikiLinks } from 'components/SettingsBar' +import { wikiLinks } from 'components/settings/SettingsBar' import { useConfigs } from 'containers/ConfigsContext' -import { GITHUB_OAUTH } from 'env' +import { usePlatform } from 'containers/PlatformContext' import * as React from 'react' import { useStates } from 'utils/hooks/useStates' import { SettingsSection } from './SettingsSection' @@ -16,6 +16,7 @@ export function AccessTokenSettings(props: React.PropsWithChildren) { const useAccessToken = useStates('') const useAccessTokenHint = useStates('') const focusInput = useStates(false) + const platform = usePlatform() const { val: accessTokenHint } = useAccessTokenHint const { val: accessToken } = useAccessToken @@ -78,10 +79,7 @@ export function AccessTokenSettings(props: React.PropsWithChildren) { className={'link-button'} onClick={() => { // use js here to make sure redirect_uri is latest url - const url = `https://github.com/login/oauth/authorize?client_id=${ - GITHUB_OAUTH.clientId - }&scope=repo&redirect_uri=${encodeURIComponent(window.location.href)}` - window.location.href = url + window.location.href = platform.getOAuthLink() }} > Create with OAuth (recommended) diff --git a/src/components/settings/FileTreeSettings.tsx b/src/components/settings/FileTreeSettings.tsx index f47ea61..abef626 100644 --- a/src/components/settings/FileTreeSettings.tsx +++ b/src/components/settings/FileTreeSettings.tsx @@ -1,4 +1,4 @@ -import { wikiLinks } from 'components/SettingsBar' +import { wikiLinks } from 'components/settings/SettingsBar' import { SimpleToggleField } from 'components/SimpleToggleField' import { useConfigs } from 'containers/ConfigsContext' import * as React from 'react' diff --git a/src/components/SettingsBar.tsx b/src/components/settings/SettingsBar.tsx similarity index 63% rename from src/components/SettingsBar.tsx rename to src/components/settings/SettingsBar.tsx index 3a966b0..ceb222a 100644 --- a/src/components/SettingsBar.tsx +++ b/src/components/settings/SettingsBar.tsx @@ -1,13 +1,15 @@ import { Link } from '@primer/components' import { Icon } from 'components/Icon' +import { usePlatform } from 'containers/PlatformContext' import { VERSION } from 'env' +import { GitHub } from 'platforms/GitHub' import * as React from 'react' import { useStates } from 'utils/hooks/useStates' -import { AccessTokenSettings } from './settings/AccessTokenSettings' -import { FileTreeSettings } from './settings/FileTreeSettings' -import { SettingsSection } from './settings/SettingsSection' -import { SidebarSettings } from './settings/SidebarSettings' -import { SimpleField, SimpleToggleField } from './SimpleToggleField' +import { SimpleField, SimpleToggleField } from '../SimpleToggleField' +import { AccessTokenSettings } from './AccessTokenSettings' +import { FileTreeSettings } from './FileTreeSettings' +import { SettingsSection } from './SettingsSection' +import { SidebarSettings } from './SidebarSettings' const WIKI_HOME_LINK = 'https://github.com/EnixCoda/Gitako/wiki' export const wikiLinks = { @@ -23,23 +25,27 @@ type Props = { toggleShowSettings: () => void } -const moreFields: SimpleField[] = [ - { - key: 'copyFileButton', - label: 'Copy file shortcut', - wikiLink: wikiLinks.copyFileButton, - }, - { - key: 'copySnippetButton', - label: 'Copy snippet shortcut', - wikiLink: wikiLinks.copySnippet, - }, -] - function SettingsBarContent() { const useReloadHint = useStates('') const { val: reloadHint } = useReloadHint + const platform = usePlatform() + const moreFields: SimpleField[] = + platform === GitHub + ? [ + { + key: 'copyFileButton', + label: 'Copy file shortcut', + wikiLink: wikiLinks.copyFileButton, + }, + { + key: 'copySnippetButton', + label: 'Copy snippet shortcut', + wikiLink: wikiLinks.copySnippet, + }, + ] + : [] + return ( <>

Settings

@@ -48,15 +54,17 @@ function SettingsBarContent() { - - {moreFields.map(field => ( - - - - ))} + {moreFields.length > 0 && ( + + {moreFields.map(field => ( + + + + ))} - {reloadHint &&
{reloadHint}
} -
+ {reloadHint &&
{reloadHint}
} +
+ )} Report bug / Request feature. diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index 1453b81..4e26ae9 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -1,4 +1,5 @@ import { platform } from 'platforms' +import { GITHUB_OAUTH } from 'env' import * as React from 'react' import { useEvent } from 'react-use' import { bodySpacingClassName } from 'utils/DOMHelper' @@ -155,6 +156,11 @@ export const GitHub: Platform = { return accessToken }, useResizeStylesheets, + getOAuthLink() { + return `https://github.com/login/oauth/authorize?client_id=${ + GITHUB_OAUTH.clientId + }&scope=repo&redirect_uri=${encodeURIComponent(window.location.href)}` + }, } export function useGitHubAttachCopySnippetButton(copySnippetButton: boolean) { diff --git a/src/platforms/Gitee/index.ts b/src/platforms/Gitee/index.ts index c9c46b0..89df05d 100644 --- a/src/platforms/Gitee/index.ts +++ b/src/platforms/Gitee/index.ts @@ -1,4 +1,5 @@ import { platform } from 'platforms' +import { GITEE_OAUTH } from 'env' import * as React from 'react' import { useEvent } from 'react-use' import { bodySpacingClassName } from 'utils/DOMHelper' @@ -104,7 +105,7 @@ export const Gitee: Platform = { userName, repoName, branchName, - userUrl: data?.parent?.url, + userUrl: data?.html_url?.replace(/(.*)\/.*?$/, '$1'), repoUrl: data?.html_url, } @@ -155,6 +156,11 @@ export const Gitee: Platform = { return accessToken }, useResizeStylesheets, + getOAuthLink() { + return `https://gitee.com/oauth/authorize?client_id=${ + GITEE_OAUTH.clientId + }&scope=repo&response_type=code&redirect_uri=${encodeURIComponent(window.location.href)}` + }, } export function useGiteeAttachCopySnippetButton(copySnippetButton: boolean) { diff --git a/src/platforms/platform.d.ts b/src/platforms/platform.d.ts index 6f74eb4..95c6bf5 100644 --- a/src/platforms/platform.d.ts +++ b/src/platforms/platform.d.ts @@ -4,6 +4,7 @@ type Platform = { getTreeData(metaData: MetaData, accessToken?: string): Promise shouldShow(metaData?: Partial): boolean getCurrentPath(branchName: string): string[] | null - setOAuth(code: string): Promise useResizeStylesheets(size: number): void + setOAuth(code: string): Promise + getOAuthLink(): string }