brackt/app/lib/owner-map.ts
Chris Parsons ab3437cd73
Display team owner names instead of team names in draft UI (#24)
* Show manager username instead of team name on draft board

Both the read-only draft board and the active draft room now display
the manager's username (falling back to displayName if no username is
set, and to team name if the team is unassigned) in the column headers
of the draft grid.

https://claude.ai/code/session_01C97GauJaAB83NVWxdpNVh1

* Address code review: extract ownerMap helper, fix TeamsDraftedGrid

- Extract ownerMap building into app/lib/owner-map.ts to eliminate
  duplication across the two loaders
- Guard against null username AND displayName so the map only contains
  real string values
- Apply username display to TeamsDraftedGrid (the "Teams" tab in the
  active draft room), which was missed in the initial change

https://claude.ai/code/session_01C97GauJaAB83NVWxdpNVh1

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-02-22 16:56:07 -08:00

39 lines
1.2 KiB
TypeScript

import { findUserByClerkId } from "~/models/user";
/**
* Builds a map of teamId -> manager username (or displayName fallback).
* Accepts any array of objects that have a `team` with `id` and `ownerId`.
*/
export async function buildOwnerMap(
slots: Array<{ team: { id: string; ownerId: string | null } }>
): Promise<Record<string, string>> {
const ownerIds = slots
.map((s) => s.team.ownerId)
.filter((id): id is string => id !== null);
const uniqueOwnerIds = [...new Set(ownerIds)];
const ownerEntries = await Promise.all(
uniqueOwnerIds.map(async (ownerId) => {
const user = await findUserByClerkId(ownerId);
const name = user?.username || user?.displayName || null;
return name ? { ownerId, name } : null;
})
);
const ownerByClerkId = new Map(
ownerEntries
.filter((e): e is NonNullable<typeof e> => e !== null)
.map((e) => [e.ownerId, e.name])
);
const ownerMap: Record<string, string> = {};
for (const slot of slots) {
const name = slot.team.ownerId
? ownerByClerkId.get(slot.team.ownerId)
: undefined;
if (name) {
ownerMap[slot.team.id] = name;
}
}
return ownerMap;
}