Skip to content

Commit

Permalink
rename configId
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-cat committed Sep 23, 2024
1 parent 6e5f73e commit 404866e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 36 deletions.
2 changes: 1 addition & 1 deletion sandbox/src/stories/Hoc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { User } from 'configcat-common';


export class HocComponent extends React.Component<
{ featureFlagKey: string } & WithConfigCatClientProps,
{ featureFlagKey: string, user?: User } & WithConfigCatClientProps,
{ isEnabled: boolean, loading: boolean }
> {
constructor(props: { featureFlagKey: string, user?: User } & WithConfigCatClientProps) {
Expand Down
36 changes: 18 additions & 18 deletions sandbox/src/stories/MultipleConfigCatConfigs.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import React from 'react';
import { ConfigCatProvider, User, useFeatureFlagByConfigId, withConfigCatClient } from 'configcat-react';
import { ConfigCatProvider, useFeatureFlag, User, withConfigCatClient } from 'configcat-react-local';
import { HocComponent} from './Hoc';


const CC_CONFIGID = { BACKEND: "BACKEND", SHARED: "SHARED"};

const CC_SDK = {
BACKEND: "TODO - INSERT BACKEND SDK KEY",
SHARED:"TODO - INSERT SHARED SDK KEY"}
BACKEND: "fnvWCKwpfPETf3O6BQgQAg/iMRmhH6DmECovlBvRtjpDw",
SHARED:"configcat-sdk-1/fnvWCKwpfPETf3O6BQgQAg/Idklvfo85EaPWoX9zOygNA"}

const userObject = new User('microFrontendUser1');

export const C1 = (args: { featureFlagKey: string, configId: string }) => {
export const C1 = (args: { featureFlagKey: string, providerId: string }) => {

const { value: isFeatureEnabled, loading } = useFeatureFlagByConfigId(args.configId, args.featureFlagKey, false, userObject);
const { value: isFeatureEnabled, loading } = useFeatureFlag(args.featureFlagKey, false, userObject, args.providerId);

return (
<div>
Expand All @@ -35,19 +35,19 @@ export const MultipleConfigCatConfigs = () => {
<h1>Embeded provider test</h1>

<div>
<ConfigCatProvider sdkKey={CC_SDK.SHARED} options={{pollIntervalSeconds:10}} configId={CC_CONFIGID.SHARED}>

<C1 featureFlagKey={"sharedfeature1"} configId={CC_CONFIGID.SHARED}></C1>

<ConfigCatProvider sdkKey={CC_SDK.BACKEND} options={{pollIntervalSeconds:10}} configId={CC_CONFIGID.BACKEND}>
<C1 featureFlagKey={"isDebugModeOn"} configId={CC_CONFIGID.BACKEND}></C1>
<C1 featureFlagKey={"sharedfeature1"} configId={CC_CONFIGID.SHARED}></C1>


{/* Higher-Order Components sample */}
<ConfigCatHocComponent featureFlagKey={"isDebugModeOn"} user={userObject}/>
</ConfigCatProvider>

<ConfigCatProvider sdkKey={CC_SDK.SHARED} options={{pollIntervalSeconds:10}} id={CC_CONFIGID.SHARED}>
<>
{/* <C1 featureFlagKey={"sharedfeature1"} providerId={CC_CONFIGID.SHARED}></C1> */}

<ConfigCatProvider sdkKey={CC_SDK.BACKEND} options={{pollIntervalSeconds:10}} id={CC_CONFIGID.BACKEND}>
<>
<C1 featureFlagKey={"isDebugModeOn"} providerId={CC_CONFIGID.BACKEND}></C1>
{/* <C1 featureFlagKey={"sharedfeature1"} providerId={CC_CONFIGID.SHARED}></C1> */}
</>
{/* Higher-Order Components sample */}
<ConfigCatHocComponent featureFlagKey={"isDebugModeOn"} user={userObject}/>
</ConfigCatProvider>
</>
</ConfigCatProvider>
</div>

Expand Down
8 changes: 4 additions & 4 deletions src/ConfigCatContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ ConfigCatContext.displayName = "ConfigCatContext";

const ConfigCatContextRegistry = new Map<string, React.Context<ConfigCatContextData | undefined>>();

export function getOrCreateConfigCatContext(configId: string): React.Context<ConfigCatContextData | undefined> {
export function getOrCreateConfigCatContext(providerId: string): React.Context<ConfigCatContextData | undefined> {

let context: React.Context<ConfigCatContextData | undefined> | undefined = ConfigCatContextRegistry.get(configId.toLowerCase());
let context: React.Context<ConfigCatContextData | undefined> | undefined = ConfigCatContextRegistry.get(providerId.toLowerCase());

if (!context) {
context = React.createContext<ConfigCatContextData | undefined>(
void 0
);

context.displayName = "ConfigCatContext_" + configId;
context.displayName = "ConfigCatContext_" + providerId;

ConfigCatContextRegistry.set(configId.toLowerCase(), context);
ConfigCatContextRegistry.set(providerId.toLowerCase(), context);
}

return context;
Expand Down
6 changes: 3 additions & 3 deletions src/ConfigCatHOC.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export interface WithConfigCatClientProps {

function withConfigCatClient<P>(
WrappedComponent: React.ComponentType<P & WithConfigCatClientProps>,
configId?: string
providerId?: string
): React.ComponentType<Omit<P, keyof WithConfigCatClientProps>> {

const configCatContext = configId ? getOrCreateConfigCatContext(configId) : ConfigCatContext;
const configCatContext = providerId ? getOrCreateConfigCatContext(providerId) : ConfigCatContext;

return (props: P) => (
<configCatContext.Consumer>
{(context: ConfigCatContextData | undefined) => {
if (!context) {
throw createConfigCatProviderError("withConfigCatClient", configId);
throw createConfigCatProviderError("withConfigCatClient", providerId);
}
return (
<WrappedComponent
Expand Down
10 changes: 5 additions & 5 deletions src/ConfigCatHooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { useContext, useEffect, useState } from "react";
import ConfigCatContext, { getOrCreateConfigCatContext } from "./ConfigCatContext";
import { createConfigCatProviderError } from "./ConfigCatProvider";

function useFeatureFlag<T extends SettingValue>(key: string, defaultValue: T, user?: User, configId?: string): {
function useFeatureFlag<T extends SettingValue>(key: string, defaultValue: T, user?: User, providerId?: string): {
value: SettingTypeOf<T>;
loading: boolean;
} {
const configCatContext = useContext(ConfigCatContext);

if (!configCatContext) throw createConfigCatProviderError("useFeatureFlag", configId);
if (!configCatContext) throw createConfigCatProviderError("useFeatureFlag", providerId);

const [featureFlagValue, setFeatureFlag] = useState(defaultValue as SettingTypeOf<T>);
const [loading, setLoading] = useState(true);
Expand All @@ -24,11 +24,11 @@ function useFeatureFlag<T extends SettingValue>(key: string, defaultValue: T, us
return { value: featureFlagValue, loading };
}

function useConfigCatClient(configId?: string): IConfigCatClient {
function useConfigCatClient(providerId?: string): IConfigCatClient {

const configCatContext = useContext(configId ? getOrCreateConfigCatContext(configId) : ConfigCatContext);
const configCatContext = useContext(providerId ? getOrCreateConfigCatContext(providerId) : ConfigCatContext);

if (!configCatContext) throw createConfigCatProviderError("useConfigCatClient", configId);
if (!configCatContext) throw createConfigCatProviderError("useConfigCatClient", providerId);

return configCatContext.client;
}
Expand Down
7 changes: 4 additions & 3 deletions src/ConfigCatProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import type { ClientCacheState, HookEvents, IConfig, IConfigCatClient, IConfigCatClientSnapshot, IConfigCatKernel, IEvaluationDetails, RefreshResult, SettingKeyValue, SettingTypeOf, SettingValue, User } from "configcat-common";
import { PollingMode, getClient } from "configcat-common";
import type { PropsWithChildren } from "react";
import React, { Component } from "react";
import { Component} from "react";
import React from "react";
import { LocalStorageCache } from "./Cache";
import ConfigCatContext, { type ConfigCatContextData, getOrCreateConfigCatContext } from "./ConfigCatContext";
import { HttpConfigFetcher } from "./ConfigFetcher";
Expand All @@ -14,7 +15,7 @@ type ConfigCatProviderProps = {
sdkKey: string;
pollingMode?: PollingMode;
options?: IReactAutoPollOptions | IReactLazyLoadingOptions | IReactManualPollOptions;
configId?: string;
id?: string;
};

type ConfigCatProviderState = {
Expand Down Expand Up @@ -86,7 +87,7 @@ class ConfigCatProvider extends Component<PropsWithChildren<ConfigCatProviderPro

render(): JSX.Element {

const context: React.Context<ConfigCatContextData | undefined> = this.props.configId ? getOrCreateConfigCatContext(this.props.configId) : ConfigCatContext;
const context: React.Context<ConfigCatContextData | undefined> = this.props.id ? getOrCreateConfigCatContext(this.props.id) : ConfigCatContext;

return (
<context.Provider value={this.state}>
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { IAutoPollOptions, IConfigCatLogger, ILazyLoadingOptions, IManualPollOptions } from "configcat-common";
import type { GetValueType, WithConfigCatClientProps } from "./ConfigCatHOC";
import withConfigCatClient from "./ConfigCatHOC";
import { useConfigCatClient, useConfigCatClientByConfigId, useFeatureFlag, useFeatureFlagByConfigId } from "./ConfigCatHooks";
import { useConfigCatClient, useFeatureFlag } from "./ConfigCatHooks";
import ConfigCatProvider from "./ConfigCatProvider";

export { createConsoleLogger, createFlagOverridesFromMap } from "configcat-common";
Expand All @@ -20,7 +20,7 @@ export type IReactManualPollOptions = IManualPollOptions;
export type IReactConfigCatLogger = IConfigCatLogger;

export type { WithConfigCatClientProps, GetValueType };
export { ConfigCatProvider, useFeatureFlag, useConfigCatClient, withConfigCatClient, useFeatureFlagByConfigId, useConfigCatClientByConfigId };
export { ConfigCatProvider, useFeatureFlag, useConfigCatClient, withConfigCatClient };

/* Public types re-export from common-js */

Expand Down

0 comments on commit 404866e

Please sign in to comment.