import { useEffect, useState } from "react"; import { StorageForecastBadge, synologyKey, useStoragePools } from "../components/StorageForecast"; import { Link } from "react-router-dom"; import { api, type CurrentUser, type IntegrationSummary, type SynologyStorageResponse, type SynologySystemInfo, type SynologyVolume, type SynologyDisk, } from "../api/client"; import { useSortable } from "../hooks/useSortable"; import SortableTh from "../components/SortableTh"; import { downloadCsv } from "../utils/csv"; import IntegrationPageTitle from "../components/IntegrationPageTitle"; import { ErrorNotice } from "../components/Notice"; import { EmptyRow } from "../components/EmptyState"; function healthBadge(status: string) { return status === "normal" ? ( Normal ) : ( {status} ); } function formatBytes(bytes: number | null): string { if (bytes === null) return "—"; const units = ["B", "KB", "MB", "GB", "TB", "PB"]; let value = bytes; let unit = 0; while (value >= 1024 && unit < units.length - 1) { value /= 1024; unit++; } return `${value.toFixed(1)} ${units[unit]}`; } // DSM reports uptime as the raw output of the Linux `uptime` command (e.g. // "20:13:21 up 5 days, 15:02, 0 users, load average: ..."); pull out the // day/hour/minute portion and fall back to the raw string if the format // doesn't match what we expect. function formatUptime(raw: string | null): string { if (!raw) return "—"; const match = raw.match(/up\s+(?:(\d+)\s+days?,\s+)?(?:(\d+):(\d+)|(\d+)\s*min)/); if (!match) return raw; const days = match[1] ? Number(match[1]) : 0; const hours = match[2] ? Number(match[2]) : 0; const minutes = match[3] ? Number(match[3]) : match[4] ? Number(match[4]) : 0; const parts: string[] = []; if (days) parts.push(`${days}d`); if (days || hours) parts.push(`${hours}h`); parts.push(`${minutes}m`); return parts.join(" "); } export default function Synology({ user: _user }: { user: CurrentUser }) { const [integrations, setIntegrations] = useState(null); const [selectedId, setSelectedId] = useState(null); const [error, setError] = useState(null); const [data, setData] = useState(null); const [loading, setLoading] = useState(false); const [system, setSystem] = useState(null); const [systemError, setSystemError] = useState(null); const [loadingSystem, setLoadingSystem] = useState(false); useEffect(() => { api.integrations .list() .then((res) => { const synologyIntegrations = res.integrations.filter((i) => i.type === "synology"); setIntegrations(synologyIntegrations); if (!selectedId && synologyIntegrations.length > 0) setSelectedId(synologyIntegrations[0].id); }) .catch((err) => setError(err instanceof Error ? err.message : String(err))); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const selected = integrations?.find((i) => i.id === selectedId) ?? null; const { pools, forecastDays } = useStoragePools(); function loadStorage(id: number) { setLoading(true); setError(null); api.integrations.synology .storage(id) .then((res) => setData(res)) .catch((err) => setError(err instanceof Error ? err.message : String(err))) .finally(() => setLoading(false)); } function loadSystem(id: number) { setLoadingSystem(true); setSystemError(null); api.integrations.synology .system(id) .then((res) => setSystem(res)) .catch((err) => setSystemError(err instanceof Error ? err.message : String(err))) .finally(() => setLoadingSystem(false)); } useEffect(() => { if (selected?.enabled) { loadStorage(selected.id); loadSystem(selected.id); } else { setData(null); setSystem(null); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedId]); const { sorted: sortedVolumes, sortKey: volumeSortKey, sortDir: volumeSortDir, requestSort: requestVolumeSort } = useSortable( data?.volumes, ); const { sorted: sortedDisks, sortKey: diskSortKey, sortDir: diskSortDir, requestSort: requestDiskSort } = useSortable(data?.disks); function exportVolumesCsv() { if (!sortedVolumes) return; downloadCsv( "synology-volumes.csv", ["Volume", "Type", "Used", "Total", "Status"], sortedVolumes.map((v) => [v.id, v.deviceType, formatBytes(v.sizeUsed), formatBytes(v.sizeTotal), v.status]), ); } function exportDisksCsv() { if (!sortedDisks) return; downloadCsv( "synology-disks.csv", ["Disk", "Device", "Temp", "S.M.A.R.T.", "Status"], sortedDisks.map((d) => [d.name, d.device, d.temp ?? "", d.smartStatus, d.status]), ); } return ( <> {integrations?.length === 0 ? (
No Synology integration configured yet. Add one under Integrations.
) : ( <> {integrations && integrations.length > 1 && (
)} {selected && !selected.enabled ? (
"{selected.name}" is disabled. Enable it under Integrations → Manage integrations to see its storage.
) : (

System

{systemError ? (
{systemError}
) : (
Hostname
{system?.hostname ?? "—"}
Model
{system?.model ?? "—"}
Uptime
{formatUptime(system?.uptime ?? null)}
IP addresses
{system?.ipAddresses.length ? system.ipAddresses.join(", ") : "—"}
CPU
{system?.cpu.loadPercent !== null && system?.cpu.loadPercent !== undefined ? `${system.cpu.loadPercent.toFixed(0)}%` : "—"} {system?.cpu.cores ? ( {" "} ({system.cpu.cores} cores {system.cpu.clockSpeedMHz ? ` @ ${(system.cpu.clockSpeedMHz / 1000).toFixed(1)} GHz` : ""}) ) : null}
Memory
{formatBytes(system?.memory.usedBytes ?? null)} / {formatBytes(system?.memory.totalBytes ?? null)}
Serial
{system?.serial ?? "—"}
Firmware
{system?.firmwareVersion ?? "—"}
)}

Volumes

label="Volume" sortKeyName="id" activeKey={volumeSortKey} direction={volumeSortDir} onSort={requestVolumeSort} /> label="Type" sortKeyName="deviceType" activeKey={volumeSortKey} direction={volumeSortDir} onSort={requestVolumeSort} /> label="Used" sortKeyName="sizeUsed" activeKey={volumeSortKey} direction={volumeSortDir} onSort={requestVolumeSort} /> label="Status" sortKeyName="status" activeKey={volumeSortKey} direction={volumeSortDir} onSort={requestVolumeSort} /> {sortedVolumes?.map((v) => ( ))} {sortedVolumes?.length === 0 && ( )}
{v.id} {v.deviceType} {formatBytes(v.sizeUsed)} / {formatBytes(v.sizeTotal)}
p.integrationId === selected?.id && p.key === synologyKey(v.id))} forecastDays={forecastDays} />
{healthBadge(v.status)}

Disks

label="Disk" sortKeyName="name" activeKey={diskSortKey} direction={diskSortDir} onSort={requestDiskSort} /> label="Device" sortKeyName="device" activeKey={diskSortKey} direction={diskSortDir} onSort={requestDiskSort} /> label="Temp" sortKeyName="temp" activeKey={diskSortKey} direction={diskSortDir} onSort={requestDiskSort} /> label="S.M.A.R.T." sortKeyName="smartStatus" activeKey={diskSortKey} direction={diskSortDir} onSort={requestDiskSort} /> label="Status" sortKeyName="status" activeKey={diskSortKey} direction={diskSortDir} onSort={requestDiskSort} /> {sortedDisks?.map((d) => ( ))} {sortedDisks?.length === 0 && ( )}
{d.name} {d.device} {d.temp !== null ? `${d.temp}°C` : "—"} {healthBadge(d.smartStatus)} {healthBadge(d.status)} {d.exceedBadSectorThreshold && Bad sectors} {d.belowRemainLifeThreshold && Low life}
)} )} ); }