11import { Str } from 'expensify-common' ;
2- import React , { useEffect } from 'react' ;
2+ import React , { useEffect , useRef } from 'react' ;
33import { View } from 'react-native' ;
44import useLocalize from '@hooks/useLocalize' ;
55import useOnyx from '@hooks/useOnyx' ;
@@ -27,7 +27,7 @@ function findColumnName(header: string): string {
2727
2828 case 'category' :
2929 case 'categories' :
30- attribute = CONST . CSV_IMPORT_COLUMNS . EMAIL ;
30+ attribute = CONST . CSV_IMPORT_COLUMNS . CATEGORY ;
3131 break ;
3232
3333 case 'glcode' :
@@ -103,6 +103,19 @@ function findColumnName(header: string): string {
103103 attribute = CONST . CSV_IMPORT_COLUMNS . CURRENCY ;
104104 break ;
105105
106+ case 'date' :
107+ case 'transactiondate' :
108+ case 'transaction_date' :
109+ attribute = CONST . CSV_IMPORT_COLUMNS . DATE ;
110+ break ;
111+
112+ case 'merchant' :
113+ case 'merchants' :
114+ case 'vendor' :
115+ case 'vendors' :
116+ attribute = CONST . CSV_IMPORT_COLUMNS . MERCHANT ;
117+ break ;
118+
106119 case 'rateid' :
107120 attribute = CONST . CSV_IMPORT_COLUMNS . RATE_ID ;
108121 break ;
@@ -156,6 +169,7 @@ function ImportColumn({column, columnName, columnRoles, columnIndex, shouldShowD
156169 const { translate} = useLocalize ( ) ;
157170 const [ spreadsheet ] = useOnyx ( ONYXKEYS . IMPORTED_SPREADSHEET ) ;
158171 const { containsHeader = true } = spreadsheet ?? { } ;
172+ const hasAutoDetected = useRef ( false ) ;
159173
160174 const options : Array < DropdownOption < string > > = ( columnRoles ?? [ ] ) . map ( ( item ) => ( {
161175 text : item . text ,
@@ -166,17 +180,27 @@ function ImportColumn({column, columnName, columnRoles, columnIndex, shouldShowD
166180
167181 const columnValuesString = column . slice ( containsHeader ? 1 : 0 ) . join ( ', ' ) ;
168182
169- const colName = findColumnName ( column . at ( 0 ) ?? '' ) ;
170- const defaultSelectedIndex = columnRoles ?. findIndex ( ( item ) => item . value === colName ) ;
171- const finalIndex = defaultSelectedIndex !== - 1 ? defaultSelectedIndex : 0 ;
183+ const currentColumnValue = spreadsheet ?. columns ?. [ columnIndex ] ;
184+ // Treat 'ignore' as unmapped so auto-detection can still run
185+ const isMapped = currentColumnValue && currentColumnValue !== CONST . CSV_IMPORT_COLUMNS . IGNORE ;
186+ const autoDetectedColName = isMapped ? '' : findColumnName ( column . at ( 0 ) ?? '' ) ;
187+
188+ const foundIndex = columnRoles ?. findIndex ( ( item ) => item . value === ( currentColumnValue ?? autoDetectedColName ) ) ?? - 1 ;
189+ const selectedIndex = foundIndex !== - 1 ? foundIndex : 0 ;
172190
173191 useEffect ( ( ) => {
174- if ( defaultSelectedIndex === - 1 ) {
192+ // Only run auto-detection once on mount
193+ if ( hasAutoDetected . current ) {
175194 return ;
176195 }
177- setColumnName ( columnIndex , colName ) ;
178- // eslint-disable-next-line react-hooks/exhaustive-deps -- we don't want this effect to run again
179- } , [ ] ) ;
196+
197+ if ( isMapped || ! autoDetectedColName ) {
198+ return ;
199+ }
200+
201+ hasAutoDetected . current = true ;
202+ setColumnName ( columnIndex , autoDetectedColName ) ;
203+ } , [ isMapped , autoDetectedColName , columnIndex ] ) ;
180204
181205 const columnHeader = containsHeader ? column . at ( 0 ) : translate ( 'spreadsheet.column' , columnName ) ;
182206
@@ -208,7 +232,7 @@ function ImportColumn({column, columnName, columnRoles, columnIndex, shouldShowD
208232 onOptionSelected = { ( option ) => {
209233 setColumnName ( columnIndex , option . value ) ;
210234 } }
211- defaultSelectedIndex = { finalIndex }
235+ defaultSelectedIndex = { selectedIndex }
212236 options = { options }
213237 success = { false }
214238 />
0 commit comments