Files
maternal-app/maternal-web/hooks/useSelectedFamily.ts
Andrei ce939b909b
Some checks failed
ParentFlow CI/CD Pipeline / Backend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Frontend Tests (push) Has been cancelled
ParentFlow CI/CD Pipeline / Security Scanning (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-app/maternal-app-backend dockerfile:Dockerfile.production name:backend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Build Docker Images (map[context:maternal-web dockerfile:Dockerfile.production name:frontend]) (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Development (push) Has been cancelled
ParentFlow CI/CD Pipeline / Deploy to Production (push) Has been cancelled
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
feat: Add multi-family support with family selector UI
- Created useSelectedFamily hook for managing family selection across app
- Added family selector dropdown to family page when user has multiple families
- Family selection persists in localStorage
- Fixed bug where users could only see their first family
- Updated dev port from 3005 to 3030 in package.json

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-09 12:54:37 +00:00

48 lines
1.4 KiB
TypeScript

import { useState, useEffect } from 'react';
import { useAuth } from '@/lib/auth/AuthContext';
const SELECTED_FAMILY_KEY = 'selectedFamilyIndex';
/**
* Hook to manage selected family across the app
* Stores selection in localStorage for persistence
*/
export function useSelectedFamily() {
const { user } = useAuth();
const userFamilies = user?.families || [];
// Initialize from localStorage or default to 0
const [selectedIndex, setSelectedIndex] = useState<number>(() => {
if (typeof window === 'undefined') return 0;
const stored = localStorage.getItem(SELECTED_FAMILY_KEY);
return stored ? parseInt(stored, 10) : 0;
});
// Ensure selectedIndex is within bounds
useEffect(() => {
if (selectedIndex >= userFamilies.length && userFamilies.length > 0) {
setSelectedIndex(0);
}
}, [userFamilies.length, selectedIndex]);
// Save to localStorage when changed
const setSelectedFamily = (index: number) => {
setSelectedIndex(index);
if (typeof window !== 'undefined') {
localStorage.setItem(SELECTED_FAMILY_KEY, index.toString());
}
};
const familyId = userFamilies[selectedIndex]?.familyId || null;
const familyRole = userFamilies[selectedIndex]?.role || null;
return {
familyId,
familyRole,
selectedIndex,
setSelectedIndex: setSelectedFamily,
userFamilies,
hasMultipleFamilies: userFamilies.length > 1,
};
}