k8s improvement
This commit is contained in:
@@ -4,27 +4,30 @@
|
||||
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { useAuth0 } from '@auth0/auth0-react';
|
||||
import { DataSyncManager } from '../sync/data-sync';
|
||||
import { useNavigationStore } from '../store/navigation';
|
||||
|
||||
export const useDataSync = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { isAuthenticated } = useAuth0();
|
||||
const syncManagerRef = useRef<DataSyncManager | null>(null);
|
||||
const navigationStore = useNavigationStore();
|
||||
|
||||
useEffect(() => {
|
||||
// Initialize data sync manager
|
||||
// Initialize data sync manager with authentication guard
|
||||
syncManagerRef.current = new DataSyncManager(queryClient, {
|
||||
enableCrossTabs: true,
|
||||
enableOptimisticUpdates: true,
|
||||
enableBackgroundSync: true,
|
||||
syncInterval: 30000,
|
||||
isAuthenticated: () => isAuthenticated,
|
||||
});
|
||||
|
||||
return () => {
|
||||
syncManagerRef.current?.cleanup();
|
||||
};
|
||||
}, [queryClient]);
|
||||
}, [queryClient, isAuthenticated]);
|
||||
|
||||
// Listen for navigation changes and trigger prefetching
|
||||
useEffect(() => {
|
||||
|
||||
@@ -6,12 +6,14 @@ import { QueryClient } from '@tanstack/react-query';
|
||||
import { useNavigationStore } from '../store/navigation';
|
||||
import { useUserStore } from '../store/user';
|
||||
import { Vehicle } from '../../features/vehicles/types/vehicles.types';
|
||||
import { apiClient } from '../api/client';
|
||||
|
||||
interface SyncConfig {
|
||||
enableCrossTabs: boolean;
|
||||
enableOptimisticUpdates: boolean;
|
||||
enableBackgroundSync: boolean;
|
||||
syncInterval: number;
|
||||
isAuthenticated?: () => boolean;
|
||||
}
|
||||
|
||||
export class DataSyncManager {
|
||||
@@ -119,6 +121,11 @@ export class DataSyncManager {
|
||||
}
|
||||
|
||||
private async performBackgroundSync() {
|
||||
// Guard: only sync if user is authenticated
|
||||
if (this.config.isAuthenticated && !this.config.isAuthenticated()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Update last sync timestamp
|
||||
useUserStore.getState().updateLastSync();
|
||||
@@ -147,28 +154,14 @@ export class DataSyncManager {
|
||||
// Helper method to fetch vehicle by ID (would normally import from vehicles API)
|
||||
private async fetchVehicleById(id: string): Promise<Vehicle | null> {
|
||||
try {
|
||||
const response = await fetch(`/api/vehicles/${id}`, {
|
||||
headers: {
|
||||
'Authorization': this.getAuthHeader(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
const response = await apiClient.get(`/vehicles/${id}`);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.warn(`Failed to fetch vehicle ${id}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private getAuthHeader(): string {
|
||||
// This would integrate with Auth0 token from interceptor
|
||||
// For now, return empty string as token is handled by axios interceptor
|
||||
return '';
|
||||
}
|
||||
|
||||
// Public methods for optimistic updates
|
||||
public async optimisticVehicleUpdate(vehicleId: string, updates: Partial<Vehicle>) {
|
||||
@@ -196,6 +189,12 @@ export class DataSyncManager {
|
||||
}
|
||||
|
||||
public async prefetchForNavigation(targetScreen: string) {
|
||||
// Guard: only prefetch if user is authenticated
|
||||
if (this.config.isAuthenticated && !this.config.isAuthenticated()) {
|
||||
console.log('DataSync: Skipping prefetch - user not authenticated');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
switch (targetScreen) {
|
||||
case 'Vehicles':
|
||||
@@ -226,17 +225,8 @@ export class DataSyncManager {
|
||||
|
||||
private async fetchVehicles(): Promise<Vehicle[]> {
|
||||
try {
|
||||
const response = await fetch('/api/vehicles', {
|
||||
headers: {
|
||||
'Authorization': this.getAuthHeader(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
const response = await apiClient.get('/vehicles');
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.warn('Failed to fetch vehicles:', error);
|
||||
return [];
|
||||
|
||||
Reference in New Issue
Block a user