import { useState, useEffect, useMemo } from "react"; import { subscribeToParticleChildren } from "@/lib/firestore-particles"; import { firestorePath } from "@/lib/firestore-paths"; import type { Particle } from "@/api/types"; interface UseParticleChildrenResult { children: Particle[]; isLoading: boolean; error: Error | null; } export function useParticleChildren( networkId: string, parentSegments: string[], ): UseParticleChildrenResult { const [children, setChildren] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const collectionPath = useMemo(() => { if (parentSegments.length === 0) return firestorePath(networkId, []); return `${firestorePath(networkId, parentSegments)}/children`; }, [networkId, parentSegments.join("/")]); useEffect(() => { setIsLoading(true); setError(null); setChildren([]); const unsubscribe = subscribeToParticleChildren( collectionPath, (data) => { setChildren(data); setIsLoading(false); }, (err) => { setError(err); setIsLoading(false); }, ); return unsubscribe; }, [collectionPath]); return { children, isLoading, error }; }