fleet/frontend/context/table.tsx
jacobshandling 549e9c87cc
UI - Enable "No team" for Policies (#21885)
## #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_


![ezgif-4-7675c92400](https://github.com/user-attachments/assets/205cf6e4-750e-4f87-9a6b-33b6b1edb7b3)


- [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>
2024-09-16 12:40:52 -04:00

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;