Skip to content

Commit

Permalink
Load Environment Variables before everything else is loaded (#5)
Browse files Browse the repository at this point in the history
This avoids unecessary errors of unloaded env vars when loading the app
for the first time (e.g. when using the /asset endpoint or the settings)
  • Loading branch information
hofermo committed Jun 26, 2024
1 parent bd93e36 commit 5982c8b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
7 changes: 3 additions & 4 deletions src/app/[locale]/asset/_components/RedirectToViewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ export const RedirectToViewer = () => {
useAsyncEffect(async () => {
try {
setIsLoading(true);
if (env.DISCOVERY_API_URL != '') {
await navigateToViewerOfAsset(decodeURIComponent(assetIdParam ?? ''));
}
await navigateToViewerOfAsset(decodeURIComponent(assetIdParam ?? ''));

} catch (e) {
setIsLoading(false);
setIsError(true);
showError(e, notificationSpawner);
}
}, [env]);
}, []);

async function navigateToViewerOfAsset(assetId: string | undefined): Promise<void> {
const aasIds = await getAasIdsOfAsset(assetId);
Expand Down
10 changes: 4 additions & 6 deletions src/app/[locale]/list/_components/AASListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,15 @@ export const AASListView = () => {
useAsyncEffect(async () => {
try {
setIsLoadingList(true);
if (env.MNESTIX_BACKEND_API_URL != '') {
const list = await aasListClient.getAasListEntries();
setAasList(list);
if (!productClassFilterValue) setAasListFiltered(list);
}
const list = await aasListClient.getAasListEntries();
setAasList(list);
if (!productClassFilterValue) setAasListFiltered(list);
} catch (e) {
showError(e, notificationSpawner);
} finally {
setIsLoadingList(false);
}
}, [env]);
}, []);

/**
* Creates the ProductClass Filter values.
Expand Down
27 changes: 13 additions & 14 deletions src/app/[locale]/viewer/[base64AasId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,19 @@ export default function Page() {
try {
setIsLoadingAas(true);
if (aas === null) {
if (env.AAS_REPO_API_URL != '' || env.REGISTRY_API_URL != '') {
const aasIdDecoded = safeBase64Decode(base64AasId);
const registrySearchResult = await handleAasRegistrySearch(aasIdDecoded);
if (registrySearchResult != null) {
setAas(registrySearchResult.registryAas as AssetAdministrationShell);
setRegistryAasData({
submodelDescriptors: registrySearchResult?.registryAasData?.submodelDescriptors,
aasRegistryRepositoryOrigin: registrySearchResult?.registryAasData?.aasRegistryRepositoryOrigin });
setAasData(registrySearchResult.registryAas as AssetAdministrationShell);
} else {
const shell = await repositoryClient.getAssetAdministrationShellById(base64AasId as string);
setAas(shell);
setAasData(shell);
}
const aasIdDecoded = safeBase64Decode(base64AasId);
const registrySearchResult = await handleAasRegistrySearch(aasIdDecoded);
if (registrySearchResult != null) {
setAas(registrySearchResult.registryAas as AssetAdministrationShell);
setRegistryAasData({
submodelDescriptors: registrySearchResult?.registryAasData?.submodelDescriptors,
aasRegistryRepositoryOrigin: registrySearchResult?.registryAasData?.aasRegistryRepositoryOrigin });
setAasData(registrySearchResult.registryAas as AssetAdministrationShell);
} else {
const shell = await repositoryClient.getAssetAdministrationShellById(base64AasId as string);
setAas(shell);
setAasData(shell);

}
} else {
setAasData(aas);
Expand Down
7 changes: 5 additions & 2 deletions src/app/env/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { createContext, useContext, useState } from 'react';
import { EnvironmentalVariables, getEnv } from './env';
import { useAsyncEffect } from 'lib/hooks/UseAsyncEffect';
import {CenteredLoadingSpinner} from "../../components/basics/CenteredLoadingSpinner";

const initialValues: EnvironmentalVariables = {
AAS_LIST_FEATURE_FLAG: false,
Expand Down Expand Up @@ -29,12 +30,14 @@ export const EnvProvider = ({
children: React.ReactNode;
}>) => {
const [env, setEnv] = useState<EnvironmentalVariables>(initialValues);

const [renderChildren, setChildren] = useState<boolean>(false);
useAsyncEffect(async () => {
const env = await getEnv();
setEnv(env);
setChildren(true);
}, []);
return <EnvContext.Provider value={env}>{children}</EnvContext.Provider>;

return renderChildren ? <EnvContext.Provider value={env}>{children}</EnvContext.Provider> : <CenteredLoadingSpinner/>;
};

export const useEnv = () => {
Expand Down

0 comments on commit 5982c8b

Please sign in to comment.