/** * Merge `/users/:id` embedded `partner_groups` with `GET /users/:id/partner-groups` cache. * After a partner accepts (or the other side's client refetches), `user.partner_groups` updates * first while `partner-groups` may still hold a pre-link `[]` — prefer non-empty user payload. */ export function resolvedPartnerGroups(user, groupsQueryData) { const fromUser = Array.isArray(user?.partner_groups) ? user.partner_groups : null; const fromApi = Array.isArray(groupsQueryData?.items) ? groupsQueryData.items : null; if (fromUser?.length) return fromUser; if (fromApi?.length) return fromApi; return fromUser || fromApi || []; } export function preferredPartnerGroupId(groups, currentId = "") { const list = Array.isArray(groups) ? groups : []; if (!list.length) return ""; if (currentId && list.some((g) => g?.id === currentId)) return currentId; const withPartner = list.find((g) => (g?.member_ids || []).filter(Boolean).length > 0); return (withPartner || list[0])?.id || ""; }