mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-19 14:14:17 +00:00
refactor: improve redirect
This commit is contained in:
committed by
Carlos Valente
parent
4c6acc4013
commit
d46dfcf82e
@@ -4,16 +4,23 @@ import { useLocation, useNavigate } from 'react-router-dom';
|
|||||||
import { useClientStore } from '../stores/clientStore';
|
import { useClientStore } from '../stores/clientStore';
|
||||||
import { socketSendJson } from '../utils/socket';
|
import { socketSendJson } from '../utils/socket';
|
||||||
|
|
||||||
|
import { useIsOnline } from './useSocket';
|
||||||
|
|
||||||
export const useClientPath = () => {
|
export const useClientPath = () => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { pathname, search } = useLocation();
|
const { pathname, search } = useLocation();
|
||||||
const redirect = useClientStore((store) => store.redirect);
|
const { redirect, setRedirect } = useClientStore((store) => ({
|
||||||
const setRedirect = useClientStore((store) => store.setRedirect);
|
redirect: store.redirect,
|
||||||
|
setRedirect: store.setRedirect,
|
||||||
|
}));
|
||||||
|
const isOnline = useIsOnline();
|
||||||
|
|
||||||
// notify of client path changes
|
// notify of client path changes
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
if (!isOnline) return;
|
||||||
|
|
||||||
socketSendJson('set-client-path', pathname + search);
|
socketSendJson('set-client-path', pathname + search);
|
||||||
}, [pathname, search]);
|
}, [pathname, search, isOnline]);
|
||||||
|
|
||||||
// navigate to new path when received from server
|
// navigate to new path when received from server
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -26,7 +33,7 @@ export const useClientPath = () => {
|
|||||||
|
|
||||||
// navigate if there is a path change
|
// navigate if there is a path change
|
||||||
if (redirect !== pathname + search) {
|
if (redirect !== pathname + search) {
|
||||||
navigate(redirect);
|
navigate(redirect, { replace: true });
|
||||||
}
|
}
|
||||||
}, [navigate, pathname, redirect, search, setRedirect]);
|
}, [navigate, pathname, redirect, search, setRedirect]);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -238,3 +238,12 @@ export const usePing = () => {
|
|||||||
|
|
||||||
return useRuntimeStore(featureSelector);
|
return useRuntimeStore(featureSelector);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** convert ping into a derived value which changes less often */
|
||||||
|
export const useIsOnline = () => {
|
||||||
|
const featureSelector = (state: RuntimeStore) => ({
|
||||||
|
isOnline: state.ping > 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
return useRuntimeStore(featureSelector);
|
||||||
|
};
|
||||||
|
|||||||
@@ -39,7 +39,6 @@ export const connectSocket = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
socketSendJson('set-client-type', 'ontime');
|
socketSendJson('set-client-type', 'ontime');
|
||||||
socketSendJson('set-client-path', location.pathname + location.search);
|
|
||||||
setOnlineStatus(true);
|
setOnlineStatus(true);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user