rspace-online/modules/rtime/applets.ts

43 lines
1.5 KiB
TypeScript

/**
* rTime applet definitions — Commitment Meter.
*/
import type { AppletDefinition, AppletLiveData } from "../../shared/applet-types";
const commitmentMeter: AppletDefinition = {
id: "commitment-meter",
label: "Commitment Meter",
icon: "⏳",
accentColor: "#7c3aed",
ports: [
{ name: "pool-in", type: "json", direction: "input" },
{ name: "committed-out", type: "number", direction: "output" },
],
renderCompact(data: AppletLiveData): string {
const { snapshot } = data;
const committed = (snapshot.committed as number) || 0;
const capacity = (snapshot.capacity as number) || 1;
const pct = Math.min(100, Math.round((committed / capacity) * 100));
const label = (snapshot.poolName as string) || "Pool";
return `
<div style="text-align:center">
<div style="font-size:13px;font-weight:600;margin-bottom:6px">${label}</div>
<div style="font-size:24px;font-weight:700;color:#e2e8f0">${pct}%</div>
<div style="font-size:10px;color:#94a3b8;margin:4px 0 8px">${committed}/${capacity} hrs</div>
<div style="background:#334155;border-radius:3px;height:6px;overflow:hidden">
<div style="background:#7c3aed;width:${pct}%;height:100%;border-radius:3px;transition:width 0.3s"></div>
</div>
</div>
`;
},
onInputReceived(portName, value, ctx) {
if (portName === "pool-in" && value && typeof value === "object") {
const pool = value as Record<string, unknown>;
ctx.emitOutput("committed-out", Number(pool.committed) || 0);
}
},
};
export const timeApplets: AppletDefinition[] = [commitmentMeter];