mirror of
https://github.com/fleetdm/fleet
synced 2026-04-21 21:47:20 +00:00
## #21468 - Memoize `app` and `table` context for improved stability - Update policies page dependencies to reference now stable context values - Widespread updates to logic to enable No teams on the Manage polices page, PolicyPage, and related pages and flows _Outstanding bugs to address:_ - [x] When navigating from another page with "No team" to Policies, team is reset to "All teams" - [x] same after saving or editing a no-team policy_  - [x] Changes file added for user-visible changes in `changes/` - [x] Manual QA for all new/changed functionality --------- Co-authored-by: Lucas Rodriguez <lucas@fleetdm.com> Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import React, { createContext, useReducer, useMemo, ReactNode } from "react";
|
|
|
|
type Props = {
|
|
children: ReactNode;
|
|
};
|
|
|
|
type InitialStateType = {
|
|
resetSelectedRows: boolean;
|
|
setResetSelectedRows: (update: boolean) => void;
|
|
};
|
|
|
|
const initialState = {
|
|
resetSelectedRows: false,
|
|
setResetSelectedRows: () => null,
|
|
};
|
|
|
|
const actions = {
|
|
RESET_SELECTED_ROWS: "RESET_SELECTED_ROWS",
|
|
};
|
|
|
|
const reducer = (state: any, action: any) => {
|
|
switch (action.type) {
|
|
case actions.RESET_SELECTED_ROWS:
|
|
return { ...state, resetSelectedRows: action.resetSelectedRows };
|
|
default:
|
|
return state;
|
|
}
|
|
};
|
|
|
|
export const TableContext = createContext<InitialStateType>(initialState);
|
|
|
|
const TableProvider = ({ children }: Props) => {
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
|
|
|
const value = useMemo(
|
|
() => ({
|
|
resetSelectedRows: state.resetSelectedRows,
|
|
setResetSelectedRows: (resetSelectedRows: boolean) => {
|
|
dispatch({ type: actions.RESET_SELECTED_ROWS, resetSelectedRows });
|
|
},
|
|
}),
|
|
[state.resetSelectedRows]
|
|
);
|
|
|
|
return (
|
|
<TableContext.Provider value={value}>{children}</TableContext.Provider>
|
|
);
|
|
};
|
|
|
|
export default TableProvider;
|