Skip to content

Commit

Permalink
Implement feature flag
Browse files Browse the repository at this point in the history
- get feature flag from AppConfig
- add Feature component and useFeature hook
- this change replace the environement variable based feature flag (#6367)
  • Loading branch information
ousmaneo committed Jul 27, 2021
1 parent 9770e10 commit 77a1657
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
48 changes: 48 additions & 0 deletions graylog2-web-interface/src/components/features/Feature.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import React from 'react';
import PropTypes from 'prop-types';

import useFeature from 'hooks/useFeature';

type Props = {
name: string;
fallback?: React.ReactNode,
children: React.ReactNode,
}

const Feature = ({ name, fallback, children }: Props) => {
const hasFeature = useFeature(name);

if (hasFeature) {
return children;
}

return fallback;
};

Feature.propTypes = {
name: PropTypes.string.isRequired,
fallback: PropTypes.node,
children: PropTypes.node.isRequired,
};

Feature.defaultProps = {
fallback: null,
};

export default Feature;
29 changes: 29 additions & 0 deletions graylog2-web-interface/src/hooks/useFeature.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2020 Graylog, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import PropTypes from 'prop-types';

import AppConfig from 'util/AppConfig';

const useFeature = (name: string) => {
return AppConfig.isFeatureEnabled(name);
};

useFeature.propTypes = {
name: PropTypes.string.isRequired,
};

export default useFeature;
7 changes: 5 additions & 2 deletions graylog2-web-interface/src/util/AppConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*/
import Immutable from 'immutable';

const AppConfig = {
gl2ServerUrl() {
if (typeof (GRAYLOG_API_URL) !== 'undefined') {
Expand All @@ -36,8 +38,9 @@ const AppConfig = {
},

isFeatureEnabled(feature) {
// eslint-disable-next-line no-undef
return typeof (FEATURES) !== 'undefined' && FEATURES.split(',')
return Immutable.Map(this.appConfig().featureFlags)
.filter((value) => value.trim().toLowerCase() === 'on')
.keySeq().toList()
.filter((s) => typeof s === 'string')
.map((s) => s.trim().toLowerCase())
.includes(feature.toLowerCase());
Expand Down

0 comments on commit 77a1657

Please sign in to comment.