Skip to content

Commit 18047c3

Browse files
preetriti1Priti Sambandam
and
Priti Sambandam
authored
feat(designer): Adding support for recently used connection for a connector (#5612)
Co-authored-by: Priti Sambandam <[email protected]>
1 parent 05feb27 commit 18047c3

File tree

10 files changed

+104
-2
lines changed

10 files changed

+104
-2
lines changed

apps/Standalone/src/designer/app/AzureLogicAppsDesigner/laDesigner.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ import type { QueryClient } from '@tanstack/react-query';
6565
import { useDispatch, useSelector } from 'react-redux';
6666
import { useHostingPlan } from '../../state/workflowLoadingSelectors';
6767
import CodeViewEditor from './CodeView';
68+
import { BaseUserPreferenceService } from '@microsoft/logic-apps-shared';
6869

6970
const apiVersion = '2020-06-01';
7071
const httpClient = new HttpClient();
@@ -779,6 +780,7 @@ const getDesignerServices = (
779780
hostService,
780781
chatbotService,
781782
customCodeService,
783+
userPreferenceService: new BaseUserPreferenceService(),
782784
};
783785
};
784786

apps/Standalone/src/designer/app/AzureLogicAppsDesigner/laDesignerConsumption.tsx

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
guid,
3636
startsWith,
3737
StandardCustomCodeService,
38+
BaseUserPreferenceService,
3839
} from '@microsoft/logic-apps-shared';
3940
import type { CustomCodeFileNameMapping, Workflow } from '@microsoft/logic-apps-designer';
4041
import {
@@ -547,6 +548,7 @@ const getDesignerServices = (
547548
hostService,
548549
chatbotService,
549550
customCodeService,
551+
userPreferenceService: new BaseUserPreferenceService(),
550552
};
551553
};
552554

libs/designer/src/lib/core/actions/bjsworkflow/add.ts

+27-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ import {
4747
getIconUriFromConnector,
4848
getRecordEntry,
4949
isNumber,
50+
UserPreferenceService,
51+
LoggerService,
52+
LogEntryLevel,
5053
} from '@microsoft/logic-apps-shared';
5154
import type {
55+
Connection,
5256
Connector,
5357
DiscoveryOperation,
5458
DiscoveryResultTypes,
@@ -313,8 +317,9 @@ export const trySetDefaultConnectionForNode = async (
313317
const connectorId = connector.id;
314318
const connections = (await getConnectionsForConnector(connectorId)).filter((c) => c.properties.overallStatus !== 'Error');
315319
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 }));
318323
} else if (isConnectionRequired) {
319324
dispatch(initEmptyConnectionMap(nodeId));
320325
dispatch(openPanel({ nodeId, panelMode: 'Connection', referencePanelMode: 'Operation' }));
@@ -434,3 +439,23 @@ export const getNonDuplicateId = (existingActionNames: Record<string, string>, a
434439
}
435440
return nodeId;
436441
};
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+
};

libs/designer/src/lib/core/actions/bjsworkflow/connections.ts

