Skip to content

Commit

Permalink
Merge pull request #2499 from UltimateHackingKeyboard/feat-unique-hos…
Browse files Browse the repository at this point in the history
…t-connection-name

feat: ensure unique host connection name
  • Loading branch information
mondalaci authored Jan 13, 2025
2 parents dedc3d7 + e5a86cb commit 8031f0e
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions packages/uhk-web/src/app/store/reducers/user-configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export function reducer(
const hostConnection = new HostConnection();
hostConnection.type = HostConnections.BLE;
hostConnection.address = bleAddress;
hostConnection.name = 'Bluetooth device';
hostConnection.name = generateHostConnectionName(userConfiguration.hostConnections, 'Bluetooth device');

userConfiguration.hostConnections.push(hostConnection);
}
Expand Down Expand Up @@ -876,11 +876,14 @@ export function reducer(
case UserConfig.ActionTypes.RenameHostConnection: {
const payload = (action as UserConfig.RenameHostConnectionAction).payload;
const userConfiguration: UserConfiguration = Object.assign(new UserConfiguration(), state.userConfiguration);
const isDuplicated = state.userConfiguration.hostConnections.some((hostConnection, index) => {
return index !== payload.index && hostConnection.name === payload.newName;
});

userConfiguration.hostConnections = userConfiguration.hostConnections.map((hostConnection, index) => {
if (index === payload.index) {
const connection = new HostConnection(hostConnection);
connection.name = payload.newName;
connection.name = isDuplicated ? connection.name : payload.newName;

return connection;
}
Expand Down Expand Up @@ -1097,7 +1100,7 @@ export function reducer(
const newHostConnection = new HostConnection();
newHostConnection.type = HostConnections.Dongle;
newHostConnection.address = bleAddress;
newHostConnection.name = 'Dongle';
newHostConnection.name = generateHostConnectionName(userConfiguration.hostConnections, 'Dongle');

userConfiguration.hostConnections[i] = newHostConnection;
break;
Expand Down Expand Up @@ -1468,3 +1471,22 @@ function addNewMacroToState(state: State): State {
isSelectedMacroNew: true
};
}

function generateHostConnectionName(hostConnections: HostConnection[], baseName: string): string {
let iter: number = 1;
let name = baseName;

while (true) {
if (iter !== 1) {
name = `${baseName} ${iter}`;
}

const exists = hostConnections.find(host => host.name === name);

if (!exists) {
return name;
}

iter++;
}
}

0 comments on commit 8031f0e

Please sign in to comment.