From 3c2b3ca5ef4d7e25f027b1c6838cbab24e6bf96a Mon Sep 17 00:00:00 2001
From: Leonardo <leomilho@gmail.com>
Date: Tue, 5 Nov 2024 14:15:14 +0100
Subject: [PATCH] Revert "feat: fallback for all env variables to prevent
 crash"

This reverts commit 6d5ed2284943a19abe8f556ff4321f4bb3cee4d0.
---
 apps/web/src/components/navbar/navbar.tsx     |  9 ++-
 apps/web/src/main.tsx                         |  5 +-
 libs/config/src/featureFlags.ts               | 61 +++++++------------
 libs/shared/lib/data-access/broker/broker.tsx |  3 +-
 .../security/useAuthentication.tsx            |  5 +-
 libs/shared/lib/inspector/InspectorPanel.tsx  |  3 +-
 libs/shared/lib/sidebar/index.tsx             |  5 +-
 7 files changed, 34 insertions(+), 57 deletions(-)

diff --git a/apps/web/src/components/navbar/navbar.tsx b/apps/web/src/components/navbar/navbar.tsx
index ecd864a38..5cb0e428e 100644
--- a/apps/web/src/components/navbar/navbar.tsx
+++ b/apps/web/src/components/navbar/navbar.tsx
@@ -13,9 +13,8 @@ import { useAuthCache, useAuthentication } from '@graphpolaris/shared/lib/data-a
 import { DropdownItem } from '@graphpolaris/shared/lib/components/dropdowns';
 import GpLogo from './gp-logo';
 import { Popover, PopoverContent, PopoverTrigger } from '@graphpolaris/shared/lib/components/layout/Popover';
-import { useDispatch } from 'react-redux';
-import { getEnvVariable, showManagePermissions, showSharableExploration } from 'config';
-import { Button, Dialog, DialogContent, DialogTrigger, useActiveSaveStateAuthorization, useSessionCache } from '@graphpolaris/shared';
+import { showSharableExploration } from 'config';
+import { Button, useActiveSaveStateAuthorization } from '@graphpolaris/shared';
 import { ManagementTrigger, ManagementViews } from '@graphpolaris/shared/lib/management';
 
 const AuthURL = import.meta.env.GP_AUTH_URL;
