mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-29 22:55:24 +00:00
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import type { Host, HostFolder } from "@/types/ui-types";
|
|
import type { StatusValue } from "@/lib/server-status-store";
|
|
import { isFolder } from "./tree/visible-rows";
|
|
|
|
/**
|
|
* Copies each host's live status onto the tree so sorting, filtering and
|
|
* grouping by status read the same value as the row's dot. A host with
|
|
* status checks off gets no status.
|
|
*/
|
|
export function withLiveStatus(
|
|
folder: HostFolder,
|
|
getStatus: (hostId: number) => StatusValue,
|
|
): HostFolder {
|
|
return {
|
|
...folder,
|
|
children: folder.children.map((child) => {
|
|
if (isFolder(child)) return withLiveStatus(child, getStatus);
|
|
const host = child as Host;
|
|
const status =
|
|
host.statusCheckEnabled === false
|
|
? undefined
|
|
: getStatus(Number(host.id));
|
|
if (host.status === status) return host;
|
|
return { ...host, status, online: status === "online" };
|
|
}),
|
|
};
|
|
}
|
|
|
|
/** The flat-list version of withLiveStatus. */
|
|
export function withLiveHostStatus(
|
|
hosts: Host[],
|
|
getStatus: (hostId: number) => StatusValue,
|
|
): Host[] {
|
|
return withLiveStatus({ name: "", children: hosts }, getStatus)
|
|
.children as Host[];
|
|
}
|