+3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
equals,
3636
ConnectionReferenceKeyFormat,
3737
getRecordEntry,
38+
UserPreferenceService,
3839
} from '@microsoft/logic-apps-shared';
3940
import type { Dispatch } from '@reduxjs/toolkit';
4041
import { createAsyncThunk } from '@reduxjs/toolkit';
@@ -81,6 +82,8 @@ export const updateNodeConnection = createAsyncThunk(
8182
const { nodeId, connector, connection, connectionProperties, authentication } = payload;
8283

8384
dispatch(updateErrorDetails({ id: nodeId, clear: true }));
85+
86+
UserPreferenceService()?.setMostRecentlyUsedConnectionId(connector.id, connection.id);
8487
return updateNodeConnectionAndProperties(
8588
{
8689
nodeId,

libs/designer/src/lib/core/state/designerOptions/designerOptionsInterfaces.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import type {
2121
LogicApps,
2222
ICopilotService,
2323
IDesignerUiInteractionsService,
24+
IUserPreferenceService,
2425
} from '@microsoft/logic-apps-shared';
2526
import type { MaximumWaitingRunsMetadata } from '../../../ui/settings';
2627

@@ -71,4 +72,5 @@ export interface ServiceOptions {
7172
customCodeService?: ICustomCodeService;
7273
copilotService?: ICopilotService;
7374
uiInteractionsService?: IDesignerUiInteractionsService;
75+
userPreferenceService?: IUserPreferenceService;
7476
}

libs/designer/src/lib/core/state/designerOptions/designerOptionsSlice.ts

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
InitCustomCodeService,
2323
InitCopilotService,
2424
InitUiInteractionsService,
25+
InitUserPreferenceService,
2526
} from '@microsoft/logic-apps-shared';
2627
import { createAsyncThunk, createSlice } from '@reduxjs/toolkit';
2728
import type { PayloadAction } from '@reduxjs/toolkit';
@@ -68,6 +69,7 @@ export const initializeServices = createAsyncThunk(
6869
customCodeService,
6970
copilotService,
7071
uiInteractionsService,
72+
userPreferenceService,
7173
}: ServiceOptions) => {
7274
const loggerServices: ILoggerService[] = [];
7375
if (loggerService) {
@@ -123,6 +125,10 @@ export const initializeServices = createAsyncThunk(
123125
InitUiInteractionsService(uiInteractionsService);
124126
}
125127

128+
if (userPreferenceService) {
129+
InitUserPreferenceService(userPreferenceService);
130+
}
131+
126132
InitEditorService(editorService);
127133
InitConnectionParameterEditorService(connectionParameterEditorService);
128134

libs/logic-apps-shared/src/designer-client-services/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ export * from './lib/copilot';
2828
export * from './lib/customcode';
2929
export * from './lib/tenant';
3030
export * from './lib/uiInteractionsService';
31+
export * from './lib/userpreference';

libs/logic-apps-shared/src/designer-client-services/lib/base/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,5 @@ export type { TemplateServiceOptions } from './template';
4444
// Tenant
4545
export { BaseTenantService } from './tenant';
4646
export type { BaseTenantServiceOptions } from './tenant';
47+
48+
export { BaseUserPreferenceService } from './userpreference';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { IUserPreferenceService } from '../userpreference';
2+
3+
/**
4+
* The default user preference service.
5+
*/
6+
export class BaseUserPreferenceService implements IUserPreferenceService {
7+
private _store: Storage | undefined;
8+
9+
constructor() {
10+
this._store = typeof Storage !== 'undefined' ? localStorage : undefined;
11+
}
12+
13+
/**
14+
* Gets the most recently used connection id for the specified connector.
15+
* @arg {string} connectorId - The connector id.
16+
* @return {Promise<string | undefined>}
17+
*/
18+
getMostRecentlyUsedConnectionId(connectorId: string): string | undefined {
19+
return this._store?.getItem(connectorId.toLowerCase()) ?? undefined;
20+
}
21+
22+
/**
23+
* Sets the most recently used connection id for the specified connector.
24+
* @arg {string} connectorId - The connector id.
25+
* @arg {string} connectionId - The connection id.
26+
* @return {Promise<string>}
27+
*/
28+
setMostRecentlyUsedConnectionId(connectorId: string, connectionId: string) {
29+
this._store?.setItem(connectorId.toLowerCase(), connectionId);
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* The user preference service.
3+
*/
4+
export interface IUserPreferenceService {
5+
/**
6+
* Sets most recently used connection id for the specified connector.
7+
* @arg {string} connectorId - The connector id.
8+
* @arg {string} connectionId - The connection id.
9+
*/
10+
setMostRecentlyUsedConnectionId(connectorId: string, connectionId: string): void;
11+
12+
/**
13+
* Gets most recently used connection id for the specified connector.
14+
* @arg {string} connectorId - The connector id.
15+
* @return {string | null | undefined}
16+
*/
17+
getMostRecentlyUsedConnectionId(connectorId: string): string | undefined;
18+
}
19+
20+
let service: IUserPreferenceService;
21+
22+
export const InitUserPreferenceService = (preferenceService: IUserPreferenceService): void => {
23+
service = preferenceService;
24+
};
25+
26+
export const UserPreferenceService = (): IUserPreferenceService => {
27+
return service;
28+
};

0 commit comments

Comments
 (0)