Skip to content

Commit 58f92af

Browse files
authored
Merge pull request #385 from devtron-labs/chore/sync-persist-filters
chore: sync persist filters
2 parents 908b905 + 0b0d348 commit 58f92af

File tree

8 files changed

+46
-13
lines changed

8 files changed

+46
-13
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@devtron-labs/devtron-fe-common-lib",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"description": "Supporting common component library",
55
"type": "module",
66
"main": "dist/index.js",

src/Common/Hooks/useUrlFilters/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface UseUrlFiltersProps<T, K> {
2525
* Callback function for parsing the search params
2626
*/
2727
parseSearchParams?: (searchParams: URLSearchParams) => K
28+
localStorageKey?: `${string}__${string}`
2829
}
2930

3031
export type UseUrlFiltersReturnType<T, K = unknown> = K & {

src/Common/Hooks/useUrlFilters/useUrlFilters.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { useMemo } from 'react'
17+
import { useEffect, useMemo } from 'react'
1818
import { useHistory, useLocation } from 'react-router-dom'
1919
import { DEFAULT_BASE_PAGE_SIZE, EXCLUDED_FALSY_VALUES, SortingOrder } from '../../Constants'
2020
import { DEFAULT_PAGE_NUMBER, URL_FILTER_KEYS } from './constants'
2121
import { UseUrlFiltersProps, UseUrlFiltersReturnType } from './types'
22+
import { setItemInLocalStorageIfKeyExists } from './utils'
2223

2324
const { PAGE_SIZE, PAGE_NUMBER, SEARCH_KEY, SORT_BY, SORT_ORDER } = URL_FILTER_KEYS
2425

@@ -42,11 +43,20 @@ const { PAGE_SIZE, PAGE_NUMBER, SEARCH_KEY, SORT_BY, SORT_ORDER } = URL_FILTER_K
4243
const useUrlFilters = <T = string, K = unknown>({
4344
initialSortKey,
4445
parseSearchParams,
46+
localStorageKey,
4547
}: UseUrlFiltersProps<T, K> = {}): UseUrlFiltersReturnType<T, K> => {
4648
const location = useLocation()
4749
const history = useHistory()
4850
const searchParams = new URLSearchParams(location.search)
4951

52+
const getParsedSearchParams: UseUrlFiltersProps<T, K>['parseSearchParams'] = (searchParamsToParse) => {
53+
if (parseSearchParams) {
54+
return parseSearchParams(searchParamsToParse)
55+
}
56+
57+
return {} as K
58+
}
59+
5060
const { pageSize, pageNumber, searchKey, sortBy, sortOrder, parsedParams } = useMemo(() => {
5161
const _pageSize = searchParams.get(PAGE_SIZE)
5262
const _pageNumber = searchParams.get(PAGE_NUMBER)
@@ -58,7 +68,7 @@ const useUrlFilters = <T = string, K = unknown>({
5868
// Fallback to ascending order
5969
const sortByOrder = Object.values(SortingOrder).includes(_sortOrder) ? _sortOrder : SortingOrder.ASC
6070

61-
const _parsedParams = parseSearchParams ? parseSearchParams(searchParams) : ({} as K)
71+
const _parsedParams = getParsedSearchParams(searchParams)
6272

6373
return {
6474
pageSize: Number(_pageSize) || DEFAULT_BASE_PAGE_SIZE,
@@ -126,6 +136,7 @@ const useUrlFilters = <T = string, K = unknown>({
126136

127137
const clearFilters = () => {
128138
history.replace({ search: '' })
139+
setItemInLocalStorageIfKeyExists(localStorageKey, '')
129140
}
130141

131142
const updateSearchParams = (paramsToSerialize: Partial<K>) => {
@@ -143,10 +154,24 @@ const useUrlFilters = <T = string, K = unknown>({
143154
searchParams.delete(key)
144155
}
145156
})
157+
// Skipping primary params => pageSize, pageNumber, searchKey, sortBy, sortOrder
158+
setItemInLocalStorageIfKeyExists(localStorageKey, JSON.stringify(getParsedSearchParams(searchParams)))
146159
// Not replacing the params as it is being done by _resetPageNumber
147160
_resetPageNumber()
148161
}
149162

163+
useEffect(() => {
164+
// if we have search string, set secondary params in local storage accordingly
165+
if (location.search) {
166+
localStorage.setItem(localStorageKey, JSON.stringify(parsedParams))
167+
return
168+
}
169+
const localStorageValue = localStorage.getItem(localStorageKey)
170+
if (localStorageValue) {
171+
updateSearchParams(JSON.parse(localStorageValue))
172+
}
173+
}, [])
174+
150175
return {
151176
pageSize,
152177
changePage,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export const setItemInLocalStorageIfKeyExists = (localStorageKey: string, value: string) => {
2+
if (localStorageKey) {
3+
localStorage.setItem(localStorageKey, value)
4+
}
5+
}

src/Shared/Components/CICDHistory/History.components.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import { ReactComponent as ZoomOut } from '../../../Assets/Icon/ic-exit-fullscre
3434
import './cicdHistory.scss'
3535

3636
export const LogResizeButton = ({
37-
shortcutCombo = ['F'],
37+
shortcutCombo = ['Shift', 'F'],
3838
showOnlyWhenPathIncludesLogs = true,
3939
fullScreenView,
4040
setFullScreenView,

src/Shared/Components/SelectPicker/FilterSelectPicker.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import { useEffect, useMemo, useState } from 'react'
66
import { ReactComponent as ICFilter } from '@Icons/ic-filter.svg'
77
import { ReactComponent as ICFilterApplied } from '@Icons/ic-filter-applied.svg'
8+
import { ComponentSizeType } from '@Shared/constants'
89
import SelectPicker from './SelectPicker.component'
910
import { FilterSelectPickerProps, SelectPickerOptionType, SelectPickerProps } from './type'
11+
import { Button } from '../Button'
1012

1113
const FilterSelectPicker = ({
1214
appliedFilterOptions,
@@ -58,14 +60,13 @@ const FilterSelectPicker = ({
5860

5961
return (
6062
<div className="p-8 dc__border-top-n1">
61-
<button
62-
type="button"
63-
className="dc__unset-button-styles w-100 br-4 h-28 flex bcb-5 cn-0 fw-6 lh-28 fs-12 h-28 br-4 pt-5 pr-12 pb-5 pl-12"
63+
<Button
64+
text="Apply"
65+
dataTestId="filter-select-picker-apply"
6466
onClick={handleApplyClick}
65-
aria-label="Apply filters"
66-
>
67-
Apply
68-
</button>
67+
size={ComponentSizeType.small}
68+
fullWidth
69+
/>
6970
</div>
7071
)
7172
}

src/Shared/Components/SelectPicker/type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ export interface FilterSelectPickerProps
266266
| 'shouldMenuAlignRight'
267267
| 'optionListError'
268268
| 'reloadOptionList'
269+
| 'isOptionDisabled'
269270
> {
270271
appliedFilterOptions: SelectPickerOptionType[]
271272
handleApplyFilter: (filtersToApply: SelectPickerOptionType<number | string>[]) => void

0 commit comments

Comments
 (0)