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 | 1690x 2508x 2508x 2508x 145x 806x 30x 1884x 1884x 1884x 1884x 1884x 210x 1884x 1884x 1884x 1884x 9848x 1884x 1884x 9848x 1884x 32x 96x 32x 16x 31x | import type React from "react";
import { useSuspenseQuery } from "@tanstack/react-query";
import { Trans, useTranslation } from "react-i18next";
import { HighlightText } from "~app/components/highlight-text";
import { suspendedFallback } from "~app/components/suspense-wrapper";
import { useFormat } from "~app/hooks/use-format";
import { useLocale } from "~app/hooks/use-locale";
import { useTrpcMutationState } from "~app/hooks/use-trpc-mutation-state";
import type { ReceiptPageEntry } from "~app/trpc-types";
import { formatCurrency } from "~app/utils/currency";
import { useTRPC } from "~app/utils/trpc";
import { Badge } from "~components/badge";
import { Checkbox } from "~components/checkbox";
import { Icon } from "~components/icons";
import { Link } from "~components/link";
import { Skeleton } from "~components/skeleton";
import { Text } from "~components/text";
import { Tooltip } from "~components/tooltip";
import { cn } from "~components/utils";
import type { ViewReactNode } from "~components/view";
import { View } from "~components/view";
import type { ReceiptId } from "~db/ids";
import type { Interval } from "~utils/array";
import { round } from "~utils/math";
import {
ReceiptPreviewSyncIcon,
skeletonReceiptPreviewSyncIcon,
} from "./receipt-preview-sync-icon";
const ReceiptPreviewShape: React.FC<
{
title: ViewReactNode;
checkbox: ViewReactNode;
sum: ViewReactNode;
icon: ViewReactNode;
infoTooltip?: React.ReactNode;
} & React.ComponentProps<typeof View>
> = ({ title, checkbox, sum, icon, className, infoTooltip, ...props }) => {
const hasInfoTooltip = Boolean(infoTooltip);
const checkboxComponent = (
<View className="flex min-w-8 shrink-0 items-center justify-center">
{checkbox}
</View>
);
return (
<View
className={cn(
"w-full overflow-hidden first-of-type:rounded-t-2xl last-of-type:rounded-b-2xl",
className,
)}
testID="receipt-preview"
{...props}
>
<View className="flex-row justify-between gap-2">
<View className="flex-row gap-2">
<View className="items-center justify-center">
{checkboxComponent}
</View>
<View className="min-w-0 truncate overflow-hidden p-2">{title}</View>
<Tooltip
content={infoTooltip}
isDisabled={!hasInfoTooltip}
className="box-content flex p-2"
skipMobile
>
<Icon
name="info"
className={cn(
"text-primary size-6 cursor-pointer self-center opacity-75",
{ hidden: !hasInfoTooltip },
)}
/>
</Tooltip>
</View>
<View className="flex-row gap-2">
<View className="flex-row justify-end self-center p-2 text-right">
{sum}
</View>
<View className="hidden flex-row items-center justify-center p-2 sm:flex">
{icon}
</View>
</View>
</View>
</View>
);
};
export const ReceiptPreviewSkeleton = () => (
<ReceiptPreviewShape
title={
<View className="flex flex-col items-start gap-1">
<Skeleton className="h-5 w-48 rounded-sm" />
<Skeleton className="h-4 w-14 rounded-sm" />
</View>
}
checkbox={<Checkbox isDisabled color="secondary" />}
sum={<Skeleton className="h-5 w-16 rounded-sm" />}
icon={skeletonReceiptPreviewSyncIcon}
/>
);
export const ReceiptPreview = suspendedFallback<{
id: ReceiptId;
highlights: Interval[];
isSelected: boolean;
onValueChange: (nextValue: boolean) => void;
matchedItems: ReceiptPageEntry["matchedItems"];
filterQuery: string;
}>(
({
id,
highlights,
isSelected,
onValueChange,
matchedItems,
filterQuery,
}) => {
const { t } = useTranslation("receipts");
const trpc = useTRPC();
const { data: receipt } = useSuspenseQuery(
trpc.receipts.get.queryOptions({ id }),
);
const { formatPlainDate } = useFormat();
const lastMutationState = useTrpcMutationState<"receipts.remove">(
trpc.receipts.remove.mutationKey(),
(vars) => vars.id === id,
);
const isRemoving = lastMutationState?.status === "pending";
const locale = useLocale();
const isOwner = receipt.selfPeerId === receipt.ownerPeerId;
const emptyItems = receipt.items.filter(
(item) => item.consumers.length === 0,
);
const title = (
<>
<View className="flex flex-row items-center gap-2">
<Tooltip
content={t("receipt.emptyItems", { amount: emptyItems.length })}
isDisabled={emptyItems.length === 0}
skipMobile
>
<Badge
color="warning"
isInvisible={emptyItems.length === 0}
className="translate-x-full"
>
{filterQuery ? (
<HighlightText intervals={highlights}>
{receipt.name}
</HighlightText>
) : (
<Text>{receipt.name}</Text>
)}
</Badge>
</Tooltip>
{isOwner ? <Icon name="key" className="text-primary size-3" /> : null}
</View>
<Text className="text-default-400 text-xs">
{formatPlainDate(receipt.issued)}
</Text>
</>
);
const sum = round(
receipt.items.reduce((acc, item) => acc + item.price * item.quantity, 0),
);
return (
<Link to="/receipts/$id" params={{ id: receipt.id }} color="foreground">
<ReceiptPreviewShape
className={isSelected ? "bg-secondary/20" : undefined}
title={title}
checkbox={
<Checkbox
isSelected={isSelected}
onValueChange={isRemoving ? undefined : onValueChange}
isDisabled={isRemoving}
color="secondary"
/>
}
infoTooltip={
matchedItems.length === 0 ? null : (
<View>
{matchedItems.map(
({ id: itemId, highlights: itemHighlights }) => {
const matchedItem = receipt.items.find(
(item) => item.id === itemId,
);
if (!matchedItem) {
return (
<Text key={itemId}>
{t("receipt.matchedItem.notFound")}
</Text>
);
}
return (
<Text key={itemId}>
{/* This casting intentional, text can be rendered inside text */}
{/* It's a rare case so once-in-a-while casting is cheaper than expanding Text children type */}
{
(
<Trans
t={t}
i18nKey="receipt.matchedItem.found"
components={{
name: (
<HighlightText intervals={itemHighlights}>
{matchedItem.name}
</HighlightText>
),
}}
/>
) as unknown as string
}
</Text>
);
},
)}
</View>
)
}
sum={
<Text className="font-medium">
{formatCurrency(locale, receipt.currencyCode, sum)}
</Text>
}
icon={<ReceiptPreviewSyncIcon receipt={receipt} />}
/>
</Link>
);
},
<ReceiptPreviewSkeleton />,
);
|