refactor: add DEBOUNCE_MS to runtime configuration and update usage in Home component

This commit is contained in:
Tudor Stanciu 2025-10-04 20:08:39 +03:00
parent 91f667e4c3
commit e3e77e3d98
5 changed files with 18 additions and 5 deletions

View File

@ -34,6 +34,7 @@ export function generateRuntimeConfig(
const envConfig = { const envConfig = {
BASE_PATH: basePath || '/', BASE_PATH: basePath || '/',
FRONTEND_API_KEY: config.apiKeys.frontend, FRONTEND_API_KEY: config.apiKeys.frontend,
DEBOUNCE_MS: config.debounceMs,
}; };
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars

View File

@ -3,5 +3,6 @@
// eslint-disable-next-line no-undef // eslint-disable-next-line no-undef
window.env = { window.env = {
"BASE_PATH": "/", "BASE_PATH": "/",
"FRONTEND_API_KEY": "frontend-dev-key" "FRONTEND_API_KEY": "frontend-dev-key",
"DEBOUNCE_MS": 2000
}; };

View File

@ -3,9 +3,7 @@ import MapComponent from '../components/MapComponent';
import BitipAPI from '../services/api'; import BitipAPI from '../services/api';
import { SimplifiedGeoIPResponse } from '../types'; import { SimplifiedGeoIPResponse } from '../types';
import './Home.css'; import './Home.css';
import config from '../services/config';
const DEBOUNCE_MS =
parseInt(import.meta.env.VITE_DEBOUNCE_MS as string) || 2000;
const Home: React.FC = () => { const Home: React.FC = () => {
const [inputIP, setInputIP] = useState<string>(''); const [inputIP, setInputIP] = useState<string>('');
@ -31,7 +29,7 @@ const Home: React.FC = () => {
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, DEBOUNCE_MS), }, config.DEBOUNCE_MS),
[] []
); );

View File

@ -0,0 +1,12 @@
const runtimeEnv = window.env || {};
const compileEnv = import.meta.env;
const DEBOUNCE_MS =
runtimeEnv.DEBOUNCE_MS || parseInt(compileEnv.VITE_DEBOUNCE_MS);
const processedEnv = {
DEBOUNCE_MS,
};
const env = { ...compileEnv, ...runtimeEnv, ...processedEnv };
export default env;

View File

@ -12,6 +12,7 @@ declare global {
env: { env: {
BASE_PATH: string; BASE_PATH: string;
FRONTEND_API_KEY: string; FRONTEND_API_KEY: string;
DEBOUNCE_MS: number;
}; };
} }
} }