All files / packages/app/features/receipt-components receipt-item.tsx

94.87% Statements 37/39
87.5% Branches 14/16
94.11% Functions 16/17
94.44% Lines 34/36

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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251                                                                              3765x 4144x   4144x 4144x 4144x 4144x 4144x   4144x 4144x   74x   4144x   4144x 4144x     4144x 4144x     4091x   4144x 3739x       4091x 3x 6x     4084x   4091x                                                                   4x           4x                                                                                                           17131x 53985x   17131x                   17131x                                                   176x   88x 441x 441x                                 882x                                      
import React from "react";
 
import { Trans, useTranslation } from "react-i18next";
 
import { LoadablePeer } from "~app/components/app/loadable-peer";
import { SkeletonPeer } from "~app/components/app/peer";
import { ErrorMessage } from "~app/components/error-message";
import { RemoveComponent } from "~app/components/remove-button";
import { ReceiptItemConsumerInput } from "~app/features/receipt-components/receipt-item-consumer-input";
import { useLocale } from "~app/hooks/use-locale";
import { useTrpcMutationState } from "~app/hooks/use-trpc-mutation-state";
import { formatCurrency } from "~app/utils/currency";
import { useTRPC } from "~app/utils/trpc";
import { Button } from "~components/button";
import { Card } from "~components/card";
import { Divider } from "~components/divider";
import { Dropdown } from "~components/dropdown";
import { Icon } from "~components/icons";
import { Skeleton } from "~components/skeleton";
import { Text } from "~components/text";
import { View } from "~components/view";
import type { ViewHandle } from "~components/view.base";
import { round } from "~utils/math";
 
import { useActionsHooksContext, useReceiptContext } from "./context";
import { useCanEdit, useIsOwner } from "./hooks";
import { ReceiptItemConsumers } from "./receipt-item-consumers";
import { ReceiptItemNameInput } from "./receipt-item-name-input";
import { ReceiptItemPayers } from "./receipt-item-payers";
import { ReceiptItemPriceInput } from "./receipt-item-price-input";
import { ReceiptItemQuantityInput } from "./receipt-item-quantity-input";
import type { Item } from "./state";
import { SORT_USERS } from "./utils";
 
type Props = {
	item: Item;
	ref: React.Ref<ViewHandle | null>;
};
 
