Skip to content

Commit

Permalink
style: 修复 hooks 写法问题
Browse files Browse the repository at this point in the history
  • Loading branch information
yyz945947732 committed Aug 29, 2023
1 parent a1a937a commit d0b46af
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 59 deletions.
89 changes: 45 additions & 44 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:@typescript-eslint/recommended"
],
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint",
"unused-imports",
"simple-import-sort"
],
"rules": {
"no-unused-vars": "off",
"unused-imports/no-unused-imports": "warn",
"unused-imports/no-unused-vars": [
"warn",
{
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^_"
}
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:react/recommended",
"plugin:react/jsx-runtime",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended"
],
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"@typescript-eslint/no-explicit-any": "off"
},
"settings": {
"react": {
"version": "detect"
"overrides": [],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"react",
"@typescript-eslint",
"unused-imports",
"simple-import-sort"
],
"rules": {
"no-unused-vars": "off",
"unused-imports/no-unused-imports": "warn",
"unused-imports/no-unused-vars": [
"warn",
{
"vars": "all",
"varsIgnorePattern": "^_",
"args": "after-used",
"argsIgnorePattern": "^_"
}
],
"simple-import-sort/imports": "warn",
"simple-import-sort/exports": "warn",
"react/react-in-jsx-scope": "off",
"react/prop-types": "off",
"@typescript-eslint/no-explicit-any": "off"
},
"settings": {
"react": {
"version": "detect"
}
}
}
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
"coveralls": "^3.1.1",
"eslint": "^8.46.0",
"eslint-plugin-react": "^7.33.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"eslint-plugin-storybook": "^0.6.13",
"eslint-plugin-unused-imports": "^3.0.0",
Expand Down
20 changes: 9 additions & 11 deletions src/core/components/RouteGuard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@ function RouteGuard(props: RouteGuardProps) {

const isInRouter = useInRouterContext();

/* istanbul ignore if */
if (!isInRouter) {
console.warn(
`请在 react-router-dom V6 的 <Router /> 组件中使用 Auth.${RouteGuard.name} 组件。如果你不是使用 V6 版本请不要使用这个组件。@see https://reactrouter.com/en/main/router-components/router。
`
);
return <>{children}</>;
}

const location = useLocation();
const allowRoutes = useAuthData(routes, { authKey });

const isAllow = useMemo(() => {
if (!routes) {
/* istanbul ignore if */
if (!isInRouter) {
console.warn(
`请在 react-router-dom V6 的 <Router /> 组件中使用 Auth.${RouteGuard.name} 组件。如果你不是使用 V6 版本请不要使用这个组件。@see https://reactrouter.com/en/main/router-components/router。
`
);
return true;
} else if (!routes) {
return true;
} else if (!allowRoutes) {
return false;
Expand All @@ -51,7 +49,7 @@ function RouteGuard(props: RouteGuardProps) {
} else {
return true;
}
}, [location, allowRoutes]);
}, [isInRouter, routes, allowRoutes, location]);

return <>{isAllow ? children : NoAuthFallback}</>;
}
Expand Down
9 changes: 5 additions & 4 deletions src/core/hooks/useAuthData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,16 @@ const defaultOptions: Required<Options> = {

/** 获取根据权限过滤的集合 */
export function useAuthData(data?: any[], options?: Options) {
/* istanbul ignore if */
if (!Array.isArray(data)) {
return data;
}
const combineOptions = defaults(options, defaultOptions);
const { auth, disabled } = useContext(ProviderContext);
const { authProxy } = useContext(ProxyContext);
const { validator } = useContext(ValidatorContext);

/* istanbul ignore if */
if (!Array.isArray(data)) {
return data;
}

function filterFn(item: any) {
return (
!item[combineOptions.authKey] ||
Expand Down

0 comments on commit d0b46af

Please sign in to comment.