summaryrefslogtreecommitdiff
path: root/extension/react-app/src/redux/hooks.ts
blob: a6aef869ec81f1ffdb6f4f43030ce2fb0cb30303 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { RootStore } from "./store";
import { selectDebugContextValue } from "./selectors/debugContextSelectors";
import { updateValue } from "./slices/debugContexSlice";
import { SerializedDebugContext } from "../../../src/client";

export function useDebugContextValue(
  key: keyof SerializedDebugContext,
  defaultValue: any
): [any, (value: any) => void] {
  const dispatch = useDispatch();
  const state =
    useSelector((state: RootStore) => selectDebugContextValue(state, key)) ||
    defaultValue;
  const boundAction = useCallback(
    (value: any) => dispatch(updateValue({ key, value })),
    [dispatch, key]
  );
  return [state, boundAction];
}