Skip to content

Commit

Permalink
fix(ui): memoize selectors for namespaces (#1837)
Browse files Browse the repository at this point in the history
  • Loading branch information
markphelps authored Jul 7, 2023
1 parent 3679649 commit 04a54df
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions ui/src/app/namespaces/namespacesSlice.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
import {
createAsyncThunk,
createSelector,
createSlice
} from '@reduxjs/toolkit';
import {
createNamespace,
deleteNamespace,
Expand Down Expand Up @@ -68,17 +72,27 @@ export const namespacesSlice = createSlice({

export const { currentNamespaceChanged } = namespacesSlice.actions;

export const selectNamespaces = (state: RootState) =>
Object.entries(state.namespaces.namespaces).map(
([_, value]) => value
) as INamespace[];
export const selectNamespaces = createSelector(
[
(state: RootState) =>
Object.entries(state.namespaces.namespaces).map(
([_, value]) => value
) as INamespace[]
],
(namespaces) => namespaces
);

export const selectCurrentNamespace = (state: RootState) => {
if (state.namespaces.status === LoadingStatus.SUCCEEDED) {
return state.namespaces.namespaces[state.namespaces.currentNamespace];
}
return { key: 'default', name: 'Default', description: '' } as INamespace;
};
export const selectCurrentNamespace = createSelector(
[
(state: RootState) => {
if (state.namespaces.status === LoadingStatus.SUCCEEDED) {
return state.namespaces.namespaces[state.namespaces.currentNamespace];
}
return { key: 'default', name: 'Default', description: '' } as INamespace;
}
],
(currentNamespace) => currentNamespace
);

export const fetchNamespacesAsync = createAsyncThunk(
'namespaces/fetchNamespaces',
Expand Down

0 comments on commit 04a54df

Please sign in to comment.