All files / packages/app/features/peer peer-connection-input.tsx

100% Statements 32/32
100% Branches 12/12
100% Functions 13/13
100% Lines 32/32

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                                  41x   117x 117x 117x     117x       117x 8x     117x           117x       4x           117x       117x       2x 2x         117x   4x       117x         117x 2x       117x 8x                         4x                 84x 54x     2x               73x     64x                         145x   72x                                 4x                               25x 25x      
import React from "react";
 
import { useMutation, useSuspenseQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
import { z } from "zod";
 
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import { useAppForm } from "~app/utils/forms";
import { useTRPC } from "~app/utils/trpc";
import { emailSchema } from "~app/utils/validation";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import { Input } from "~components/input";
import { SkeletonInput } from "~components/skeleton-input";
import type { PeerId, UserId } from "~db/ids";
import { options as peersUnlinkOptions } from "~mutations/peers/unlink";
import { options as userConnectionsAddOptions } from "~mutations/user-connection-intentions/add";
import { options as userConnectionsRemoveOptions } from "~mutations/user-connection-intentions/remove";
 
type Props = {
	id: PeerId;
	isLoading: boolean;
};
 
export const PeerConnectionInput: React.FC<Props> = suspendedFallback(
	({ isLoading, id }) => {
		const { t } = useTranslation("peers");
		const trpc = useTRPC();
		const { data: peer } = useSuspenseQuery(
			trpc.peers.get.queryOptions({ id }),
		);
		const { data: connectionIntentions } = useSuspenseQuery(
			trpc.userConnectionIntentions.getAll.queryOptions(),
		);
		const outboundConnectionIntention =
			connectionIntentions.outbound.find(
				(element) => element.peer.id === peer.id,
			) ?? null;
 
		const connectPeerMutation = useMutation(
			trpc.userConnectionIntentions.add.mutationOptions(
				useTrpcMutationOptions(userConnectionsAddOptions),
			),
		);
 
		const form = useAppForm({
			defaultValues: { value: peer.connectedUser?.email ?? "" },
			validators: { onChange: z.object({ value: emailSchema }) },
			onSubmit: ({ value }) => {
				connectPeerMutation.mutate({
					peerId: peer.id,
					email: value.value,
				});
			},
		});
		const [inputShown, setInputShown] = React.useState(
			Boolean(peer.connectedUser),
		);
 
		const cancelRequestMutation = useMutation(
			trpc.userConnectionIntentions.remove.mutationOptions(
				useTrpcMutationOptions(userConnectionsRemoveOptions, {
					onSuccess: () => {
						form.reset();
						setInputShown(false);
					},
				}),
			),
		);
		const cancelRequest = React.useCallback(
			(userId: UserId) =>
				cancelRequestMutation.mutate({ targetUserId: userId }),
			[cancelRequestMutation],
		);
 
		const unlinkMutation = useMutation(
			trpc.peers.unlink.mutationOptions(
				useTrpcMutationOptions(peersUnlinkOptions),
			),
		);
		const unlinkPeer = React.useCallback(
			() => unlinkMutation.mutate({ id: peer.id }),
			[unlinkMutation, peer.id],
		);
 
		if (outboundConnectionIntention) {
			return (
				<Input
					label={t("peer.connection.outbound.label")}
					value={outboundConnectionIntention.user.email}
					isReadOnly
					mutation={cancelRequestMutation}
					endContent={
						<Button
							title={t("peer.connection.cancel.title")}
							variant="light"
							isLoading={cancelRequestMutation.isPending}
							color="danger"
							isIconOnly
							onPress={() => cancelRequest(outboundConnectionIntention.user.id)}
						>
							<Icon name="trash" className="size-6" />
						</Button>
					}
				/>
			);
		}
 
		if (!inputShown) {
			return (
				<Button
					color="primary"
					onPress={() => setInputShown(true)}
					isDisabled={isLoading}
				>
					{t("peer.connection.connect")}
				</Button>
			);
		}
 
		return (
			<form.AppField name="value">
				{(field) => (
					<field.TextField
						value={field.state.value}
						onValueChange={field.setValue}
						name={field.name}
						onBlur={field.handleBlur}
						fieldError={
							field.state.meta.isDirty ? field.state.meta.errors : undefined
						}
						label={t("peer.connection.email.label")}
						mutation={[connectPeerMutation, unlinkMutation]}
						isDisabled={isLoading}
						isReadOnly={Boolean(peer.connectedUser)}
						endContent={
							<form.Subscribe selector={(state) => state.canSubmit}>
								{(canSubmit) =>
									peer.connectedUser ? (
										<Button
											title={t("peer.connection.unlink.title")}
											variant="light"
											isLoading={unlinkMutation.isPending}
											isIconOnly
											onPress={unlinkPeer}
										>
											<Icon name="unlink" className="size-6" />
										</Button>
									) : (
										<Button
											title={t("peer.connection.link.title")}
											variant="light"
											isLoading={connectPeerMutation.isPending}
											isDisabled={!canSubmit}
											onPress={() => {
												void field.form.handleSubmit();
											}}
											isIconOnly
										>
											<Icon name="link" className="size-6" />
										</Button>
									)
								}
							</form.Subscribe>
						}
					/>
				)}
			</form.AppField>
		);
	},
	() => {
		const { t } = useTranslation("peers");
		return <SkeletonInput label={t("peer.connection.email.label")} />;
	},
);