* upload & view attachments to particles * allow download of attachments
30 lines
978 B
TypeScript
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 };
|
|
}
|