All files / packages/app/features/receipt receipt-participant-actions.tsx

100% Statements 20/20
83.33% Branches 5/6
100% Functions 7/7
100% Lines 20/20

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                                                        13x 7x 7x 7x     7x 2x                                   7x                           78x           33x 33x 33x     33x                 33x   2x                                     33x 18x                           33x                             2x                     39x         33x                                  
import React from "react";
 
import { useMutation, useSuspenseQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import { DebtSyncStatus } from "~app/components/app/debt-sync-status";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import type { Participant } from "~app/hooks/use-participants";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { Debt, Receipt } from "~app/trpc-types";
import { areDebtsSynced } from "~app/utils/debts";
import { getReceiptDebtName } from "~app/utils/receipt";
import { useTRPC } from "~app/utils/trpc";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import { View } from "~components/view";
import type { DebtId } from "~db/ids";
import { options as debtsAddOptions } from "~mutations/debts/add";
import { options as debtsUpdateOptions } from "~mutations/debts/update";
 
type Props = {
	receipt: Receipt;
	participant: Participant;
	outcomingDebtId?: DebtId;
};
 
const ReceiptParticipantNoDebtAction: React.FC<
	Omit<Props, "outcomingDebtId">
> = ({ receipt, participant }) => {
	const { t } = useTranslation(["receipts", "debts"]);
	const trpc = useTRPC();
	const addMutation = useMutation(
		trpc.debts.add.mutationOptions(useTrpcMutationOptions(debtsAddOptions)),
	);
	const addDebt = React.useCallback(() => {
		addMutation.mutate({
			currencyCode: receipt.currencyCode,
			peerId: participant.peerId,
			amount: participant.balance,
			timestamp: receipt.issued,
			note: getReceiptDebtName(receipt.name),
			receiptId: receipt.id,
		});
	}, [
		addMutation,
		participant.balance,
		receipt.currencyCode,
		receipt.issued,
		receipt.name,
		participant.peerId,
		receipt.id,
	]);
 
	return (
		<Button
			title={t("participants.actions.sendButton")}
			isLoading={addMutation.isPending}
			isDisabled={addMutation.isPending}
			isIconOnly
			color="primary"
			onPress={addDebt}
		>
			<Icon name="send" className="size-6" />
		</Button>
	);
};
 
const ReceiptParticipantDebtActions = suspendedFallback<
	Omit<Props, "outcomingDebtId"> & {
		outcomingDebtId: DebtId;
	}
>(
	({ receipt, participant, outcomingDebtId }) => {
		const { t } = useTranslation(["receipts", "debts"]);
		const trpc = useTRPC();
		const { data: participantDebt } = useSuspenseQuery(
			trpc.debts.get.queryOptions({ id: outcomingDebtId }),
		);
		const updateMutation = useMutation(
			trpc.debts.update.mutationOptions(
				useTrpcMutationOptions(debtsUpdateOptions, {
					context: {
						currDebt: participantDebt,
					},
				}),
			),
		);
		const updateDebt = React.useCallback(
			(currentDebt: Debt) => {
				updateMutation.mutate({
					id: currentDebt.id,
					update: {
						amount: participant.balance,
						currencyCode: receipt.currencyCode,
						timestamp: receipt.issued,
						receiptId: receipt.id,
					},
				});
			},
			[
				updateMutation,
				participant.balance,
				receipt.currencyCode,
				receipt.issued,
				receipt.id,
			],
		);
 
		const expectedDebt = React.useMemo(
			() => ({
				amount: participant.balance,
				currencyCode: receipt.currencyCode,
				timestamp: receipt.issued,
				updatedAt: participantDebt.updatedAt,
			}),
			[
				participantDebt.updatedAt,
				receipt.currencyCode,
				receipt.issued,
				participant.balance,
			],
		);
 
		return (
			<View className="flex flex-row items-center gap-3 empty:hidden">
				{areDebtsSynced(expectedDebt, participantDebt) ? (
					<DebtSyncStatus
						debt={expectedDebt}
						theirDebt={participantDebt.their}
						size="lg"
					/>
				) : (
					<Button
						title={t("participants.actions.updateButton")}
						isLoading={updateMutation.isPending}
						isDisabled={updateMutation.isPending}
						isIconOnly
						color="primary"
						onPress={() => updateDebt(participantDebt)}
					>
						<Icon name="sync" className="size-6" />
					</Button>
				)}
			</View>
		);
	},
	<Icon name="sync" className="size-9" />,
);
 
export const ReceiptParticipantActions: React.FC<Props> = ({
	receipt,
	participant,
	outcomingDebtId,
}) => (
	<>
		{participant.balance === 0 ? (
			<Icon name="zero" className="size-9" />
		) : outcomingDebtId ? (
			<ReceiptParticipantDebtActions
				receipt={receipt}
				participant={participant}
				outcomingDebtId={outcomingDebtId}
			/>
		) : (
			<ReceiptParticipantNoDebtAction
				receipt={receipt}
				participant={participant}
			/>
		)}
	</>
);