mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-04 23:18:01 +00:00
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router';
|
|
import { MessageTag } from 'ontime-types';
|
|
import { useShallow } from 'zustand/shallow';
|
|
|
|
import { useClientStore } from '../stores/clientStore';
|
|
import { sendSocket } from '../utils/socket';
|
|
|
|
import { useIsOnline } from './useSocket';
|
|
|
|
export const useClientPath = () => {
|
|
const navigate = useNavigate();
|
|
const { pathname, search } = useLocation();
|
|
const { redirect, setRedirect } = useClientStore(
|
|
useShallow((store) => ({
|
|
redirect: store.redirect,
|
|
setRedirect: store.setRedirect,
|
|
})),
|
|
);
|
|
const isOnline = useIsOnline();
|
|
|
|
// notify of client path changes
|
|
useEffect(() => {
|
|
if (!isOnline) return;
|
|
|
|
sendSocket(MessageTag.ClientSetPath, pathname + search);
|
|
}, [pathname, search, isOnline]);
|
|
|
|
// 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, { replace: true });
|
|
}
|
|
}, [navigate, pathname, redirect, search, setRedirect]);
|
|
};
|