import { connectionAccountIdentityLabel } from "@opengeni/contracts/connection-account-label"; import type { ConnectionMetadata } from "@opengeni/sdk"; import { DropdownMenuItem } from "../../../ui/dropdown-menu"; import { ComposerMenuSwitchIndicator } from "../../../ui/composer-menu"; import type { ConnectedAccountGroup, ConnectionAccountChoices, } from "./session-connection-accounts"; export function connectionAccountLabel(account: ConnectionMetadata, fallback: string): string { return connectionAccountIdentityLabel(account.metadata, fallback); } export type ConnectionAccountPickerProps = { groups: ConnectedAccountGroup[]; choices: ConnectionAccountChoices; onChoose: (serverId: string, connectionIds: string[]) => void; disabled?: boolean | undefined; presentation?: "menu" | "dialog" | undefined; emptySelectionHint?: string | undefined; }; export type ConnectionAccountControls = ConnectionAccountPickerProps & { loading?: boolean | undefined; error?: string | null | undefined; accessDenied?: boolean | undefined; onRefresh?: (() => void) | undefined; }; export function ConnectionAccountPicker(props: ConnectionAccountPickerProps) { return props.groups.map((group) => { const chosen = props.choices[group.serverId] ?? group.accounts.map((account) => account.id); const missing = chosen.filter((id) => !group.accounts.some((account) => account.id === id)); return (
{props.presentation !== "menu" ? (

{group.name} accounts

) : null} {group.accounts.map((account, index) => { const checked = chosen.includes(account.id); const label = connectionAccountLabel(account, `${group.name} account ${index + 1}`); const ownership = account.subjectId === null ? "This workspace" : "Only me"; const onToggle = () => props.onChoose( group.serverId, checked ? chosen.filter((id) => id !== account.id) : [...chosen, account.id], ); const content = ( <> {label} {ownership} ); const className = "flex min-h-11 w-full items-center gap-3 rounded-md px-2 py-2 text-left outline-hidden focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50"; return props.presentation === "menu" ? ( { event.preventDefault(); onToggle(); }} > {content} ) : ( ); })} {missing.map((id, index) => { const label = `Remove disconnected account${missing.length > 1 ? ` ${index + 1}` : ""}`; const onRemove = () => props.onChoose( group.serverId, chosen.filter((chosenId) => chosenId !== id), ); const className = "min-h-11 px-2 text-left text-xs text-status-failed underline"; return props.presentation === "menu" ? ( { event.preventDefault(); onRemove(); }} > {label} ) : ( ); })} {!group.accounts.length ? (

Connect an account in Capabilities.

) : null} {!chosen.length && group.accounts.length > 0 ? (

{props.emptySelectionHint ?? "No accounts selected."}

) : null}
); }); }