import React from 'react';
import {Container, Icon} from "semantic-ui-react";
import {Button, Header, Loader, Modal, Segment, Table} from "../Theme";
import {
APIButton,
HotspotToggle,
ThrottleToggle,
Toggle,
} from "../Common";
import {useDockerized} from "../../hooks/customHooks";
import {
getServices,
startService,
stopService,
restartService,
enableService,
disableService,
getServiceLogs,
getDisks,
getMounts,
getSmartStatus,
restartServices,
} from "../../api/controller";
import {toast} from "react-semantic-toasts-2";
import {RestartButton, ShutdownButton} from "./Settings";
// Service status color mapping
const statusColors = {
running: 'green',
stopped: 'red',
failed: 'red',
unknown: 'yellow',
};
function ServiceRow({service, onAction, dockerized}) {
const [loading, setLoading] = React.useState(false);
const [logsOpen, setLogsOpen] = React.useState(false);
const [logs, setLogs] = React.useState('');
const [logsLoading, setLogsLoading] = React.useState(false);
const handleAction = async (action, actionFn) => {
setLoading(true);
try {
await actionFn(service.name);
toast({
type: 'success',
title: `${action} ${service.name}`,
description: `Successfully ${action.toLowerCase()}ed ${service.name}`,
time: 3000,
});
if (onAction) onAction();
} catch (e) {
toast({
type: 'error',
title: `${action} Failed`,
description: e.message,
time: 5000,
});
} finally {
setLoading(false);
}
};
const handleViewLogs = async () => {
setLogsOpen(true);
setLogsLoading(true);
try {
const result = await getServiceLogs(service.name, 100);
setLogs(result.logs || 'No logs available');
} catch (e) {
setLogs(`Error fetching logs: ${e.message}`);
} finally {
setLogsLoading(false);
}
};
const isRunning = service.status === 'running';
const isStopped = service.status === 'stopped';
const statusColor = statusColors[service.status] || 'grey';
// Build view URL if service is viewable
let viewUrl = null;
if (service.viewable && service.port && isRunning) {
const protocol = service.use_https ? 'https' : 'http';
const host = window.location.hostname;
const path = service.view_path || '';
viewUrl = `${protocol}://${host}:${service.port}${path}`;
}
return (
{service.name}
{service.description && {service.description}
}
{service.port || '-'}
{service.status}
{isRunning ? (
{!dockerized && (
{
if (checked) {
await handleAction('Enable', enableService);
} else {
await handleAction('Disable', disableService);
}
}}
label={service.enabled ? 'Enabled' : 'Disabled'}
/>
)}
);
}
function ServicesSection() {
const [services, setServices] = React.useState([]);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState(null);
const [restarting, setRestarting] = React.useState(false);
const dockerized = useDockerized();
const handleRestartServices = async () => {
setRestarting(true);
try {
await restartServices();
toast({
type: 'success',
title: 'Services Restarting',
description: 'All WROLPi services are being restarted.',
time: 5000,
});
// Refresh service list after restart initiated
fetchServices();
} catch (e) {
toast({
type: 'error',
title: 'Restart Failed',
description: e.message,
time: 5000,
});
} finally {
setRestarting(false);
}
};
const fetchServices = async () => {
setLoading(true);
setError(null);
try {
const result = await getServices();
setServices(Array.isArray(result) ? result : []);
} catch (e) {
setError(e.message);
setServices([]);
} finally {
setLoading(false);
}
};
React.useEffect(() => {
fetchServices();
// Refresh every 10 seconds
const interval = setInterval(fetchServices, 10000);
return () => clearInterval(interval);
}, []);
if (loading && services.length === 0) {
return
;
}
if (error) {
return
Error loading services: {error}
Retry
;
}
return (
Service
Port
Status
Actions
{!dockerized && Boot}
{services.map(service => (
))}
{restarting ? 'Restarting...' : 'Restart All Services'}
{services.length === 0 && (
No services found. Services may not be available in this environment.
)}
);
}
// Helper to get health status color from SMART assessment
const getHealthColor = (assessment) => {
if (!assessment) return 'grey';
const upper = assessment.toUpperCase();
if (upper === 'PASS') return 'green';
if (upper === 'WARN') return 'yellow';
if (upper === 'FAIL') return 'red';
return 'grey';
};
// Helper to format power-on hours
const formatPowerOnHours = (hours) => {
if (hours === null || hours === undefined) return '-';
const days = Math.floor(hours / 24);
const years = Math.floor(days / 365);
if (years > 0) return `${hours.toLocaleString()} hrs (${years}y ${days % 365}d)`;
if (days > 0) return `${hours.toLocaleString()} hrs (${days}d)`;
return `${hours} hrs`;
};
function SmartDetailsModal({drive, open, onClose}) {
if (!drive) return null;
const healthColor = getHealthColor(drive.assessment);
return (
SMART Details: {drive.device}
Model
{drive.model || '-'}
Serial
{drive.serial || '-'}
Capacity
{drive.capacity || '-'}
Assessment
{drive.assessment || 'Unknown'}
Temperature
{drive.temperature !== null && drive.temperature !== undefined
? `${drive.temperature}°C`
: '-'}
Power-On Hours
{formatPowerOnHours(drive.power_on_hours)}
Reallocated Sectors
0 ? 'orange' : 'inherit'}}>
{drive.reallocated_sectors !== null && drive.reallocated_sectors !== undefined
? drive.reallocated_sectors
: '-'}
Pending Sectors
0 ? 'orange' : 'inherit'}}>
{drive.pending_sectors !== null && drive.pending_sectors !== undefined
? drive.pending_sectors
: '-'}
SMART Enabled
{drive.smart_enabled ? 'Yes' : 'No'}
Close
);
}
function DiskSection() {
const [disks, setDisks] = React.useState([]);
const [mounts, setMounts] = React.useState([]);
const [smartDrives, setSmartDrives] = React.useState([]);
const [smartAvailable, setSmartAvailable] = React.useState(true);
const [loading, setLoading] = React.useState(true);
const [error, setError] = React.useState(null);
const [selectedDrive, setSelectedDrive] = React.useState(null);
const [detailsOpen, setDetailsOpen] = React.useState(false);
const dockerized = useDockerized();
const fetchDiskInfo = async () => {
setLoading(true);
setError(null);
try {
const [disksResult, mountsResult, smartResult] = await Promise.allSettled([
getDisks(),
getMounts(),
getSmartStatus(),
]);
if (disksResult.status === 'fulfilled') {
setDisks(Array.isArray(disksResult.value) ? disksResult.value : []);
}
if (mountsResult.status === 'fulfilled') {
setMounts(Array.isArray(mountsResult.value) ? mountsResult.value : []);
}
if (smartResult.status === 'fulfilled' && smartResult.value) {
// Handle new API format: {available: bool, drives: [...]}
const smartData = smartResult.value;
setSmartAvailable(smartData.available !== false);
setSmartDrives(Array.isArray(smartData.drives) ? smartData.drives : []);
} else {
setSmartAvailable(false);
setSmartDrives([]);
}
} catch (e) {
setError(e.message);
} finally {
setLoading(false);
}
};
const handleShowDetails = (drive) => {
setSelectedDrive(drive);
setDetailsOpen(true);
};
React.useEffect(() => {
fetchDiskInfo();
}, []);
if (dockerized) {
return (
Disk management is not available in Docker environments.
);
}
if (loading) {
return
;
}
return (
{error && Some disk information unavailable: {error}
}
{mounts.length > 0 ? (
Device
Mount Point
Filesystem
{mounts.map((mount, idx) => (
{mount.device}
{mount.mount_point}
{mount.fstype}
))}
) : (
No mounts found.
)}
{!smartAvailable ? (
SMART monitoring not available (pySMART not installed or not supported)
) : smartDrives.length > 0 ? (
Device
Health
Temperature
Actions
{smartDrives.map((drive) => (
{drive.device}
{drive.assessment || 'Unknown'}
{drive.temperature !== null && drive.temperature !== undefined
? `${drive.temperature}°C`
: '-'}
handleShowDetails(drive)}
/>
))}
) : (
No SMART-capable drives detected.
)}
setDetailsOpen(false)}
/>
Refresh
);
}
function AdminControlsSection() {
const dockerized = useDockerized();
return (
{!dockerized && (
)}
);
}
export function ControllerPage() {
return (
);
}
export default ControllerPage;