'use client'; import { useState, useRef, useEffect } from 'react'; import { useQuery } from '@tanstack/react-query'; import api from '@/lib/api'; import { FileText, ChevronDown, Monitor, Hammer, Pin } from 'lucide-react'; const statusColors: Record = { running: 'badge-green', pending: 'badge-yellow', building: 'badge-blue', deploying: 'badge-blue', failed: 'badge-red', build_failed: 'badge-red', cancelled: 'badge-gray', stopped: 'badge-gray', }; export function WorkloadLogsPanel({ appId, showBuildLogs = true, isRunning = false, isStopped = false, emptyPodMessage, }: { appId: string; showBuildLogs?: boolean; isRunning?: boolean; isStopped?: boolean; emptyPodMessage?: string; }) { const [showLogs, setShowLogs] = useState(false); const [logTab, setLogTab] = useState<'pod' | 'build'>('pod'); const logsEndRef = useRef(null); useEffect(() => { if (!showBuildLogs && logTab === 'build') { setLogTab('pod'); } }, [showBuildLogs, logTab]); const { data: logsData } = useQuery<{ logs: string }>({ queryKey: ['logs', appId], queryFn: () => api.get(`/deployments/applications/${appId}/logs`).then((r) => r.data), enabled: showLogs && logTab === 'pod', refetchInterval: showLogs && logTab === 'pod' ? 3000 : false, }); const { data: buildLogsData } = useQuery<{ buildLog: string | null; status: string; version: string | null; }>({ queryKey: ['build-logs', appId], queryFn: () => api.get(`/deployments/applications/${appId}/build-logs`).then((r) => r.data), enabled: showBuildLogs && showLogs && logTab === 'build', refetchInterval: showBuildLogs && showLogs && logTab === 'build' ? 5000 : false, }); const podPlaceholder = emptyPodMessage || (isRunning ? 'Loading logs...' : isStopped ? 'Service is stopped. Start it to see logs.' : 'Waiting for workload pods to be ready...'); return (

Logs

{showLogs && logTab === 'pod' && ( Live (every 3s) )} {showBuildLogs && showLogs && logTab === 'build' && ( Auto-refresh (every 5s) )}
{showLogs && (
{showBuildLogs ? (
) : (

Workload pod output (no image build for this service).

)} {logTab === 'pod' && (
              {logsData?.logs || podPlaceholder}
            
)} {showBuildLogs && logTab === 'build' && (
{buildLogsData?.version && (
{buildLogsData.version} {buildLogsData.status}
)}
                {buildLogsData?.buildLog ||
                  (buildLogsData?.status === 'building'
                    ? 'Build in progress... Logs will appear when complete.'
                    : buildLogsData?.status === 'pending'
                      ? 'Build is pending...'
                      : buildLogsData?.status === 'no_deployment'
                        ? 'No deployments yet. Deploy your app to see build logs.'
                        : 'No build logs available for this deployment.')}
              
)}
)}
); }