-
Notifications
You must be signed in to change notification settings - Fork 593
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7326 from sahil143/odc-5038
convert Topology filters localStorage to userSettings
- Loading branch information
Showing
10 changed files
with
103 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as React from 'react'; | ||
import { useUserSettingsCompatibility } from '@console/shared'; | ||
import { DEFAULT_TOPOLOGY_FILTERS } from './const'; | ||
import { DisplayFilters } from '../topology-types'; | ||
import { TOPOLOGY_DISPLAY_FILTERS_LOCAL_STORAGE_KEY } from '../redux/const'; | ||
|
||
const TOPOLOGY_DISPLAY_FILTERS_USER_SETTINGS_KEY = `devconsole.topology.filters`; | ||
|
||
const getTopologyFilters = (appliedFilters: AppliedFilters) => { | ||
const filters = [...DEFAULT_TOPOLOGY_FILTERS]; | ||
filters.forEach((filter) => { | ||
if (appliedFilters[filter.id] !== undefined) { | ||
filter.value = appliedFilters[filter.id]; | ||
} | ||
}); | ||
|
||
return filters; | ||
}; | ||
|
||
const getAppliedFilters = (filters: DisplayFilters): { [id: string]: boolean } => { | ||
if (!filters?.length) { | ||
return {}; | ||
} | ||
|
||
return filters.reduce((acc, filter) => { | ||
acc[filter.id] = filter.value; | ||
return acc; | ||
}, {}); | ||
}; | ||
|
||
type AppliedFilters = { [filterKey: string]: boolean }; | ||
type SetTopologyFilters = (filters: DisplayFilters) => void; | ||
|
||
const useFilterContextValues = (): [ | ||
DisplayFilters, | ||
AppliedFilters, | ||
boolean, | ||
SetTopologyFilters, | ||
] => { | ||
const [appliedFilters, setAppliedFilters, appliedFiltersLoaded] = useUserSettingsCompatibility( | ||
TOPOLOGY_DISPLAY_FILTERS_USER_SETTINGS_KEY, | ||
TOPOLOGY_DISPLAY_FILTERS_LOCAL_STORAGE_KEY, | ||
getAppliedFilters(DEFAULT_TOPOLOGY_FILTERS), | ||
); | ||
const [filtersLoaded, setFiltersLoaded] = React.useState<boolean>(false); | ||
const [filters, setFilters] = React.useState<DisplayFilters>([]); | ||
|
||
React.useEffect(() => { | ||
if (appliedFiltersLoaded && !filtersLoaded) { | ||
setFilters(getTopologyFilters(appliedFilters)); | ||
setFiltersLoaded(true); | ||
} | ||
}, [appliedFilters, appliedFiltersLoaded, filtersLoaded]); | ||
|
||
const setTopologyFilters = React.useCallback( | ||
(displayFilters: DisplayFilters) => { | ||
setFilters(displayFilters); | ||
setAppliedFilters(getAppliedFilters(displayFilters)); | ||
}, | ||
[setAppliedFilters], | ||
); | ||
|
||
return [filters, appliedFilters, appliedFilters && filtersLoaded, setTopologyFilters]; | ||
}; | ||
|
||
type FilterContextType = { | ||
filters?: DisplayFilters; | ||
appliedFilters?: AppliedFilters; | ||
setTopologyFilters?: SetTopologyFilters; | ||
}; | ||
|
||
export const FilterContext = React.createContext<FilterContextType>({}); | ||
|
||
export const FilterProvider: React.FC = ({ children }) => { | ||
const [filters, appliedFilters, loaded, setTopologyFilters] = useFilterContextValues(); | ||
return ( | ||
<FilterContext.Provider value={{ filters, appliedFilters, setTopologyFilters }}> | ||
{loaded ? children : null} | ||
</FilterContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 4 additions & 9 deletions
13
frontend/packages/topology/src/filters/useAppliedDisplayFilters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,8 @@ | ||
// FIXME upgrading redux types is causing many errors at this time | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-ignore | ||
// @ts-ignore | ||
import { useSelector } from 'react-redux'; | ||
import { RootState } from '@console/internal/redux'; | ||
import { DisplayFilters } from '../topology-types'; | ||
import { getAppliedTopologyFilters } from './filter-utils'; | ||
import { useContext } from 'react'; | ||
import { FilterContext } from './FilterProvider'; | ||
|
||
const useAppliedDisplayFilters = (): DisplayFilters => { | ||
return useSelector((state: RootState) => getAppliedTopologyFilters(state)); | ||
const useAppliedDisplayFilters = (): { [filterKey: string]: boolean } => { | ||
return useContext(FilterContext).appliedFilters; | ||
}; | ||
|
||
export { useAppliedDisplayFilters }; |
12 changes: 4 additions & 8 deletions
12
frontend/packages/topology/src/filters/useDisplayFilters.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.