Skip to content
This repository has been archived by the owner on Mar 14, 2023. It is now read-only.

Feature Request: Implement activation strategies #3

Open
aneeshrelan opened this issue Oct 4, 2020 · 7 comments
Open

Feature Request: Implement activation strategies #3

aneeshrelan opened this issue Oct 4, 2020 · 7 comments
Assignees
Labels
enhancement New feature or request

Comments

@aneeshrelan
Copy link

I see that the current library only supports a default toggle on/off activation strategy. It'll be really cool to implement different activation strategies to have granular control on the React app. Thoughts?

@fvoordeckers fvoordeckers self-assigned this Oct 5, 2020
@fvoordeckers
Copy link
Owner

For sure! Just not sure about the timeframe yet.

@fvoordeckers fvoordeckers added the enhancement New feature or request label Oct 5, 2020
@aneeshrelan
Copy link
Author

If possible, can I try contributing here for an initial set of strategy?

@fvoordeckers
Copy link
Owner

Please do, would be really great. Feel free to create a merge request.
For the next few months I will not be able to fully integrate this.

If you add a MR, I'll be happy to review, merge and release it.

@aneeshrelan
Copy link
Author

Thanks! I'll try to get up something in a few weeks, even I'm caught up with other work, but honestly I need this for a project so I should be able to get an initial one

@aneeshrelan
Copy link
Author

Please assign this to me

@saborrie
Copy link

saborrie commented May 7, 2021

For anyone who ends up here, just know that you can still use this library and add the strategies on top yourself:

import { useFlag } from "react-unleash-flags";
import useCurrentUser from './hooks/useCurrentUser'; // write your own custom hook to get the logged in user

// this useFlagIsEnabled returns boolean, so its not the same as consuming useFlag directly
export function useIsFlagEnabled(s) {
  const currentUser= useCurrentUser();
  const f = useFlag(s);

  if (f?.enabled !== true) return false;

  // example implementation of userWithId strategy
  for (let strategy of f.strategies) {
    switch (strategy.name) {
      case "default":
        return f.enabled;
      case "userWithId": {
        if (strategy.parameters.userIds.includes(currentUser?.id)) return true;
        break;
      }
      // TODO you can add all the strategies you use here
    }
  }

  return false;
}

export function FeatureFlag({ name, children }) {
  const enabled = useIsFlagEnabled(name);

  if (enabled) return children;
  return null;
}

@fvoordeckers
Copy link
Owner

@saborrie Good point! The idea is to provide a set of basic strategies in the feature (id or % rollout ...)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants