Sifa professional network frontend (Next.js, React, TailwindCSS) sifa.id/

fix(profile): make useProfileEdit safe outside ProfileEditProvider (#318)

IdentityCard is used on events, embed, and homepage pages that don't
have a ProfileEditProvider. The hook now returns safe defaults (non-owner,
no preview) instead of throwing, fixing the Docker build failure.

authored by

Guido X Jansen and committed by
GitHub
c8fad46d 3af7ce6d

+20 -8
+16 -4
src/components/profile-edit-provider.tsx
··· 118 118 ); 119 119 } 120 120 121 + const NO_OP = () => {}; 122 + const NO_OP_RESULT = { success: false } as never; 123 + 124 + /** Safe defaults when used outside a ProfileEditProvider (e.g. embed, events, homepage). */ 125 + const DEFAULT_CONTEXT: ProfileEditContextValue = { 126 + profile: {} as Profile, 127 + isActualOwner: false, 128 + previewMode: false, 129 + togglePreview: NO_OP, 130 + updateProfile: NO_OP, 131 + addItem: NO_OP, 132 + updateItem: NO_OP, 133 + removeItem: NO_OP, 134 + }; 135 + 121 136 export function useProfileEdit(): ProfileEditContextValue { 122 137 const context = useContext(ProfileEditContext); 123 - if (context === null) { 124 - throw new Error('useProfileEdit must be used within a ProfileEditProvider'); 125 - } 126 - return context; 138 + return context ?? DEFAULT_CONTEXT; 127 139 }
+4 -4
tests/components/profile-edit-provider.test.tsx
··· 124 124 expect(result.current.profile.education[0]?.institution).toBe('Stanford'); 125 125 }); 126 126 127 - it('throws when useProfileEdit is used outside provider', () => { 128 - expect(() => { 129 - renderHook(() => useProfileEdit()); 130 - }).toThrow('useProfileEdit must be used within a ProfileEditProvider'); 127 + it('returns safe defaults when useProfileEdit is used outside provider', () => { 128 + const { result } = renderHook(() => useProfileEdit()); 129 + expect(result.current.isActualOwner).toBe(false); 130 + expect(result.current.previewMode).toBe(false); 131 131 }); 132 132 });