mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 15:39:13 +00:00
## Problem
In organization Team settings, admins could only assign one project at a
time to a team member with project-scoped access, both when sending an
invite and when managing an existing member's access. The platform API
(role_scoped_projects) already accepts an array of project refs, so this
was purely a UI constraint. Teams customers with larger orgs hit this
repeatedly, since granting access to several projects meant repeating
the picker one project at a time.
## Solution
Added a new shared component, elect, and used it in bothces that
previously used thonProjectSelector:
- Invite members: the project field changed from a scalar projectRef to
projectRefs: string[]; the invite payload now sends every selected
project. anage access: the one-at-a-is replaced by theulti-select, wired
into the list (add/remove updates thatlist; the per-row role dropdowns
and remove buttons are unchanged).
The multi-select reuses the existing debounced-search + infinite-scroll
project daer (same as the old single-ve the same), combined with the
generic ui-patterns/multi-select primitives for the checkable list and
removable trigger badges. It supports:
- Type-to-search against the paginated project list eselecting a project
eitherits badge on the trigger
- Projects the current user isn't permitted to remove (a role-removal
permission check) show as selected-but-locked, with an explanatory
tooltip
existing "All projects" toflows — the multi-select onlyrenders when that
toggle is off, and toggling it on/off behaves exactly as before.
Also fixed an unrelated pre-existing cosmetic bug noticed while testing:
the Managess sheet used bg-surface-2) instead of the sheetprimitive's
default opaque background, making it visibly see-through compared to
every other sheet in the app.
## Review instructions
1. Open Invite members, turn os if needed, toggle "Grant this role on
all projects" off, and confirm you can search and select several
projects, with each shown as a removable badge.
2. Send the invite and confirm applied to the new member'saccess.
3. Open Manage access on an existing member with project-scoped access —
confirm tcurrent projects are pre-se more projects via themulti-select,
remove one via its badge and another via the row's "X", and save.Confirm
the saved access ma
4. Toggle "Apply roles to all projects" on and off in Manage access and
confirm thexisting toggle behavior is
5. Confirm the Manage access sheet background is now fully opaque,
matching other sheets in the app.
6. pnpm --filter studio typecher studio test for the touchedfiles all
pass.
---------
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
110 lines
3.1 KiB
TypeScript
110 lines
3.1 KiB
TypeScript
import * as z from 'zod'
|
|
|
|
import type { OrganizationMember } from '@/data/organizations/organization-members-query'
|
|
|
|
export const MAX_BATCH_INVITE_SIZE = 50
|
|
|
|
/** Max characters to show when an invalid token is long (e.g. comma-less paste of many addresses). */
|
|
const MAX_INVALID_EMAIL_SNIPPET_LENGTH = 120
|
|
|
|
function formatInvalidEmailSnippet(token: string): string {
|
|
if (token.length <= MAX_INVALID_EMAIL_SNIPPET_LENGTH) return token
|
|
return `${token.slice(0, MAX_INVALID_EMAIL_SNIPPET_LENGTH)}…`
|
|
}
|
|
|
|
export const emailSchema = z
|
|
.string()
|
|
.min(1, 'At least one email address is required')
|
|
.refine(
|
|
(val) => {
|
|
const emails = parseEmails(val)
|
|
if (emails.length === 0) return false
|
|
return emails.every((e) => z.string().email().safeParse(e).success)
|
|
},
|
|
(val) => {
|
|
const emails = parseEmails(val)
|
|
const invalid = emails.find((e) => !z.string().email().safeParse(e).success)
|
|
return {
|
|
message: invalid
|
|
? `Invalid email address: "${formatInvalidEmailSnippet(invalid)}"`
|
|
: 'At least one email address is required',
|
|
}
|
|
}
|
|
)
|
|
.refine(
|
|
(val) => parseEmails(val).length <= MAX_BATCH_INVITE_SIZE,
|
|
(val) => {
|
|
const count = parseEmails(val).length
|
|
return {
|
|
message: `You can invite up to ${MAX_BATCH_INVITE_SIZE} members at a time. Remove ${count - MAX_BATCH_INVITE_SIZE} email ${count - MAX_BATCH_INVITE_SIZE === 1 ? 'address' : 'addresses'} to continue.`,
|
|
}
|
|
}
|
|
)
|
|
|
|
export function parseEmails(value: string): string[] {
|
|
const emails = value
|
|
.split(/[\s,]+/)
|
|
.map((e) => e.trim().toLowerCase())
|
|
.filter(Boolean)
|
|
return [...new Set(emails)]
|
|
}
|
|
|
|
export type CategorizedEmails = {
|
|
alreadyInvited: string[]
|
|
alreadyMembers: string[]
|
|
toInvite: string[]
|
|
}
|
|
|
|
export type BatchInvitationFailure = {
|
|
email: string
|
|
error: string
|
|
}
|
|
|
|
export type BatchInvitationResult = {
|
|
succeeded: string[]
|
|
failed: BatchInvitationFailure[]
|
|
}
|
|
|
|
export function categorizeInviteEmails(
|
|
emails: string[],
|
|
members: OrganizationMember[]
|
|
): CategorizedEmails {
|
|
const alreadyInvited: string[] = []
|
|
const alreadyMembers: string[] = []
|
|
const toInvite: string[] = []
|
|
|
|
for (const email of emails) {
|
|
const existingMember = members.find((m) => m.primary_email === email)
|
|
if (existingMember !== undefined) {
|
|
if (existingMember.invited_id) {
|
|
alreadyInvited.push(email)
|
|
} else {
|
|
alreadyMembers.push(email)
|
|
}
|
|
} else {
|
|
toInvite.push(email)
|
|
}
|
|
}
|
|
|
|
return { alreadyInvited, alreadyMembers, toInvite }
|
|
}
|
|
|
|
export function buildProjectPayload(
|
|
applyToOrg: boolean,
|
|
projectRefs: string[]
|
|
): { projects: string[] } | Record<string, never> {
|
|
if (applyToOrg) return {}
|
|
if (projectRefs.length === 0) {
|
|
throw new Error('At least one project is required when applyToOrg is false')
|
|
}
|
|
return { projects: projectRefs }
|
|
}
|
|
|
|
export function buildSsoPayload(
|
|
requireSso: 'auto' | 'sso' | 'non-sso'
|
|
): { requireSso: boolean } | Record<string, never> {
|
|
if (requireSso === 'sso') return { requireSso: true }
|
|
if (requireSso === 'non-sso') return { requireSso: false }
|
|
return {}
|
|
}
|