@@ -298,17 +298,42 @@ function hasEligibleActiveAdminFromWorkspaces(policies: OnyxCollection<Policy> |
298298 return false ;
299299}
300300
301+ function cloneCustomUnitWithNewIDs ( unit : CustomUnit , newCustomUnitID : string , newDefaultRateID ?: string ) : CustomUnit {
302+ if ( newDefaultRateID ) {
303+ // The server-side DUPLICATE_POLICY assigns newDefaultRateID to the source's default rate.
304+ // Mirror getDefaultMileageRate's selection (enabled rates, sorted by index with
305+ // CONST.DEFAULT_NUMBER_ID for missing indexes) so the optimistic clone aligns with the
306+ // rate the expense flow will later treat as default. Other source rates get fresh server
307+ // IDs, so we drop them from the optimistic state to avoid stale duplicates.
308+ const defaultRate = Object . values ( unit . rates )
309+ . filter ( ( rate ) => rate . enabled !== false )
310+ . sort ( ( a , b ) => ( a . index ?? CONST . DEFAULT_NUMBER_ID ) - ( b . index ?? CONST . DEFAULT_NUMBER_ID ) )
311+ . at ( 0 ) ;
312+ return {
313+ ...unit ,
314+ customUnitID : newCustomUnitID ,
315+ rates : defaultRate ? { [ newDefaultRateID ] : { ...defaultRate , customUnitRateID : newDefaultRateID } } : { } ,
316+ } ;
317+ }
318+
319+ return {
320+ ...unit ,
321+ customUnitID : newCustomUnitID ,
322+ } ;
323+ }
324+
301325function getCustomUnitsForDuplication (
302326 policy : Policy ,
303327 isDistanceRatesOptionSelected : boolean ,
304328 isPerDiemOptionSelected : boolean ,
305329 customUnitIDs : {
306330 distanceCustomUnitID : string ;
307331 perDiemCustomUnitID : string ;
332+ customUnitRateID ?: string ;
308333 } ,
309334) : Record < string , CustomUnit > | undefined {
310335 const customUnits = policy ?. customUnits ;
311- const { distanceCustomUnitID, perDiemCustomUnitID} = customUnitIDs ?? { } ;
336+ const { distanceCustomUnitID, perDiemCustomUnitID, customUnitRateID } = customUnitIDs ?? { } ;
312337
313338 if ( ( ! isDistanceRatesOptionSelected && ! isPerDiemOptionSelected ) || ! customUnits || Object . keys ( customUnits ) . length === 0 ) {
314339 return undefined ;
@@ -339,20 +364,23 @@ function getCustomUnitsForDuplication(
339364 return undefined ;
340365 }
341366
342- return { [ distanceCustomUnitID ] : distanceCustomUnit , [ perDiemCustomUnitID ] : perDiemUnit } ;
367+ return {
368+ [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID , customUnitRateID ) ,
369+ [ perDiemCustomUnitID ] : cloneCustomUnitWithNewIDs ( perDiemUnit , perDiemCustomUnitID ) ,
370+ } ;
343371 }
344372
345373 if ( isDistanceRatesOptionSelected && distanceCustomUnitID ) {
346374 if ( ! distanceCustomUnit ) {
347375 return undefined ;
348376 }
349- return { [ distanceCustomUnitID ] : distanceCustomUnit } ;
377+ return { [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID , customUnitRateID ) } ;
350378 }
351379
352380 if ( ! perDiemUnit || ! perDiemCustomUnitID ) {
353381 return undefined ;
354382 }
355- return { [ perDiemCustomUnitID ] : perDiemUnit } ;
383+ return { [ perDiemCustomUnitID ] : cloneCustomUnitWithNewIDs ( perDiemUnit , perDiemCustomUnitID ) } ;
356384}
357385
358386/**
0 commit comments