All files / packages/app/features/connection-intentions inbound-connection-intention.tsx

89.65% Statements 26/29
80% Branches 8/10
100% Functions 9/9
89.65% Lines 26/29

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                                          14x 21x 21x                                           32x 49x 49x 49x   49x         49x 2x     2x           49x         49x 2x         49x 49x     49x 37x 3x       3x 3x       49x         49x 49x                                           1x                           37x                      
import React from "react";
 
import { skipToken, useMutation, useQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import {
	PeersSuggest,
	SkeletonPeersSuggest,
} from "~app/components/app/peers-suggest";
import { ConfirmModal } from "~app/components/confirm-modal";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { InboundIntention } from "~app/trpc-types";
import { useTRPC } from "~app/utils/trpc";
import { Button } from "~components/button";
import { Input } from "~components/input";
import { SkeletonInput } from "~components/skeleton-input";
import { View } from "~components/view";
import type { PeerId } from "~db/ids";
import { options as userConnectionsAcceptOptions } from "~mutations/user-connection-intentions/accept";
import { options as userConnectionsRejectOptions } from "~mutations/user-connection-intentions/reject";
 
export const SkeletonInboundConnectionIntention = () => {
	const { t } = useTranslation("peers");
	return (
		<View className="gap-2">
			<View className="flex flex-row justify-between">
				<SkeletonInput
					className="max-w-xs"
					size="sm"
					label={t("intentions.form.email.label")}
					skeletonClassName="w-48"
				/>
				<Button color="warning" variant="bordered" isDisabled>
					{t("intentions.form.rejectButton")}
				</Button>
			</View>
			<SkeletonPeersSuggest label={t("intentions.peerSuggestLabel")} />
		</View>
	);
};
 
type Props = {
	intention: InboundIntention;
};
 
export const InboundConnectionIntention: React.FC<Props> = ({ intention }) => {
	const trpc = useTRPC();
	const [peerId, setPeerId] = React.useState<PeerId>();
	const { t } = useTranslation("peers");
 
	const acceptConnectionMutation = useMutation(
		trpc.userConnectionIntentions.accept.mutationOptions(
			useTrpcMutationOptions(userConnectionsAcceptOptions),
		),
	);
	const acceptConnection = React.useCallback(() => {
		Iif (!peerId) {
			return;
		}
		acceptConnectionMutation.mutate({
			userId: intention.user.id,
			peerId,
		});
	}, [acceptConnectionMutation, intention.user.id, peerId]);
 
	const rejectConnectionMutation = useMutation(
		trpc.userConnectionIntentions.reject.mutationOptions(
			useTrpcMutationOptions(userConnectionsRejectOptions),
		),
	);
	const rejectConnection = React.useCallback(() => {
		rejectConnectionMutation.mutate({
			sourceUserId: intention.user.id,
		});
	}, [rejectConnectionMutation, intention.user.id]);
 
	const peersSuggestOptions = React.useMemo(
		() => ({ type: "not-connected" as const }),
		[],
	);
	const onPeerClick = React.useCallback(
		(openModal: () => void) => (nextPeerId: PeerId) => {
			Iif (nextPeerId === peerId) {
				setPeerId(undefined);
				return;
			}
			setPeerId(nextPeerId);
			openModal();
		},
		[peerId],
	);
	const peerQuery = useQuery(
		trpc.peers.get.queryOptions(peerId ? { id: peerId } : skipToken),
	);
 
	const isLoading =
		acceptConnectionMutation.isPending || rejectConnectionMutation.isPending;
	return (
		<View testID="inbound-connection-intention" className="gap-2">
			<View className="flex flex-row justify-between">
				<Input
					isReadOnly
					className="max-w-xs"
					size="sm"
					defaultValue={intention.user.email}
					label={t("intentions.form.email.label")}
					type="email"
				/>
				<Button
					color="warning"
					variant="bordered"
					isDisabled={isLoading}
					onPress={rejectConnection}
				>
					{t("intentions.form.rejectButton")}
				</Button>
			</View>
			<ConfirmModal
				onConfirm={acceptConnection}
				onCancel={() => setPeerId(undefined)}
				isLoading={acceptConnectionMutation.isPending}
				title={t("intentions.modal.title")}
				subtitle={
					peerQuery.data
						? t("intentions.modal.description", {
								email: intention.user.email,
								peerName: peerQuery.data.name,
							})
						: undefined
				}
				confirmText={t("intentions.modal.confirmText")}
			>
				{({ openModal }) => (
					<PeersSuggest
						label={t("intentions.peerSuggestLabel")}
						onPeerClick={onPeerClick(openModal)}
						options={peersSuggestOptions}
						closeOnSelect
					/>
				)}
			</ConfirmModal>
		</View>
	);
};