import React from 'react'; import type { Scope } from '@growi/core/dist/interfaces'; import { useTranslation } from 'next-i18next'; import type { UseFormRegisterReturn } from 'react-hook-form'; import { useIsDeviceLargerThanMd } from '~/stores/ui'; import styles from './AccessTokenScopeList.module.scss'; const moduleClass = styles['access-token-scope-list'] ?? ''; interface scopeObject { [key: string]: Scope | scopeObject; } interface AccessTokenScopeListProps { scopeObject: scopeObject; register: UseFormRegisterReturn<'scopes'>; disabledScopes: Set level?: number; } /** * Renders the permission object recursively as nested checkboxes. */ export const AccessTokenScopeList: React.FC = ({ scopeObject, register, disabledScopes, level = 1, }) => { const { data: isDeviceLargerThanMd } = useIsDeviceLargerThanMd(); // Convert object into an array to determine "first vs. non-first" elements const entries = Object.entries(scopeObject); const { t } = useTranslation('commons'); return ( <> {entries.map(([scopeKey, scopeValue], idx) => { const showHr = (level === 1 || level === 2) && idx !== 0; if (typeof scopeValue === 'object') { return (
{showHr &&
}
{/* Render recursively */}
); } // If it's a string, render a checkbox return (
{t(`accesstoken_scopes_desc.${scopeKey.replace(/:/g, '.')}`)}
); })} ); };