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

100% Statements 43/43
100% Branches 22/22
100% Functions 20/20
100% Lines 37/37

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                                              390x     560x 560x 560x 560x 105x   347x 1313x     330x     1313x 443x                             332x                                           952x                                               264x   88x 147x 147x                                                 441x                               197x 603x   603x 603x 603x 603x 390x   589x 589x   589x   13x 13x   13x         589x 589x     589x                                         1324x           2013x                           1679x                                
import React from "react";
 
import { Trans, useTranslation } from "react-i18next";
 
import { LoadablePeerAvatar } from "~app/components/app/loadable-peer-avatar";
import { PeersSuggest } from "~app/components/app/peers-suggest";
import { EmptyCard } from "~app/components/empty-card";
import { useBooleanState } from "~app/hooks/use-boolean-state";
import type { ReceiptDebts } from "~app/trpc-types";
import { AvatarGroup } from "~components/avatar";
import { Button } from "~components/button";
import { Divider } from "~components/divider";
import { Icon } from "~components/icons";
import { Modal } from "~components/modal";
import { SkeletonAvatar } from "~components/skeleton-avatar";
import { Text } from "~components/text";
import { View } from "~components/view";
import type { PeerId } from "~db/ids";
 
import { useActionsHooksContext, useReceiptContext } from "./context";
import { useIsOwner } from "./hooks";
import { ReceiptParticipant } from "./receipt-participant";
 
const ReceiptParticipantsPreview: React.FC<{ switchModal: () => void }> = ({
	switchModal,
}) => {
	const { t } = useTranslation("receipts");
	const { participants, ownerPeerId, payers } = useReceiptContext();
	const isOwner = useIsOwner();
	if (participants.length === 0) {
		return <Button onPress={switchModal}>{t("participants.addButton")}</Button>;
	}
	const payerParticipants = participants.filter(({ peerId }) =>
		payers.some((payer) => payer.peerId === peerId),
	);
	const surePayerParticipants =
		payerParticipants.length === 0
			? [{ peerId: ownerPeerId, part: 1 }]
			: payerParticipants;
	const debtParticipants = participants.filter(({ debt }) => debt.total !== 0);
	return (
		<View
			className="flex flex-col gap-2 xs:flex-row"
			onPress={switchModal}
			testID="participants-preview"
		>
			<View className="flex flex-row gap-2">
				<Trans
					t={t}
					i18nKey="participants.payedBy"
					components={{
						text: <Text variant="h4" />,
						by: (
							<AvatarGroup>
								{surePayerParticipants.map((participant) => (
									<LoadablePeerAvatar
										key={participant.peerId}
										id={participant.peerId}
										foreign={!isOwner && payerParticipants.length !== 0}
										dimmed={payerParticipants.length === 0}
									/>
								))}
							</AvatarGroup>
						),
					}}
				/>
			</View>
			{debtParticipants.length === 0 ? null : (
				<View className="flex flex-row gap-2">
					<Trans
						t={t}
						i18nKey="participants.payedFor"
						components={{
							text: <Text variant="h4" />,
							for: (
								<AvatarGroup>
									{debtParticipants.map((participant) => (
										<LoadablePeerAvatar
											key={participant.peerId}
											id={participant.peerId}
											foreign={!isOwner}
										/>
									))}
								</AvatarGroup>
							),
						}}
					/>
				</View>
			)}
			<Button
				variant="bordered"
				color="primary"
				onPress={switchModal}
				isIconOnly
			>
				<Icon name="pencil" className="size-6" />
			</Button>
		</View>
	);
};
 
const skeletonParticipants = Array.from({ length: 3 }, (_, index) => index);
 
export const ReceiptParticipantsPreviewSkeleton: React.FC = () => {
	const { t } = useTranslation("receipts");
	return (
		<View className="flex cursor-pointer flex-col gap-2 xs:flex-row">
			<View className="flex flex-row gap-2">
				<Trans
					t={t}
					i18nKey="participants.payedBy"
					components={{
						text: <Text variant="h4" />,
						by: (
							<AvatarGroup>
								<SkeletonAvatar dimmed />
							</AvatarGroup>
						),
					}}
				/>
			</View>
			<View className="flex flex-row gap-2">
				<Trans
					t={t}
					i18nKey="participants.payedFor"
					components={{
						text: <Text variant="h4" />,
						for: (
							<AvatarGroup>
								{skeletonParticipants.map((index) => (
									<SkeletonAvatar key={index} />
								))}
							</AvatarGroup>
						),
					}}
				/>
			</View>
			<Button variant="bordered" color="primary" isIconOnly isDisabled>
				<Icon name="pencil" className="size-6" />
			</Button>
		</View>
	);
};
 
export const ReceiptParticipants: React.FC<{
	debts?: ReceiptDebts;
}> = ({ debts }) => {
	const { t } = useTranslation("receipts");
	const { receiptDisabled, participants, selfPeerId, getPeersSuggestOptions } =
		useReceiptContext();
	const isOwner = useIsOwner();
	const [isModalOpen, { switchValue: switchModalOpen }] = useBooleanState();
	const isSelfAdded = participants.some(
		(participant) => participant.peerId === selfPeerId,
	);
	const [localFilterIds, setLocalFilterIds] = React.useState<PeerId[]>([]);
	const { addParticipant } = useActionsHooksContext();
 
	const onPeerClick = React.useCallback(
		(peerId: PeerId) => {
			setLocalFilterIds((prevIds) => [...prevIds, peerId]);
			addParticipant(peerId, "editor", {
				onSettled: () =>
					setLocalFilterIds((prevIds) => prevIds.filter((id) => id !== peerId)),
			});
		},
		[addParticipant],
	);
	const suggestOptions = React.useMemo(
		() => getPeersSuggestOptions(),
		[getPeersSuggestOptions],
	);
	return (
		<>
			<ReceiptParticipantsPreview switchModal={switchModalOpen} />
			<Modal
				label={t("participants.picker.label")}
				isOpen={isModalOpen}
				onOpenChange={switchModalOpen}
				className="mb-24 max-w-xl sm:mb-32"
				testID="participants-picker"
				header={
					<View className="flex-row gap-2">
						<Icon name="user" className="size-6" />
						<Text className="text-xl">{t("participants.picker.title")}</Text>
					</View>
				}
				bodyClassName="flex flex-col gap-4 py-6"
			>
				{participants.length === 0 ? null : (
					<>
						<View className="flex flex-col gap-4 sm:gap-2">
							{participants.map((participant) => (
								<ReceiptParticipant
									key={participant.peerId}
									participant={participant}
									debtId={
										debts?.direction === "outcoming"
											? debts.debts.find(
													({ peerId }) => participant.peerId === peerId,
												)?.id
											: debts?.hasMine && participant.peerId === selfPeerId
												? debts.id
												: undefined
									}
								/>
							))}
						</View>
						<Divider />
					</>
				)}
				{isOwner ? (
					<PeersSuggest
						filterIds={participants.map((participant) => participant.peerId)}
						selected={localFilterIds}
						multiselect
						additionalIds={isSelfAdded ? [] : [selfPeerId]}
						onPeerClick={onPeerClick}
						isDisabled={receiptDisabled}
						options={suggestOptions}
						label={t("participants.picker.addLabel")}
					/>
				) : participants.length === 0 ? (
					<EmptyCard title={t("participants.empty")} />
				) : null}
			</Modal>
		</>
	);
};