* plumb for firestore * chore: cleanup orion api to only include essentials * setup boilerplate for data and rendering This includes zod types creation for API response validation, and exploration of path based resolution of rendering particles. * chore: structure container particles for rendering children * wire firestore crud for particles * integrate visibility to particles * docs: explain particle view resolver
47 lines
1.2 KiB
TypeScript
47 lines
1.2 KiB
TypeScript
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<Particle[]>([]);
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [error, setError] = useState<Error | null>(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 };
|
|
}
|