From cb5bcdbb9c407159c258f7e0c7b36c84d38aeda2 Mon Sep 17 00:00:00 2001 From: Chirag Madlani <12962843+chirag-madlani@users.noreply.github.com> Date: Fri, 5 Sep 2025 23:06:30 +0530 Subject: [PATCH] fix(ui): pick right mode env production / dev (#23272) * fix(ui): pick right mode env production / dev * fix unit tests --- .../src/main/resources/ui/src/setupTests.js | 4 +++ .../ui/src/utils/EnvironmentUtils.ts | 11 +++++--- .../ui/src/utils/ServiceUtilClassBase.ts | 4 +-- .../ui/src/utils/date-time/DateTimeUtils.ts | 20 ++++++++------ .../src/main/resources/ui/src/vite-env.d.ts | 26 +++++++++++++++++++ 5 files changed, 52 insertions(+), 13 deletions(-) create mode 100644 openmetadata-ui/src/main/resources/ui/src/vite-env.d.ts diff --git a/openmetadata-ui/src/main/resources/ui/src/setupTests.js b/openmetadata-ui/src/main/resources/ui/src/setupTests.js index 49ef7c1a593..a717e34a025 100644 --- a/openmetadata-ui/src/main/resources/ui/src/setupTests.js +++ b/openmetadata-ui/src/main/resources/ui/src/setupTests.js @@ -144,3 +144,7 @@ jest.mock('./utils/AdvancedSearchClassBase', () => { }, }; }); + +jest.mock('./utils/EnvironmentUtils', () => ({ + isDev: jest.fn().mockReturnValue('test'), +})); diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/EnvironmentUtils.ts b/openmetadata-ui/src/main/resources/ui/src/utils/EnvironmentUtils.ts index 27ff786412b..666c6788c86 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/EnvironmentUtils.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/EnvironmentUtils.ts @@ -11,10 +11,15 @@ * limitations under the License. */ -import process from 'process'; +const development: boolean = (() => { + // Use Vite's import.meta.env which is properly typed now + if (typeof import.meta !== 'undefined' && import.meta.env) { + return import.meta.env.MODE === 'development'; + } -const development: boolean = - !process.env.NODE_ENV || process.env.NODE_ENV === 'development'; + // Fallback to process.env (defined by Vite's define option) + return !process.env.NODE_ENV || process.env.NODE_ENV === 'development'; +})(); export const isDev = (): boolean => { return development; diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/ServiceUtilClassBase.ts b/openmetadata-ui/src/main/resources/ui/src/utils/ServiceUtilClassBase.ts index fe20c941de8..717169a6dab 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/ServiceUtilClassBase.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/ServiceUtilClassBase.ts @@ -371,7 +371,7 @@ class ServiceUtilClassBase { return EntityType.TABLE; } - public getServiceLogo(type: string) { + public getServiceLogo(type: string): string { const serviceTypes = this.getSupportedServiceFromList(); switch (toLower(type)) { case this.DatabaseServiceTypeSmallCase.CustomDatabase: @@ -684,7 +684,7 @@ class ServiceUtilClassBase { public getServiceTypeLogo( searchSource: SearchSuggestions[number] | SearchSourceAlias - ) { + ): string { const type = get(searchSource, 'serviceType', ''); const entityType = get(searchSource, 'entityType', ''); diff --git a/openmetadata-ui/src/main/resources/ui/src/utils/date-time/DateTimeUtils.ts b/openmetadata-ui/src/main/resources/ui/src/utils/date-time/DateTimeUtils.ts index 187ba3fbb51..e32a4bcbbb0 100644 --- a/openmetadata-ui/src/main/resources/ui/src/utils/date-time/DateTimeUtils.ts +++ b/openmetadata-ui/src/main/resources/ui/src/utils/date-time/DateTimeUtils.ts @@ -57,9 +57,9 @@ export const formatDateTimeLong = (timestamp?: number, format?: string) => { return ''; } - return DateTime.fromMillis(toNumber(timestamp), { locale: i18next.language }).toFormat( - format ?? DATE_TIME_WITH_OFFSET_FORMAT - ); + return DateTime.fromMillis(toNumber(timestamp), { + locale: i18next.language, + }).toFormat(format ?? DATE_TIME_WITH_OFFSET_FORMAT); }; /** @@ -121,9 +121,9 @@ export const customFormatDateTime = ( return formatDateTime(milliseconds); } - return DateTime.fromMillis(milliseconds, { locale: i18next.language }).toFormat( - format - ); + return DateTime.fromMillis(milliseconds, { + locale: i18next.language, + }).toFormat(format); }; /** @@ -133,7 +133,9 @@ export const customFormatDateTime = ( */ export const getRelativeTime = (timeStamp?: number): string => { return !isNil(timeStamp) - ? DateTime.fromMillis(timeStamp, { locale: i18next.language }).toRelative() ?? '' + ? DateTime.fromMillis(timeStamp, { + locale: i18next.language, + }).toRelative() ?? '' : ''; }; @@ -176,7 +178,9 @@ export const getRelativeCalendar = ( baseTimeStamp?: number ): string => { return capitalize( - DateTime.fromMillis(timeStamp, { locale: i18next.language }).toRelativeCalendar({ + DateTime.fromMillis(timeStamp, { + locale: i18next.language, + }).toRelativeCalendar({ base: baseTimeStamp ? DateTime.fromMillis(baseTimeStamp, { locale: i18next.language }) : DateTime.now(), diff --git a/openmetadata-ui/src/main/resources/ui/src/vite-env.d.ts b/openmetadata-ui/src/main/resources/ui/src/vite-env.d.ts new file mode 100644 index 00000000000..571be2c755e --- /dev/null +++ b/openmetadata-ui/src/main/resources/ui/src/vite-env.d.ts @@ -0,0 +1,26 @@ +/* + * Copyright 2025 Collate. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +// eslint-disable-next-line spaced-comment +/// + +interface ImportMetaEnv { + readonly MODE: string; + readonly BASE_URL: string; + readonly PROD: boolean; + readonly DEV: boolean; + // Add other env variables as needed +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +}