fix: catch unexpected PJAX with scope

This commit is contained in:
EnixCoda 2021-04-13 21:49:51 +08:00
parent f026a7dfd2
commit 4b96e01845
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 13 additions and 1 deletions

View file

@ -15,6 +15,7 @@ import { cx } from 'utils/cx'
import { parseURLSearch, run } from 'utils/general'
import { loadWithPJAX, useOnPJAXDone, usePJAX } from 'utils/hooks/usePJAX'
import { useProgressBar } from 'utils/hooks/useProgressBar'
import { useStateIO } from 'utils/hooks/useStateIO'
import * as keyHelper from 'utils/keyHelper'
import { Icon } from './Icon'
import { LoadingIndicator } from './LoadingIndicator'
@ -47,9 +48,14 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
})
}, [])
/**
* Catch unexpected PJAX, force trigger init on scope change.
*/
const pageScope = useStateIO(platform.resolvePageScope?.())
useOnPJAXDone(() => pageScope.onChange(platform.resolvePageScope?.()))
React.useEffect(() => {
props.init()
}, [accessToken])
}, [accessToken, pageScope.value])
React.useEffect(
function attachKeyDown() {

View file

@ -118,6 +118,11 @@ export const GitHub: Platform = {
}
return metaData
},
resolvePageScope() {
if (URLHelper.parse().type === 'releases') return 'releases'
if (URLHelper.isInPullPage()) return 'pull'
return 'general'
},
async getDefaultBranchName({ userName, repoName }, accessToken) {
const data = await API.getRepoMeta(userName, repoName, accessToken)
return data.default_branch

View file

@ -2,6 +2,7 @@ type Platform = {
isEnterprise(): boolean
// branch name might not be available when resolving from DOM and URL
resolveMeta(): MakeOptional<MetaData, 'branchName'> | null
resolvePageScope?(): string
getDefaultBranchName(
metaData: Pick<MetaData, 'userName' | 'repoName'>,
accessToken?: string,