import { Plus, RotateCcw, Server, Settings, Trash2 } from 'lucide-react'; import { useEffect, useState } from 'react'; import { Badge, Button, Card, CardHeader, Modal } from '../../components/ui'; import { useToast } from '../../components/toast'; import { PageTitle } from '../../layout/AppLayout'; import { cn } from '../../lib/utils'; import type { RuntimeResource } from '../../types/runtime'; import { LoadingModal, useMinimumLoading } from '../operations/operation-utils'; import { loadLauncherRuntimeResources, loadRuntimeWorkloadEnv, saveRuntimeWorkloadEnv } from '../launcher/launcher-api'; import { COMMON_APP_NAMESPACE, commonAppComponents, type CommonAppComponent } from './common-app-utils'; function statusTone(status?: string): 'green' | 'amber' | 'red' | 'neutral' { if (['Running', 'Bound', 'ClusterIP', 'LoadBalancer'].includes(status ?? '')) return 'green'; if (['Pending', 'ContainerCreating'].includes(status ?? '')) return 'amber'; if (['Error', 'CrashLoopBackOff', 'ImagePullBackOff', 'Failed'].includes(status ?? '')) return 'red'; return 'neutral'; } function apiKind(kind: string) { return kind.toLowerCase(); } function findResource(resources: RuntimeResource[], kind: string, name: string) { return resources.find((resource) => resource.kind === kind && resource.name === name); } function serviceResource(resources: RuntimeResource[], name?: string) { if (!name) return undefined; return resources.find((resource) => resource.kind === 'Service' && resource.name === name); } function relatedResources(resources: RuntimeResource[], component: CommonAppComponent) { const names = new Set([component.workloadName, component.serviceName].filter(Boolean)); return resources.filter((resource) => { if (names.has(resource.name)) return true; return resource.name.includes(component.workloadName); }); } type EnvVariable = { key: string; value: string; isSecret: boolean; editable: boolean; source: string; }; export function CommonAppComponentsPage() { const { toast } = useToast(); const [resources, setResources] = useState([]); const [loading, setLoading] = useState(true); const [envTarget, setEnvTarget] = useState(null); const [envVariables, setEnvVariables] = useState([]); const [envLoading, setEnvLoading] = useState(false); const [envSaving, setEnvSaving] = useState(false); const loadingVisible = useMinimumLoading(loading || envLoading); const refresh = () => { setLoading(true); loadLauncherRuntimeResources(COMMON_APP_NAMESPACE) .then((runtime) => setResources(runtime.resources ?? [])) .catch((error: Error) => { toast({ type: 'error', title: '공통앱 구성요소 조회 실패', message: error.message }); }) .finally(() => setLoading(false)); }; useEffect(() => { refresh(); }, []); const openEnv = (component: CommonAppComponent) => { setEnvTarget(component); setEnvVariables([]); setEnvLoading(true); loadRuntimeWorkloadEnv(COMMON_APP_NAMESPACE, apiKind(component.kind), component.workloadName) .then((result) => setEnvVariables(result.variables ?? [])) .catch((error: Error) => { toast({ type: 'error', title: '설정값 조회 실패', message: error.message }); }) .finally(() => setEnvLoading(false)); }; const updateEnvVariable = (index: number, patch: Partial>) => { setEnvVariables((prev) => prev.map((variable, variableIndex) => ( variableIndex === index ? { ...variable, ...patch } : variable ))); }; const removeEnvVariable = (index: number) => { setEnvVariables((prev) => prev.filter((_, variableIndex) => variableIndex !== index)); }; const saveEnv = () => { if (!envTarget) return; setEnvSaving(true); saveRuntimeWorkloadEnv( COMMON_APP_NAMESPACE, apiKind(envTarget.kind), envTarget.workloadName, envVariables .filter((variable) => variable.editable) .map((variable) => ({ key: variable.key.trim(), value: variable.value, isSecret: variable.isSecret })) .filter((variable) => variable.key) ) .then((result) => { setEnvVariables(result.variables ?? []); toast({ type: 'success', title: '설정값 저장', message: `${envTarget.name} 설정값을 반영했습니다.` }); refresh(); }) .catch((error: Error) => { toast({ type: 'error', title: '설정값 저장 실패', message: error.message }); }) .finally(() => setEnvSaving(false)); }; return ( <> 새로고침 } />
{commonAppComponents.map((component) => { const workload = findResource(resources, component.kind, component.workloadName); const service = serviceResource(resources, component.serviceName); const details = relatedResources(resources, component); const status = workload?.status ?? 'NotFound'; return ( {status}} />

{component.repository}

{component.kind}/{component.workloadName}

Ready

{workload?.ready ?? '-'}

Service

{service ? `${service.status} · ${service.detail}` : '-'}

Image

{workload?.detail ?? '-'}

{details.length === 0 ? ( ) : null} {details.map((resource) => ( ))}
Kind Name Status Ready Age Detail
관련 리소스가 없습니다.
{resource.kind} {resource.name} {resource.status} {resource.ready} {resource.age} {resource.detail || '-'}
); })}
setEnvTarget(null)} className="!max-w-4xl" footer={ <> } >
{envLoading ? ( ) : null} {!envLoading && envVariables.length === 0 ? ( ) : null} {!envLoading && envVariables.map((variable, index) => ( ))}
Key Value Secret Source
설정값을 불러오는 중입니다.
등록된 설정값이 없습니다.
updateEnvVariable(index, { key: event.target.value })} /> updateEnvVariable(index, { value: event.target.value })} /> {variable.source}
); }