refactor sort() comparison function to be consistent across browsers (#7440)
Co-authored-by: Jared Snider <116684653+JaredSnider-Bitwarden@users.noreply.github.com>
This commit is contained in:
parent
76085cc989
commit
5328ffb349
|
@ -15,17 +15,14 @@
|
||||||
<div class="tw-p-2">
|
<div class="tw-p-2">
|
||||||
<div *ngIf="availableAccounts$ | async as availableAccounts">
|
<div *ngIf="availableAccounts$ | async as availableAccounts">
|
||||||
<ul class="tw-grid tw-list-none tw-gap-2" role="listbox">
|
<ul class="tw-grid tw-list-none tw-gap-2" role="listbox">
|
||||||
<ng-container
|
<ng-container *ngFor="let availableAccount of availableAccounts; first as isFirst">
|
||||||
*ngFor="let availableAccount of availableAccounts; first as isFirst"
|
<li *ngIf="availableAccount.isActive" class="tw-mb-4" role="option">
|
||||||
role="option"
|
|
||||||
>
|
|
||||||
<li *ngIf="availableAccount.isActive" class="tw-mb-4">
|
|
||||||
<auth-account [account]="availableAccount" (loading)="loading = $event"></auth-account>
|
<auth-account [account]="availableAccount" (loading)="loading = $event"></auth-account>
|
||||||
</li>
|
</li>
|
||||||
<div *ngIf="isFirst" class="tw-uppercase tw-text-muted">
|
<div *ngIf="isFirst" class="tw-uppercase tw-text-muted">
|
||||||
{{ "availableAccounts" | i18n }}
|
{{ "availableAccounts" | i18n }}
|
||||||
</div>
|
</div>
|
||||||
<li *ngIf="!availableAccount.isActive">
|
<li *ngIf="!availableAccount.isActive" role="option">
|
||||||
<auth-account [account]="availableAccount" (loading)="loading = $event"></auth-account>
|
<auth-account [account]="availableAccount" (loading)="loading = $event"></auth-account>
|
||||||
</li>
|
</li>
|
||||||
</ng-container>
|
</ng-container>
|
||||||
|
|
|
@ -82,15 +82,25 @@ export class AccountSwitcherService {
|
||||||
}
|
}
|
||||||
|
|
||||||
return options.sort((a, b) => {
|
return options.sort((a, b) => {
|
||||||
// Active account is always first
|
/**
|
||||||
|
* Make sure the compare function is "well-formed" to account for browser inconsistencies.
|
||||||
|
*
|
||||||
|
* For specifics, see the sections "Description" and "Sorting with a non-well-formed comparator"
|
||||||
|
* on this page:
|
||||||
|
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Active account (if one exists) is always first
|
||||||
if (a.isActive) {
|
if (a.isActive) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// "Add Account" button is always last
|
// If account "b" is the 'Add account' button, keep original order of "a" and "b"
|
||||||
if (a.id === this.SPECIAL_ADD_ACCOUNT_ID) {
|
if (b.id === this.SPECIAL_ADD_ACCOUNT_ID) {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 1;
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
Loading…
Reference in New Issue