All files / packages/app/features/receipt receipt-screen.tsx

100% Statements 18/18
83.33% Branches 15/18
100% Functions 5/5
100% Lines 18/18

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                                                                                  301x 589x                       78x   529x 529x 529x 529x       529x   529x 529x 529x 529x 529x       311x             557x               529x                                                                                                                           147x 147x                                    
import React from "react";
 
import { useSuspenseQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import { LoadablePeerAvatar } from "~app/components/app/loadable-peer-avatar";
import { PageHeader } from "~app/components/page-header";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import {
	ActionsHooksContext,
	ReceiptContext,
} from "~app/features/receipt-components/context";
import {
	ReceiptItems,
	ReceiptItemsSkeleton,
} from "~app/features/receipt-components/receipt-items";
import {
	ReceiptParticipants,
	ReceiptParticipantsPreviewSkeleton,
} from "~app/features/receipt-components/receipt-participants";
import { useBooleanState } from "~app/hooks/use-boolean-state";
import { getPathHooks } from "~app/utils/navigation";
import { useTRPC } from "~app/utils/trpc";
import { BackLink } from "~components/back-link";
import { Icon } from "~components/icons";
import { Skeleton } from "~components/skeleton";
import { SkeletonAvatar } from "~components/skeleton-avatar";
import { SkeletonDateInput } from "~components/skeleton-date-input";
import { View } from "~components/view";
 
import { useActionHooks, useGetReceiptContext } from "./hooks";
import { ReceiptAmountInput } from "./receipt-amount-input";
import { ReceiptDateInput } from "./receipt-date-input";
import { ReceiptGuestControlButton } from "./receipt-guest-control-button";
import { ReceiptNameInput } from "./receipt-name-input";
import { ReceiptParticipantActions } from "./receipt-participant-actions";
import { ReceiptRemoveButton } from "./receipt-remove-button";
import { ReceiptSyncButton } from "./receipt-sync-button";
 
type HeaderProps = Omit<React.ComponentProps<typeof PageHeader>, "backHref">;
 
const Header: React.FC<HeaderProps> = ({ startContent, ...props }) => (
	<PageHeader
		startContent={
			<>
				<BackLink to="/receipts" />
				<Icon name="receipt" className="size-9" />
				{startContent}
			</>
		}
		{...props}
	/>
);
 
export const ReceiptScreen = suspendedFallback(
	() => {
		const { useParams } = getPathHooks("/_protected/receipts/$id");
		const { id } = useParams();
		const trpc = useTRPC();
		const { data: receipt } = useSuspenseQuery(
			trpc.receipts.get.queryOptions({ id }),
		);
 
		const [deleteLoading, setDeleteLoading] = React.useState(false);
		const [isEditing, { setFalse: unsetEditing, setTrue: setEditing }] =
			useBooleanState();
		const isOwner = receipt.selfPeerId === receipt.ownerPeerId;
		const disabled = !isOwner || deleteLoading;
		const actionsHooks = useActionHooks(receipt);
		const getReceiptContext = useGetReceiptContext(
			receipt,
			disabled,
			(participant) =>
				isOwner && receipt.selfPeerId !== participant.peerId ? (
					<ReceiptParticipantActions
						participant={participant}
						receipt={receipt}
						outcomingDebtId={
							receipt.debts.direction === "outcoming"
								? receipt.debts.debts.find(
										({ peerId }) => peerId === participant.peerId,
									)?.id
								: undefined
						}
					/>
				) : null,
		);
 
		return (
			<>
				<Header
					aside={
						isOwner ? (
							<ReceiptRemoveButton
								receipt={receipt}
								setLoading={setDeleteLoading}
							/>
						) : null
					}
					endContent={
						disabled ? null : isEditing ? (
							<ReceiptNameInput
								receipt={receipt}
								isLoading={deleteLoading}
								unsetEditing={unsetEditing}
							/>
						) : (
							<Icon name="pencil" className="size-6" onClick={setEditing} />
						)
					}
				>
					{isEditing ? undefined : receipt.name}
				</Header>
 
				<ReceiptContext value={getReceiptContext}>
					<ActionsHooksContext value={actionsHooks}>
						<View className="items-start gap-2">
							<View className="flex w-full flex-row items-start justify-between gap-2">
								<ReceiptDateInput receipt={receipt} isLoading={deleteLoading} />
								<View className="flex flex-row gap-2">
									<LoadablePeerAvatar id={receipt.ownerPeerId} />
									{isOwner ? (
										<ReceiptSyncButton
											key={
												// This is required for stability of the hooks in the component
												receipt.participants.length
											}
											receipt={receipt}
											isLoading={deleteLoading}
										/>
									) : (
										<ReceiptGuestControlButton receipt={receipt} />
									)}
								</View>
							</View>
							<View className="flex flex-col justify-center gap-2 sm:flex-row">
								<ReceiptAmountInput
									receipt={receipt}
									isLoading={deleteLoading}
								/>
								<ReceiptParticipants debts={receipt.debts} />
							</View>
						</View>
						<ReceiptItems />
					</ActionsHooksContext>
				</ReceiptContext>
			</>
		);
	},
	() => {
		const { t } = useTranslation("receipts");
		return (
			<>
				<Header>{t("receipt.loading")}</Header>
				<View className="items-start gap-2">
					<View className="flex w-full flex-row items-start justify-between gap-2">
						<SkeletonDateInput />
						<SkeletonAvatar />
					</View>
					<View className="flex flex-col justify-center gap-2 sm:flex-row">
						<Skeleton className="h-7 w-12 self-center rounded-md" />
						<ReceiptParticipantsPreviewSkeleton />
					</View>
				</View>
				<ReceiptItemsSkeleton amount={3} />
			</>
		);
	},
);