Skip to content

SmythOS Studio: Open-Source Visual AI Agent Builder and deployable runtime stack from SmythOS. Start with an intuitive drag-and-drop workspace, extend with custom code, and deploy your agents anywhere — local, cloud, or edge — with full governance and control.

2.9 KB
Raw Blame
import { useAuthCtx } from '@react/shared/contexts/auth.context';
import { Observability } from '@src/shared/observability';
import React, { ReactNode, useEffect, useMemo, useState } from 'react';
import {
  FEATURE_FLAG_PAYLOAD_RELEASE_TYPE,
  FEATURE_FLAGS,
} from '../../../../shared/constants/featureflags';

/**
 * Custom hook to check if the current environment is production.
 *
 * @returns {boolean} True if the current environment is production, false otherwise.
 */
export function useIsProdEnv(): boolean {
  const isProdEnv = useMemo(() => {
    // Check if the environment is production
    return process.env.NODE_ENV === 'production';
  }, []);

  return isProdEnv;
}

interface FeatureFlaggedProps {
  children: ReactNode;
  forceHidden?: boolean; // Optional prop to force feature to be hidden
  featureFlag: FEATURE_FLAGS;
  alternateContent?: ReactNode; // Optional prop to render alternate content when feature is hidden
}

/**
 * Custom hook to determine feature visibility and whether it should be shown
 * @param {FEATURE_FLAGS} featureFlag - The feature flag to check
 * @param {boolean} forceHidden - Optional flag to force hide the feature
 * @returns {boolean} - shouldShowFeature
 */
export const useFeatureVisibility = (
  featureFlag: FEATURE_FLAGS,
  forceHidden: boolean = false,
): boolean => {
  const isProdEnv = useIsProdEnv();
  const { isStaffUser, loading } = useAuthCtx();
  const [featureFlagPayload, setFeatureFlagPayload] = useState<Record<string, unknown> | undefined>(
    undefined,
  );

  // Load feature flag payload from Observability API
  useEffect(() => {
    const payload = Observability.features.getFeatureFlagPayload(featureFlag);
    setFeatureFlagPayload(payload);
  }, [featureFlag]);

  const visibility = useMemo(() => {
    if (!featureFlagPayload) {
      return false;
    }

    return typeof featureFlagPayload === 'object' && 'visibility' in featureFlagPayload
      ? featureFlagPayload.visibility
      : featureFlagPayload;
  }, [featureFlagPayload]);

  const shouldShowFeature = useMemo(() => {
    if (forceHidden) {
      return false;
    }

    if (visibility === FEATURE_FLAG_PAYLOAD_RELEASE_TYPE.ALPHA_STAGING) {
      return !isProdEnv && isStaffUser;
    }

    if (visibility === FEATURE_FLAG_PAYLOAD_RELEASE_TYPE.ALPHA && loading === false) {
      return isStaffUser;
    }

    if (
      visibility === FEATURE_FLAG_PAYLOAD_RELEASE_TYPE.GA ||
      visibility === FEATURE_FLAG_PAYLOAD_RELEASE_TYPE.PUBLIC
    ) {
      return true;
    }

    return true;
  }, [visibility, isStaffUser, forceHidden, isProdEnv, loading]);

  return shouldShowFeature;
};

export const FeatureFlagged: React.FC<FeatureFlaggedProps> = ({
  children,
  featureFlag,
  alternateContent,
  forceHidden = false,
}) => {
  const shouldShowFeature = useFeatureVisibility(featureFlag, forceHidden);
  return shouldShowFeature ? <>{children}</> : alternateContent ? <>{alternateContent}</> : null;
};