import { Check, ChevronDown, Edit, X } from 'lucide-react' import { useMemo } from 'react' import { cn, Collapsible, CollapsibleContent, CollapsibleTrigger, WarningIcon } from 'ui' import type { Policy } from '@/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRow.utils' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' interface RLSTableCardProps { table: { schema: string; name: string; isRLSEnabled: boolean } role?: string policies: Policy[] handleSelectEditPolicy: (policy: Policy) => void } export const RLSTableCard = ({ table, role, policies, handleSelectEditPolicy, }: RLSTableCardProps) => { const { schema, name, isRLSEnabled } = table const trueOnlyPolicy = policies.find((x) => x.definition === 'true') const falseOnlyPolicy = policies.find((x) => x.definition === 'false') const noPolicies = isRLSEnabled && policies.length === 0 const tableAccessDescription = useMemo(() => { if (!isRLSEnabled) { return (
RLS is disabled and all data is publicly accessible. We highly recommend enabling RLS and adding policies to restrict access.
) } if (noPolicies) { return (
RLS is enabled but no policies exist for the{' '}
{role} role on this table - no data will be
returned.
The policy "{trueOnlyPolicy.name}" for the{' '}
{role} role on this table evaluates to{' '}
true, so all data from this query is
accessible to this user.
The policy "{falseOnlyPolicy.name}" for the{' '}
{role} role on this table evaluates to{' '}
false, so no data from this query is
accessible to this user.
{policies.length} {policies.length > 1 ? 'policies apply' : 'policy applies'} for the{' '}
{role} role on this table. Only rows that match{' '}
{policies.length > 1 ? 'these conditions' : 'this condition'} are returned.
{schema}.{name}
{noPolicies || falseOnlyPolicy ? 'Returns no rows' : !isRLSEnabled || !!trueOnlyPolicy ? 'Returns all rows' : null}
{policies.length} {policies.length > 1 ? 'policies' : 'policy'} applied
{policy.name}
Show rows where:{' '}
{policy.definition}