|
|
|
|
@@ -1,6 +1,5 @@
|
|
|
|
|
'use client'
|
|
|
|
|
import React, { useCallback, useEffect, useState } from 'react'
|
|
|
|
|
import { useConfig } from '@payloadcms/ui'
|
|
|
|
|
import React, { useCallback, useEffect, useState, useRef } from 'react'
|
|
|
|
|
|
|
|
|
|
export interface FeatureFlag {
|
|
|
|
|
name: string
|
|
|
|
|
@@ -14,34 +13,78 @@ export interface FeatureFlag {
|
|
|
|
|
metadata?: any
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface FeatureFlagOptions {
|
|
|
|
|
serverURL?: string
|
|
|
|
|
apiPath?: string
|
|
|
|
|
collectionSlug?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper to get config from options or defaults
|
|
|
|
|
function getConfig(options?: FeatureFlagOptions) {
|
|
|
|
|
// Check if serverURL is explicitly provided
|
|
|
|
|
if (options?.serverURL) {
|
|
|
|
|
return {
|
|
|
|
|
serverURL: options.serverURL,
|
|
|
|
|
apiPath: options.apiPath || '/api',
|
|
|
|
|
collectionSlug: options.collectionSlug || 'feature-flags'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// In browser environment, use window.location.origin
|
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
|
return {
|
|
|
|
|
serverURL: window.location.origin,
|
|
|
|
|
apiPath: options?.apiPath || '/api',
|
|
|
|
|
collectionSlug: options?.collectionSlug || 'feature-flags'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// During SSR or in non-browser environments, use relative URL
|
|
|
|
|
// This will work for same-origin requests
|
|
|
|
|
return {
|
|
|
|
|
serverURL: '',
|
|
|
|
|
apiPath: options?.apiPath || '/api',
|
|
|
|
|
collectionSlug: options?.collectionSlug || 'feature-flags'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Hook to fetch all active feature flags from the API
|
|
|
|
|
*/
|
|
|
|
|
export function useFeatureFlags(
|
|
|
|
|
initialFlags: Partial<FeatureFlag>[]
|
|
|
|
|
initialFlags: Partial<FeatureFlag>[],
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
): {
|
|
|
|
|
flags: Partial<FeatureFlag>[]
|
|
|
|
|
loading: boolean
|
|
|
|
|
error: string | null
|
|
|
|
|
refetch: () => Promise<void>
|
|
|
|
|
} {
|
|
|
|
|
const { config } = useConfig()
|
|
|
|
|
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
|
|
|
|
const [flags, setFlags] = useState<Partial<FeatureFlag>[]>(initialFlags)
|
|
|
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
|
|
|
|
|
|
// Use ref to store initialFlags to avoid re-creating fetchFlags on every render
|
|
|
|
|
const initialFlagsRef = useRef(initialFlags)
|
|
|
|
|
|
|
|
|
|
// Update ref when initialFlags changes (but won't trigger re-fetch)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
initialFlagsRef.current = initialFlags
|
|
|
|
|
}, [initialFlags])
|
|
|
|
|
|
|
|
|
|
const fetchFlags = useCallback(async () => {
|
|
|
|
|
try {
|
|
|
|
|
setLoading(true)
|
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
|
|
// Use Payload's native collection API
|
|
|
|
|
const names = initialFlags.map(f => f.name).filter(Boolean)
|
|
|
|
|
const names = initialFlagsRef.current.map(f => f.name).filter(Boolean)
|
|
|
|
|
const query = names.length > 0
|
|
|
|
|
? `?where[name][in]=${names.join(',')}&limit=1000`
|
|
|
|
|
: '?limit=1000'
|
|
|
|
|
|
|
|
|
|
const response = await fetch(`${config.serverURL}${config.routes.api}/feature-flags${query}`)
|
|
|
|
|
const response = await fetch(`${serverURL}${apiPath}/${collectionSlug}${query}`)
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
throw new Error(`Failed to fetch feature flags: ${response.statusText}`)
|
|
|
|
|
@@ -64,7 +107,7 @@ export function useFeatureFlags(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort flags based on the order of names in initialFlags
|
|
|
|
|
const sortedFlags = initialFlags.map(initialFlag => {
|
|
|
|
|
const sortedFlags = initialFlagsRef.current.map(initialFlag => {
|
|
|
|
|
const fetchedFlag = fetchedFlagsMap.get(initialFlag.name!)
|
|
|
|
|
// Use fetched flag if available, otherwise keep the initial flag
|
|
|
|
|
return fetchedFlag || initialFlag
|
|
|
|
|
@@ -77,7 +120,7 @@ export function useFeatureFlags(
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [config.serverURL, config.routes.api, initialFlags])
|
|
|
|
|
}, [serverURL, apiPath, collectionSlug]) // Remove initialFlags from dependencies
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void fetchFlags()
|
|
|
|
|
@@ -89,13 +132,16 @@ export function useFeatureFlags(
|
|
|
|
|
/**
|
|
|
|
|
* Hook to check if a specific feature flag is enabled
|
|
|
|
|
*/
|
|
|
|
|
export function useFeatureFlag(flagName: string): {
|
|
|
|
|
export function useFeatureFlag(
|
|
|
|
|
flagName: string,
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
): {
|
|
|
|
|
isEnabled: boolean
|
|
|
|
|
flag: Partial<FeatureFlag> | null
|
|
|
|
|
loading: boolean
|
|
|
|
|
error: string | null
|
|
|
|
|
} {
|
|
|
|
|
const { flags, loading, error } = useFeatureFlags([{ name: flagName }])
|
|
|
|
|
const { flags, loading, error } = useFeatureFlags([{ name: flagName }], options)
|
|
|
|
|
|
|
|
|
|
const flag = flags.find(f => f.name === flagName) || null
|
|
|
|
|
const isEnabled = flag?.enabled || false
|
|
|
|
|
@@ -106,13 +152,16 @@ export function useFeatureFlag(flagName: string): {
|
|
|
|
|
/**
|
|
|
|
|
* Hook to fetch a specific feature flag from the API
|
|
|
|
|
*/
|
|
|
|
|
export function useSpecificFeatureFlag(flagName: string): {
|
|
|
|
|
export function useSpecificFeatureFlag(
|
|
|
|
|
flagName: string,
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
): {
|
|
|
|
|
flag: FeatureFlag | null
|
|
|
|
|
loading: boolean
|
|
|
|
|
error: string | null
|
|
|
|
|
refetch: () => Promise<void>
|
|
|
|
|
} {
|
|
|
|
|
const { config } = useConfig()
|
|
|
|
|
const { serverURL, apiPath, collectionSlug } = getConfig(options)
|
|
|
|
|
const [flag, setFlag] = useState<FeatureFlag | null>(null)
|
|
|
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
|
const [error, setError] = useState<string | null>(null)
|
|
|
|
|
@@ -124,7 +173,7 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|
|
|
|
|
|
|
|
|
// Use Payload's native collection API with query filter
|
|
|
|
|
const response = await fetch(
|
|
|
|
|
`${config.serverURL}${config.routes.api}/feature-flags?where[name][equals]=${flagName}&limit=1`
|
|
|
|
|
`${serverURL}${apiPath}/${collectionSlug}?where[name][equals]=${flagName}&limit=1`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
@@ -153,7 +202,7 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|
|
|
|
} finally {
|
|
|
|
|
setLoading(false)
|
|
|
|
|
}
|
|
|
|
|
}, [config.serverURL, config.routes.api, flagName])
|
|
|
|
|
}, [serverURL, apiPath, collectionSlug, flagName])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
void fetchFlag()
|
|
|
|
|
@@ -167,14 +216,15 @@ export function useSpecificFeatureFlag(flagName: string): {
|
|
|
|
|
*/
|
|
|
|
|
export function useVariantSelection(
|
|
|
|
|
flagName: string,
|
|
|
|
|
userId: string
|
|
|
|
|
userId: string,
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
): {
|
|
|
|
|
variant: string | null
|
|
|
|
|
flag: FeatureFlag | null
|
|
|
|
|
loading: boolean
|
|
|
|
|
error: string | null
|
|
|
|
|
} {
|
|
|
|
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName)
|
|
|
|
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
|
|
|
|
|
|
|
|
|
const variant = flag?.enabled && flag.variants
|
|
|
|
|
? selectVariantForUser(userId, flag.variants)
|
|
|
|
|
@@ -188,14 +238,15 @@ export function useVariantSelection(
|
|
|
|
|
*/
|
|
|
|
|
export function useRolloutCheck(
|
|
|
|
|
flagName: string,
|
|
|
|
|
userId: string
|
|
|
|
|
userId: string,
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
): {
|
|
|
|
|
isInRollout: boolean
|
|
|
|
|
flag: FeatureFlag | null
|
|
|
|
|
loading: boolean
|
|
|
|
|
error: string | null
|
|
|
|
|
} {
|
|
|
|
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName)
|
|
|
|
|
const { flag, loading, error } = useSpecificFeatureFlag(flagName, options)
|
|
|
|
|
|
|
|
|
|
const isInRollout = flag?.enabled
|
|
|
|
|
? checkUserInRollout(userId, flag.rolloutPercentage || 100)
|
|
|
|
|
@@ -253,13 +304,14 @@ function checkUserInRollout(userId: string, percentage: number): boolean {
|
|
|
|
|
*/
|
|
|
|
|
export function withFeatureFlag<P extends Record<string, any>>(
|
|
|
|
|
flagName: string,
|
|
|
|
|
FallbackComponent?: React.ComponentType<P>
|
|
|
|
|
FallbackComponent?: React.ComponentType<P>,
|
|
|
|
|
options?: FeatureFlagOptions
|
|
|
|
|
) {
|
|
|
|
|
return function FeatureFlagWrapper(
|
|
|
|
|
WrappedComponent: React.ComponentType<P>
|
|
|
|
|
): React.ComponentType<P> {
|
|
|
|
|
return function WithFeatureFlagComponent(props: P): React.ReactElement | null {
|
|
|
|
|
const { isEnabled, loading } = useFeatureFlag(flagName)
|
|
|
|
|
const { isEnabled, loading } = useFeatureFlag(flagName, options)
|
|
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return null // or a loading spinner
|
|
|
|
|
|