@procore/web-sdk-feature-toggle
TypeScript icon, indicating that this package has built-in type declarations

2.4.0 • Public • Published

Web Platform Feature Toggle

Utilities for managing feature toggles in Procore.

Installation

yarn add @procore/web-sdk-feature-toggle

Migration from LaunchDarkly SDK

This document describes how to migrate from the LaunchDarkly SDK to the Web Platform Feature Toggle SDK.

Usage

Setting up

import { FeatureToggleProvider } from '@procore/web-sdk-feature-toggle';

function Index() {
  return (
    <FeatureToggleProvider
      apiKey="my-launchdarkly-env-key"
      context={{
        user: {
          projectId: '1',
          companyId: '2',
          locale: 'en-US',
          email: 'foo@procore.com', // required field. If email is not available - use other unique identifier (e.g. userId). If user is not signed in - use uuid
        },
      }}
    >
      <App />
    </FeatureToggleProvider>
  );
}

Accessing feature flags

import { useFeatureFlag } from '@procore/web-sdk-feature-toggle';

function MyComponent() {
  const myFeatureFlagValue = useFeatureFlag('my-feature-flag', 'default-value');
  // ...
}

Changing context

The FeatureToggleProvider is designed in a way that when the context changes, all components that use feature flags will re-evaluate the flags. However, if you like to manually update the context, you can use the updateContext method from the FeatureToggleClient instance.

import { useFeatureToggleClient } from '@procore/web-sdk-feature-toggle';

function MyComponent() {
  const { updateContext } = useFeatureToggleClient();

  // `myFeatureFlagValue` will be re-evaluated when the context changes
  const myFeatureFlagValue = useFeatureFlag('my-feature-flag', 'default-value');

  return (
    <button
      type="button"
      onClick={() => {
        updateContext({
          user: {
            email: 'bar@procore.com', // required field. If email is not available - use other unique identifier (e.g. userId). If user is not signed in - use uuid
            projectId: '3',
            companyId: '4',
            locale: 'en-GB',
          },
        });
      }}
    >
      Change context
    </button>
  );
}

API

Types

type UserContext = {
  companyId?: string;
  email: string; // required field. If email is not available - use other unique identifier (e.g. userId). If user is not signed in - use uuid
  locale?: string;
  projectId?: string;
  [key: string]: unknown;
};

interface FeatureToggleContext {
  user?: UserContext;
  [key: string]: unknown;
}

type FeatureFlagValue = unknown;
type FeatureFlags = Record<string, FeatureFlagValue>;

FeatureToggleClient

constructor

Create a new instance of the FeatureToggleClient.

function constructor(apiKey: string, context: FeatureToggleContext): void;

setContext

Set the context for the FeatureToggleClient.

function setContext(context: FeatureToggleContext): Promise<void>;

getFeatureFlag

Get the value of a feature flag.

getFeatureFlags

Get the values of all feature flags.

function getFeatureFlag(key: string): FeatureFlagValue;

__getClient (for advanced use cases or debugging)

Get the LaunchDarkly client.

function __getClient(): LDClient;

FeatureToggleProvider

React context provider for the feature toggle client instance. Prop waitTillReady can be used to only render children once the client is initialized.

import {
  FeatureToggleClient,
  FeatureToggleProvider,
  useFeatureFlag,
} from '@procore/web-sdk-feature-toggle';
import { App } from './App';

function Index() {
  return (
    <FeatureToggleProvider
      waitTillReady // optional, default: true
      apiKey="my-launchdarkly-env-key"
      context={{
        user: {
          projectId: '1',
          companyId: '2',
          locale: 'en-US',
          email: 'foo@procore.com', // required field. If email is not available - use other unique identifier (e.g. userId). If user is not signed in - use uuid
        },
      }}
    >
      <App />
    </FeatureToggleProvider>
  );
}

useFeatureFlag

React hook to get the value of a feature flag.

function useFeatureFlag(key: string): FeatureFlagValue;

useFeatureFlags

React hook to get the values of all feature flags.

function useFeatureFlags(): Record<string, FeatureFlagValue>;

