feat(pwa): implement service worker and PWA enhancements
- Added service worker (sw.js) for offline functionality, caching strategies, and performance improvements. - Registered service worker in App component to manage updates and offline readiness. - Introduced ServiceWorkerStatus component to display connection status and provide cache management controls. - Created manifest.json for PWA configuration, including app name, icons, and display settings. - Updated index.html with PWA meta tags and links to support mobile web app capabilities. - Refactored authentication guards to utilize useAuthStatus hook for improved user state management. - Removed deprecated unregister-sw.js file to streamline service worker management.
This commit is contained in:
52
worklenz-frontend/src/hooks/useAuthStatus.ts
Normal file
52
worklenz-frontend/src/hooks/useAuthStatus.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { useMemo } from 'react';
|
||||
import { useLocation } from 'react-router-dom';
|
||||
import { useAuthService } from '@/hooks/useAuth';
|
||||
import { ISUBSCRIPTION_TYPE } from '@/shared/constants';
|
||||
|
||||
export const useAuthStatus = () => {
|
||||
const authService = useAuthService();
|
||||
const location = useLocation();
|
||||
|
||||
const status = useMemo(() => {
|
||||
try {
|
||||
if (!authService || typeof authService.isAuthenticated !== 'function') {
|
||||
return { isAuthenticated: false, isLicenseExpired: false, isAdmin: false, isSetupComplete: false };
|
||||
}
|
||||
|
||||
const isAuthenticated = authService.isAuthenticated();
|
||||
if (!isAuthenticated) {
|
||||
return { isAuthenticated: false, isLicenseExpired: false, isAdmin: false, isSetupComplete: false };
|
||||
}
|
||||
|
||||
const currentSession = authService.getCurrentSession();
|
||||
const isFreePlan = currentSession?.subscription_type === ISUBSCRIPTION_TYPE.FREE;
|
||||
const isAdmin = authService.isOwnerOrAdmin() && !isFreePlan;
|
||||
const isSetupComplete = currentSession?.setup_completed ?? false;
|
||||
|
||||
const isLicenseExpired = () => {
|
||||
if (!currentSession) return false;
|
||||
if (currentSession.is_expired) return true;
|
||||
|
||||
if (
|
||||
currentSession.subscription_type === ISUBSCRIPTION_TYPE.TRIAL &&
|
||||
currentSession.trial_expire_date
|
||||
) {
|
||||
const today = new Date();
|
||||
const expiryDate = new Date(currentSession.trial_expire_date);
|
||||
const diffTime = today.getTime() - expiryDate.getTime();
|
||||
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
|
||||
return diffDays > 7;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
return { isAuthenticated, isLicenseExpired: isLicenseExpired(), isAdmin, isSetupComplete };
|
||||
} catch (error) {
|
||||
console.error('Error in useAuthStatus:', error);
|
||||
return { isAuthenticated: false, isLicenseExpired: false, isAdmin: false, isSetupComplete: false };
|
||||
}
|
||||
}, [authService]);
|
||||
|
||||
return { ...status, location };
|
||||
};
|
||||
Reference in New Issue
Block a user