diff --git a/package-lock.json b/package-lock.json index 2d194f7a..c2382a66 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "1.1.4", + "version": "1.1.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "1.1.4", + "version": "1.1.5", "license": "ISC", "dependencies": { "@types/react-dates": "^21.8.6", @@ -14,6 +14,7 @@ "dayjs": "^1.11.13", "fast-json-patch": "^3.1.1", "jsonpath-plus": "^10.0.0", + "marked": "^13.0.3", "react-dates": "^21.8.0", "react-diff-viewer-continued": "^3.4.0", "react-monaco-editor": "^0.54.0", @@ -66,7 +67,6 @@ "@rjsf/validator-ajv8": "^5.13.3", "@typeform/embed-react": "2.20.0", "dompurify": "^3.0.2", - "marked": "^13.0.3", "react": "^17.0.2", "react-dom": "^17.0.2", "react-draggable": "^4.4.5", @@ -7313,7 +7313,6 @@ "resolved": "https://registry.npmjs.org/marked/-/marked-13.0.3.tgz", "integrity": "sha512-rqRix3/TWzE9rIoFGIn8JmsVfhiuC8VIQ8IdX5TfzmeBucdY05/0UlzKaw0eVtpcN/OdVFpBk7CjKGo9iHJ/zA==", "license": "MIT", - "peer": true, "bin": { "marked": "bin/marked.js" }, diff --git a/package.json b/package.json index ab1cbfc7..f5651887 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@devtron-labs/devtron-fe-common-lib", - "version": "1.1.4", + "version": "1.1.5", "description": "Supporting common component library", "type": "module", "main": "dist/index.js", @@ -79,7 +79,6 @@ "@rjsf/validator-ajv8": "^5.13.3", "@typeform/embed-react": "2.20.0", "dompurify": "^3.0.2", - "marked": "^13.0.3", "react": "^17.0.2", "react-dom": "^17.0.2", "react-draggable": "^4.4.5", @@ -92,6 +91,7 @@ }, "dependencies": { "@types/react-dates": "^21.8.6", + "marked": "^13.0.3", "ansi_up": "^5.2.1", "dayjs": "^1.11.13", "fast-json-patch": "^3.1.1", diff --git a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts index 4e314e6e..f6b66aeb 100644 --- a/src/Common/Hooks/useUrlFilters/useUrlFilters.ts +++ b/src/Common/Hooks/useUrlFilters/useUrlFilters.ts @@ -14,8 +14,9 @@ * limitations under the License. */ -import { useEffect, useMemo } from 'react' +import { useMemo, useRef } from 'react' import { useHistory, useLocation } from 'react-router-dom' +import { getUrlWithSearchParams } from '@Common/Helper' import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants' import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants' import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types' @@ -47,7 +48,38 @@ const useUrlFilters = ({ }: UseUrlFiltersProps = {}): UseUrlFiltersReturnType => { const location = useLocation() const history = useHistory() - const searchParams = new URLSearchParams(location.search) + + const isAlreadyReadFromLocalStorage = useRef(false) + + const getSearchParams = () => { + const locationSearchParams = new URLSearchParams(location.search) + if (!isAlreadyReadFromLocalStorage.current && localStorageKey) { + if (!location.search) { + isAlreadyReadFromLocalStorage.current = true + const localStorageValue = localStorage.getItem(localStorageKey) + if (localStorageValue) { + try { + const localSearchString = getUrlWithSearchParams('', JSON.parse(localStorageValue)) + const localSearchParams = new URLSearchParams(localSearchString.split('?')[1] ?? '') + + history.replace({ search: localSearchParams.toString() }) + return localSearchParams + } catch { + localStorage.removeItem(localStorageKey) + } + } + } else { + setItemInLocalStorageIfKeyExists( + localStorageKey, + JSON.stringify(parseSearchParams(locationSearchParams)), + ) + } + } + + return locationSearchParams + } + + const searchParams = getSearchParams() const getParsedSearchParams: UseUrlFiltersProps['parseSearchParams'] = (searchParamsToParse) => { if (parseSearchParams) { @@ -136,7 +168,9 @@ const useUrlFilters = ({ const clearFilters = () => { history.replace({ search: '' }) - setItemInLocalStorageIfKeyExists(localStorageKey, '') + if (localStorageKey) { + localStorage.removeItem(localStorageKey) + } } const updateSearchParams = (paramsToSerialize: Partial) => { @@ -160,21 +194,6 @@ const useUrlFilters = ({ _resetPageNumber() } - useEffect(() => { - if (!localStorageKey) { - return - } - // if we have search string, set secondary params in local storage accordingly - if (location.search) { - setItemInLocalStorageIfKeyExists(localStorageKey, JSON.stringify(parsedParams)) - return - } - const localStorageValue = localStorage.getItem(localStorageKey) - if (localStorageValue) { - updateSearchParams(JSON.parse(localStorageValue)) - } - }, []) - return { pageSize, changePage, diff --git a/src/Shared/types.ts b/src/Shared/types.ts index 7a15c7ef..db36a59a 100644 --- a/src/Shared/types.ts +++ b/src/Shared/types.ts @@ -118,6 +118,7 @@ export enum Nodes { PodDisruptionBudget = 'PodDisruptionBudget', Event = 'Event', Namespace = 'Namespace', + Node = 'Node', Overview = 'Overview', MonitoringDashboard = 'MonitoringDashboard', UpgradeCluster = 'UpgradeCluster', @@ -527,6 +528,7 @@ export enum AggregationKeys { 'Other Resources' = 'Other Resources', Events = 'Events', Namespaces = 'Namespaces', + 'Nodes' = 'Nodes', } export type AggregationKeysType = keyof typeof AggregationKeys