@@ -47,8 +47,12 @@ import {
47
47
getIconUriFromConnector ,
48
48
getRecordEntry ,
49
49
isNumber ,
50
+ UserPreferenceService ,
51
+ LoggerService ,
52
+ LogEntryLevel ,
50
53
} from '@microsoft/logic-apps-shared' ;
51
54
import type {
55
+ Connection ,
52
56
Connector ,
53
57
DiscoveryOperation ,
54
58
DiscoveryResultTypes ,
@@ -313,8 +317,9 @@ export const trySetDefaultConnectionForNode = async (
313
317
const connectorId = connector . id ;
314
318
const connections = ( await getConnectionsForConnector ( connectorId ) ) . filter ( ( c ) => c . properties . overallStatus !== 'Error' ) ;
315
319
if ( connections . length > 0 ) {
316
- await ConnectionService ( ) . setupConnectionIfNeeded ( connections [ 0 ] ) ;
317
- dispatch ( updateNodeConnection ( { nodeId, connection : connections [ 0 ] , connector } ) ) ;
320
+ const connection = ( await tryGetMostRecentlyUsedConnectionId ( connectorId , connections ) ) ?? connections [ 0 ] ;
321
+ await ConnectionService ( ) . setupConnectionIfNeeded ( connection ) ;
322
+ dispatch ( updateNodeConnection ( { nodeId, connection, connector } ) ) ;
318
323
} else if ( isConnectionRequired ) {
319
324
dispatch ( initEmptyConnectionMap ( nodeId ) ) ;
320
325
dispatch ( openPanel ( { nodeId, panelMode : 'Connection' , referencePanelMode : 'Operation' } ) ) ;
@@ -434,3 +439,23 @@ export const getNonDuplicateId = (existingActionNames: Record<string, string>, a
434
439
}
435
440
return nodeId ;
436
441
} ;
442
+
443
+ export const tryGetMostRecentlyUsedConnectionId = async (
444
+ connectorId : string ,
445
+ allConnections : Connection [ ]
446
+ ) : Promise < Connection | undefined > => {
447
+ let connectionId : string | undefined ;
448
+ // NOTE: If no connection is available from local storage, first connection will be selected by default.
449
+ try {
450
+ connectionId = await UserPreferenceService ( ) ?. getMostRecentlyUsedConnectionId ( connectorId ) ;
451
+ } catch ( error : any ) {
452
+ LoggerService ( ) . log ( {
453
+ level : LogEntryLevel . Warning ,
454
+ message : `Failed to get most recently used connection id for the specified connector ${ connectorId } .` ,
455
+ area : 'OperationAddition' ,
456
+ error,
457
+ } ) ;
458
+ }
459
+
460
+ return connectionId ? allConnections . find ( ( c ) => equals ( c . id , connectionId ) ) : undefined ;
461
+ } ;
0 commit comments