Merge remote-tracking branch 'github/feat/1.1.2/ui' into feat/1.1.2/user-center

This commit is contained in:
LinkinStars 2023-04-18 18:22:09 +08:00
commit 9e0999ba55
2 changed files with 32 additions and 20 deletions

View File

@ -2,7 +2,7 @@ import { FC, ReactNode, useEffect, useState } from 'react';
import { useLocation, useNavigate, useLoaderData } from 'react-router-dom'; import { useLocation, useNavigate, useLoaderData } from 'react-router-dom';
import { floppyNavigation } from '@/utils'; import { floppyNavigation } from '@/utils';
import { TGuardFunc } from '@/utils/guard'; import { TGuardFunc, TGuardResult } from '@/utils/guard';
import RouteErrorBoundary from './RouteErrorBoundary'; import RouteErrorBoundary from './RouteErrorBoundary';
@ -15,11 +15,10 @@ const RouteGuard: FC<{
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const loaderData = useLoaderData(); const loaderData = useLoaderData();
const [grOk, setGrOk] = useState(true); const [gk, setKeeper] = useState<TGuardResult>({
const [routeError, setRouteError] = useState<{ ok: true,
code: string; });
msg: string; const [gkError, setGkError] = useState<TGuardResult['error']>();
}>();
const applyGuard = () => { const applyGuard = () => {
if (typeof onEnter !== 'function') { if (typeof onEnter !== 'function') {
return; return;
@ -30,14 +29,16 @@ const RouteGuard: FC<{
page, page,
}); });
setGrOk(gr.ok); setKeeper(gr);
if (gr.error?.code && /403|404|50X/i.test(gr.error.code.toString())) { if (
setRouteError({ gk.ok === false &&
code: `${gr.error.code}`, gk.error?.code &&
msg: gr.error.msg || '', /403|404|50X/i.test(gk.error.code.toString())
}); ) {
setGkError(gk.error);
return; return;
} }
setGkError(undefined);
if (gr.redirect) { if (gr.redirect) {
floppyNavigation.navigate(gr.redirect, { floppyNavigation.navigate(gr.redirect, {
handler: navigate, handler: navigate,
@ -53,12 +54,22 @@ const RouteGuard: FC<{
*/ */
applyGuard(); applyGuard();
}, [location]); }, [location]);
let asOK = gk.ok;
if (gk.ok === false && gk.redirect) {
/**
* It is possible that the route guard verification fails
* but the current page is already the target page for the route guard jump
* This should render `children`!
*/
asOK = floppyNavigation.equalToCurrentHref(gk.redirect);
}
return ( return (
<> <>
{grOk ? children : null} {asOK ? children : null}
{!grOk && routeError ? ( {gkError ? <RouteErrorBoundary errCode={gkError.code as string} /> : null}
<RouteErrorBoundary errCode={routeError.code} />
) : null}
</> </>
); );
}; };

View File

@ -5,10 +5,10 @@ import Storage from '@/utils/storage';
import { REDIRECT_PATH_STORAGE_KEY } from '@/common/constants'; import { REDIRECT_PATH_STORAGE_KEY } from '@/common/constants';
import { getLoginUrl } from '@/utils/userCenter'; import { getLoginUrl } from '@/utils/userCenter';
const differentCurrent = (target: string, base?: string) => { const equalToCurrentHref = (target: string, base?: string) => {
base ||= window.location.origin; base ||= window.location.origin;
const targetUrl = new URL(target, base); const targetUrl = new URL(target, base);
return targetUrl.toString() !== window.location.href; return targetUrl.toString() === window.location.href;
}; };
const storageLoginRedirect = () => { const storageLoginRedirect = () => {
@ -54,7 +54,7 @@ export interface NavigateConfig {
const navigate = (to: string | number, config: NavigateConfig = {}) => { const navigate = (to: string | number, config: NavigateConfig = {}) => {
let { handler = 'href' } = config; let { handler = 'href' } = config;
if (to && typeof to === 'string') { if (to && typeof to === 'string') {
if (!differentCurrent(to)) { if (equalToCurrentHref(to)) {
return; return;
} }
/** /**
@ -62,7 +62,7 @@ const navigate = (to: string | number, config: NavigateConfig = {}) => {
* 2. Auto storage login redirect * 2. Auto storage login redirect
*/ */
if (to === RouteAlias.login || to === getLoginUrl()) { if (to === RouteAlias.login || to === getLoginUrl()) {
if (!differentCurrent(RouteAlias.login)) { if (equalToCurrentHref(RouteAlias.login)) {
return; return;
} }
storageLoginRedirect(); storageLoginRedirect();
@ -133,4 +133,5 @@ export const floppyNavigation = {
isFullLink, isFullLink,
isRoutableLink, isRoutableLink,
handleRouteLinkClick, handleRouteLinkClick,
equalToCurrentHref,
}; };