mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-10 18:03:47 +00:00
31d47ed1e5
* refactor: WS * refactor refetch extract refetch keys to package/types auto invalidata all keys use refetch keys for project data no need for constant update of info, just fetch when looking at it split refetch and query keys rename types * refactor viewSettings * fixup! refactor: WS * rearange files and make types for api calls * cleanup viewsettings * switch exhaustiveCheck * combine send socket function * exhaustive check * rename type to tag * fixup! fixup! refactor: WS * remove custom etag solution * get errors from socket * lint * small change --------- Co-authored-by: Carlos Valente <carlosvalente@pm.me>
44 lines
1.2 KiB
TypeScript
44 lines
1.2 KiB
TypeScript
import { useEffect } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router-dom';
|
|
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]);
|
|
};
|