From f6675e8163b20152d1d850e5a3eebae618b70c72 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Mon, 4 Jul 2022 21:24:24 +0800 Subject: [PATCH] fix: handle profile repositories <-> repository redirecting --- src/components/SideBar.tsx | 67 +++++++++++++--------------------- src/containers/RepoContext.tsx | 10 +++-- src/content.tsx | 11 ++---- src/global.d.ts | 5 ++- src/platforms/GitHub/index.ts | 19 +++++++++- src/platforms/index.ts | 16 ++++---- src/platforms/platform.d.ts | 1 + 7 files changed, 69 insertions(+), 60 deletions(-) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 24f79d9..5ae23c7 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -25,6 +25,7 @@ import { SettingsBarContent } from './settings/SettingsBar' export function SideBar() { const state = useLoadedContext(SideBarStateContext).value + const error = useLoadedContext(SideBarErrorContext).value const configContext = useConfigs() const { sideBarWidth } = configContext.value @@ -59,17 +60,18 @@ export function SideBar() { : false : intelligentToggle, ) - const shouldShow = $shouldShow.value - const toggleBodyIndent = React.useCallback(() => { - if (sidebarToggleMode === 'persistent') { - DOMHelper.setBodyIndent(shouldShow) - } else { - DOMHelper.setBodyIndent(false) - } + // Lock false on error + const setShowSideBar = $shouldShow.onChange - if (shouldShow) { - DOMHelper.focusFileExplorer() // TODO: verify if it works - } + const shouldShow = React.useMemo( + () => !error && state !== 'disabled' && $shouldShow.value, + [error, state, $shouldShow.value], + ) + + const toggleBodyIndent = React.useCallback(() => { + DOMHelper.setBodyIndent(sidebarToggleMode === 'persistent' ? shouldShow : false) + + if (shouldShow) DOMHelper.focusFileExplorer() // TODO: verify if it works }, [shouldShow, sidebarToggleMode]) React.useEffect(() => { @@ -85,24 +87,7 @@ export function SideBar() { } }, [shouldShow, intelligentToggle]) // eslint-disable-line react-hooks/exhaustive-deps - const error = useLoadedContext(SideBarErrorContext).value - // Lock shouldShow on error - React.useEffect(() => { - if (error && shouldShow) { - $shouldShow.onChange(false) - } - }, [error]) // eslint-disable-line react-hooks/exhaustive-deps - - const setShowSideBar = React.useCallback( - (show: boolean) => { - if (!error) $shouldShow.onChange(show) - }, - [error], // eslint-disable-line react-hooks/exhaustive-deps - ) - - const toggleShowSideBar = React.useCallback(() => { - if (!error) $shouldShow.onChange(show => !show) - }, [error]) // eslint-disable-line react-hooks/exhaustive-deps + const toggleShowSideBar = React.useCallback(() => setShowSideBar(show => !show), [setShowSideBar]) useToggleSideBarWithKeyboard(state, toggleShowSideBar) const updateSideBarVisibility = React.useCallback(() => { @@ -126,22 +111,24 @@ export function SideBar() { } }, [hideSidebarOnInvalidToken, setShowSideBar]) + if (state === 'disabled') return null + return ( + + setShowSideBar(true) : undefined} + onClick={toggleShowSideBar} + /> +
- - setShowSideBar(true) : undefined} - onClick={toggleShowSideBar} - /> - setShowSideBar(false) : undefined} @@ -183,8 +170,6 @@ export function SideBar() {
{run(() => { switch (state) { - case 'disabled': - return null case 'getting-access-token': return case 'after-getting-access-token': diff --git a/src/containers/RepoContext.tsx b/src/containers/RepoContext.tsx index 6d64fdf..5778973 100644 --- a/src/containers/RepoContext.tsx +++ b/src/containers/RepoContext.tsx @@ -27,9 +27,9 @@ function resolvePartialMetaData() { repoName, type: type === 'pull' ? type : undefined, } - } else { - return partialMetaData } + + return null } function usePartialMetaData(): PartialMetaData | null { @@ -75,8 +75,10 @@ function useDefaultBranch(partialMetaData: PartialMetaData | null) { catchNetworkError(async () => { if (!partialMetaData) return $state.onChange('meta-loading') + const { userName, repoName } = partialMetaData + if (!userName || !repoName) return - const defaultBranch = await platform.getDefaultBranchName(partialMetaData, accessToken) + const defaultBranch = await platform.getDefaultBranchName({ userName, repoName }, accessToken) $defaultBranch.onChange(defaultBranch) }) }, [partialMetaData, accessToken]) // eslint-disable-line react-hooks/exhaustive-deps @@ -94,6 +96,8 @@ function useMetaData( React.useEffect(() => { if (partialMetaData && defaultBranchName && theBranch) { const { userName, repoName } = partialMetaData + if (!userName || !repoName) return + const safeMetaData: MetaData = { userName, repoName, diff --git a/src/content.tsx b/src/content.tsx index d0f37e1..200742f 100644 --- a/src/content.tsx +++ b/src/content.tsx @@ -1,16 +1,13 @@ import { Gitako } from 'components/Gitako' -import { platform } from 'platforms' import * as React from 'react' import { createRoot } from 'react-dom/client' import { insertSideBarMountPoint, persistGitakoElements } from 'utils/DOMHelper' import './content.scss' -if (platform.resolvePartialMetaData()) { - if (document.readyState === 'loading') { - document.addEventListener('DOMContentLoaded', init) - } else { - init() - } +if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', init) +} else { + init() } async function init() { diff --git a/src/global.d.ts b/src/global.d.ts index a47db99..add2cd6 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -8,7 +8,10 @@ type MetaData = { type?: EnumString<'tree' | 'blob' | 'pull' | 'commit'> } -type PartialMetaData = Omit, 'defaultBranchName'> +type PartialMetaData = Omit< + MakeOptional, + 'defaultBranchName' +> type TreeNode = { name: string diff --git a/src/platforms/GitHub/index.ts b/src/platforms/GitHub/index.ts index a99e5c0..fea3c92 100644 --- a/src/platforms/GitHub/index.ts +++ b/src/platforms/GitHub/index.ts @@ -2,6 +2,7 @@ import { useConfigs } from 'containers/ConfigsContext' import { GITHUB_OAUTH } from 'env' import { Base64 } from 'js-base64' import { configRef } from 'utils/config/helper' +import { $ } from 'utils/DOMHelper' import { resolveGitModules } from 'utils/gitSubmodule' import { sortFoldersToFront } from 'utils/treeParser' import * as API from './API' @@ -82,12 +83,28 @@ function getUrlForRedirect( } export function isEnterprise() { - return !window.location.host.endsWith('github.com') + return ( + (window.location.host !== 'github.com' && + /** + * + * Enterprise + * + */ + $('a.Header-link[aria-label="Homepage Enterprise"]', e => e.textContent === 'Enterprise')) || + false + ) } const pathSHAMap = new Map() export const GitHub: Platform = { + shouldActivate() { + return ( + window.location.host === 'github.com' || + // + !!document.querySelector('link[rel="fluid-icon"][title="GitHub"]') + ) + }, isEnterprise, resolvePartialMetaData() { if (!DOMHelper.isInRepoPage()) { diff --git a/src/platforms/index.ts b/src/platforms/index.ts index a29a355..cc85789 100644 --- a/src/platforms/index.ts +++ b/src/platforms/index.ts @@ -5,16 +5,18 @@ import { Gitee } from './Gitee' import { GitHub } from './GitHub' const platforms = { - GitHub: GitHub, - Gitee: Gitee, - Gitea: Gitea, + GitHub, + Gitee, + Gitea, } function resolvePlatform() { - for (const platform of Object.values(platforms)) { - if (platform.resolvePartialMetaData()) return platform - } - return dummyPlatformForTypeSafety + return ( + forOf(platforms, (platformName, platform) => { + const { shouldActivate = () => !!platform.resolvePartialMetaData() } = platform + if (shouldActivate()) return platform + }) || dummyPlatformForTypeSafety + ) } function getPlatformName() { diff --git a/src/platforms/platform.d.ts b/src/platforms/platform.d.ts index 75a2c1d..c7b1dc0 100644 --- a/src/platforms/platform.d.ts +++ b/src/platforms/platform.d.ts @@ -1,4 +1,5 @@ type Platform = { + shouldActivate?(): boolean isEnterprise(): boolean // branch name might not be available when resolving from DOM and URL resolvePartialMetaData(): PartialMetaData | null