From db3ca81de81f1ec0ab4c2a560c4307c54794dd4a Mon Sep 17 00:00:00 2001 From: Fedor Abrashin Date: Wed, 1 Mar 2023 15:46:32 +0300 Subject: [PATCH 1/5] feat(sendsay-docs): enable restricted access to articles --- README.md | 9 +++++ config/restrictedAccessRoutes.json | 4 +++ .../automations-by-event/welcome-series.md | 3 ++ docs/ecom/_category_.json | 4 +++ src/theme/hooks/useResctrictedPath.ts | 19 +++++++---- src/theme/hooks/useRouteAllowance.ts | 8 ++--- src/theme/types/enums.ts | 5 +++ src/theme/types/types.ts | 12 +++++-- src/theme/utils/routeAccessUtils.ts | 34 +++++++++++-------- 9 files changed, 70 insertions(+), 28 deletions(-) create mode 100644 config/restrictedAccessRoutes.json diff --git a/README.md b/README.md index 1557abe6..08554a14 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,15 @@
+# How to restrict access to an *article* or *category*: + +1) add `restrictedAccessHref` custom prop in `_category_.json` or article header
+ _value should be uniq across all docs_ +2) add full route to `config/restrictedAccessRoutes.json`
+ _to exclude it from search_ + +
+ # How to translate a new article There is a couple of pathes for each translated file: diff --git a/config/restrictedAccessRoutes.json b/config/restrictedAccessRoutes.json new file mode 100644 index 00000000..3759006a --- /dev/null +++ b/config/restrictedAccessRoutes.json @@ -0,0 +1,4 @@ +[ + "ecom/**", + "automations/automations-by-event/welcome-series" +] \ No newline at end of file diff --git a/docs/automations/automations-by-event/welcome-series.md b/docs/automations/automations-by-event/welcome-series.md index c53c57fa..a194321b 100644 --- a/docs/automations/automations-by-event/welcome-series.md +++ b/docs/automations/automations-by-event/welcome-series.md @@ -1,5 +1,8 @@ --- sidebar_position: 2 +sidebar_custom_props: { + restrictedAccessHref: "automations-by-event/welcome-series" +} --- # Как настроить приветственную серию писем diff --git a/docs/ecom/_category_.json b/docs/ecom/_category_.json index a95db8da..c107a584 100644 --- a/docs/ecom/_category_.json +++ b/docs/ecom/_category_.json @@ -1,6 +1,10 @@ { "label": "Продажи", "position": 14, + "link": { + "type": "generated-index", + "slug": "ecom" + }, "customProps": { "restrictedAccessHref": "ecom" } diff --git a/src/theme/hooks/useResctrictedPath.ts b/src/theme/hooks/useResctrictedPath.ts index 81f26c69..9161db15 100644 --- a/src/theme/hooks/useResctrictedPath.ts +++ b/src/theme/hooks/useResctrictedPath.ts @@ -5,17 +5,24 @@ import { checkAllowedRoutes, ResctrictedAccessStorage, checkHiddenSidebarItem } import { RestrictedHref } from '../types'; import { useRouteAllowance } from './useRouteAllowance'; -export const useResctrictedPath = (item: PropSidebarItem) => { - const isBrowser = useIsBrowser(); - const routeHref = item.customProps?.restrictedAccessHref as RestrictedHref; +export const useResctrictedPath = (item: PropSidebarItem | any) => { + const isStorageAllowed = useIsBrowser(); + const routeHref = (item.customProps?.restrictedAccessHref) as RestrictedHref - const { allowedRoutes, isNewAccessToRoute } = useRouteAllowance(routeHref, isBrowser); + const { allowedRoutes, isNewAccessToRoute } = useRouteAllowance(routeHref, { + isStorageAllowed, + type: item.type + }); useLayoutEffect(() => { - if (isNewAccessToRoute && isBrowser) { + if (isNewAccessToRoute && isStorageAllowed) { ResctrictedAccessStorage.setJSON(allowedRoutes); } - }, [isBrowser]); + }, [isStorageAllowed, isNewAccessToRoute]); + + if (item.type === 'link') { + console.log('!!', item.label, !checkAllowedRoutes(allowedRoutes, routeHref), allowedRoutes) + } return { isRestricted: checkHiddenSidebarItem(item) || !checkAllowedRoutes(allowedRoutes, routeHref), diff --git a/src/theme/hooks/useRouteAllowance.ts b/src/theme/hooks/useRouteAllowance.ts index 02d86c01..9f861053 100644 --- a/src/theme/hooks/useRouteAllowance.ts +++ b/src/theme/hooks/useRouteAllowance.ts @@ -1,16 +1,16 @@ import { useHistory } from '@docusaurus/router'; import { checkNewAccessToRoute, getAllowedRoutes } from '../utils'; -import { RestrictedHref } from '../types'; +import { RestrictedHref, AllowedRoutesOptions } from '../types'; -export const useRouteAllowance = (routeHref: RestrictedHref, isStorageAllowed: boolean) => { +export const useRouteAllowance = (routeHref: RestrictedHref, options: AllowedRoutesOptions ) => { const { location: { pathname: path }, } = useHistory(); const isNewAccessToRoute = checkNewAccessToRoute(routeHref, path); const allowedRoutes = getAllowedRoutes(routeHref, { - isNewAccessToRoute, - isStorageAllowed, + ...options, + isNewAccessToRoute }); return { diff --git a/src/theme/types/enums.ts b/src/theme/types/enums.ts index aea2d519..6339ef98 100644 --- a/src/theme/types/enums.ts +++ b/src/theme/types/enums.ts @@ -7,3 +7,8 @@ export const enum PropSidebarItemType { Category = 'category', Link = 'link', } + +export const enum ResctrictedAccessStorageKeys { + Categories = 'categories', + Articles = 'articles' +} diff --git a/src/theme/types/types.ts b/src/theme/types/types.ts index ba33cd9e..dd1d34f0 100644 --- a/src/theme/types/types.ts +++ b/src/theme/types/types.ts @@ -1,10 +1,16 @@ -import { ResctrictedAccessStatus } from './enums'; +import { ResctrictedAccessStatus, ResctrictedAccessStorageKeys, PropSidebarItemType } from './enums'; export type RestrictedHref = string | undefined; export type ResctrictedAccessItem = Record; export interface ResctrictedAccessItems { - categories?: ResctrictedAccessItem; - articles?: ResctrictedAccessItem; + [ResctrictedAccessStorageKeys.Categories]?: ResctrictedAccessItem; + [ResctrictedAccessStorageKeys.Articles]?: ResctrictedAccessItem; +} + +export interface AllowedRoutesOptions { + isStorageAllowed: boolean; + type: PropSidebarItemType; + isNewAccessToRoute?: boolean } diff --git a/src/theme/utils/routeAccessUtils.ts b/src/theme/utils/routeAccessUtils.ts index e5043a00..1f129856 100644 --- a/src/theme/utils/routeAccessUtils.ts +++ b/src/theme/utils/routeAccessUtils.ts @@ -1,10 +1,12 @@ import { PropSidebarItem } from '@docusaurus/plugin-content-docs'; import { ResctrictedAccessItems, - ResctrictedAccessItem, ResctrictedAccessStatus, PropSidebarItemType, RestrictedHref, + ResctrictedAccessItem, + ResctrictedAccessStorageKeys, + AllowedRoutesOptions } from '../types'; import { ResctrictedAccessStorage } from './ResctrictedAccessStorage'; import { HIDDEN_CATEGORIES_LABELS } from '../constants'; @@ -12,14 +14,9 @@ import { HIDDEN_CATEGORIES_LABELS } from '../constants'; const getRoutesFromStorage = (): ResctrictedAccessItems => ResctrictedAccessStorage.getJSON() ?? {}; -interface AllowedRoutesOptions { - isNewAccessToRoute: boolean; - isStorageAllowed: boolean; -} - export const getAllowedRoutes = ( - routeHref: string, - { isNewAccessToRoute, isStorageAllowed }: AllowedRoutesOptions + newRouteHref: string, + { isStorageAllowed, type, isNewAccessToRoute }: AllowedRoutesOptions ) => { const previouslyAccessed: ResctrictedAccessItems = isStorageAllowed ? getRoutesFromStorage() : {}; @@ -27,11 +24,15 @@ export const getAllowedRoutes = ( return previouslyAccessed; } + const storeKey = type === PropSidebarItemType.Category + ? ResctrictedAccessStorageKeys.Categories + : ResctrictedAccessStorageKeys.Articles + return { ...previouslyAccessed, - categories: { - ...(previouslyAccessed.categories ?? {}), - [routeHref]: ResctrictedAccessStatus.Allowed, + [storeKey]: { + ...(previouslyAccessed[storeKey] ?? {}), + [newRouteHref]: ResctrictedAccessStatus.Allowed, }, }; }; @@ -42,14 +43,17 @@ export const checkAllowedRoutes = (allowedRoutes: ResctrictedAccessItems, routeH } return Object.values(allowedRoutes).reduce( - (acc: boolean, type: ResctrictedAccessItem) => - acc || type[routeHref] === ResctrictedAccessStatus.Allowed, + (acc: boolean, routes: ResctrictedAccessItem) => + acc || routes[routeHref] === ResctrictedAccessStatus.Allowed, false ); }; -export const checkNewAccessToRoute = (routeHref: RestrictedHref, path: string) => - path.includes(routeHref); +export const checkNewAccessToRoute = (routeHref: RestrictedHref, path: string) => path.includes(routeHref) + +// export const getRouteWithNewAccess = (routeHrefs: RestrictedHref, path: string) => routeHrefs +// .filter((route) => path.includes(route)) +// .sort((a, b) => a.length - b.length)[0]; export const checkHiddenSidebarItem = (item: PropSidebarItem) => item.type === PropSidebarItemType.Category From 75e61a76c979b9f56118224b99fb4c39766df05a Mon Sep 17 00:00:00 2001 From: Fedor Abrashin Date: Wed, 1 Mar 2023 15:56:45 +0300 Subject: [PATCH 2/5] yarn fix --- README.md | 10 +++++----- config/restrictedAccessRoutes.json | 5 +---- .../automations-by-event/welcome-series.md | 4 +--- src/theme/hooks/useResctrictedPath.ts | 12 ++++-------- src/theme/hooks/useRouteAllowance.ts | 4 ++-- src/theme/types/enums.ts | 3 ++- src/theme/types/types.ts | 8 ++++++-- src/theme/utils/routeAccessUtils.ts | 12 +++++++----- 8 files changed, 28 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 08554a14..1a58e32d 100644 --- a/README.md +++ b/README.md @@ -6,12 +6,12 @@
-# How to restrict access to an *article* or *category*: +# How to restrict access to an _article_ or _category_: -1) add `restrictedAccessHref` custom prop in `_category_.json` or article header
- _value should be uniq across all docs_ -2) add full route to `config/restrictedAccessRoutes.json`
- _to exclude it from search_ +1. add `restrictedAccessHref` custom prop in `_category_.json` or article header
+ _value should be uniq across all docs_ +2. add full route to `config/restrictedAccessRoutes.json`
+ _to exclude it from search_
diff --git a/config/restrictedAccessRoutes.json b/config/restrictedAccessRoutes.json index 3759006a..8ed9c852 100644 --- a/config/restrictedAccessRoutes.json +++ b/config/restrictedAccessRoutes.json @@ -1,4 +1 @@ -[ - "ecom/**", - "automations/automations-by-event/welcome-series" -] \ No newline at end of file +["ecom/**", "automations/automations-by-event/welcome-series"] diff --git a/docs/automations/automations-by-event/welcome-series.md b/docs/automations/automations-by-event/welcome-series.md index a194321b..e15c934b 100644 --- a/docs/automations/automations-by-event/welcome-series.md +++ b/docs/automations/automations-by-event/welcome-series.md @@ -1,8 +1,6 @@ --- sidebar_position: 2 -sidebar_custom_props: { - restrictedAccessHref: "automations-by-event/welcome-series" -} +sidebar_custom_props: { restrictedAccessHref: 'automations-by-event/welcome-series' } --- # Как настроить приветственную серию писем diff --git a/src/theme/hooks/useResctrictedPath.ts b/src/theme/hooks/useResctrictedPath.ts index 9161db15..d83748be 100644 --- a/src/theme/hooks/useResctrictedPath.ts +++ b/src/theme/hooks/useResctrictedPath.ts @@ -2,16 +2,16 @@ import { useLayoutEffect } from 'react'; import { PropSidebarItem } from '@docusaurus/plugin-content-docs'; import useIsBrowser from '@docusaurus/useIsBrowser'; import { checkAllowedRoutes, ResctrictedAccessStorage, checkHiddenSidebarItem } from '../utils'; -import { RestrictedHref } from '../types'; +import { RestrictedHref, PropSidebarItemType } from '../types'; import { useRouteAllowance } from './useRouteAllowance'; -export const useResctrictedPath = (item: PropSidebarItem | any) => { +export const useResctrictedPath = (item: PropSidebarItem) => { const isStorageAllowed = useIsBrowser(); - const routeHref = (item.customProps?.restrictedAccessHref) as RestrictedHref + const routeHref = item.customProps?.restrictedAccessHref as RestrictedHref; const { allowedRoutes, isNewAccessToRoute } = useRouteAllowance(routeHref, { isStorageAllowed, - type: item.type + type: item.type as PropSidebarItemType, }); useLayoutEffect(() => { @@ -20,10 +20,6 @@ export const useResctrictedPath = (item: PropSidebarItem | any) => { } }, [isStorageAllowed, isNewAccessToRoute]); - if (item.type === 'link') { - console.log('!!', item.label, !checkAllowedRoutes(allowedRoutes, routeHref), allowedRoutes) - } - return { isRestricted: checkHiddenSidebarItem(item) || !checkAllowedRoutes(allowedRoutes, routeHref), }; diff --git a/src/theme/hooks/useRouteAllowance.ts b/src/theme/hooks/useRouteAllowance.ts index 9f861053..19096940 100644 --- a/src/theme/hooks/useRouteAllowance.ts +++ b/src/theme/hooks/useRouteAllowance.ts @@ -2,7 +2,7 @@ import { useHistory } from '@docusaurus/router'; import { checkNewAccessToRoute, getAllowedRoutes } from '../utils'; import { RestrictedHref, AllowedRoutesOptions } from '../types'; -export const useRouteAllowance = (routeHref: RestrictedHref, options: AllowedRoutesOptions ) => { +export const useRouteAllowance = (routeHref: RestrictedHref, options: AllowedRoutesOptions) => { const { location: { pathname: path }, } = useHistory(); @@ -10,7 +10,7 @@ export const useRouteAllowance = (routeHref: RestrictedHref, options: AllowedRou const isNewAccessToRoute = checkNewAccessToRoute(routeHref, path); const allowedRoutes = getAllowedRoutes(routeHref, { ...options, - isNewAccessToRoute + isNewAccessToRoute, }); return { diff --git a/src/theme/types/enums.ts b/src/theme/types/enums.ts index 6339ef98..b4e42533 100644 --- a/src/theme/types/enums.ts +++ b/src/theme/types/enums.ts @@ -6,9 +6,10 @@ export const enum ResctrictedAccessStatus { export const enum PropSidebarItemType { Category = 'category', Link = 'link', + Html = 'html', } export const enum ResctrictedAccessStorageKeys { Categories = 'categories', - Articles = 'articles' + Articles = 'articles', } diff --git a/src/theme/types/types.ts b/src/theme/types/types.ts index dd1d34f0..6b745573 100644 --- a/src/theme/types/types.ts +++ b/src/theme/types/types.ts @@ -1,4 +1,8 @@ -import { ResctrictedAccessStatus, ResctrictedAccessStorageKeys, PropSidebarItemType } from './enums'; +import { + ResctrictedAccessStatus, + ResctrictedAccessStorageKeys, + PropSidebarItemType, +} from './enums'; export type RestrictedHref = string | undefined; @@ -12,5 +16,5 @@ export interface ResctrictedAccessItems { export interface AllowedRoutesOptions { isStorageAllowed: boolean; type: PropSidebarItemType; - isNewAccessToRoute?: boolean + isNewAccessToRoute?: boolean; } diff --git a/src/theme/utils/routeAccessUtils.ts b/src/theme/utils/routeAccessUtils.ts index 1f129856..5c2557f0 100644 --- a/src/theme/utils/routeAccessUtils.ts +++ b/src/theme/utils/routeAccessUtils.ts @@ -6,7 +6,7 @@ import { RestrictedHref, ResctrictedAccessItem, ResctrictedAccessStorageKeys, - AllowedRoutesOptions + AllowedRoutesOptions, } from '../types'; import { ResctrictedAccessStorage } from './ResctrictedAccessStorage'; import { HIDDEN_CATEGORIES_LABELS } from '../constants'; @@ -24,9 +24,10 @@ export const getAllowedRoutes = ( return previouslyAccessed; } - const storeKey = type === PropSidebarItemType.Category - ? ResctrictedAccessStorageKeys.Categories - : ResctrictedAccessStorageKeys.Articles + const storeKey = + type === PropSidebarItemType.Category + ? ResctrictedAccessStorageKeys.Categories + : ResctrictedAccessStorageKeys.Articles; return { ...previouslyAccessed, @@ -49,7 +50,8 @@ export const checkAllowedRoutes = (allowedRoutes: ResctrictedAccessItems, routeH ); }; -export const checkNewAccessToRoute = (routeHref: RestrictedHref, path: string) => path.includes(routeHref) +export const checkNewAccessToRoute = (routeHref: RestrictedHref, path: string) => + path.includes(routeHref); // export const getRouteWithNewAccess = (routeHrefs: RestrictedHref, path: string) => routeHrefs // .filter((route) => path.includes(route)) From bd61c8b680d741c5aceffafcd1117bb3dc2518a1 Mon Sep 17 00:00:00 2001 From: Fedor Abrashin Date: Wed, 1 Mar 2023 16:05:29 +0300 Subject: [PATCH 3/5] fix search exclude --- config/restrictedAccessRoutes.json | 1 - config/searchExcludeRoutes.json | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) delete mode 100644 config/restrictedAccessRoutes.json diff --git a/config/restrictedAccessRoutes.json b/config/restrictedAccessRoutes.json deleted file mode 100644 index 8ed9c852..00000000 --- a/config/restrictedAccessRoutes.json +++ /dev/null @@ -1 +0,0 @@ -["ecom/**", "automations/automations-by-event/welcome-series"] diff --git a/config/searchExcludeRoutes.json b/config/searchExcludeRoutes.json index d02c25ee..7121c4b6 100644 --- a/config/searchExcludeRoutes.json +++ b/config/searchExcludeRoutes.json @@ -1 +1 @@ -["/exclude-from-search/**", "/ecom/**"] +["/exclude-from-search/**", "/ecom/**", "/automations/automations-by-event/welcome-series"] From eba2af9a4db290f6f19df1fd07efc715efa7015d Mon Sep 17 00:00:00 2001 From: Fedor Abrashin Date: Wed, 1 Mar 2023 16:24:31 +0300 Subject: [PATCH 4/5] remove comment --- src/theme/utils/routeAccessUtils.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/theme/utils/routeAccessUtils.ts b/src/theme/utils/routeAccessUtils.ts index 5c2557f0..a47008b6 100644 --- a/src/theme/utils/routeAccessUtils.ts +++ b/src/theme/utils/routeAccessUtils.ts @@ -53,10 +53,6 @@ export const checkAllowedRoutes = (allowedRoutes: ResctrictedAccessItems, routeH export const checkNewAccessToRoute = (routeHref: RestrictedHref, path: string) => path.includes(routeHref); -// export const getRouteWithNewAccess = (routeHrefs: RestrictedHref, path: string) => routeHrefs -// .filter((route) => path.includes(route)) -// .sort((a, b) => a.length - b.length)[0]; - export const checkHiddenSidebarItem = (item: PropSidebarItem) => item.type === PropSidebarItemType.Category ? HIDDEN_CATEGORIES_LABELS.includes(item.label) From f4daf33b5b1700a7c48745c5df7a5579078f9ea2 Mon Sep 17 00:00:00 2001 From: Fedor Abrashin Date: Wed, 1 Mar 2023 18:51:24 +0300 Subject: [PATCH 5/5] make valid filtration for search-doc.json --- config/restrictedAccessRoutes.json | 1 + config/searchExcludeRoutes.json | 2 +- src/theme/SearchBar/index.js | 15 ++++++++++-- src/theme/SearchBar/lunar-search.js | 23 +++++++++++-------- src/theme/hooks/useRouteAllowance.ts | 7 +++--- src/theme/types/types.ts | 3 ++- src/theme/utils/filterSearchExcludeOptions.ts | 23 +++++++++++++++++++ src/theme/utils/index.ts | 8 ++----- src/theme/utils/routeAccessUtils.ts | 9 ++++---- 9 files changed, 65 insertions(+), 26 deletions(-) create mode 100644 config/restrictedAccessRoutes.json create mode 100644 src/theme/utils/filterSearchExcludeOptions.ts diff --git a/config/restrictedAccessRoutes.json b/config/restrictedAccessRoutes.json new file mode 100644 index 00000000..bb428d55 --- /dev/null +++ b/config/restrictedAccessRoutes.json @@ -0,0 +1 @@ +["/ecom/", "/automations/automations-by-event/welcome-series"] diff --git a/config/searchExcludeRoutes.json b/config/searchExcludeRoutes.json index 7121c4b6..aabec3d8 100644 --- a/config/searchExcludeRoutes.json +++ b/config/searchExcludeRoutes.json @@ -1 +1 @@ -["/exclude-from-search/**", "/ecom/**", "/automations/automations-by-event/welcome-series"] +["/exclude-from-search/**"] diff --git a/src/theme/SearchBar/index.js b/src/theme/SearchBar/index.js index 2603b9f9..b32eca44 100644 --- a/src/theme/SearchBar/index.js +++ b/src/theme/SearchBar/index.js @@ -5,6 +5,8 @@ import { translate } from '@docusaurus/Translate'; import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import { usePluginData } from '@docusaurus/useGlobalData'; import useIsBrowser from '@docusaurus/useIsBrowser'; +import { filterSearchExcludeOptions } from '../utils/filterSearchExcludeOptions'; + const Search = (props) => { const initialized = useRef(false); const searchBarRef = useRef(null); @@ -54,10 +56,19 @@ const Search = (props) => { import('./DocSearch'), import('./algolia.css'), ]).then(([searchDocs, searchIndex, { default: DocSearch }]) => { - if (searchDocs.length === 0) { + const filteredSearchDocs = filterSearchExcludeOptions(searchDocs, isBrowser); + console.log( + '!!filteredSearchDocs', + searchDocs.length, + filteredSearchDocs.length, + searchIndex + ); + + if (filteredSearchDocs.length === 0) { return; } - initAlgolia(searchDocs, searchIndex, DocSearch); + + initAlgolia(filteredSearchDocs, searchIndex, DocSearch); setIndexReady(true); }); initialized.current = true; diff --git a/src/theme/SearchBar/lunar-search.js b/src/theme/SearchBar/lunar-search.js index c497c2e1..50fcb733 100644 --- a/src/theme/SearchBar/lunar-search.js +++ b/src/theme/SearchBar/lunar-search.js @@ -51,15 +51,20 @@ class LunrSearchAdapter { }; } getTitleHit(doc, position, length) { - const start = position[0]; - const end = position[0] + length; - let formattedTitle = - doc.title.substring(0, start) + - '' + - doc.title.substring(start, end) + - '' + - doc.title.substring(end, doc.title.length); - return this.getHit(doc, formattedTitle); + try { + const start = position[0]; + const end = position[0] + length; + let formattedTitle = + doc.title.substring(0, start) + + '' + + doc.title.substring(start, end) + + '' + + doc.title.substring(end, doc.title.length); + + return this.getHit(doc, formattedTitle); + } catch (err) { + console.log('!!err', doc, position, length); + } } getKeywordHit(doc, position, length) { diff --git a/src/theme/hooks/useRouteAllowance.ts b/src/theme/hooks/useRouteAllowance.ts index 19096940..99d54705 100644 --- a/src/theme/hooks/useRouteAllowance.ts +++ b/src/theme/hooks/useRouteAllowance.ts @@ -2,13 +2,14 @@ import { useHistory } from '@docusaurus/router'; import { checkNewAccessToRoute, getAllowedRoutes } from '../utils'; import { RestrictedHref, AllowedRoutesOptions } from '../types'; -export const useRouteAllowance = (routeHref: RestrictedHref, options: AllowedRoutesOptions) => { +export const useRouteAllowance = (newRouteHref: RestrictedHref, options: AllowedRoutesOptions) => { const { location: { pathname: path }, } = useHistory(); - const isNewAccessToRoute = checkNewAccessToRoute(routeHref, path); - const allowedRoutes = getAllowedRoutes(routeHref, { + const isNewAccessToRoute = checkNewAccessToRoute(newRouteHref, path); + const allowedRoutes = getAllowedRoutes({ + newRouteHref, ...options, isNewAccessToRoute, }); diff --git a/src/theme/types/types.ts b/src/theme/types/types.ts index 6b745573..8eba10ef 100644 --- a/src/theme/types/types.ts +++ b/src/theme/types/types.ts @@ -15,6 +15,7 @@ export interface ResctrictedAccessItems { export interface AllowedRoutesOptions { isStorageAllowed: boolean; - type: PropSidebarItemType; + type?: PropSidebarItemType; + newRouteHref?: string; isNewAccessToRoute?: boolean; } diff --git a/src/theme/utils/filterSearchExcludeOptions.ts b/src/theme/utils/filterSearchExcludeOptions.ts new file mode 100644 index 00000000..61d69f0f --- /dev/null +++ b/src/theme/utils/filterSearchExcludeOptions.ts @@ -0,0 +1,23 @@ +import { getAllowedRoutes, flatRoutesResponse } from './routeAccessUtils'; +import restrictedAccessRoutes from '../../../config/restrictedAccessRoutes.json'; + +const checkPartialExistanceInArray = (target: string, supportArray: string[]) => + supportArray.some((item) => target.includes(item)); + +const filterRestrictedRoutes = (restrictedRoutes: string[], itemsToExclude: string[]) => + restrictedRoutes.filter( + (restrictedRoute) => !checkPartialExistanceInArray(restrictedRoute, itemsToExclude) + ); + +export const filterSearchExcludeOptions = (searchDocs, isStorageAllowed: boolean) => { + const allowedRoutes = getAllowedRoutes({ isStorageAllowed }); + const flatAllowedRoutes = flatRoutesResponse(allowedRoutes); + const filteredRestrictedRoutes = filterRestrictedRoutes( + restrictedAccessRoutes, + flatAllowedRoutes + ); + + return searchDocs.filter( + ({ url }) => !checkPartialExistanceInArray(url, filteredRestrictedRoutes) + ); +}; diff --git a/src/theme/utils/index.ts b/src/theme/utils/index.ts index 8025d130..9cbf4c4c 100644 --- a/src/theme/utils/index.ts +++ b/src/theme/utils/index.ts @@ -1,8 +1,4 @@ export { getTermination } from './getTermination'; -export { - checkHiddenSidebarItem, - checkNewAccessToRoute, - getAllowedRoutes, - checkAllowedRoutes, -} from './routeAccessUtils'; +export * from './routeAccessUtils'; +export { filterSearchExcludeOptions } from './filterSearchExcludeOptions'; export { ResctrictedAccessStorage } from './ResctrictedAccessStorage'; diff --git a/src/theme/utils/routeAccessUtils.ts b/src/theme/utils/routeAccessUtils.ts index a47008b6..b68d4b01 100644 --- a/src/theme/utils/routeAccessUtils.ts +++ b/src/theme/utils/routeAccessUtils.ts @@ -14,10 +14,8 @@ import { HIDDEN_CATEGORIES_LABELS } from '../constants'; const getRoutesFromStorage = (): ResctrictedAccessItems => ResctrictedAccessStorage.getJSON() ?? {}; -export const getAllowedRoutes = ( - newRouteHref: string, - { isStorageAllowed, type, isNewAccessToRoute }: AllowedRoutesOptions -) => { +export const getAllowedRoutes = (options?: AllowedRoutesOptions) => { + const { isStorageAllowed, type, isNewAccessToRoute, newRouteHref } = options ?? {}; const previouslyAccessed: ResctrictedAccessItems = isStorageAllowed ? getRoutesFromStorage() : {}; if (!isNewAccessToRoute) { @@ -38,6 +36,9 @@ export const getAllowedRoutes = ( }; }; +export const flatRoutesResponse = (routesByTypes: ResctrictedAccessItems) => + Object.values(routesByTypes).flatMap((routes) => Object.keys(routes)); + export const checkAllowedRoutes = (allowedRoutes: ResctrictedAccessItems, routeHref: string) => { if (!routeHref) { return true;