File size: 1,054 Bytes
f0743f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useCallback, useEffect, useRef } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';
import type { Location } from 'react-router-dom';

export function useCustomLink<T = HTMLAnchorElement>(
  route: string,
  callback?: (event: React.MouseEvent<T>) => void,
) {
  const navigate = useNavigate();
  const location = useLocation();
  const clickHandler = useCallback(
    (event: React.MouseEvent<T>) => {
      if (callback) {
        callback(event);
      }
      if (event.button === 0 && !(event.ctrlKey || event.metaKey)) {
        event.preventDefault();
        navigate(route, { state: { prevLocation: location } });
      }
    },
    [navigate, route, callback, location],
  );
  return clickHandler;
}

export const usePreviousLocation = () => {
  const location = useLocation();
  const previousLocationRef: React.MutableRefObject<Location<unknown> | undefined> = useRef();

  useEffect(() => {
    previousLocationRef.current = location.state?.prevLocation;
  }, [location]);

  return previousLocationRef;
};