import React, { useState, useCallback, useEffect, useMemo, useRef } from 'react'; import { Div } from '../basic/Div'; import { Input } from '../basic/Input'; import { Span } from '../basic/Span'; import { Label } from '../basic/Label'; import { Icon } from '../basic/Icon'; import { IconName } from '../basic/IconTypes'; import { Accordion } from './Accordion'; import { ExtensionBadge, ExtensionInfo } from './ExtensionBadge'; // G7Core 전역 객체에서 createChangeEvent 가져오기 const G7Core = (window as any).G7Core; // useControllableState 훅 타입 정의 const useControllableState = G7Core?.useControllableState as | (( controlledValue: T | undefined, defaultValue: T, onChange?: (value: T) => void, options?: { isEqual?: (a: T, b: T) => boolean } ) => [T, (value: T | ((prev: T) => T)) => void]) | undefined; /** * 권한 값 인터페이스 (id + scope_type) */ export interface PermissionValue { id: number; scope_type: string | null; } /** * 스코프 옵션 인터페이스 */ export interface ScopeOption { value: string | null; label: string; } // G7Core.t() 번역 함수 참조 const t = (key: string, params?: Record) => (window as any).G7Core?.t?.(key, params) ?? key; /** * 스코프 옵션별 설명 다국어 키 매핑 */ const SCOPE_DESCRIPTION_KEYS: Record = { '': 'common.scope_type_all_desc', self: 'common.scope_type_self_desc', role: 'common.scope_type_role_desc', }; /** * 스코프 드롭다운 컴포넌트 * * 네이티브 ) => { e.stopPropagation(); handleLeafChange(permission.id, e.target.checked); }} disabled={disabled} className="w-4 h-4 mt-0.5 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600" />
{permission.name} {showScopeDropdown && ( handleScopeTypeChange(permission.id, val)} disabled={disabled} /> )}
{permission.description && ( {permission.description} )}
); }; /** * 카테고리(2depth) 렌더링 */ const renderCategory = (category: PermissionNode, moduleNode: PermissionNode) => { const isFullySelected = isNodeFullySelected(category); const isPartiallySelected = isNodePartiallySelected(category); const selectedCount = getSelectedCount(category); const assignableChildren = category.children.filter((p) => p.is_assignable); // 하위에 할당 가능한 권한이 없으면 렌더링하지 않음 if (assignableChildren.length === 0 && category.children.length === 0) { return null; } // 하위에 카테고리가 더 있는 경우 재귀 렌더링 const hasSubCategories = category.children.some((child) => !child.is_assignable); const gridColsClass = desktopColumns === 1 ? 'grid-cols-1' : desktopColumns === 3 ? 'grid-cols-1 md:grid-cols-3' : 'grid-cols-1 md:grid-cols-2'; return ( {/* Trigger */}
{ if (el) el.indeterminate = isPartiallySelected; }} onChange={(e: React.ChangeEvent) => { e.stopPropagation(); handleGroupChange(category, e.target.checked); }} onClick={(e: React.MouseEvent) => e.stopPropagation()} onMouseDown={(e: React.MouseEvent) => e.stopPropagation()} disabled={disabled} className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600" /> {category.name} {selectedCount}/{category.leaf_count}
{/* Content */}
{hasSubCategories ? // 하위 카테고리가 있으면 재귀 렌더링 category.children.map((child) => child.is_assignable ? renderLeafPermission(child) : renderCategory(child, moduleNode) ) : // 리프 권한만 있으면 그리드로 렌더링 assignableChildren.map((permission) => renderLeafPermission(permission))}
); }; /** * 확장 타입 판별 (코어, 모듈, 플러그인) * API 응답의 extension_type 필드를 우선 사용하고, 없으면 identifier 기반 폴백 */ const getExtensionType = ( node: PermissionNode ): 'core' | 'module' | 'plugin' => { // API에서 extension_type을 직접 제공하는 경우 우선 사용 if (node.extension_type === 'core') return 'core'; if (node.extension_type === 'module') return 'module'; if (node.extension_type === 'plugin') return 'plugin'; // 폴백: identifier 기반 판별 (하위 호환) const identifier = node.extension_identifier ?? node.module ?? ''; if (identifier === 'core' || !identifier) return 'core'; const isPlugin = installedPlugins?.some( (p) => p.identifier === identifier ); return isPlugin ? 'plugin' : 'module'; }; /** * 모듈(1depth) 렌더링 */ const renderModule = (moduleNode: PermissionNode) => { const isFullySelected = isNodeFullySelected(moduleNode); const isPartiallySelected = isNodePartiallySelected(moduleNode); const selectedCount = getSelectedCount(moduleNode); const extensionType = getExtensionType(moduleNode); return ( {/* Trigger */}
{ if (el) el.indeterminate = isPartiallySelected; }} onChange={(e: React.ChangeEvent) => { e.stopPropagation(); handleGroupChange(moduleNode, e.target.checked); }} onClick={(e: React.MouseEvent) => e.stopPropagation()} onMouseDown={(e: React.MouseEvent) => e.stopPropagation()} disabled={disabled} className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:bg-gray-700 dark:border-gray-600" /> {moduleNode.name} {extensionType !== 'core' && ( )} {selectedCount}/{moduleNode.leaf_count}
{/* Content */}
{moduleNode.children.map((category) => category.is_assignable ? renderLeafPermission(category) : renderCategory(category, moduleNode) )}
); }; return (
{data.map((moduleNode) => renderModule(moduleNode))}
); };