From f9fae168934f718a28eed94ce6671d95828076f2 Mon Sep 17 00:00:00 2001 From: Nil Amrutlal Date: Thu, 31 Aug 2023 17:17:45 +0100 Subject: [PATCH] implement env variables in graph-client --- packages/apps/graph-client/.env.example | 2 ++ .../chainweb/chain-block/chain-block.tsx | 4 ++- packages/apps/graph-client/src/utils/env.ts | 27 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/apps/graph-client/.env.example create mode 100644 packages/apps/graph-client/src/utils/env.ts diff --git a/packages/apps/graph-client/.env.example b/packages/apps/graph-client/.env.example new file mode 100644 index 0000000000..58af42c3fc --- /dev/null +++ b/packages/apps/graph-client/.env.example @@ -0,0 +1,2 @@ +# Depth necessary on a block to call it "confirmed", omit to use default value: 6 +CONFIRMED_CONFIRMATION_DEPTH= diff --git a/packages/apps/graph-client/src/components/chainweb/chain-block/chain-block.tsx b/packages/apps/graph-client/src/components/chainweb/chain-block/chain-block.tsx index fbb53aba9c..fd6db3dba9 100644 --- a/packages/apps/graph-client/src/components/chainweb/chain-block/chain-block.tsx +++ b/packages/apps/graph-client/src/components/chainweb/chain-block/chain-block.tsx @@ -1,3 +1,4 @@ +import { env } from '../../../utils/env'; import { IBlock } from '../../../utils/hooks/use-parsed-blocks'; import { Box } from '../../box'; import { Text } from '../../text'; @@ -43,7 +44,8 @@ export const ChainBlock = (props: IChainBlockProps): JSX.Element => { - {block.confirmationDepth >= 6 ? ( + {block.confirmationDepth >= + env('CONFIRMED_CONFIRMATION_DEPTH', 6) ? ( ) : ( diff --git a/packages/apps/graph-client/src/utils/env.ts b/packages/apps/graph-client/src/utils/env.ts new file mode 100644 index 0000000000..2df74917dc --- /dev/null +++ b/packages/apps/graph-client/src/utils/env.ts @@ -0,0 +1,27 @@ +export interface IEnvInterface { + CONFIRMED_CONFIRMATION_DEPTH?: number; +} + +type RequiredEnv = Required; + +export const dotenv: IEnvInterface = { + CONFIRMED_CONFIRMATION_DEPTH: Number( + process.env.CONFIRMED_CONFIRMATION_DEPTH, + ), +}; + +export const env = ( + key: T, + defaultValue: TDefault, +): TDefault | NonNullable => { + console.log(dotenv[key]); + const falsyKey = + (typeof dotenv[key] === 'number' && isNaN(Number(dotenv[key]))) || + dotenv[key] === null || + dotenv[key] === undefined; + // TODO: uncomment this line when there's env vars that are strings + //dotenv[key] === ''; + return falsyKey + ? (defaultValue as TDefault) + : (dotenv[key] as NonNullable); +};