feat: order streams by last child creation
This commit is contained in:
@@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user