50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
/**
|
|
* Profile Context
|
|
*
|
|
* React context for profile data.
|
|
*/
|
|
|
|
import { createContext, useContext } from 'react';
|
|
|
|
import type { ProfileContextValue } from './types';
|
|
|
|
const defaultContext: ProfileContextValue = {
|
|
profiles: [],
|
|
primaryProfile: null,
|
|
isLoading: false,
|
|
error: null,
|
|
refetch: async () => {},
|
|
updateProfile: async () => {
|
|
throw new Error('ProfileProvider not mounted');
|
|
},
|
|
isUpdating: false,
|
|
getProfileByType: () => null,
|
|
isDevMode: false,
|
|
};
|
|
|
|
export const ProfileContext = createContext<ProfileContextValue>(defaultContext);
|
|
|
|
/**
|
|
* Hook to access profile data and operations.
|
|
*
|
|
* Must be used within a ProfileProvider or ProfileProviderWithDevBridge.
|
|
*
|
|
* @example
|
|
* ```tsx
|
|
* function ProfileBadge() {
|
|
* const { profiles, primaryProfile, isLoading } = useProfile();
|
|
*
|
|
* if (isLoading) return <Spinner />;
|
|
* if (!primaryProfile) return null;
|
|
*
|
|
* return (
|
|
* <Badge>
|
|
* {primaryProfile.displayName} - {primaryProfile.completionPercentage}%
|
|
* </Badge>
|
|
* );
|
|
* }
|
|
* ```
|
|
*/
|
|
export function useProfile(): ProfileContextValue {
|
|
return useContext(ProfileContext);
|
|
}
|