Merge pull request #380 from answerdev/feat/1.1.0/ui

fix(guard.isIgnoredPath): Avoid incorrect matches when using `indexOf`
This commit is contained in:
haitao.jarvis 2023-05-31 15:29:04 +08:00 committed by GitHub
commit 9ac3453b91
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 9 deletions

View File

@ -65,12 +65,13 @@ const Index: FC = () => {
clearTimeout(checkTimer);
};
}, []);
if (getUaType() !== USER_AGENT_NAMES.WeCom) {
return (
<Card className="text-center">
<Card.Body>
<Card.Title as="h3" className="mb-3">
{ucAgent?.agent_info.display_name} {t('login')}
{ucAgent?.agent_info?.display_name} {t('login')}
</Card.Title>
{qrcodeDataUrl ? (
<>
@ -82,7 +83,7 @@ const Index: FC = () => {
/>
<div className="text-secondary mt-3">
{t('qrcode_login_tip', {
agentName: ucAgent?.agent_info.display_name,
agentName: ucAgent?.agent_info?.display_name,
})}
</div>
</>

View File

@ -13,7 +13,7 @@ const Index = () => {
const { t } = useTranslation('translation');
const [searchParam] = useSearchParams();
const { agent: ucAgent } = userCenterStore();
let agentName = ucAgent?.agent_info.name || '';
let agentName = ucAgent?.agent_info?.name || '';
if (searchParam.get('agent_name')) {
agentName = searchParam.get('agent_name') || '';
}

View File

@ -35,7 +35,7 @@ const Index: FC = () => {
<Card>
<Card.Body>
<h3 className="text-center pt-3 mb-3">
{ucAgent?.agent_info.display_name} {t('login')}
{ucAgent?.agent_info?.display_name} {t('login')}
</h3>
<p className="text-danger text-center">
{t('login_failed_email_tip')}

View File

@ -13,7 +13,7 @@ const Index = () => {
const { t } = useTranslation('translation');
const [searchParam] = useSearchParams();
const { agent: ucAgent } = userCenterStore();
let agentName = ucAgent?.agent_info.name || '';
let agentName = ucAgent?.agent_info?.name || '';
if (searchParam.get('agent_name')) {
agentName = searchParam.get('agent_name') || '';
}

View File

@ -10,6 +10,47 @@ const equalToCurrentHref = (target: string, base?: string) => {
const targetUrl = new URL(target, base);
return targetUrl.toString() === window.location.href;
};
const matchToCurrentHref = (target: string) => {
target = (target || '').trim();
// Empty string or `/` can match any path
if (!target || target === '/') {
return true;
}
const { pathname, search, hash } = window.location;
const tPart = target.split('?');
/**
* With the current requirements, `hash` and `search` can simply be matched
* Later extended to field-by-field matching if necessary
*/
if (tPart[1]) {
const tChip = tPart[1].split('#');
const tSearch = tChip[0] || '';
const tHash = tChip[1] || '';
if (tHash && hash.indexOf(tHash) === -1) {
return false;
}
if (tSearch && search.indexOf(tSearch) === -1) {
return false;
}
}
/**
* As determination above, `tPart[0]` must be a valid string
*/
let pathMatch = true;
const tPath = tPart[0].split('/').filter((_) => !!_);
const lPath = pathname.split('/').filter((_) => !!_);
tPath.forEach((p, i) => {
const lp = lPath[i];
if (p !== lp) {
pathMatch = false;
}
});
return pathMatch;
};
const storageLoginRedirect = () => {
const { pathname } = window.location;
@ -136,5 +177,6 @@ export const floppyNavigation = {
isRoutableLink,
handleRouteLinkClick,
equalToCurrentHref,
matchToCurrentHref,
storageLoginRedirect,
};

View File

@ -94,13 +94,15 @@ export const IGNORE_PATH_LIST = [
'/user-center/',
];
export const isIgnoredPath = (ignoredPath: string | string[]) => {
export const isIgnoredPath = (ignoredPath?: string | string[]) => {
if (!ignoredPath) {
ignoredPath = IGNORE_PATH_LIST;
}
if (!Array.isArray(ignoredPath)) {
ignoredPath = [ignoredPath];
}
const { pathname } = window.location;
const matchingPath = ignoredPath.find((_) => {
return pathname.indexOf(_) !== -1;
const matchingPath = ignoredPath.find((p) => {
return floppyNavigation.matchToCurrentHref(p);
});
return !!matchingPath;
};