@@ -66,6 +66,21 @@ export function setupImportControls(this: any) {
6666 } ) ;
6767 }
6868
69+ const schwabButton = document . getElementById ( 'import-schwab-btn' ) ;
70+ const schwabInput = document . getElementById ( 'import-schwab-input' ) as HTMLInputElement | null ;
71+
72+ if ( schwabButton && schwabInput ) {
73+ schwabButton . addEventListener ( 'click' , ( event ) => {
74+ event . preventDefault ( ) ;
75+ schwabInput . value = '' ;
76+ schwabInput . click ( ) ;
77+ } ) ;
78+
79+ schwabInput . addEventListener ( 'change' , ( event ) => {
80+ this . handleSchwabCsvFileSelection ( event ) ;
81+ } ) ;
82+ }
83+
6984 const mergeButton = document . getElementById ( 'import-merge-btn' ) ;
7085 if ( mergeButton ) {
7186 mergeButton . addEventListener ( 'click' , ( event ) => {
@@ -131,13 +146,31 @@ export function setupImportControls(this: any) {
131146 ofxInput . dispatchEvent ( new Event ( 'change' , { bubbles : true } ) ) ;
132147 }
133148 } else if ( name . endsWith ( '.csv' ) ) {
134- // Trigger the Robinhood CSV import path
135- const dt = new DataTransfer ( ) ;
136- dt . items . add ( file ) ;
137- if ( robinhoodInput ) {
138- robinhoodInput . files = dt . files ;
139- robinhoodInput . dispatchEvent ( new Event ( 'change' , { bubbles : true } ) ) ;
140- }
149+ // Sniff the CSV format and route to the right importer;
150+ // unknown layouts open the column mapper.
151+ file . text ( ) . then ( ( text : string ) => {
152+ const format = this . detectCsvFormat ( text ) ;
153+ const fileName = file . name || 'CSV import' ;
154+ if ( format === 'robinhood' ) {
155+ return this . importRobinhoodCsvContent ( text , { fileName } ) ;
156+ }
157+ if ( format === 'schwab' ) {
158+ return this . importSchwabCsvContent ( text , { fileName } ) ;
159+ }
160+ this . openCsvColumnMapper ( text , { fileName } ) ;
161+ return undefined ;
162+ } ) . catch ( ( error : unknown ) => {
163+ console . error ( 'CSV import error:' , error ) ;
164+ const message = error instanceof Error ? error . message : 'Unknown error' ;
165+ this . showNotification ( `Failed to import CSV: ${ message } ` , 'error' ) ;
166+ this . appendImportLog ( {
167+ type : 'error' ,
168+ message : `Failed to import ${ file . name || 'CSV file' } : ${ message } ` ,
169+ timestamp : new Date ( )
170+ } ) ;
171+ } ) . finally ( ( ) => {
172+ this . hideLoadingIndicator ( ) ;
173+ } ) ;
141174 } else {
142175 this . showNotification ( 'Unsupported file type. Please use OFX, QFX, or CSV files.' , 'error' ) ;
143176 }
0 commit comments