From 17c4becff20dbaaff60eb215a6882a5c6c5e7a23 Mon Sep 17 00:00:00 2001 From: EnixCoda Date: Tue, 24 Nov 2020 19:27:26 +0800 Subject: [PATCH] fix: pjax messed history up --- src/components/SideBar.tsx | 6 ++++-- src/utils/hooks/usePJAX.ts | 14 ++++---------- src/utils/hooks/useProgressBar.ts | 25 ++++++++++++------------- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/src/components/SideBar.tsx b/src/components/SideBar.tsx index 5998ddb..58a6dcd 100644 --- a/src/components/SideBar.tsx +++ b/src/components/SideBar.tsx @@ -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 = function RawGitako(props) { props.init() }, [accessToken || '']) // '' prevents duplicated requests - const loadWithPJAX = usePJAX() + usePJAX() + useProgressBar() const { errorDueToAuth, diff --git a/src/utils/hooks/usePJAX.ts b/src/utils/hooks/usePJAX.ts index 9f8d74e..71e0fb3 100644 --- a/src/utils/hooks/usePJAX.ts +++ b/src/utils/hooks/usePJAX.ts @@ -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) { diff --git a/src/utils/hooks/useProgressBar.ts b/src/utils/hooks/useProgressBar.ts index 0e2959a..2b5bd01 100644 --- a/src/utils/hooks/useProgressBar.ts +++ b/src/utils/hooks/useProgressBar.ts @@ -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) }