Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

4.3 KB
Raw Blame
import { ChevronRight, EllipsisVertical, Pencil, Shield, Trash2 } from 'lucide-react'
import {
  Button,
  Card,
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from 'ui'

const policies = [
  {
    id: 'policy-1',
    name: 'Anyone can view all active listings',
    command: 'select',
    appliedTo: 'public',
  },
  {
    id: 'policy-2',
    name: 'Users can delete their own listings',
    command: 'delete',
    appliedTo: 'authenticated',
  },
  {
    id: 'policy-3',
    name: 'Admins can update any listing',
    command: 'update',
    appliedTo: 'authenticated',
  },
]

// Studio: See also createNavigationHandler in apps/studio/lib/navigation.ts
// It handles all of the below, plus modifier clicks and middle mouse button clicks.
const handlePolicyNavigation = (
  policyId: string,
  event: React.MouseEvent | React.KeyboardEvent
) => {
  const url = `/${policyId}`
  if (event.metaKey || event.ctrlKey) {
    // window.open(`${url}`, '_blank') Disabled for demo purposes
  } else {
    // router.push(url) Disabled for demo purposes
  }
}

export default function TableRowLinkActions() {
  return (
    <Card className="w-full">
      <Table>
        <TableHeader>
          <TableRow>
            <TableHead className="w-1">
              <span className="sr-only">Icon</span>
            </TableHead>
            <TableHead>Name</TableHead>
            <TableHead>Command</TableHead>
            <TableHead>Applied to</TableHead>
            <TableHead className="w-1">
              <span className="sr-only">Actions</span>
            </TableHead>
          </TableRow>
        </TableHeader>
        <TableBody>
          {policies.map((policy) => (
            <TableRow
              key={policy.id}
              className="relative cursor-pointer focus-inset"
              onClick={(event) => {
                if (event.currentTarget !== event.target) return
                handlePolicyNavigation(policy.id, event)
              }}
              onKeyDown={(event) => {
                if (event.currentTarget !== event.target) return
                if (event.key === 'Enter' || event.key === ' ') {
                  event.preventDefault()
                  handlePolicyNavigation(policy.id, event)
                }
              }}
              tabIndex={0}
            >
              <TableCell className="w-1">
                <Shield aria-label="policy icon" size={16} className="text-foreground-muted" />
              </TableCell>
              <TableCell>{policy.name}</TableCell>
              <TableCell>
                <code className="text-foreground-muted uppercase">{policy.command}</code>
              </TableCell>
              <TableCell className="text-foreground-lighter">{policy.appliedTo}</TableCell>
              <TableCell className="flex justify-end items-center h-full gap-3">
                <div
                  className="flex justify-end items-center h-full gap-3"
                  onClick={(event) => event.stopPropagation()}
                  onKeyDown={(event) => event.stopPropagation()}
                >
                  <DropdownMenu>
                    <DropdownMenuTrigger asChild>
                      <Button
                        icon={<EllipsisVertical />}
                        aria-label="More actions"
                        className="w-7 hit-area-2"
                      />
                    </DropdownMenuTrigger>
                    <DropdownMenuContent side="bottom" align="end" className="w-40">
                      <DropdownMenuItem className="gap-x-2">
                        <Pencil size={14} />
                        <span>Edit policy</span>
                      </DropdownMenuItem>
                      <DropdownMenuItem className="gap-x-2">
                        <Trash2 size={14} />
                        <span>Delete</span>
                      </DropdownMenuItem>
                    </DropdownMenuContent>
                  </DropdownMenu>
                  <ChevronRight aria-hidden={true} size={14} className="text-foreground-muted/60" />
                  <button tabIndex={-1} className="sr-only">
                    Go to policy
                  </button>
                </div>
              </TableCell>
            </TableRow>
          ))}
        </TableBody>
      </Table>
    </Card>
  )
}