import { useMemo } from 'react'; import { useCurrentUser } from '@/hooks/useCurrentUser'; import { useCrews } from '@/hooks/useCrews'; export interface Crewmate { pubkey: string; /** Crews (names) the person shares with the viewer. */ sharedCrews: string[]; } /** * The people in the viewer's crews — founder + members of every crew the * logged-in user belongs to (as founder or listed member), minus themselves. * * Powers the "Crew-mates" tab in Contacts: the human network the user has * already opted into by being in shared crews. */ export function useCrewmates() { const { user } = useCurrentUser(); const { data: crews, isLoading } = useCrews(200); const crewmates = useMemo(() => { if (!user || !crews) return []; const myCrews = crews.filter( (c) => c.pubkey === user.pubkey || c.members.includes(user.pubkey), ); const map = new Map>(); for (const crew of myCrews) { const people = [crew.pubkey, ...crew.members]; for (const pk of people) { if (pk === user.pubkey) continue; if (!map.has(pk)) map.set(pk, new Set()); map.get(pk)!.add(crew.name); } } return Array.from(map.entries()) .map(([pubkey, names]) => ({ pubkey, sharedCrews: Array.from(names) })) .sort((a, b) => b.sharedCrews.length - a.sharedCrews.length); }, [user, crews]); return { crewmates, isLoading }; }