import React from 'react'
import { SCOPE_GROUPS } from '../api/oauthScopes'
import { Lock, ShieldCheck, AlertTriangle, Loader2, LogIn } from 'lucide-react'
import { useTranslation } from '../i18n'
import { useOAuthAuthorize } from './oauthAuthorize/useOAuthAuthorize'
export default function OAuthAuthorizePage(): React.ReactElement {
const { t } = useTranslation()
// Page = wiring container: the validateโconsent state machine lives in the hook.
const {
pageState, validation, submitting, errorMsg, selectedScopes, clientId,
scopesByGroup, submitConsent, toggleScope, toggleGroup, handleLoginRedirect,
} = useOAuthAuthorize()
// ---- Render states ----
if (pageState === 'loading' || pageState === 'auto_approving') {
return (
{pageState === 'auto_approving' ? t('oauth.authorize.authorizing') : t('oauth.authorize.loading')}
)
}
if (pageState === 'error') {
return (
{t('oauth.authorize.errorTitle')}
{errorMsg}
)
}
if (pageState === 'login_required') {
return (
{t('oauth.authorize.loginTitle')}
{t('oauth.authorize.loginDescription', { client: validation?.client?.name || clientId })}
)
}
// pageState === 'consent'
return (
{/* Left panel โ app identity + actions */}
{t('oauth.authorize.requestLabel')}
{validation?.client?.name || clientId}
{t('oauth.authorize.requestDescription')}
{t('oauth.authorize.trustNote')}
{/* Right panel โ selectable scopes */}
{Object.keys(scopesByGroup).length > 0 && (
{validation?.scopeSelectable ? t('oauth.authorize.choosePermissions') : t('oauth.authorize.permissionsRequested')}
{validation?.scopeSelectable ? (
/* DCR client โ user selects which scopes to grant */
{Object.entries(scopesByGroup).map(([group, groupScopes]) => {
const allGroupSelected = groupScopes.every(s => selectedScopes.includes(s))
const someGroupSelected = groupScopes.some(s => selectedScopes.includes(s))
return (
)
})}
) : (
/* Settings-created client โ scopes are fixed, show read-only */
{Object.entries(scopesByGroup).map(([group, groupScopes]) => (
{group}
{groupScopes.map(s => {
const keys = SCOPE_GROUPS[s]
return (
{s.endsWith(':delete') ? '๐๏ธ' : s.endsWith(':write') ? 'โ๏ธ' : '๐๏ธ'}
{keys ? t(keys.labelKey) : s}
{keys ? t(keys.descriptionKey) : ''}
)
})}
))}
)}
)}
{/* Always-available tools โ granted regardless of scopes */}
{t('oauth.authorize.alwaysIncluded')}
{[
{ name: 'list_trips', desc: t('oauth.authorize.alwaysTool.listTrips') },
{ name: 'get_trip_summary', desc: t('oauth.authorize.alwaysTool.getTripSummary') },
].map(({ name, desc }) => (
))}
)
}