2023-12-31 16:35:15 +03:00
|
|
|
import { useCallback } from "react";
|
|
|
|
|
import { useLocation, useNavigate } from "react-router-dom";
|
|
|
|
|
|
|
|
|
|
export default function useOverlayState(key: string) {
|
|
|
|
|
const location = useLocation();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const currentLocationState = location.state;
|
|
|
|
|
|
|
|
|
|
const setOverlayStateValue = useCallback(
|
|
|
|
|
(value: string) => {
|
|
|
|
|
const newLocationState = { ...currentLocationState };
|
|
|
|
|
newLocationState[key] = value;
|
|
|
|
|
navigate(location.pathname, { state: newLocationState });
|
|
|
|
|
},
|
2024-02-29 01:23:56 +03:00
|
|
|
// we know that these deps are correct
|
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
|
[key, navigate],
|
2023-12-31 16:35:15 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const overlayStateValue = location.state && location.state[key];
|
|
|
|
|
return [overlayStateValue, setOverlayStateValue];
|
|
|
|
|
}
|