feat(Core): force map page reload after 30 mins of user inactivity (switched browser/tab)
Some checks failed
Build / 🚀 Deploy to test env (fly.io) (push) Has been cancelled
Build / Manual Approval (push) Has been cancelled
Build / 🛠 Build (1.17, 18.x, 27) (push) Has been cancelled
Build / 🛠 Build Docker Images (linux/amd64) (push) Has been cancelled
Build / 🛠 Build Docker Images (linux/arm64) (push) Has been cancelled
Build / merge (push) Has been cancelled
Build / 🏷 Create Release (push) Has been cancelled

This commit is contained in:
Dmitry Popov
2025-04-17 00:38:20 +02:00
parent f5d502c5ad
commit 2bd343b2da
3 changed files with 19 additions and 19 deletions

View File

@@ -1,14 +1,14 @@
import { ErrorBoundary } from 'react-error-boundary';
import { PrimeReactProvider } from 'primereact/api';
import { ErrorBoundary } from 'react-error-boundary';
import { ReactFlowProvider } from 'reactflow';
import { MapHandlers } from '@/hooks/Mapper/types/mapHandlers.ts';
import { ErrorInfo, useCallback, useEffect, useRef } from 'react';
import { ReactFlowProvider } from 'reactflow';
import { useMapperHandlers } from './useMapperHandlers';
import './common-styles/main.scss';
import { MapRootProvider } from '@/hooks/Mapper/mapRootProvider';
import { MapRootContent } from '@/hooks/Mapper/components/mapRootContent/MapRootContent.tsx';
import { MapRootProvider } from '@/hooks/Mapper/mapRootProvider';
import './common-styles/main.scss';
const ErrorFallback = () => {
return <div className="!z-100 absolute w-screen h-screen bg-transparent"></div>;
@@ -20,7 +20,7 @@ export default function MapRoot({ hooks }) {
const mapperHandlerRefs = useRef([providerRef]);
const { handleCommand, handleMapEvent, handleMapEvents } = useMapperHandlers(mapperHandlerRefs.current, hooksRef);
const { handleCommand, handleMapEvent } = useMapperHandlers(mapperHandlerRefs.current, hooksRef);
const logError = useCallback((error: Error, info: ErrorInfo) => {
if (!hooksRef.current) {
@@ -35,7 +35,6 @@ export default function MapRoot({ hooks }) {
}
hooksRef.current.handleEvent('map_event', handleMapEvent);
hooksRef.current.handleEvent('map_events', handleMapEvents);
}, []);
return (

View File

@@ -1,5 +1,8 @@
import { RefObject, useCallback } from 'react';
import { MapHandlers } from '@/hooks/Mapper/types/mapHandlers.ts';
import { RefObject, useCallback } from 'react';
// Force reload the page after 30 minutes of inactivity
const FORCE_PAGE_RELOAD_TIMEOUT = 1000 * 60 * 30;
export const useMapperHandlers = (handlerRefs: RefObject<MapHandlers>[], hooksRef: RefObject<any>) => {
const handleCommand = useCallback(
@@ -13,7 +16,13 @@ export const useMapperHandlers = (handlerRefs: RefObject<MapHandlers>[], hooksRe
[hooksRef.current],
);
const handleMapEvent = useCallback(({ type, body }) => {
const handleMapEvent = useCallback(({ type, body, timestamp }) => {
const timeDiff = Date.now() - Date.parse(timestamp);
// If the event is older than the timeout, force reload the page
if (timeDiff > FORCE_PAGE_RELOAD_TIMEOUT) {
window.location.reload();
return;
}
handlerRefs.forEach(ref => {
if (!ref.current) {
return;
@@ -23,14 +32,5 @@ export const useMapperHandlers = (handlerRefs: RefObject<MapHandlers>[], hooksRe
});
}, []);
const handleMapEvents = useCallback(
events => {
events.forEach(event => {
handleMapEvent(event);
});
},
[handleMapEvent],
);
return { handleCommand, handleMapEvent, handleMapEvents };
return { handleCommand, handleMapEvent };
};

View File

@@ -279,7 +279,8 @@ defmodule WandererAppWeb.MapEventHandler do
socket
|> Phoenix.LiveView.Utils.push_event("map_event", %{
type: type,
body: body
body: body,
timestamp: DateTime.utc_now()
})
end