clean routes
This commit is contained in:
49
apps/website/lib/feature/FeatureFlagProvider.tsx
Normal file
49
apps/website/lib/feature/FeatureFlagProvider.tsx
Normal file
@@ -0,0 +1,49 @@
|
||||
'use client';
|
||||
|
||||
import React, { createContext, useContext, useMemo, ReactNode } from 'react';
|
||||
import { FeatureFlagContextType, MockFeatureFlagService, mockFeatureFlags } from './FeatureFlagService';
|
||||
|
||||
const FeatureFlagContext = createContext<FeatureFlagContextType>(mockFeatureFlags);
|
||||
|
||||
interface FeatureFlagProviderProps {
|
||||
children: ReactNode;
|
||||
flags?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Provider for feature flags on the client side
|
||||
* Can be initialized with specific flags or defaults to mock implementation
|
||||
*/
|
||||
export function FeatureFlagProvider({ children, flags }: FeatureFlagProviderProps) {
|
||||
const service = useMemo(() => {
|
||||
if (flags) {
|
||||
return new MockFeatureFlagService(flags);
|
||||
}
|
||||
return mockFeatureFlags;
|
||||
}, [flags]);
|
||||
|
||||
return (
|
||||
<FeatureFlagContext.Provider value={service}>
|
||||
{children}
|
||||
</FeatureFlagContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to access feature flags in client components
|
||||
*/
|
||||
export function useFeatureFlags(): FeatureFlagContextType {
|
||||
const context = useContext(FeatureFlagContext);
|
||||
if (!context) {
|
||||
throw new Error('useFeatureFlags must be used within a FeatureFlagProvider');
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to check if a specific feature is enabled
|
||||
*/
|
||||
export function useFeatureFlag(flag: string): boolean {
|
||||
const { isEnabled } = useFeatureFlags();
|
||||
return isEnabled(flag);
|
||||
}
|
||||
70
apps/website/lib/feature/FeatureFlagService.ts
Normal file
70
apps/website/lib/feature/FeatureFlagService.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* FeatureFlagService - Manages feature flags for both server and client
|
||||
*
|
||||
* Server: Reads from process.env.FEATURE_FLAGS (comma-separated)
|
||||
* Client: Reads from session context or provides mock implementation
|
||||
*/
|
||||
|
||||
// Server-side implementation
|
||||
export class FeatureFlagService {
|
||||
private flags: Set<string>;
|
||||
|
||||
constructor(flags?: string[]) {
|
||||
if (flags) {
|
||||
this.flags = new Set(flags);
|
||||
} else {
|
||||
// Parse from environment variable
|
||||
const flagsEnv = process.env.FEATURE_FLAGS;
|
||||
this.flags = flagsEnv
|
||||
? new Set(flagsEnv.split(',').map(f => f.trim()))
|
||||
: new Set();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a feature flag is enabled
|
||||
*/
|
||||
isEnabled(flag: string): boolean {
|
||||
return this.flags.has(flag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all enabled flags
|
||||
*/
|
||||
getEnabledFlags(): string[] {
|
||||
return Array.from(this.flags);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create service with environment flags
|
||||
*/
|
||||
static fromEnv(): FeatureFlagService {
|
||||
return new FeatureFlagService();
|
||||
}
|
||||
}
|
||||
|
||||
// Client-side context interface
|
||||
export interface FeatureFlagContextType {
|
||||
isEnabled: (flag: string) => boolean;
|
||||
getEnabledFlags: () => string[];
|
||||
}
|
||||
|
||||
// Mock implementation for client-side when no context is available
|
||||
export class MockFeatureFlagService implements FeatureFlagContextType {
|
||||
private flags: Set<string>;
|
||||
|
||||
constructor(flags: string[] = []) {
|
||||
this.flags = new Set(flags);
|
||||
}
|
||||
|
||||
isEnabled(flag: string): boolean {
|
||||
return this.flags.has(flag);
|
||||
}
|
||||
|
||||
getEnabledFlags(): string[] {
|
||||
return Array.from(this.flags);
|
||||
}
|
||||
}
|
||||
|
||||
// Default mock instance for client-side usage
|
||||
export const mockFeatureFlags = new MockFeatureFlagService(['alpha_features']);
|
||||
Reference in New Issue
Block a user