All files / packages/mutations/src/peers update.ts

77.41% Statements 24/31
40% Branches 4/10
83.33% Functions 15/18
76.66% Lines 23/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90                        41x 5x 5x   2x   3x         41x 5x 2x 2x   1x   1x             41x     5x               41x     5x               41x   5x 5x   5x           5x             3x 3x     2x                
import type { TRPCMutationInput } from "~app/trpc";
import type { ForeignPeer, Peer } from "~app/trpc-types";
import type { PeerId } from "~db/ids";
 
import {
	invalidateSuggest as invalidateSuggestPeers,
	updateRevert as updateRevertPeers,
} from "../cache/peers";
import type { UseContextedMutationOptions } from "../context";
import type { SnapshotFn, UpdateFn } from "../types";
 
const applyUpdate =
	(update: TRPCMutationInput<"peers.update">["update"]): UpdateFn<Peer> =>
	(item) => {
		switch (update.type) {
			case "name":
				return { ...item, name: update.name };
			case "publicName":
				return { ...item, publicName: update.publicName };
		}
	};
 
const getRevert =
	(update: TRPCMutationInput<"peers.update">["update"]): SnapshotFn<Peer> =>
	(snapshot) =>
	(peer) => {
		switch (update.type) {
			case "name":
				return { ...peer, name: snapshot.name };
			case "publicName":
				return { ...peer, publicName: snapshot.publicName };
		}
	};
 
type OwnPeerSnapshot = Exclude<ForeignPeer, { remoteId: PeerId }>;
 
const applyForeignUpdate =
	(
		update: TRPCMutationInput<"peers.update">["update"],
	): UpdateFn<OwnPeerSnapshot> =>
	(peer) => {
		if ("remoteId" in peer) {
			return peer;
		}
		return applyUpdate(update)(peer);
	};
 
const getForeignRevert =
	(
		update: TRPCMutationInput<"peers.update">["update"],
	): SnapshotFn<OwnPeerSnapshot> =>
	(snapshot) =>
	(peer) => {
		if ("remoteId" in peer || "remoteId" in snapshot) {
			return peer;
		}
		return getRevert(update)(snapshot)(peer);
	};
 
export const options: UseContextedMutationOptions<"peers.update"> = {
	mutationKey: "peers.update",
	onMutate: (controllerContext) => (updateObject) =>
		updateRevertPeers(controllerContext, {
			get: (controller) =>
				controller.update(
					updateObject.id,
					applyUpdate(updateObject.update),
					getRevert(updateObject.update),
				),
			getForeign: (controller) =>
				controller.updateOwn(
					updateObject.id,
					applyForeignUpdate(updateObject.update),
					getForeignRevert(updateObject.update),
				),
			getPaged: undefined,
		}),
	onSuccess: (controllerContext) => () =>
		invalidateSuggestPeers(controllerContext),
	errorToastOptions:
		({ t }) =>
		(errors) => ({
			text: t("toasts.updatePeer.error", {
				ns: "peers",
				peersAmount: errors.length,
				errors,
			}),
		}),
};