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.

) } if (trueOnlyPolicy) { return ( <>

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.

) } if (falseOnlyPolicy) { return ( <>

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.

) } return ( <>

{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.

) }, [ isRLSEnabled, noPolicies, trueOnlyPolicy, falseOnlyPolicy, policies, role, handleSelectEditPolicy, ]) return (
{!isRLSEnabled ? ( ) : noPolicies || falseOnlyPolicy ? ( ) : ( )}

{schema}.{name}

{noPolicies || falseOnlyPolicy ? 'Returns no rows' : !isRLSEnabled || !!trueOnlyPolicy ? 'Returns all rows' : null}

{tableAccessDescription}
) } const TableAccessPolicySummary = ({ policies, handleSelectEditPolicy, }: { policies: Policy[] handleSelectEditPolicy: (policy: Policy) => void }) => { return (

{policies.length} {policies.length > 1 ? 'policies' : 'policy'} applied

    {policies.map((policy) => (
  • {policy.name}

    Show rows where:{' '} {policy.definition}

    } className="w-7" tooltip={{ content: { side: 'bottom', text: 'Edit policy' } }} onClick={() => { handleSelectEditPolicy(policy) }} />
  • ))}
) }