|
| 1 | +import React, { createContext, useContext, ReactNode, useMemo } from 'react'; |
| 2 | +import { fn } from 'storybook/test'; |
| 3 | + |
| 4 | +// Types |
| 5 | +export interface ChromeConfig { |
| 6 | + environment: string; |
| 7 | + [key: string]: any; |
| 8 | +} |
| 9 | + |
| 10 | +export interface FeatureFlagsConfig { |
| 11 | + [flagName: string]: boolean; |
| 12 | +} |
| 13 | + |
| 14 | +// Chrome Context |
| 15 | +const ChromeContext = createContext<ChromeConfig>({ |
| 16 | + environment: 'prod' |
| 17 | +}); |
| 18 | + |
| 19 | +export const ChromeProvider: React.FC<{ value: ChromeConfig; children: ReactNode }> = ({ value, children }) => ( |
| 20 | + <ChromeContext.Provider value={value}>{children}</ChromeContext.Provider> |
| 21 | +); |
| 22 | + |
| 23 | +// Feature Flags Context |
| 24 | +const FeatureFlagsContext = createContext<FeatureFlagsConfig>({}); |
| 25 | + |
| 26 | +export const FeatureFlagsProvider: React.FC<{ value: FeatureFlagsConfig; children: ReactNode }> = ({ value, children }) => ( |
| 27 | + <FeatureFlagsContext.Provider value={value}>{children}</FeatureFlagsContext.Provider> |
| 28 | +); |
| 29 | + |
| 30 | +// Chrome spy for testing - IS the spy function |
| 31 | +export const chromeAppNavClickSpy = fn(); |
| 32 | + |
| 33 | +// Mock Hook Implementations (only for Storybook) |
| 34 | +export const useChrome = () => { |
| 35 | + const chromeConfig = useContext(ChromeContext); |
| 36 | + |
| 37 | + return useMemo(() => ({ |
| 38 | + getEnvironment: () => chromeConfig.environment, |
| 39 | + getEnvironmentDetails: () => ({ |
| 40 | + environment: chromeConfig.environment, |
| 41 | + sso: 'https://sso.redhat.com', |
| 42 | + portal: 'https://console.redhat.com' |
| 43 | + }), |
| 44 | + isProd: () => chromeConfig.environment === 'prod', |
| 45 | + isBeta: () => chromeConfig.environment !== 'prod', |
| 46 | + appNavClick: chromeAppNavClickSpy, |
| 47 | + appObjectId: () => undefined, |
| 48 | + appAction: () => undefined, |
| 49 | + updateDocumentTitle: (title: string) => { |
| 50 | + // Mock document title update for Storybook |
| 51 | + if (typeof document !== 'undefined') { |
| 52 | + document.title = title; |
| 53 | + } |
| 54 | + }, |
| 55 | + auth: chromeConfig.auth || { |
| 56 | + getUser: () => Promise.resolve({ |
| 57 | + identity: { |
| 58 | + user: { |
| 59 | + username: 'test-user', |
| 60 | + email: 'test@redhat.com', |
| 61 | + is_org_admin: true, |
| 62 | + is_internal: false |
| 63 | + } |
| 64 | + } |
| 65 | + }), |
| 66 | + getToken: () => Promise.resolve('mock-jwt-token-12345') |
| 67 | + }, |
| 68 | + getBundle: () => 'settings', |
| 69 | + getApp: () => 'user-preferences', |
| 70 | + getUserPermissions: () => Promise.resolve([ |
| 71 | + { |
| 72 | + permission: 'user-preferences:*:*', |
| 73 | + resourceDefinitions: [] |
| 74 | + }, |
| 75 | + ]), |
| 76 | + ...chromeConfig |
| 77 | + }), [chromeConfig]); |
| 78 | +}; |
| 79 | + |
| 80 | +export const useFlag = (flagName: string): boolean => { |
| 81 | + const flags = useContext(FeatureFlagsContext); |
| 82 | + return flags[flagName] || false; |
| 83 | +}; |
| 84 | + |
| 85 | +// Re-export for Unleash mock compatibility |
| 86 | +export const FlagProvider = FeatureFlagsProvider; |
| 87 | +export const useFlagsStatus = () => ({ flagsReady: true, flagsError: false }); |
| 88 | +export const useUnleashContext = () => ({}); |
| 89 | +export const IFlagProvider = FeatureFlagsProvider; |
| 90 | + |
| 91 | +// Export contexts for direct use if needed |
| 92 | +export { ChromeContext, FeatureFlagsContext }; |
0 commit comments