mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-05 07:28:01 +00:00
a15eb0db4c
--------- Co-authored-by: kellhogs <fabianpos99@gmail.com> Co-authored-by: Carlos Valente <carlosvalente@pm.me> Co-authored-by: Carlos Valente <34649812+cpvalente@users.noreply.github.com>
35 lines
1.0 KiB
TypeScript
35 lines
1.0 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
|
|
import { useClientStore } from '../stores/clientStore';
|
|
import { socketSendJson } from '../utils/socket';
|
|
|
|
export const useClientPath = () => {
|
|
const navigate = useNavigate();
|
|
const { pathname, search } = useLocation();
|
|
const redirect = useClientStore((store) => store.redirect);
|
|
const setRedirect = useClientStore((store) => store.setRedirect);
|
|
|
|
// notify of client path changes
|
|
useEffect(() => {
|
|
//remove leading '/' from path
|
|
const fullPath = (pathname.startsWith('/') ? pathname.slice(1) : pathname) + search;
|
|
socketSendJson('set-client-path', fullPath);
|
|
}, [pathname, search]);
|
|
|
|
// navigate to new path when received from server
|
|
useEffect(() => {
|
|
if (redirect === '') {
|
|
return;
|
|
}
|
|
|
|
// clear redirect
|
|
setRedirect('');
|
|
|
|
// navigate if there is a path change
|
|
if (redirect !== pathname + search) {
|
|
navigate(redirect);
|
|
}
|
|
}, [navigate, pathname, redirect, search, setRedirect]);
|
|
};
|