mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
169 lines
5.4 KiB
TypeScript
169 lines
5.4 KiB
TypeScript
import { EyeIcon, MoreVerticalIcon, XIcon } from 'lucide-react'
|
|
import { useRef, useState } from 'react'
|
|
import {
|
|
Button,
|
|
AlertDialog,
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuSeparator,
|
|
DropdownMenuTrigger,
|
|
Sheet,
|
|
TableCell,
|
|
TableRow,
|
|
} from 'ui'
|
|
|
|
import { OAuthAppAvatar } from './OAuthAppAvatar'
|
|
import { OAuthAppsGrantDetailsSheet } from './OAuthAppsGrantDetailsSheet'
|
|
import { OAuthAppsRevokeDialogContent } from './OAuthAppsRevokeDialogContent'
|
|
import { OAuthApproval, OAuthApprovalTarget } from '@/data/oauth-apps/oauth-apps-approvals-query'
|
|
import { getOAuthDatabaseTokenAccess } from '@/data/oauth-apps/types'
|
|
|
|
export interface OAuthAppsAuthorizedRowProps {
|
|
approval: OAuthApproval
|
|
className?: string
|
|
canRevoke: boolean
|
|
onCloseLinkedAccess?: () => void
|
|
onFocusFallback: () => void
|
|
}
|
|
|
|
export const OAuthAppsAuthorizedRow = ({
|
|
approval,
|
|
className,
|
|
canRevoke,
|
|
onCloseLinkedAccess,
|
|
onFocusFallback,
|
|
}: OAuthAppsAuthorizedRowProps) => {
|
|
const [dialogContent, setDialogContent] = useState<'grants' | 'revoke' | null>(null)
|
|
const activeContent = dialogContent ?? (onCloseLinkedAccess ? 'grants' : null)
|
|
const AccessRoot = activeContent === 'revoke' ? AlertDialog : Sheet
|
|
const menuTriggerRef = useRef<HTMLButtonElement | null>(null)
|
|
const returnFocusRef = useRef<HTMLButtonElement | null>(null)
|
|
|
|
const databaseAccess = getOAuthDatabaseTokenAccess(approval.attributes.org_grant?.approved_scopes)
|
|
|
|
const handleCloseAccess = (revoked: boolean) => {
|
|
setDialogContent(null)
|
|
onCloseLinkedAccess?.()
|
|
// Wait for the closing surface to finish restoring its own focus.
|
|
setTimeout(() => {
|
|
if (!revoked) {
|
|
const trigger = returnFocusRef.current
|
|
if (trigger?.isConnected) trigger.focus()
|
|
else if (menuTriggerRef.current?.isConnected) menuTriggerRef.current.focus()
|
|
else onFocusFallback()
|
|
} else {
|
|
onFocusFallback()
|
|
}
|
|
})
|
|
}
|
|
|
|
const handleOpenAccess = () => {
|
|
returnFocusRef.current = menuTriggerRef.current
|
|
setDialogContent('grants')
|
|
}
|
|
|
|
const handleOpenRevoke = () => {
|
|
returnFocusRef.current = menuTriggerRef.current
|
|
setDialogContent('revoke')
|
|
}
|
|
|
|
return (
|
|
<TableRow
|
|
className={`cursor-pointer ${className ?? ''}`}
|
|
onClick={(event) => {
|
|
if (
|
|
event.target instanceof Element &&
|
|
(!event.currentTarget.contains(event.target) ||
|
|
event.target.closest('button, a, [role="dialog"], [role="alertdialog"], [role="menu"]'))
|
|
)
|
|
return
|
|
handleOpenAccess()
|
|
}}
|
|
>
|
|
<TableCell className="pr-2">
|
|
<OAuthAppAvatar name={approval.attributes.app.name} icon={approval.attributes.app.icon} />
|
|
</TableCell>
|
|
<TableCell className="pl-0">
|
|
<div>
|
|
<button
|
|
type="button"
|
|
tabIndex={0}
|
|
className="min-w-0 truncate text-left focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2"
|
|
onClick={(event) => {
|
|
returnFocusRef.current = event.currentTarget
|
|
setDialogContent('grants')
|
|
}}
|
|
>
|
|
{approval.attributes.app.name}
|
|
</button>
|
|
</div>
|
|
</TableCell>
|
|
<TableCell>
|
|
{getGrantTargetLabel(approval.attributes.target)}
|
|
{databaseAccess && (
|
|
<p className="text-sm text-warning">
|
|
{`Database token access · ${databaseAccess === 'write' ? 'Read and write' : 'Read only'}`}
|
|
</p>
|
|
)}
|
|
</TableCell>
|
|
<TableCell className="text-right">
|
|
<AccessRoot
|
|
open={activeContent !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) handleCloseAccess(false)
|
|
}}
|
|
>
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button
|
|
ref={menuTriggerRef}
|
|
icon={<MoreVerticalIcon />}
|
|
className="px-1"
|
|
aria-label="Manage app"
|
|
/>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end" side="bottom" className="w-40">
|
|
<DropdownMenuItem className="gap-x-2" onSelect={handleOpenAccess}>
|
|
<EyeIcon size={14} />
|
|
View access
|
|
</DropdownMenuItem>
|
|
{canRevoke && <DropdownMenuSeparator />}
|
|
{canRevoke && (
|
|
<DropdownMenuItem className="gap-x-2" onSelect={handleOpenRevoke}>
|
|
<XIcon size={14} />
|
|
Revoke access
|
|
</DropdownMenuItem>
|
|
)}
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
{activeContent === 'grants' && (
|
|
<OAuthAppsGrantDetailsSheet
|
|
approval={approval}
|
|
onRevoke={canRevoke ? handleOpenRevoke : undefined}
|
|
onCloseAutoFocus={preventDefault}
|
|
/>
|
|
)}
|
|
{activeContent === 'revoke' && (
|
|
<OAuthAppsRevokeDialogContent
|
|
approval={approval}
|
|
onClose={handleCloseAccess}
|
|
onCloseAutoFocus={preventDefault}
|
|
/>
|
|
)}
|
|
</AccessRoot>
|
|
</TableCell>
|
|
</TableRow>
|
|
)
|
|
}
|
|
|
|
const preventDefault = (event: Event) => {
|
|
event.preventDefault()
|
|
}
|
|
|
|
const getGrantTargetLabel = (target: OAuthApprovalTarget) => {
|
|
if (target === 'members') return 'Members'
|
|
if (target === 'organization') return 'Organization'
|
|
return 'Organization & members'
|
|
}
|