All files / packages/app/features/debt debt-control-buttons.tsx

91.66% Statements 11/12
90% Branches 9/10
100% Functions 4/4
91.66% Lines 11/12

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                                    505x 552x 552x 552x   552x                                                       552x             552x 2x     2x     552x                       21x                              
import React from "react";
 
import { skipToken, useMutation } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import { ConfirmModal } from "~app/components/confirm-modal";
import { DebtIntention } from "~app/features/debts-intentions/debt-intention";
import { useTrpcMutationOptions } from "~app/hooks/use-trpc-mutation-options";
import type { Debt } from "~app/trpc-types";
import { useTRPC } from "~app/utils/trpc";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import { options as acceptDebtIntentionOptions } from "~mutations/debt-intentions/accept";
 
type Props = {
	debt: Debt;
};
 
export const DebtControlButtons: React.FC<Props> = ({ debt }) => {
	const { t } = useTranslation("debts");
	const trpc = useTRPC();
	const intention = React.useMemo(
		() =>
			debt.their
				? {
						id: debt.id,
						peerId: debt.peerId,
						amount: debt.their.amount,
						currencyCode: debt.their.currencyCode,
						updatedAt: debt.their.updatedAt,
						timestamp: debt.their.timestamp,
						note: debt.note,
						receiptId: debt.receiptId,
						current: {
							amount: debt.amount,
							currencyCode: debt.currencyCode,
							timestamp: debt.timestamp,
						},
					}
				: undefined,
		[
			debt.amount,
			debt.currencyCode,
			debt.id,
			debt.note,
			debt.receiptId,
			debt.their,
			debt.timestamp,
			debt.peerId,
		],
	);
	const acceptMutation = useMutation(
		trpc.debtIntentions.accept.mutationOptions(
			useTrpcMutationOptions(acceptDebtIntentionOptions, {
				context: intention ? { intention } : skipToken,
			}),
		),
	);
	const acceptSyncIntention = React.useCallback(() => {
		Iif (!intention) {
			return;
		}
		acceptMutation.mutate({ id: intention.id });
	}, [acceptMutation, intention]);
 
	return (
		<>
			{intention &&
			Temporal.ZonedDateTime.compare(debt.updatedAt, intention.updatedAt) <
				0 ? (
				<ConfirmModal
					onConfirm={acceptSyncIntention}
					title={t("debt.acceptIntention.title")}
					subtitle={<DebtIntention intention={intention} />}
					confirmText={t("debt.acceptIntention.confirmText")}
				>
					{({ openModal }) => (
						<Button
							onPress={openModal}
							variant="ghost"
							color="warning"
							isIconOnly
							testID="sync-button"
						>
							<Icon name="sync" className="size-6" />
						</Button>
					)}
				</ConfirmModal>
			) : null}
		</>
	);
};