mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 17:30:16 +01:00
36 lines
1 KiB
TypeScript
36 lines
1 KiB
TypeScript
|
|
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;
|
||
|
|
};
|