( users: readonly User[], /** * Includes the authenticated user in the list if they are not already * present. So the current user can always select themselves easily. */ authUser: User, /** * Username of the currently selected user. */ selectedValue: string, )
| 131 | }; |
| 132 | |
| 133 | function mapUsersToOptions( |
| 134 | users: readonly User[], |
| 135 | /** |
| 136 | * Includes the authenticated user in the list if they are not already |
| 137 | * present. So the current user can always select themselves easily. |
| 138 | */ |
| 139 | authUser: User, |
| 140 | /** |
| 141 | * Username of the currently selected user. |
| 142 | */ |
| 143 | selectedValue: string, |
| 144 | ): UserOption[] { |
| 145 | const includeAuthenticatedUser = (users: readonly User[]) => { |
| 146 | const hasAuthenticatedUser = users.some( |
| 147 | (u) => u.username === authUser.username, |
| 148 | ); |
| 149 | if (hasAuthenticatedUser) { |
| 150 | return users; |
| 151 | } |
| 152 | return [authUser, ...users]; |
| 153 | }; |
| 154 | |
| 155 | const sortSelectedFirst = (a: User) => |
| 156 | selectedValue && a.username === selectedValue ? -1 : 0; |
| 157 | |
| 158 | return includeAuthenticatedUser(users) |
| 159 | .toSorted(sortSelectedFirst) |
| 160 | .map((user) => ({ |
| 161 | label: user.name || user.username, |
| 162 | value: user.username, |
| 163 | avatarUrl: user.avatar_url, |
| 164 | })); |
| 165 | } |
no test coverage detected