import { CheckIcon, DatabaseIcon, UserCheckIcon, UserXIcon } from 'lucide-react' import { Separator, ToggleGroup, ToggleGroupItem } from 'ui' import { UserImpersonationSelector } from './UserImpersonationSelector' import { useRoleImpersonationSelection } from './useRoleImpersonationSelection' import { useRoleImpersonationStateSnapshot, type RoleImpersonationController, } from '@/state/role-impersonation-state' export interface RoleImpersonationSelectorProps { header?: string serviceRoleLabel?: string disallowAuthenticatedOption?: boolean title?: string } /** * Tightly coupled with the global role impersonation store. * Use RoleImpersonationSelectorInterface to control the logic externally. */ export const RoleImpersonationSelector = (props: RoleImpersonationSelectorProps) => { const state = useRoleImpersonationStateSnapshot() as unknown as RoleImpersonationController return } type RoleImpersonationSelectorInterfaceProps = RoleImpersonationSelectorProps & { state: RoleImpersonationController } export const RoleImpersonationSelectorInterface = ( props: RoleImpersonationSelectorInterfaceProps ) => { const { state } = props const serviceRoleLabel = props.serviceRoleLabel ?? 'Postgres' const disallowAuthenticatedOption = props.disallowAuthenticatedOption ?? false const { selectedOption, onSelectedChange, keepAuthenticatedSelected } = useRoleImpersonationSelection(state) const roleSummary = { service_role: 'Bypasses RLS and can return all rows.', anon: 'Returns rows available to anonymous users.', authenticated: 'Returns rows available to the selected user.', }[selectedOption] return (
{ event.preventDefault() }} > { if (value === 'service_role' || value === 'anon' || value === 'authenticated') { void onSelectedChange(value) } }} variant="default" aria-label={props.header ?? props.title ?? 'Run query as role'} className="w-full flex-col items-stretch gap-0.5" > {serviceRoleLabel} Superuser {selectedOption === 'service_role' && } Anonymous Not logged in {selectedOption === 'anon' && } {!disallowAuthenticatedOption && ( Authenticated Logged-in user {selectedOption === 'authenticated' && } )} {!disallowAuthenticatedOption && ( <> )}
) }