MCPcopy Create free account
hub / github.com/coder/coder / mapUsersToOptions

Function mapUsersToOptions

site/src/modules/tasks/TasksSidebar/UserCombobox.tsx:142–174  ·  view source on GitHub ↗
(
	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,
)

Source from the content-addressed store, hash-verified

140};
141
142function 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}

Callers 1

UserComboboxFunction · 0.70

Calls 1

includeAuthenticatedUserFunction · 0.70

Tested by

no test coverage detected