Files
llink/js/src/hooks/use-create-particle.ts
T
Arjun Patel bf0147d542 refactor: reference human id instead of email (#73)
* refactor: update api and client to reference humanIds

* fix: prevent deletion of network member

This may cause various side effects if there is data in other services
which reference this member
2026-03-24 19:48:02 -07:00

54 lines
1.6 KiB
TypeScript

import { useMutation } from "@tanstack/react-query";
import { createParticle, updateStreamLastChildAt } from "@/lib/firestore-particles";
import type { ParticleType, ParticlePropertiesMap } from "@/api/types";
import { particlePath, ParticlePath, toFirestoreChildrenPath, toFirestoreDocPath } from "@/lib/particle-path";
interface CreateParticleParams<T extends ParticleType = ParticleType> {
// Path to which the new particle will be added as a child
path: ParticlePath;
type: T;
properties: ParticlePropertiesMap[T];
createdByHumanId: string;
}
export function useCreateParticle() {
return useMutation({
mutationFn: async (params: CreateParticleParams) => {
const collectionPath = toFirestoreChildrenPath(params.path);
const result = await createParticle(
collectionPath,
params.type,
params.properties,
params.createdByHumanId,
);
const streamDocPath = toFirestoreDocPath(params.path);
await updateStreamLastChildAt(streamDocPath);
return result;
}
});
}
type CreateStreamParticleParams = {
networkId: string;
properties: ParticlePropertiesMap["stream"];
createdByHumanId: string;
visibleTo?: string[];
};
export function useCreateStreamParticle() {
return useMutation({
mutationFn: async (params: CreateStreamParticleParams) => {
const path = particlePath(params.networkId, []);
const networkCollectionPath = toFirestoreChildrenPath(path);
return await createParticle(
networkCollectionPath,
"stream",
params.properties,
params.createdByHumanId,
params.visibleTo,
);
}
});
}