useFeatureToggleClient

React hook to get the feature toggle client instance.

function useFeatureToggleClient(): FeatureToggleClient;

Local Development

To run storybook with a real LaunchDarkly client, you need to set the LD_ENV_ID environment variable in .env file.

To use LaunchDarkly mock, add the following code to storybook/main.js:

module.exports = {
  webpackFinal: (config) => {
    config.resolve.alias['launchdarkly-js-client-sdk'] = require.resolve(
      '../src/__tests__/launchdarkly-js-client-sdk'
    );
    return config;
  },
};

Support

#team-ui-foundations

Readme

Keywords

none

Package Sidebar

Install

npm i @procore/web-sdk-feature-toggle

Weekly Downloads

100

Version

2.4.0

License

SEE LICENSE IN LICENSE

Unpacked Size

71.5 kB

Total Files

11

Last publish

Collaborators

  • dancingshell
  • justinmwatts
  • antonyayoub
  • rysmithprocore
  • robbiegprocore
  • jadamsss
  • jeremy.bouzigard
  • jgentes
  • faraz.hanif
  • timdoherty
  • ajaykumar-procore
  • b.bookout
  • jalyng
  • chadryder
  • htael
  • refaiepcn
  • james.lawson
  • vinayakaprabhu
  • davidshure
  • james.cleary
  • jl4ever
  • andersonbispoprocore
  • dev-account-admin
  • brockpcor
  • rowan.ibrahim
  • sseanwang
  • ramysaid2
  • vinaya-procore
  • lalovar-procore
  • bhargavrnd
  • ihor.diachenko_procore
  • farismmk
  • gideon-procore
  • dannyporrello
  • alanprocore
  • chance.eakin.procore
  • stevenliprocore
  • javio-procore
  • kani-procore
  • enyaga
  • danny.ou
  • messanjah
  • david-christensen-procore
  • shradha.khard
  • winson.chu
  • eyvettesou
  • lzhou888
  • jnhoang1
  • nickprocore
  • neil.mckeeman
  • pam-whisenhunt
  • jgee67
  • youssefamer
  • mike-arndt-procore
  • bob.laskowski
  • cagmz
  • mariah_delaney
  • lukenispel
  • fabriciobd
  • bikash.sahoo
  • bbreyel921
  • kimhin267
  • andy.mayer
  • phil.custer
  • elijah.procore
  • juliana.hernandez
  • judy-lu-pc
  • procore-it-support
  • andrewburke-pc
  • jkleintech
  • rachel.arkebauer
  • procore-npm-bot
  • grafffffff
  • yoyis3000
  • james.dabbs-procore
  • laurenbrandsteinprocore
  • scottbieser-procore
  • amir-iskander
  • zach.mckenzie.procore
  • amyprocore
  • shayonj_procore
  • heplayskeys
  • mike.south
  • thomasoboyle
  • dischorde
  • derek-carter-procore
  • dlgasser
  • cfprocore
  • evan.waits
  • jeremy-marcus
  • jmejia-fsl
  • ersgonzalo
  • timofeee
  • stephan-procore
  • aleclarsenprocore
  • sarah.freitas
  • yihai.zweifel
  • jay-rajan
  • jacky-lei
  • apcarroll_procore
  • procore_halzy
  • mehrdad-panahandeh
  • peter.jin
  • uddhavjoglekar
  • brookyboy009
  • denzylbalram
  • changprocore
  • allenanle.procore
  • devin.cunningham.procore
  • ari-procore
  • noor.ali
  • hgouhierprocore
  • cyrille.bai
  • brad.urani
  • dmccraw-procore
  • patrick.lardin
  • abhijit.patwardhan
  • matt.harris0223
  • alan.bresani
  • jesse.olsen
  • dtorres-procore
  • dineshkumar.jayak
  • jason-kaye
  • yadhu.prakash
  • leandro-proc
  • andrew.wheeler
  • sherylnapigkit
  • lydiahara
  • kahliholmes
  • sateesh-kadiyala-procore
  • epalinprocore
  • dennis.heckman
  • jamie-dugan-procore
  • viktoriia_azarovska
  • daniel.ferreira-contractor
  • willpankonien
  • ladavarga
  • steven.hinkle
  • txin1
  • chris.berber
  • etokarev
  • ritchlee
  • karina.mendez-contractor
  • worldofsatyaki
  • greg.sparks
  • kyle.williams
  • kuldeepsingh4556
  • jeremy.lund
  • brocktillotsonprocore
  • stajics
  • ryanfuentesprocore
  • tyler.wasden.procore
  • fabiomelo513
  • cody_schindler_procore
  • amit.gurav-contractor
  • yoasyo25
  • kalyani.gosavi
  • hectorthiele
  • andersontr15
  • vishal-procore
  • omar.wagdy
  • yogevfine1
  • charan_procore
  • scorgiat-procore
  • mbartlett413
  • attachi
  • ahmed.ghorab
  • varomir
  • alyelashram_procore
  • ilya.dryha-contractor
  • evan.cerwonka.procore
  • vsobol-c
  • dmitri_wm
  • kellikearns
  • richard.bunn
  • chaitra-m-15
  • conner-procore
  • mishaelowoyemi
  • peterknif
  • aleh.haurylenia-contractor
  • miguel.garcia-procore
  • codyrobertsprocore
  • a.elbadawei
  • lnspatz914
  • melch-procore
  • mustafa-abdelrahman
  • atoaima
  • jasaswini
  • adarsh.gautam
  • amin.jaipuri
  • max.helmetag
  • s_kudryk
  • hyogman
  • kyle.liu
  • davidkangpro
  • stevenkang3
  • cbathgate
  • victorbendeck-pc
  • sarah.heredia
  • moaz-ashraf
  • aly-el-kerdany
  • procore-oss-user
  • abhishekkumar123
  • stephanie.brereton
  • saurasumprocore
  • mona.khairbek
  • elewando-procore
  • jyang-procore
  • tedyang
  • deiab
  • jgreene_procore
  • asamay
  • kenny.foisy
  • ganesh.raghupathy
  • rajatmenhdiratta
  • yzhou2024
  • dlameter-procore
  • decha-sanson
  • kylepietz
  • connie-feng-procore
  • roger-procore
  • matheusprocore
  • fernandocamilotti
  • simona.iancu
  • jacksonleach-procore
  • g2mitchell
  • tatsiana.clifton
  • phunguyen-pcor
  • pmfrawley
  • brian.smith1
  • scottstern
  • neil1023
  • srichaitanya.peddinti
  • jake-pitkin
  • erikthoreson
  • lhuang325
  • abhijit-procore
  • rodayna.ehab
  • fairchild
  • mustafa-u-abdelrahman
  • aberkowitz
  • pwhisenhunt-procore
  • mariia.solodovnik
  • nigeld-procore
  • samad.virani
  • bohdan-horai-procore
  • mathenes_procore
  • vinoth.kuppusamy
  • zayter
  • alan.facchini-contractor
  • cassianomatos-procore
  • amitk030
  • sflang-procore
  • tracy.otto
  • daniel-pierre-procore
  • glidenor
  • ashish.sharma2024
  • gaurav.sharma.procore
  • andres-mendez-procore
  • roobo-romeski
  • kylemartinez-procore
  • sean.spearman.procore
  • gturkadze
  • jeffgiaquinto
  • ezrasimeloff
  • bill-wagner
  • kellen.stewart
  • rodrigo.dejuana
  • saranahal2
  • andrew.isaac
  • agamaleldin
  • mostafaeltazy
  • magdyyxx
  • andreszorrilla-procore
  • mohitsharma97
  • tejeshwar
  • swati.jadhav
  • squidbeaks
  • subham.panigrahi
  • deepak.kumarts
  • mahesh-s96
  • mohamed.adel
  • rana.eltayar
  • mahmoud-sharshar
  • syamphanindra
  • veroniaosama
  • imanselim
  • helmy162-procore
  • pclemons
  • samuelvelez8383
  • vinitdeshkar-procore