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