export const ReceiptItem: React.FC<Props> = ({ item, ref }) => {
	const { t } = useTranslation("receipts");
	const { currencyCode, participants, confirmItemRemoval } =
		useReceiptContext();
	const { addItemConsumer, removeItem } = useActionsHooksContext();
	const canEdit = useCanEdit();
	const isOwner = useIsOwner();
	const locale = useLocale();
 
	const trpc = useTRPC();
	const removeItemMutationState = useTrpcMutationState<"receiptItems.remove">(
		trpc.receiptItems.remove.mutationKey(),
		(vars) => vars.id === item.id,
	);
	const isRemovalPending = removeItemMutationState?.status === "pending";
 
	const participantsIds = React.useMemo(
		() => participants.map(({ peerId }) => peerId),
		[participants],
	);
	const addedParticipantsIds = React.useMemo(
		() => item.consumers.map((consumer) => consumer.peerId),
		[item.consumers],
	);
	const notAddedParticipantsIds = React.useMemo(
		() =>
			participantsIds.filter(
				(participantId) => !addedParticipantsIds.includes(participantId),
			),
		[addedParticipantsIds, participantsIds],
	);
	const onAddEveryItemParticipant = React.useCallback(() => {
		for (const participantId of notAddedParticipantsIds) {
			addItemConsumer(item.id, participantId, 1);
		}
	}, [addItemConsumer, item.id, notAddedParticipantsIds]);
	const sortedConsumers = item.consumers.toSorted(SORT_USERS);
 
	return (
		<View ref={ref} testID="receipt-item">
			<Card
				headerClassName="flex flex-col items-start justify-between gap-2"
				header={
					<>
						<View className="flex w-full flex-row justify-between gap-4">
							<View className="flex flex-row items-center gap-4">
								<ReceiptItemNameInput
									item={item}
									isDisabled={isRemovalPending}
								/>
								{canEdit && notAddedParticipantsIds.length > 1 ? (
									<Button
										variant="faded"
										size="sm"
										onPress={onAddEveryItemParticipant}
									>
										{t("item.participants.everyone")}
									</Button>
								) : null}
							</View>
							{canEdit ? (
								<Dropdown
									items={[
										{
											key: "remove",
											children: (
												<RemoveComponent
													onRemove={() => removeItem(item.id)}
													mutation={{ isPending: isRemovalPending }}
													subtitle={t("item.removeButton.confirmSubtitle")}
												>
													{({ openModal }) => (
														<View
															className="text-danger flex-row items-center gap-1"
															onPress={
																confirmItemRemoval &&
																item.consumers.length !== 0
																	? openModal
																	: () => removeItem(item.id)
															}
														>
															<Icon name="trash" className="m-1 size-4" />
															<Text>{t("item.removeButton.label")}</Text>
														</View>
													)}
												</RemoveComponent>
											),
										},
									]}
								>
									<Button variant="light" isIconOnly>
										<Icon name="ellipsis" className="size-4" />
									</Button>
								</Dropdown>
							) : null}
						</View>
						<View className="flex-row flex-wrap items-center gap-2">
							<ReceiptItemPriceInput
								item={item}
								isDisabled={isRemovalPending}
								className="w-full shrink-0 sm:w-36"
							/>
							<Text>{t("item.crossMark")}</Text>
							<ReceiptItemQuantityInput
								item={item}
								isDisabled={isRemovalPending}
								className="w-full shrink-0 sm:w-36"
							/>
							<Text>
								{t("item.amount", {
									amount: formatCurrency(
										locale,
										currencyCode,
										round(item.quantity * item.price),
									),
								})}
							</Text>
						</View>
					</>
				}
				bodyClassName="gap-2"
			>
				{canEdit ? (
					<View className="flex w-full flex-1 flex-col items-center justify-stretch self-end sm:flex-row sm:justify-between sm:gap-4">
						<ReceiptItemPayers item={item} className="sm:max-w-[40%]" />
						<Icon name="arrow-right" className="size-9 rotate-90 sm:rotate-0" />
						<ReceiptItemConsumers item={item} className="sm:max-w-[40%]" />
					</View>
				) : null}
				{sortedConsumers.length <= 1 ? null : (
					<>
						{sortedConsumers.map((consumer) => {
							const matchedParticipant = participants.find(
								(participant) => participant.peerId === consumer.peerId,
							);
							Iif (!matchedParticipant) {
								return (
									<ErrorMessage
										key={consumer.peerId}
										message={t("item.participants.orphanedError", {
											peerId: consumer.peerId,
										})}
									/>
								);
							}
							return (
								<View
									key={consumer.peerId}
									className="items-start justify-between gap-2 sm:gap-4 min-[500px]:flex-row"
								>
									<LoadablePeer
										id={matchedParticipant.peerId}
										foreign={!isOwner}
									/>
									<View className="flex-row gap-2 self-end">
										<ReceiptItemConsumerInput
											consumer={consumer}
											item={item}
											isDisabled={isRemovalPending}
										/>
									</View>
								</View>
							);
						})}
					</>
				)}
			</Card>
		</View>
	);
};
 
const consumersSkeletonItems = Array.from({ length: 2 }, (_, index) => index);
 
export const ReceiptItemSkeleton: React.FC = () => {
	const { t } = useTranslation("receipts");
	return (
		<Card
			header={<Skeleton className="h-7 w-24 rounded-md" />}
			headerClassName="justify-between gap-4"
			bodyClassName="gap-2"
		>
			<View className="flex-row items-center gap-2">
				<Trans
					t={t}
					i18nKey="item.skeletonAmount"
					components={{
						skeleton: <Skeleton className="h-7 w-12 rounded-md" />,
					}}
				/>
			</View>
			<Divider />
			{consumersSkeletonItems.map((index) => (
				<View
					key={index}
					className="items-start justify-between gap-2 sm:gap-4 min-[500px]:flex-row"
				>
					<SkeletonPeer />
					<View className="flex-row gap-2 self-end">
						<Trans
							t={t}
							i18nKey="item.consumer.skeletonAmount"
							components={{
								skeleton: <Skeleton className="h-7 w-10 rounded-md" />,
							}}
						/>
					</View>
				</View>
			))}
		</Card>
	);
};