Skip to content

Commit

Permalink
Merge branch 'develop' into feat/dynamic-data-table
Browse files Browse the repository at this point in the history
  • Loading branch information
shivani170 committed Nov 28, 2024
2 parents f596d82 + cd4c4c2 commit 612a8b9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down
55 changes: 37 additions & 18 deletions src/Common/Hooks/useUrlFilters/useUrlFilters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -47,7 +48,38 @@ const useUrlFilters = <T = string, K = unknown>({
}: UseUrlFiltersProps<T, K> = {}): UseUrlFiltersReturnType<T, K> => {
const location = useLocation()
const history = useHistory()
const searchParams = new URLSearchParams(location.search)

const isAlreadyReadFromLocalStorage = useRef<boolean>(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<T, K>['parseSearchParams'] = (searchParamsToParse) => {
if (parseSearchParams) {
Expand Down Expand Up @@ -136,7 +168,9 @@ const useUrlFilters = <T = string, K = unknown>({

const clearFilters = () => {
history.replace({ search: '' })
setItemInLocalStorageIfKeyExists(localStorageKey, '')
if (localStorageKey) {
localStorage.removeItem(localStorageKey)
}
}

const updateSearchParams = (paramsToSerialize: Partial<K>) => {
Expand All @@ -160,21 +194,6 @@ const useUrlFilters = <T = string, K = unknown>({
_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,
Expand Down
2 changes: 2 additions & 0 deletions src/Shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export enum Nodes {
PodDisruptionBudget = 'PodDisruptionBudget',
Event = 'Event',
Namespace = 'Namespace',
Node = 'Node',
Overview = 'Overview',
MonitoringDashboard = 'MonitoringDashboard',
UpgradeCluster = 'UpgradeCluster',
Expand Down Expand Up @@ -527,6 +528,7 @@ export enum AggregationKeys {
'Other Resources' = 'Other Resources',
Events = 'Events',
Namespaces = 'Namespaces',
'Nodes' = 'Nodes',
}

export type AggregationKeysType = keyof typeof AggregationKeys
Expand Down

0 comments on commit 612a8b9

Please sign in to comment.