Files
Reims-Go/frontend/src/components/ElevationDialog.tsx
T

95 lines
3.5 KiB
TypeScript

import * as Alert from '@radix-ui/react-alert-dialog'
import React, { useRef } from 'react'
import { t } from '../i18n'
import { Locale } from '../types'
import { Button } from '../ui/controls'
import type { ElevationPrompt } from '../wails'
interface Props {
locale: Locale
request: ElevationPrompt
onResolve: (approved: boolean) => void
}
function actionText(locale: Locale, action: string): { title: string; description: string } {
switch (action) {
case 'hyperv.enable':
return {
title: t(locale, 'elevateActionHyperVEnableTitle'),
description: t(locale, 'elevateActionHyperVEnableDesc'),
}
case 'hyperv.addAdminGroup':
return {
title: t(locale, 'elevateActionHyperVAdminTitle'),
description: t(locale, 'elevateActionHyperVAdminDesc'),
}
case 'sandbox.enable':
return {
title: t(locale, 'elevateActionSandboxEnableTitle'),
description: t(locale, 'elevateActionSandboxEnableDesc'),
}
default:
return {
title: t(locale, 'elevateActionFallbackTitle', { action }),
description: t(locale, 'elevateActionFallbackDesc'),
}
}
}
export const ElevationDialog: React.FC<Props> = ({ locale, request, onResolve }) => {
const cancelRef = useRef<HTMLButtonElement | null>(null)
const resolvedRef = useRef(false)
const resolveOnce = (approved: boolean) => {
if (resolvedRef.current) return
resolvedRef.current = true
onResolve(approved)
}
const prevFocusRef = useRef<HTMLElement | null>(
typeof document !== 'undefined' && document.activeElement instanceof HTMLElement
? document.activeElement
: null,
)
const { title: actionTitle, description } = actionText(locale, request.action)
const sourceDetail = [request.publisher, request.trust].filter(Boolean).join(' · ')
return (
<Alert.Root open onOpenChange={(open) => { if (!open) resolveOnce(false) }}>
<Alert.Portal>
<Alert.Overlay className="overlay" />
<Alert.Content
className="dialog rg-p-dialog sm"
data-tone="warn"
onOpenAutoFocus={(e) => {
e.preventDefault()
cancelRef.current?.focus({ preventScroll: true })
}}
onCloseAutoFocus={(e) => {
e.preventDefault()
const prev = prevFocusRef.current
if (prev?.isConnected) prev.focus({ preventScroll: true })
}}
>
<div className="dialog-body">
<Alert.Title className="dialog-title">{t(locale, 'elevateTitle')}</Alert.Title>
<div className="stack-2" style={{ marginTop: 'var(--sp-2)', fontSize: 'var(--fs-13)' }}>
<div style={{ fontWeight: 600, color: 'var(--text-1)' }}>{actionTitle}</div>
<div className="muted">
{t(locale, 'elevateSource', { name: request.plugin_name || request.plugin_id })}
{sourceDetail ? ` (${sourceDetail})` : ''}
</div>
<Alert.Description className="dialog-desc" style={{ marginTop: 0 }}>{description}</Alert.Description>
</div>
</div>
<div className="dialog-foot">
<Alert.Cancel asChild>
<Button ref={cancelRef} autoFocus onClick={() => resolveOnce(false)}>{t(locale, 'cancel')}</Button>
</Alert.Cancel>
<Alert.Action asChild>
<Button variant="primary" onClick={() => resolveOnce(true)}>{t(locale, 'elevateConfirm')}</Button>
</Alert.Action>
</div>
</Alert.Content>
</Alert.Portal>
</Alert.Root>
)
}