Skip to content

Commit 9ae46d2

Browse files
committed
Prevents repeated re-init, hides debug in production
Ensures persisted data is restored only once to avoid re-initialization conflicts Restricts environment logs to non-production mode, reducing noise and potential exposure in production
1 parent 2be3589 commit 9ae46d2

File tree

3 files changed

+35
-18
lines changed

3 files changed

+35
-18
lines changed

editor/src/App.tsx

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useState, useEffect, useReducer } from 'react';
1+
import React, { useState, useEffect, useReducer, useRef } from 'react';
22
import './treeview.css';
33
import { TopBar } from './components/TopBar';
44
import { TabBar } from './components/TabBar';
@@ -55,6 +55,9 @@ export default function App() {
5555
const [selectedBayId, setSelectedBayId] = useState<string | null>(null);
5656
const [selectedBayObj, setSelectedBayObj] = useState<Bay | null>(null);
5757

58+
// Track if we've initialized from persistence to prevent re-initialization
59+
const hasInitializedFromPersistence = useRef(false);
60+
5861
const { script } = state;
5962
const { isValid, errors: validationErrors } = state.validation;
6063

@@ -278,10 +281,11 @@ export default function App() {
278281
dispatch({ type: 'SET_VALIDATION', isValid: valid, errors: formatErrors(errors) });
279282
}, [script]);
280283

281-
// Initialize selections from persisted data on startup
284+
// Initialize selections from persisted data on startup (only once)
282285
useEffect(() => {
283-
if (!isLoadingSelections && selections) {
284-
if (selections.facilityId && selections.facilityId !== selectedFacilityId) {
286+
if (!isLoadingSelections && selections && !hasInitializedFromPersistence.current) {
287+
// Only restore facility if we don't already have one selected
288+
if (selections.facilityId && !selectedFacilityId) {
285289
setSelectedFacilityId(selections.facilityId);
286290
console.log('Restored facility ID from persistence:', selections.facilityId);
287291
// Note: The facility object will be restored by the FacilitySelectorPortal
@@ -294,6 +298,9 @@ export default function App() {
294298
if (selections.bayId) {
295299
console.log('Will restore bay ID from persistence:', selections.bayId);
296300
}
301+
302+
// Mark as initialized to prevent re-running
303+
hasInitializedFromPersistence.current = true;
297304
}
298305
}, [isLoadingSelections, selections, selectedFacilityId]);
299306

editor/src/components/EnvironmentDebug.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@ export const EnvironmentDebug: React.FC = () => {
66
const loginBase = import.meta.env.VITE_LOGIN_BASE_URL;
77
const nodeEnv = import.meta.env.VITE_NODE_ENV;
88
const mode = import.meta.env.MODE;
9+
10+
// Check runtime configuration for production mode
11+
const runtimeNodeEnv = (window as any)?.runtimeConfig?.VITE_NODE_ENV;
12+
const isProduction = runtimeNodeEnv === 'production';
913

10-
// Only show in development or when there are issues
11-
if (!isDevelopment && backendBase && loginBase) {
14+
// Only show in development or when there are issues, hide in production
15+
if (isProduction || (!isDevelopment && backendBase && loginBase)) {
1216
return null;
1317
}
1418

editor/src/lib/env.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,20 @@ export const ENV_URLS = {
7979
oauthToken: loginBase ? `${loginBase}/connect/token` : import.meta.env.VITE_OAUTH_TOKEN_URL || '',
8080
};
8181

82-
// Debug environment loading
83-
console.log('🔍 Environment Debug:', {
84-
backendBase,
85-
loginBase,
86-
'runtimeConfig.VITE_NODE_ENV': (window as any)?.runtimeConfig?.VITE_NODE_ENV || 'NOT SET',
87-
'import.meta.env.VITE_NODE_ENV': import.meta.env.VITE_NODE_ENV || 'NOT SET',
88-
'import.meta.env.VITE_BACKEND_BASE_URL': import.meta.env.VITE_BACKEND_BASE_URL,
89-
'import.meta.env.VITE_LOGIN_BASE_URL': import.meta.env.VITE_LOGIN_BASE_URL,
90-
'import.meta.env.VITE_OAUTH_WEB_CLIENT_ID': import.meta.env.VITE_OAUTH_WEB_CLIENT_ID ? 'SET' : 'NOT SET',
91-
ENV_URLS
92-
});
82+
// Debug environment loading (only in development)
83+
const isProduction = (window as any)?.runtimeConfig?.VITE_NODE_ENV === 'production';
84+
if (!isProduction) {
85+
console.log('🔍 Environment Debug:', {
86+
backendBase,
87+
loginBase,
88+
'runtimeConfig.VITE_NODE_ENV': (window as any)?.runtimeConfig?.VITE_NODE_ENV || 'NOT SET',
89+
'import.meta.env.VITE_NODE_ENV': import.meta.env.VITE_NODE_ENV || 'NOT SET',
90+
'import.meta.env.VITE_BACKEND_BASE_URL': import.meta.env.VITE_BACKEND_BASE_URL,
91+
'import.meta.env.VITE_LOGIN_BASE_URL': import.meta.env.VITE_LOGIN_BASE_URL,
92+
'import.meta.env.VITE_OAUTH_WEB_CLIENT_ID': import.meta.env.VITE_OAUTH_WEB_CLIENT_ID ? 'SET' : 'NOT SET',
93+
ENV_URLS
94+
});
95+
}
9396

9497
// OAuth Web Client Configuration (for authorization code flow)
9598
// Function to get OAuth client ID - with runtime support
@@ -160,7 +163,10 @@ export const OAUTH_CONFIG = {
160163
};
161164

162165
export function assertRequiredUrls() {
163-
console.log('🔍 Asserting required URLs:', ENV_URLS);
166+
const isProduction = (window as any)?.runtimeConfig?.VITE_NODE_ENV === 'production';
167+
if (!isProduction) {
168+
console.log('🔍 Asserting required URLs:', ENV_URLS);
169+
}
164170

165171
if (!ENV_URLS.graphql) {
166172
console.error('❌ GraphQL endpoint is not configured:', {

0 commit comments

Comments
 (0)