|
1 | | -import { type User } from '@prisma/client'; |
| 1 | +import { SplitType, type User } from '@prisma/client'; |
2 | 2 | import { format, isToday } from 'date-fns'; |
3 | 3 | import { type TFunction } from 'next-i18next'; |
| 4 | +import { type AddExpenseState, type Participant } from '~/store/addStore'; |
| 5 | + |
| 6 | +export type ParametersExceptTranslation<F> = F extends (t: TFunction, ...rest: infer R) => any |
| 7 | + ? R |
| 8 | + : never; |
4 | 9 |
|
5 | 10 | export const displayName = ( |
| 11 | + t?: TFunction, |
6 | 12 | user?: Pick<User, 'name' | 'email' | 'id'> | null, |
7 | 13 | currentUserId?: number, |
8 | | - t?: TFunction, |
9 | 14 | ): string => { |
10 | 15 | if (currentUserId === user?.id) { |
11 | 16 | return t ? t('ui.actors.you', { ns: 'common' }) : 'You'; |
12 | 17 | } |
13 | 18 | return user?.name ?? user?.email ?? ''; |
14 | 19 | }; |
15 | 20 |
|
16 | | -export const toUIDate = (date: Date, { useToday = false, year = false } = {}): string => |
17 | | - useToday && isToday(date) ? 'Today' : format(date, year ? 'dd MMM yyyy' : 'MMM dd'); |
| 21 | +export const toUIDate = ( |
| 22 | + t: TFunction, |
| 23 | + date: Date, |
| 24 | + { useToday = false, year = false } = {}, |
| 25 | +): string => |
| 26 | + useToday && isToday(date) ? t('ui.today') : format(date, year ? 'dd MMM yyyy' : 'MMM dd'); |
| 27 | + |
| 28 | +export function generateSplitDescription( |
| 29 | + t: TFunction, |
| 30 | + splitType: SplitType, |
| 31 | + participants: Participant[], |
| 32 | + splitShares: AddExpenseState['splitShares'], |
| 33 | + paidBy: Participant | undefined, |
| 34 | + currentUser: Participant | undefined, |
| 35 | +): string { |
| 36 | + // Only enhance the description for EQUAL split type |
| 37 | + if (splitType !== SplitType.EQUAL) { |
| 38 | + return t('ui.add_expense_details.split_type_section.split_unequally'); |
| 39 | + } |
| 40 | + |
| 41 | + if (!paidBy || !currentUser) { |
| 42 | + return t('ui.add_expense_details.split_type_section.split_equally'); |
| 43 | + } |
| 44 | + |
| 45 | + // Get participants who are actually selected for the split (have non-zero shares) |
| 46 | + // If split shares are not initialized yet (undefined), include all participants |
| 47 | + const selectedParticipants = participants.filter((p) => { |
| 48 | + const share = splitShares[p.id]?.[SplitType.EQUAL]; |
| 49 | + return share === undefined || share !== 0n; |
| 50 | + }); |
| 51 | + |
| 52 | + // If no one is selected, fall back to default |
| 53 | + if (selectedParticipants.length === 0) { |
| 54 | + return t('ui.add_expense_details.split_type_section.split_equally'); |
| 55 | + } |
| 56 | + |
| 57 | + // Case 1: Paying for exactly one person |
| 58 | + if (selectedParticipants.length === 1) { |
| 59 | + const beneficiary = selectedParticipants[0]; |
| 60 | + const beneficiaryName = beneficiary?.name?.split(' ')[0] || beneficiary?.email || 'someone'; |
| 61 | + return `${t('common:ui.expense.user.paid')} ${t('common:ui.expense.for')} ${beneficiaryName}`; |
| 62 | + } |
| 63 | + |
| 64 | + // Case 2: Splitting with multiple people |
| 65 | + if (selectedParticipants.length > 1) { |
| 66 | + return `t('ui.add_expense_details.split_type_section.split_equally') (${selectedParticipants.length})`; |
| 67 | + } |
| 68 | + |
| 69 | + // Fallback to default for all other cases |
| 70 | + return t('ui.add_expense_details.split_type_section.split_equally'); |
| 71 | +} |
0 commit comments