import { useEffect, useId, useRef, useState } from 'react' import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog' import { Button } from '@/components/ui/button' import { Textarea } from '@/components/ui/textarea' import type { PermissionReply, PermissionRequest } from '@opencode-manager/shared/opencode' import { getPermissionLabel, getPermissionPhrase, getPermissionDetail, getPermissionAlwaysScope, } from '@opencode-manager/shared/notifications' import { cn } from '@/lib/utils' import { showToast } from '@/lib/toast' interface PermissionRequestDialogProps { permission: PermissionRequest | null pendingCount: number isFromDifferentSession?: boolean sessionTitle?: string repoDirectory?: string | null onOpenSession?: () => void onRespond: ( permissionID: string, sessionID: string, response: PermissionReply, message?: string, ) => Promise open?: boolean onOpenChange?: (open: boolean) => void } export function PermissionRequestDialog({ permission, pendingCount, isFromDifferentSession, sessionTitle, repoDirectory, onOpenSession, onRespond, open: parentOpen, onOpenChange, }: PermissionRequestDialogProps) { const [isLoading, setIsLoading] = useState(false) const [loadingAction, setLoadingAction] = useState(null) const [rejectReason, setRejectReason] = useState('') const reasonInputId = useId() const allowOnceRef = useRef(null) useEffect(() => { setRejectReason('') }, [permission?.id]) if (!permission) return null if (parentOpen === false) return null const handleResponse = async (response: PermissionReply) => { setIsLoading(true) setLoadingAction(response) const message = response === 'reject' ? rejectReason.trim() : '' try { await onRespond(permission.id, permission.sessionID, response, message === '' ? undefined : message) } catch { showToast.error('Failed to respond to permission. Please try again.') } finally { setIsLoading(false) setLoadingAction(null) } } const typeLabel = getPermissionLabel(permission.action) const details = getPermissionDetail(permission) const alwaysScope = getPermissionAlwaysScope(permission) const hasMultiple = pendingCount > 1 const displaySessionName = sessionTitle || `Session ${permission.sessionID.slice(0, 8)}...` return ( {})}> { event.preventDefault() allowOnceRef.current?.focus() }} > Permission Request {hasMultiple && ( +{pendingCount - 1} more )} {`Allow ${getPermissionPhrase(permission.action)}?`}
{typeLabel}
{permission.message && (

{permission.message}

)} {details.primary && (
{details.label && ( {details.label} )}
                  {details.primary}
                
)} {details.secondary && (
                {details.secondary}
              
)}
{repoDirectory && (
Repo: {repoDirectory.split('/').pop() ?? repoDirectory}
)}
{isFromDifferentSession ? (
From another session: {displaySessionName}
) : (
Session: {displaySessionName}
)} {onOpenSession && ( )}