11import { type Balance , SplitType , type User } from '@prisma/client' ;
22import { ArrowRightIcon , HandCoins } from 'lucide-react' ;
3- import { type User as NextUser } from 'next-auth' ;
43import React , { useState } from 'react' ;
54import { toast } from 'sonner' ;
6- import { useTranslation } from 'next-i18next' ;
75
86import { DEFAULT_CATEGORY } from '~/lib/category' ;
97import { api } from '~/utils/api' ;
@@ -14,13 +12,21 @@ import { Button } from '../ui/button';
1412import { AppDrawer , DrawerClose } from '../ui/drawer' ;
1513import { Input } from '../ui/input' ;
1614import { EntityAvatar } from '../ui/avatar' ;
15+ import { useTranslationWithUtils } from '~/hooks/useTranslationWithUtils' ;
16+ import { useSession } from 'next-auth/react' ;
1717
1818export const SettleUp : React . FC < {
1919 balances ?: Balance [ ] ;
2020 friend : User ;
21- currentUser : NextUser ;
22- } > = ( { balances, friend, currentUser } ) => {
23- const { t } = useTranslation ( 'friend_details' ) ;
21+ } > = ( { balances, friend } ) => {
22+ const { t, displayName } = useTranslationWithUtils ( ) ;
23+ const { data } = useSession ( ) ;
24+ const currentUser = data ?. user ;
25+
26+ if ( ! currentUser ) {
27+ return null ;
28+ }
29+
2430 if ( ! balances ) {
2531 return (
2632 < Button size = "sm" variant = "outline" responsiveIcon disabled >
@@ -47,13 +53,13 @@ export const SettleUp: React.FC<{
4753 const utils = api . useUtils ( ) ;
4854
4955 function saveExpense ( ) {
50- if ( ! balanceToSettle || ! amount || ! parseFloat ( amount ) ) {
56+ if ( ! balanceToSettle || ! amount || ! parseFloat ( amount ) || ! currentUser ) {
5157 return ;
5258 }
5359
5460 addExpenseMutation . mutate (
5561 {
56- name : t ( 'ui.settle_up_name' , { ns : 'common' } ) ,
62+ name : t ( 'ui.settle_up_name' ) ,
5763 currency : balanceToSettle . currency ,
5864 amount : toSafeBigInt ( amount ) ,
5965 splitType : SplitType . SETTLEMENT ,
@@ -77,7 +83,7 @@ export const SettleUp: React.FC<{
7783 } ,
7884 onError : ( error ) => {
7985 console . error ( 'Error while saving expense:' , error ) ;
80- toast . error ( t ( 'ui.errors.saving_expense' , { ns : 'common' } ) ) ;
86+ toast . error ( t ( 'ui.errors.saving_expense' ) ) ;
8187 } ,
8288 } ,
8389 ) ;
@@ -91,7 +97,7 @@ export const SettleUp: React.FC<{
9197 className = "flex w-[150px] items-center gap-2 rounded-md border bg-cyan-500 px-3 text-sm font-normal text-black focus:bg-cyan-600 focus:ring-0 focus-visible:outline-hidden lg:w-[180px]"
9298 disabled = { ! balances . length }
9399 >
94- < HandCoins className = "size-4" /> { t ( 'ui.actions.settle_up' , { ns : 'common' } ) }
100+ < HandCoins className = "size-4" /> { t ( 'ui.actions.settle_up' ) }
95101 </ Button >
96102 }
97103 disableTrigger = { ! balances ?. length }
@@ -114,7 +120,7 @@ export const SettleUp: React.FC<{
114120 className = "text-cyan-500 lg:hidden"
115121 onClick = { ( ) => setBalanceToSettle ( undefined ) }
116122 >
117- { t ( 'ui.actions.back' , { ns : 'common' } ) }
123+ { t ( 'ui.actions.back' ) }
118124 </ Button >
119125 ) : (
120126 < DrawerClose >
@@ -124,15 +130,13 @@ export const SettleUp: React.FC<{
124130 className = "text-cyan-500 lg:hidden"
125131 onClick = { ( ) => ( 1 < balances . length ? setBalanceToSettle ( undefined ) : null ) }
126132 >
127- { t ( 'ui.actions.back' , { ns : 'common' } ) }
133+ { t ( 'ui.actions.back' ) }
128134 </ Button >
129135 </ DrawerClose >
130136 ) ) }
131137 </ div >
132138 < div className = "mt-4 mb-2 text-center" >
133- { balanceToSettle
134- ? t ( 'ui.settle_up' , { ns : 'common' } )
135- : t ( 'ui.select_currency' , { ns : 'common' } ) }
139+ { balanceToSettle ? t ( 'ui.settle_up_name' ) : t ( 'ui.select_currency' ) }
136140 </ div >
137141 { balanceToSettle && (
138142 < DrawerClose >
@@ -142,7 +146,7 @@ export const SettleUp: React.FC<{
142146 className = "mx-auto text-cyan-500 lg:hidden"
143147 onClick = { ( ) => saveExpense ( ) }
144148 >
145- { t ( 'ui.actions.save' , { ns : 'common' } ) }
149+ { t ( 'ui.actions.save' ) }
146150 </ Button >
147151 </ DrawerClose >
148152 ) }
@@ -169,8 +173,8 @@ export const SettleUp: React.FC<{
169173 </ div >
170174 < p className = "mt-2 text-center text-sm text-gray-400" >
171175 { isCurrentUserPaying
172- ? `${ t ( 'ui.settle_up_details.youre_paying ' ) } ${ friend . name ?? friend . email } `
173- : `${ friend . name ?? friend . email } ${ t ( 'ui.settle_up_details.is_paying_you ' ) } ` }
176+ ? `${ t ( 'ui.actors.you ' ) } ${ t ( 'ui.expense.you.pay' ) } ${ displayName ( friend ) } `
177+ : `${ displayName ( friend ) } ${ t ( 'ui.expense.user.pay' ) } ${ t ( 'ui.actors.you ' ) } ` }
174178 </ p >
175179 </ div >
176180 < div className = "mt-3 flex items-center gap-2" >
@@ -190,7 +194,7 @@ export const SettleUp: React.FC<{
190194 { balanceToSettle &&
191195 ( 1 < balances . length ? (
192196 < Button size = "sm" variant = "secondary" onClick = { ( ) => setBalanceToSettle ( undefined ) } >
193- { t ( 'ui.actions.back' , { ns : 'common' } ) }
197+ { t ( 'ui.actions.back' ) }
194198 </ Button >
195199 ) : (
196200 < DrawerClose >
@@ -199,15 +203,15 @@ export const SettleUp: React.FC<{
199203 variant = "secondary"
200204 onClick = { ( ) => ( 1 < balances . length ? setBalanceToSettle ( undefined ) : null ) }
201205 >
202- { t ( 'ui.actions.back' , { ns : 'common' } ) }
206+ { t ( 'ui.actions.back' ) }
203207 </ Button >
204208 </ DrawerClose >
205209 ) ) }
206210 </ div >
207211 { balanceToSettle && (
208212 < DrawerClose >
209213 < Button size = "sm" className = "mx-auto" onClick = { ( ) => saveExpense ( ) } >
210- { t ( 'ui.actions.save' , { ns : 'common' } ) }
214+ { t ( 'ui.actions.save' ) }
211215 </ Button >
212216 </ DrawerClose >
213217 ) }
0 commit comments