fix: simplify detection of code page

This commit is contained in:
EnixCoda 2020-06-19 00:03:05 +08:00
parent ddf409b326
commit 8ae4834d67
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
6 changed files with 7 additions and 53 deletions

View file

@ -25,8 +25,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
const intelligentToggle = configContext.val.intelligentToggle
React.useEffect(() => {
const shouldShow =
intelligentToggle === null ? platform.shouldShow(props.metaData) : intelligentToggle
const shouldShow = intelligentToggle === null ? platform.shouldShow() : intelligentToggle
props.setShouldShow(shouldShow)
}, [intelligentToggle, props.metaData])
@ -60,11 +59,7 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
const updateSideBarVisibility = React.useCallback(
function updateSideBarVisibility() {
if (configContext.val.intelligentToggle === null) {
props.setShouldShow(
platform.shouldShow({
branchName: props.metaData?.branchName,
}),
)
props.setShouldShow(platform.shouldShow())
}
},
[props.metaData?.branchName, configContext.val.intelligentToggle],

View file

@ -29,26 +29,6 @@ export function isInRepoPage() {
return Boolean(document.querySelector(repoHeaderSelector))
}
// route types related to determining if sidebar should show
const TYPES = {
TREE: 'tree',
BLOB: 'blob',
COMMIT: 'commit',
// known but not related types: issues, pulls, wiki, insight,
// TODO: record more types
}
export function isInCodePage(metaData?: Partial<MetaData>) {
const mergedRepo = { ...parse(), ...metaData }
const { type, branchName } = mergedRepo
return Boolean(
isInRepoPage() &&
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName)),
)
}
function isCommitPath(path: string[]) {
return isCompleteCommitSHA(path[0])
}

View file

@ -135,8 +135,8 @@ export const GitHub: Platform = {
return root
},
shouldShow(metaData) {
return URLHelper.isInCodePage(metaData)
shouldShow() {
return DOMHelper.isInCodePage()
},
getCurrentPath(branchName) {
return URLHelper.getCurrentPath(branchName)

View file

@ -1,5 +1,4 @@
import { raiseError } from 'analytics'
import { isInRepoPage } from './DOMHelper'
export function parse(): Partial<MetaData> & { path: string[] } {
const { pathname } = window.location
@ -25,26 +24,6 @@ export function parseSHA() {
return type === 'blob' || type === 'tree' ? path[0] : undefined
}
// route types related to determining if sidebar should show
const TYPES = {
TREE: 'tree',
BLOB: 'blob',
COMMIT: 'commit',
// known but not related types: issues, pulls, wiki, insight,
// TODO: record more types
}
export function isInCodePage(metaData?: Partial<MetaData>) {
const mergedRepo = { ...parse(), ...metaData }
const { type, branchName } = mergedRepo
return Boolean(
isInRepoPage() &&
(!type || type === TYPES.TREE || type === TYPES.BLOB) &&
type !== TYPES.COMMIT &&
(branchName || (!type && !branchName)),
)
}
function isCommitPath(path: string[]) {
return isCompleteCommitSHA(path[0])
}

View file

@ -131,8 +131,8 @@ export const Gitee: Platform = {
return root
},
shouldShow(metaData) {
return URLHelper.isInCodePage(metaData)
shouldShow() {
return DOMHelper.isInCodePage()
},
getCurrentPath(branchName) {
return URLHelper.getCurrentPath(branchName)

View file

@ -6,7 +6,7 @@ type Platform = {
accessToken?: string,
): Promise<Pick<MetaData, 'userUrl' | 'repoUrl' | 'defaultBranchName'>>
getTreeData(metaData: MetaData, accessToken?: string): Promise<TreeNode>
shouldShow(metaData?: Partial<MetaData>): boolean
shouldShow(): boolean
getCurrentPath(branchName: string): string[] | null
setOAuth(code: string): Promise<string | null>
getOAuthLink(): string