refactor: enhance mock data detection in useSocket hook

- Added environment variable checks for mock data state

- Prioritized environment variables over localStorage values

- Improved code organization and readability

- Consolidated mock data detection logic
This commit is contained in:
courtmanr@gmail.com
2025-03-15 22:23:14 +00:00
parent 960eb27ee2
commit c34c8adc95
+17 -6
View File
@@ -26,9 +26,15 @@ const useSocket = (url) => {
// For production, we use the same origin that served the page
let socketUrl;
if (isDevelopment) {
// Check if we're using mock data
const useMockData = localStorage.getItem('use_mock_data') === 'true' ||
localStorage.getItem('MOCK_DATA_ENABLED') === 'true';
// First check environment variables, then fall back to localStorage
const envUseMockData = import.meta.env.VITE_USE_MOCK_DATA === 'true';
const envMockDataEnabled = import.meta.env.VITE_MOCK_DATA_ENABLED === 'true';
// If environment variables are set, they take precedence
// Otherwise, check localStorage
const useMockData = (envUseMockData || envMockDataEnabled) ||
(localStorage.getItem('use_mock_data') === 'true' ||
localStorage.getItem('MOCK_DATA_ENABLED') === 'true');
// Always connect to the backend server (7654), which will handle the mock data if needed
socketUrl = `http://${currentHost}:7654`;
@@ -37,9 +43,14 @@ const useSocket = (url) => {
socketUrl = window.location.origin;
}
// Check if we're using mock data
const useMockData = localStorage.getItem('use_mock_data') === 'true' ||
localStorage.getItem('MOCK_DATA_ENABLED') === 'true';
// Check if we're using mock data - prioritize environment variables
const envUseMockData = import.meta.env.VITE_USE_MOCK_DATA === 'true';
const envMockDataEnabled = import.meta.env.VITE_MOCK_DATA_ENABLED === 'true';
// First check environment variables, then fall back to localStorage
const useMockData = (envUseMockData || envMockDataEnabled) ||
(localStorage.getItem('use_mock_data') === 'true' ||
localStorage.getItem('MOCK_DATA_ENABLED') === 'true');
// Store the mock data status as a state variable so it can be exposed in the return value
const [isMockData, setIsMockData] = useState(useMockData);