fix: pjax messed history up

This commit is contained in:
EnixCoda 2020-11-24 19:27:26 +08:00
parent 48174507e2
commit 17c4becff2
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
3 changed files with 20 additions and 25 deletions

View file

@ -19,7 +19,8 @@ import * as React from 'react'
import { useUpdateEffect } from 'react-use'
import { cx } from 'utils/cx'
import { parseURLSearch } from 'utils/general'
import { useOnPJAXDone, usePJAX } from 'utils/hooks/usePJAX'
import { loadWithPJAX, useOnPJAXDone, usePJAX } from 'utils/hooks/usePJAX'
import { useProgressBar } from 'utils/hooks/useProgressBar'
import * as keyHelper from 'utils/keyHelper'
import { Icon } from './Icon'
@ -94,7 +95,8 @@ const RawGitako: React.FC<Props & ConnectorState> = function RawGitako(props) {
props.init()
}, [accessToken || '']) // '' prevents duplicated requests
const loadWithPJAX = usePJAX()
usePJAX()
useProgressBar()
const {
errorDueToAuth,

View file

@ -1,14 +1,11 @@
import { Config, Pjax } from 'pjax-api'
import * as React from 'react'
import { useEvent } from 'react-use'
import { useProgressBar } from './useProgressBar'
const config: Config = {
areas: [
// github
'.repository-content',
'[data-pjax="#js-repo-pjax-container"]',
'.page-content',
// gitee
'#git-project-content',
],
@ -20,6 +17,7 @@ const config: Config = {
return path
},
},
link: 'a:not(a)', // this helps fixing the go-back-in-history issue
form: 'form:not(form)', // prevent blocking form submissions
fallback(target, reason) {
// prevent unexpected reload
@ -36,14 +34,10 @@ export function usePJAX() {
},
})
}, [])
}
const progressBar = useProgressBar()
useEvent('pjax:fetch', progressBar.mount, window)
useEvent('pjax:unload', progressBar.unmount, window)
return React.useCallback(url => {
Pjax.assign(url, config)
}, [])
export const loadWithPJAX = (url: string) => {
Pjax.assign(url, config)
}
export function useOnPJAXDone(callback: () => void, both = true) {

View file

@ -1,22 +1,21 @@
import * as NProgress from 'nprogress'
import * as React from 'react'
import { useEvent } from 'react-use'
const progressBar = {
mount() {
NProgress.start()
},
unmount() {
NProgress.done()
},
}
export function useProgressBar() {
const [progressBar] = React.useState(() => {
return {
mount() {
NProgress.start()
},
unmount() {
NProgress.done()
},
}
})
React.useEffect(() => {
NProgress.configure({ showSpinner: false })
}, [])
return progressBar
useEvent('pjax:fetch', progressBar.mount, window)
useEvent('pjax:unload', progressBar.unmount, window)
}