@@ -26,7 +25,7 @@ export const Navbar = () => {
   const authCache = useAuthCache();
   const authorization = useActiveSaveStateAuthorization();
   const [menuOpen, setMenuOpen] = useState(false);
-  const buildInfo = getEnvVariable('GRAPHPOLARIS_VERSION');
+  const buildInfo = import.meta.env.GRAPHPOLARIS_VERSION;
   const [managementOpen, setManagementOpen] = useState<boolean>(false);
   const [current, setCurrent] = useState<ManagementViews>('overview');
 
@@ -89,7 +88,7 @@ export const Navbar = () => {
                   <DropdownItem
                     value="Log out"
                     onClick={() => {
-                      location.replace(`${getEnvVariable('GP_AUTH_URL')}/outpost.goauthentik.io/sign_out`);
+                      location.replace(`${AuthURL}/outpost.goauthentik.io/sign_out`);
                     }}
                   />
                 </>
diff --git a/apps/web/src/main.tsx b/apps/web/src/main.tsx
index f7029e4d6..e25807e6d 100644
--- a/apps/web/src/main.tsx
+++ b/apps/web/src/main.tsx
@@ -5,13 +5,12 @@ import * as Sentry from '@sentry/react';
 import { store } from '@graphpolaris/shared/lib/data-access/store';
 import App from './app/App';
 import { createRoot } from 'react-dom/client';
-import { getEnvVariable } from 'config';
 import './main.css';
 import { ErrorBoundary } from '@graphpolaris/shared/lib/components/errorBoundary';
 
-if (getEnvVariable('SENTRY_ENABLED')) {
+if (import.meta.env.SENTRY_ENABLED) {
   Sentry.init({
-    dsn: getEnvVariable('SENTRY_URL'),
+    dsn: import.meta.env.SENTRY_URL,
     integrations: [Sentry.browserTracingIntegration(), Sentry.replayIntegration()],
     tracesSampleRate: 1.0,
     replaysSessionSampleRate: 0.1,
diff --git a/libs/config/src/featureFlags.ts b/libs/config/src/featureFlags.ts
index 21d94384f..b6364d037 100644
--- a/libs/config/src/featureFlags.ts
+++ b/libs/config/src/featureFlags.ts
@@ -1,49 +1,32 @@
-const envFallbacks: Record<string, any> = {
-  SENTRY_ENABLED: false,
-  SENTRY_URL: '',
-  GRAPHPOLARIS_VERSION: 'prod',
-  GP_AUTH_URL: '',
-  BACKEND_WSS_URL: '',
-  BACKEND_URL: '',
-  BACKEND_USER: '',
-  WIP_TABLEVIS: true,
-  WIP_NODELINKVIS: true,
-  WIP_RAWJSONVIS: true,
-  WIP_PAOHVIS: true,
-  WIP_MATRIXVIS: true,
-  WIP_SEMANTICSUBSTRATESVIS: true,
-  WIP_MAPVIS: true,
-  WIP_INSIGHT_SHARING: true,
-  WIP_VIEWER_PERMISSIONS: true,
-  WIP_SHARABLE_EXPLORATION: true,
-};
-
-type EnvFallbackKey = keyof typeof envFallbacks;
-
 // Safely retrieve environment variable values with a default fallback
-const getEnvVariable = (key: EnvFallbackKey): any => {
-  const value = import.meta.env[key];
-  if (value === undefined) {
-    console.error(`Environment variable ${key} is missing, using fallback value.`);
-    return envFallbacks[key];
-  }
-  return value;
+const getEnvVariable = (key: string, defaultValue: string = 'false'): string => {
+  return import.meta.env[key] ?? defaultValue;
 };
 
 // Check if the environment is production
 const isProduction = (): boolean => {
-  return getEnvVariable('GRAPHPOLARIS_VERSION') === 'prod';
+  return getEnvVariable('GRAPHPOLARIS_VERSION', 'dev') === 'prod';
+};
+
+// Check if the Manage Permissions feature is enabled
+const showManagePermissions = (): boolean => {
+  return !isProduction() || (isProduction() && getEnvVariable('WIP_VIEWER_PERMISSIONS') === 'false');
+};
+
+// Check if the Insight Sharing feature is enabled
+const showInsightSharing = (): boolean => {
+  return !isProduction() || (isProduction() && getEnvVariable('WIP_INSIGHT_SHARING') === 'false');
 };
 
-// Utility to check if a WIP feature is enabled
-const isWIPFeatureEnabled = (featureKey: string): boolean => {
-  return getEnvVariable(`WIP_${featureKey.toUpperCase()}`) === 'false';
+// Check if the Insight Sharing feature is enabled
+const showSharableExploration = (): boolean => {
+  return !isProduction() || (isProduction() && getEnvVariable('WIP_SHARABLE_EXPLORATION') === 'false');
 };
 
-// Feature flags with checks for production and WIP feature flags
-const showManagePermissions = (): boolean => !isProduction() || isWIPFeatureEnabled('viewer_permissions');
-const showInsightSharing = (): boolean => !isProduction() || isWIPFeatureEnabled('insight_sharing');
-const showSharableExploration = (): boolean => !isProduction() || isWIPFeatureEnabled('sharable_exploration');
-const isVisualizationReleased = (visualizationName: string): boolean => !isProduction() || isWIPFeatureEnabled(visualizationName);
+// Utility to check if a specific visualization is released based on environment variables
+const isVisualizationReleased = (visualizationName: string): boolean => {
+  const visualizationFlag = getEnvVariable(`WIP_${visualizationName.toUpperCase()}`, 'false');
+  return !isProduction() || (isProduction() && visualizationFlag === 'false');
+};
 
-export { getEnvVariable, isProduction, showManagePermissions, showInsightSharing, showSharableExploration, isVisualizationReleased };
+export { isProduction, showManagePermissions, showInsightSharing, showSharableExploration, isVisualizationReleased };
diff --git a/libs/shared/lib/data-access/broker/broker.tsx b/libs/shared/lib/data-access/broker/broker.tsx
index 361434842..f915a2f24 100644
--- a/libs/shared/lib/data-access/broker/broker.tsx
+++ b/libs/shared/lib/data-access/broker/broker.tsx
@@ -4,7 +4,6 @@
  * © Copyright Utrecht University (Department of Information and Computing Sciences)
  */
 
-import { getEnvVariable } from 'config';
 import { UseIsAuthorizedState } from '../store/authSlice';
 import { ReceiveMessageI, SendMessageI, SendMessageWithSessionI } from './types';
 
@@ -39,7 +38,7 @@ export class Broker {
 
   /** Get the singleton instance of the Broker. */
   public static instance(): Broker {
-    if (!this.singletonInstance) this.singletonInstance = new Broker(getEnvVariable('BACKEND_WSS_URL'));
+    if (!this.singletonInstance) this.singletonInstance = new Broker(import.meta.env.BACKEND_WSS_URL);
     return this.singletonInstance;
   }
 
diff --git a/libs/shared/lib/data-access/security/useAuthentication.tsx b/libs/shared/lib/data-access/security/useAuthentication.tsx
index 18310f738..fcb507b86 100644
--- a/libs/shared/lib/data-access/security/useAuthentication.tsx
+++ b/libs/shared/lib/data-access/security/useAuthentication.tsx
@@ -1,9 +1,8 @@
-import { getEnvVariable } from 'config';
 import { useAppDispatch, useAuthCache } from '../store';
 import { authenticated, changeRoom, UserAuthenticationHeader } from '../store/authSlice';
 
-const domain = getEnvVariable('BACKEND_URL');
-const userURI = getEnvVariable('BACKEND_USER');
+const domain = import.meta.env.BACKEND_URL;
+const userURI = import.meta.env.BACKEND_USER;
 
 export const fetchSettings: RequestInit = {
   method: 'GET',
diff --git a/libs/shared/lib/inspector/InspectorPanel.tsx b/libs/shared/lib/inspector/InspectorPanel.tsx
index 6efabffa9..cbc35aefc 100644
--- a/libs/shared/lib/inspector/InspectorPanel.tsx
+++ b/libs/shared/lib/inspector/InspectorPanel.tsx
@@ -9,10 +9,9 @@ import { SelectionConfig } from '../vis/components/config/SelectionConfig';
 import { SchemaSettings } from '../schema/panel/SchemaSettings';
 import { QuerySettings } from '../querybuilder/panel/querysidepanel/QuerySettings';
 import { useActiveVisualization } from '@graphpolaris/shared/lib/data-access';
-import { getEnvVariable } from 'config';
 
 export function InspectorPanel(props: { children?: React.ReactNode }) {
-  const buildInfo = getEnvVariable('GRAPHPOLARIS_VERSION');
+  const buildInfo = import.meta.env.GRAPHPOLARIS_VERSION;
   const selection = useSelection();
   const focus = useFocus();
   const dispatch = useDispatch();
diff --git a/libs/shared/lib/sidebar/index.tsx b/libs/shared/lib/sidebar/index.tsx
index 8307fea03..7c2f42419 100644
--- a/libs/shared/lib/sidebar/index.tsx
+++ b/libs/shared/lib/sidebar/index.tsx
@@ -1,7 +1,6 @@
 import React from 'react';
 import { Button, Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from '../components';
 import ColorMode from '../components/color-mode';
-import { getEnvVariable } from 'config';
 
 export type SideNavTab = 'Schema' | 'Search' | undefined;
 
@@ -30,8 +29,8 @@ export function Sidebar({
   tab: SideNavTab;
   openMonitoringDialog: () => void;
 }) {
-  const isProd = getEnvVariable('GRAPHPOLARIS_VERSION') === 'prod';
-  const isInsightSharingWIP = getEnvVariable('WIP_INSIGHT_SHARING') === true;
+  const isProd = import.meta.env.GRAPHPOLARIS_VERSION === 'prod';
+  const isInsightSharingWIP = import.meta.env.WIP_INSIGHT_SHARING === 'true';
 
   return (
     <div className="side-bar w-fit h-full flex shrink">
-- 
GitLab