feat: order streams by last child creation

This commit is contained in:
talksik
2026-03-19 10:46:19 -07:00
parent 0723afc412
commit 71dd0149bc
7 changed files with 76 additions and 35 deletions
+25 -11
View File
@@ -1,9 +1,11 @@
import { useMutation } from "@tanstack/react-query";
import { createParticle } from "@/lib/firestore-particles";
import { createParticle, updateStreamParticleLastChildParticle } 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> {
collectionPath: string;
// Path to which the new particle will be added as a child
path: ParticlePath;
type: T;
properties: ParticlePropertiesMap[T];
createdByEmail: string;
@@ -11,29 +13,41 @@ interface CreateParticleParams<T extends ParticleType = ParticleType> {
export function useCreateParticle() {
return useMutation({
mutationFn: (params: CreateParticleParams) =>
createParticle(
params.collectionPath,
mutationFn: async (params: CreateParticleParams) => {
const collectionPath = toFirestoreChildrenPath(params.path);
const result = await createParticle(
collectionPath,
params.type,
params.properties,
params.createdByEmail,
),
);
const streamDocPath = toFirestoreDocPath(params.path);
await updateStreamParticleLastChildParticle(streamDocPath);
return result;
}
});
}
type CreateStreamParticleParams = Omit<CreateParticleParams<"stream">, "type"> & {
type CreateStreamParticleParams = {
networkId: string;
properties: ParticlePropertiesMap["stream"];
createdByEmail: string;
visibleTo?: string[];
};
export function useCreateStreamParticle() {
return useMutation({
mutationFn: (params: CreateStreamParticleParams) =>
createParticle(
params.collectionPath,
mutationFn: async (params: CreateStreamParticleParams) => {
const path = particlePath(params.networkId, []);
const networkCollectionPath = toFirestoreChildrenPath(path);
return await createParticle(
networkCollectionPath,
"stream",
params.properties,
params.createdByEmail,
params.visibleTo,
),
);
}
});
}