When an API call is made to decide what to show/enable or what to persist, ensure the API usage is precise, consistent with the correct source of truth, and race-safe.
1) Avoid redundant entitlement/plan calls
2) Use filtered/parameterized API queries for membership decisions
3) Prevent stale writes after async calls
Example pattern (branch/target suggestion):
const branchRequestId = useRef(0);
async function loadAndSuggestBranch({ repo, workspaceBranch }: {
repo: { id: number } | undefined;
workspaceBranch: string | undefined;
}) {
if (!repo || !workspaceBranch) return undefined;
const requestId = ++branchRequestId.current;
// Query precisely by branch name; avoid paginated first-page ambiguity.
const res = await CloudServiceClient.getRepositoryBranches(
RepositoryBranchesRequest.create({
repositoryId: repo.id,
query: workspaceBranch,
})
);
if (requestId !== branchRequestId.current) return undefined;
const exists = res.branches.includes(workspaceBranch);
return exists ? workspaceBranch : undefined;
}
// When persisting after async confirmation, use an updater
// so the saved selection matches the current UI state.