Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(extension): use isFeatureEnabled #775

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

pczeglik-iohk
Copy link
Contributor

@pczeglik-iohk pczeglik-iohk commented Dec 6, 2023

Proposed solution

I'm proposing a new (slightly updated) way of using feature flags in the browser extension code base.

Instead of reaching out directly in code to process.env, I've created a helper method isFeatureEnabled which is an abstraction layer on top of process.env. It does the same thing, but in more maintainable way. In the future if we decide to use other solution for feature flags it will be easier to refactor.

Apart of easier usage and better semantic there is one more benefit of this approach. isFeatureEnabled throws Error in case feature flag is not defined in .env.

Ex. Code is using USE_HIDE_MY_BALANCE feature flag, but it is not defined in jest's config file set-env-vars.js

FAIL  src/features/address-book/hooks/__tests__/useGetFilteredAddressBook.test.tsx
  ● Test suite failed to run

    'HIDE_MY_BALANCE' is not set

      29 | export const isFeatureEnabled = (name: FeatureFlag): boolean => {
      30 |   if (!process.env[`USE_${name}`]) {
    > 31 |     throw new Error(`'USE_${name}' is not set`);
         |           ^
      32 |   }
      33 |   return process.env[`USE_${feature}`] === 'true';
      34 | };

Additionally I configured jest to use the same .env file as engineer working with the code. In case there is no .env then .env.defaults will be used.

How to use this helper?

  1. Define feature flag in .env.defaults and .env.example (please keep flags sorted) ex. USE_MY_FEATURE=true
  2. Extend FeatureFlag type in utils/feature-flags.ts (please keep flags sorted) ex. MY_FEATURE
  3. In code instead of using process.env.USE_MY_FEATURE === 'true' use isFeatureEnabled('MY_FEATURE')

@github-actions github-actions bot added the browser Changes to the browser application. label Dec 6, 2023
@@ -10,25 +10,24 @@ TOKEN_PRICE_POLLING_IN_SEC=300
SAVED_PRICE_DURATION_IN_MINUTES=720

# Feature Flags
USE_PASSWORD_VERIFICATION=false
USE_ADA_HANDLE=true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only sorting. It would be good to have such things sorted so it's easier to manage


export const POSTHOG_ENABLED = process.env.USE_POSTHOG_ANALYTICS === 'true';
export const POSTHOG_ENABLED = isFeatureEnabled('POSTHOG_ANALYTICS');
export const POSTHOG_OPTED_OUT_EVENTS_DISABLED = process.env.USE_POSTHOG_ANALYTICS_FOR_OPTED_OUT === 'false';
export const PUBLIC_POSTHOG_HOST = process.env.PUBLIC_POSTHOG_HOST;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you like this approach, I would like to do similar refactoring to address accessing env vars like getEnvironmentVariable

process.env.CARDANO_SERVICES_URL_PREVIEW = 'https://preview-prod.com';
process.env.CARDANO_SERVICES_URL_PREPROD = 'https://preprod-prod.com';
process.env.CARDANO_SERVICES_URL_MAINNET = 'https://mainnet-url.com';
require('dotenv-defaults').config({
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use .env file with fallback to .env.defaults instead of manually maintained list of feature flags and env vars

Copy link

github-actions bot commented Dec 6, 2023

Allure report

allure-report-publisher generated test report!

smokeTests: ❌ test report for 6957e7dd

passed failed skipped flaky total result
Total 0 31 0 0 31

Copy link

sonarcloud bot commented Dec 7, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

No Coverage information No Coverage information
0.0% 0.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
browser Changes to the browser application.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant