mirror of
https://github.com/Termix-SSH/Termix.git
synced 2026-10-30 15:15:24 +00:00
feat: convert wake-on-lan into a plugin (B20)
This commit is contained in:
1 parent
d4ec444558
commit
2ac7779eda
50 files changed
+17844
-199
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { toast } from "sonner";
|
||||
import type { PluginApiClient, TermixApp } from "@termix/plugin-sdk/frontend";
|
||||
|
||||
export function hasMacAddress(host: Record<string, unknown>): boolean {
|
||||
const settings = host.pluginSettings as
|
||||
Record<string, Record<string, unknown>> | undefined;
|
||||
const macAddress = settings?.["wake-on-lan"]?.macAddress;
|
||||
return typeof macAddress === "string" && macAddress.trim().length > 0;
|
||||
}
|
||||
|
||||
function errorMessage(error: unknown, fallback: string): string {
|
||||
const message = (error as { response?: { data?: { error?: string } } })
|
||||
?.response?.data?.error;
|
||||
return message ?? fallback;
|
||||
}
|
||||
|
||||
export function wakeHost(
|
||||
api: PluginApiClient,
|
||||
t: TermixApp["t"],
|
||||
hostId: string,
|
||||
): void {
|
||||
void api
|
||||
.post(`/host/${hostId}/wake`)
|
||||
.then(() => toast.success(t("hostAction.sent")))
|
||||
.catch((error: unknown) => {
|
||||
toast.error(errorMessage(error, t("hostAction.failed")));
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { Zap } from "lucide-react";
|
||||
import type { TermixApp } from "@termix/plugin-sdk/frontend";
|
||||
import { hasMacAddress, wakeHost } from "./host-action.js";
|
||||
|
||||
export function activate(app: TermixApp): void {
|
||||
app.registerHostAction({
|
||||
id: "wake-on-lan",
|
||||
titleKey: "hostAction.title",
|
||||
icon: Zap,
|
||||
kind: "open",
|
||||
order: 50,
|
||||
when: (host) => hasMacAddress(host as Record<string, unknown>),
|
||||
run: (host) => wakeHost(app.api, app.t, String(host.id)),
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user