Files
llink/js/src/hooks/use-particle-attachments.ts
T
Arjun PatelandGitHub d6280439d0 feat: support file and image attachments (#98)
* upload & view attachments to particles

* allow download of attachments
2026-03-30 10:31:24 -07:00

30 lines
978 B
TypeScript

import { useMemo } from "react";
import type { Particle } from "@/api/types";
import { useParticleChildren } from "@/hooks/use-particle";
import { particlePath, type ParticlePath, parseParticlePath } from "@/lib/particle-path";
type FileParticle = Extract<Particle, { type: "file" }>;
/**
* Fetches file children (attachments) of a particle in a stream.
* Uses a one-shot query since attachments are immutable after creation.
*/
export function useParticleAttachments(
streamPath: ParticlePath,
particleId: string,
) {
const childrenPath = useMemo(() => {
const { networkId, segments } = parseParticlePath(streamPath);
return particlePath(networkId, [...segments, particleId]);
}, [streamPath, particleId]);
const { data: children, isLoading } = useParticleChildren(childrenPath);
const attachments = useMemo(
() => (children ?? []).filter((c): c is FileParticle => c.type === "file"),
[children],
);
return { attachments, isLoading };
}