Fix useTeam/getTeam to fetch single team by id instead of listing all teams

Co-Authored-By: aman <aman@stack-auth.com>
This commit is contained in:
Devin AI 2026-07-13 21:17:16 +00:00
parent 433f7a692d
commit ce732130a1
3 changed files with 33 additions and 22 deletions

View File

@ -81,7 +81,7 @@ const urlPrefetchers: Record<string, ((match: RegExpMatchArray, query: URLSearch
],
"/projects/*/teams": [
([_, projectId]) => {
useAdminApp(projectId).useTeams();
useAdminApp(projectId).useTeams({ limit: 1 });
},
],
"/projects/*/teams/*": [
@ -213,16 +213,6 @@ const urlPrefetchers: Record<string, ((match: RegExpMatchArray, query: URLSearch
() => {
useDashboardInternalUser();
},
([_, projectId]) => {
const project = useAdminApp(projectId).useProject();
const teams = useDashboardInternalUser().useTeams();
const ownerTeam = teams.find((team) => team.id === project.ownerTeamId);
if (ownerTeam) {
return [() => {
ownerTeam.useUsers();
}];
}
},
],
"/projects/*/payments/**": [
([_, projectId]) => {

View File

@ -86,6 +86,16 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
], TeamsCrud['Server']['List']>(async ([userId, orderBy, desc, cursor, limit, query]) => {
return await this._interface.listServerTeamsPaginated({ userId, orderBy, desc, cursor, limit, query });
});
private readonly _serverTeamCache = createCache<string[], TeamsCrud['Server']['Read'] | null>(async ([teamId]) => {
try {
return await this._interface.getServerTeam(teamId);
} catch (error) {
if (KnownErrors.TeamNotFound.isInstance(error)) {
return null;
}
throw error;
}
});
protected async _refreshTeamMembership(teamId: string, userId: string) {
await Promise.all([
@ -709,15 +719,15 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
// END_PLATFORM
selectedTeam: crud.selected_team ? app._serverTeamFromCrud(crud.selected_team) : null,
async getTeam(teamId: string) {
const teams = await this.listTeams();
return teams.find((t) => t.id === teamId) ?? null;
const team = Result.orThrow(await app._serverTeamCache.getOrWait([teamId], "write-only"));
return team == null ? null : app._serverTeamFromCrud(team);
},
// IF_PLATFORM react-like
useTeam(teamId: string) {
const teams = this.useTeams();
const team = useAsyncCache(app._serverTeamCache, [teamId], "user.useTeam()");
return useMemo(() => {
return teams.find((t) => t.id === teamId) ?? null;
}, [teams, teamId]);
return team == null ? null : app._serverTeamFromCrud(team);
}, [team]);
},
// END_PLATFORM
async listTeams(options?: ServerListTeamsOptions): Promise<ServerTeam[] & { nextCursor: string | null }> {
@ -1038,6 +1048,7 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
async update(update: Partial<ServerTeamUpdateOptions>) {
await app._interface.updateServerTeam(crud.id, serverTeamUpdateOptionsToCrud(update));
await Promise.all([
app._serverTeamCache.refresh([crud.id]),
app._serverTeamsCache.refreshWhere(() => true),
app._serverUsersCache.refreshWhere(() => true),
]);
@ -1045,6 +1056,7 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
async delete() {
await app._interface.deleteServerTeam(crud.id);
await Promise.all([
app._serverTeamCache.refresh([crud.id]),
app._serverTeamsCache.refreshWhere(() => true),
app._serverUsersCache.refreshWhere(() => true),
]);
@ -1543,6 +1555,7 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
async createTeam(data: ServerTeamCreateOptions): Promise<ServerTeam> {
const team = await this._interface.createServerTeam(serverTeamCreateOptionsToCrud(data));
await this._serverTeamCache.refresh([team.id]);
await this._serverTeamsCache.refreshWhere(() => true);
return this._serverTeamFromCrud(team);
}
@ -1586,8 +1599,11 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
return await this._getTeamByApiKey(options.apiKey);
} else {
const teamId = options;
const teams = await this.listTeams();
return teams.find((t) => t.id === teamId) ?? null;
if (teamId == null) {
return null;
}
const team = Result.orThrow(await this._serverTeamCache.getOrWait([teamId], "write-only"));
return team == null ? null : this._serverTeamFromCrud(team);
}
}
@ -1599,10 +1615,13 @@ export class _HexclaveServerAppImplIncomplete<HasTokenStore extends boolean, Pro
return this._useTeamByApiKey(options.apiKey);
} else {
const teamId = options;
const teams = this.useTeams();
if (teamId == null) {
return null;
}
const team = useAsyncCache(this._serverTeamCache, [teamId], "serverApp.useTeam()");
return useMemo(() => {
return teams.find((t) => t.id === teamId) ?? null;
}, [teams, teamId]);
return team == null ? null : this._serverTeamFromCrud(team);
}, [team]);
}
}
// END_PLATFORM

View File

@ -113,7 +113,9 @@ teamId: string
Returns: ServerTeam | null
Find in listTeams() by id.
GET /api/v1/teams/{teamId} [server-only]
Returns the team with the requested ID, or null if it does not exist.
Does not error.