All files / packages/app/features/peer-debts peer-debts-screen.tsx

95% Statements 19/20
100% Branches 4/4
80% Functions 4/5
94.73% Lines 18/19

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                                                                30x 524x 524x 524x   524x 524x     524x                                                                                                         30x       511x 511x     1736x 511x                                                 30x 649x     649x 649x 649x 649x                        
import React from "react";
 
import { useSuspenseQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";
 
import { DebtsGroupSkeleton } from "~app/components/app/debts-group";
import { LoadablePeer } from "~app/components/app/loadable-peer";
import { PageHeader } from "~app/components/page-header";
import { PeerDebtsGroup } from "~app/components/peer-debts-group";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { NavigationContext } from "~app/contexts/navigation-context";
import { Peer } from "~app/features/peer/peer";
import { ShowResolvedDebtsOption } from "~app/features/settings/show-resolved-debts-option";
import { useBooleanState } from "~app/hooks/use-boolean-state";
import { useDefaultLimit } from "~app/hooks/use-default-limit";
import { getPathHooks } from "~app/utils/navigation";
import { useTRPC } from "~app/utils/trpc";
import { BackLink } from "~components/back-link";
import { Button } from "~components/button";
import { Icon } from "~components/icons";
import { ButtonLink } from "~components/link";
import { Modal } from "~components/modal";
import { Text } from "~components/text";
import { View } from "~components/view";
import type { PeerId } from "~db/ids";
 
import { PeerDebtsList } from "./peer-debts-list";
 
type HeaderProps = {
	peerId: PeerId;
};
 
const Header: React.FC<HeaderProps> = ({ peerId }) => {
	const { t } = useTranslation("debts");
	const { useNavigate } = React.use(NavigationContext);
	const navigate = useNavigate();
	const [editModalOpen, { setTrue: openEditModal, setFalse: closeEditModal }] =
		useBooleanState();
	const onPeerRemove = React.useCallback(() => {
		navigate({ to: "/debts", replace: true });
	}, [navigate]);
	return (
		<>
			<PageHeader
				startContent={<BackLink to="/debts" />}
				aside={
					<View className="flex flex-row gap-2">
						<Button
							isIconOnly
							variant="bordered"
							color="secondary"
							onPress={openEditModal}
							aria-label="Edit peer"
						>
							<Icon name="pencil" className="size-6" />
						</Button>
						<ButtonLink
							to="/debts/transfer"
							search={{ from: peerId }}
							color="primary"
							title={t("peer.buttons.transfer")}
							variant="bordered"
							isIconOnly
						>
							<Icon name="transfer" className="size-6" />
						</ButtonLink>
						<ButtonLink
							color="primary"
							to="/debts/add"
							search={{ peerId }}
							title={t("peer.buttons.add")}
							variant="bordered"
							isIconOnly
						>
							<Icon name="add" className="size-6" />
						</ButtonLink>
					</View>
				}
				endContent={<LoadablePeer id={peerId} />}
			/>
 
			<Modal
				isOpen={editModalOpen}
				onOpenChange={closeEditModal}
				header={<Text className="text-xl">{t("peer.modal.editTitle")}</Text>}
				bodyClassName="flex flex-col gap-4 py-6"
				label={t("peer.modal.editTitle")}
			>
				<Peer id={peerId} onRemove={onPeerRemove} />
			</Modal>
		</>
	);
};
 
const PeerDebtsGroupWithButtons = suspendedFallback<{
	peerId: PeerId;
}>(
	({ peerId }) => {
		const trpc = useTRPC();
		const { data: debts } = useSuspenseQuery(
			trpc.debts.getAllPeer.queryOptions({ peerId }),
		);
		const nonResolvedDebts = debts.items.filter((element) => element.sum !== 0);
		return (
			<View className="flex-row items-center justify-center gap-4 px-16">
				<PeerDebtsGroup peerId={peerId} />
				{nonResolvedDebts.length === 0 ? null : (
					<ButtonLink
						color="primary"
						to="/debts/peer/$id/exchange"
						params={{ id: peerId }}
						variant="bordered"
						isIconOnly
					>
						<Icon name="exchange" />
					</ButtonLink>
				)}
				{nonResolvedDebts.length === debts.items.length ? null : (
					<ShowResolvedDebtsOption className="absolute right-0" />
				)}
			</View>
		);
	},
	<View className="flex-row items-center justify-center">
		<DebtsGroupSkeleton amount={3} />
	</View>,
);
 
export const PeerDebtsScreen = () => {
	const { useParams, useQueryState, useDefaultedQueryState } = getPathHooks(
		"/_protected/debts/peer/$id/",
	);
	const { id: peerId } = useParams();
	const limitState = useDefaultedQueryState("limit", useDefaultLimit());
	const offsetState = useQueryState("offset");
	return (
		<>
			<Header peerId={peerId} />
			<PeerDebtsGroupWithButtons peerId={peerId} />
			<PeerDebtsList
				peerId={peerId}
				limitState={limitState}
				offsetState={offsetState}
			/>
		</>
	);
};