50eb76a10d
- Add shared task_runner.run_saved_task helper used by routers/tasks.py and widgets/sources.py SshTaskWidgetSource. - Saved tasks now target ssh_tasks service instances via default_service_id; the legacy default_machine_id and saved_task_runs are removed. - Actions page lists ssh_tasks services for default and run-time selection. - Update types, API client, hooks, tests, docs, and changelog. Backend tests: 222 passed. Frontend lint/build/test: clean (71 passed).
169 lines
4.0 KiB
TypeScript
169 lines
4.0 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import {
|
|
deleteMonitoringMachine,
|
|
deleteSSHKey,
|
|
fetchMonitoringSettings,
|
|
fetchSSHKeys,
|
|
fetchSavedTaskRuns,
|
|
fetchSavedTasks,
|
|
generateSSHKey,
|
|
resetLocalDatabase,
|
|
saveMonitoringMachine,
|
|
saveSSHKey,
|
|
saveTask,
|
|
deleteTask,
|
|
runTask,
|
|
testMonitoringMachineSSH,
|
|
} from "../api/client";
|
|
import type {
|
|
MonitoringMachineInput,
|
|
ResetLocalDatabaseInput,
|
|
SavedTaskInput,
|
|
SSHKeyInput,
|
|
SSHValidationResult,
|
|
} from "../types";
|
|
|
|
export function useMonitoringSettings() {
|
|
return useQuery({
|
|
queryKey: ["settings", "monitoring-machines"],
|
|
queryFn: fetchMonitoringSettings,
|
|
refetchInterval: 30_000,
|
|
});
|
|
}
|
|
|
|
export function useSSHKeys() {
|
|
return useQuery({
|
|
queryKey: ["settings", "ssh-keys"],
|
|
queryFn: fetchSSHKeys,
|
|
refetchInterval: 30_000,
|
|
});
|
|
}
|
|
|
|
export function useGenerateSSHKey() {
|
|
return useMutation({
|
|
mutationFn: (payload: {
|
|
name: string;
|
|
passphrase: string;
|
|
notes: string;
|
|
bits?: number;
|
|
}) => generateSSHKey(payload),
|
|
});
|
|
}
|
|
|
|
export function useSaveSSHKey() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (key: SSHKeyInput) => saveSSHKey(key),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["settings"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteSSHKey() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (keyId: string) => deleteSSHKey(keyId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["settings"] });
|
|
queryClient.invalidateQueries({ queryKey: ["monitoring"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTasks() {
|
|
return useQuery({
|
|
queryKey: ["tasks"],
|
|
queryFn: fetchSavedTasks,
|
|
refetchInterval: 30_000,
|
|
});
|
|
}
|
|
|
|
export function useTaskRuns(taskId?: string) {
|
|
return useQuery({
|
|
queryKey: ["tasks", taskId ?? "none", "runs"],
|
|
queryFn: () => fetchSavedTaskRuns(taskId ?? ""),
|
|
enabled: Boolean(taskId),
|
|
refetchInterval: 30_000,
|
|
});
|
|
}
|
|
|
|
export function useSaveTask() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (task: SavedTaskInput) => saveTask(task),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["tasks"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useDeleteTask() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (taskId: string) => deleteTask(taskId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["tasks"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useRunTask() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: ({
|
|
taskId,
|
|
serviceId,
|
|
}: {
|
|
taskId: string;
|
|
serviceId?: string;
|
|
}) => runTask(taskId, serviceId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["tasks"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useSaveMonitoringMachine() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (machine: MonitoringMachineInput) =>
|
|
saveMonitoringMachine(machine),
|
|
onSettled: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["settings"] });
|
|
queryClient.invalidateQueries({ queryKey: ["monitoring"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useTestMonitoringMachineSSH() {
|
|
return useMutation<SSHValidationResult, Error, MonitoringMachineInput>({
|
|
mutationFn: testMonitoringMachineSSH,
|
|
});
|
|
}
|
|
|
|
export function useDeleteMonitoringMachine() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (machineId: string) => deleteMonitoringMachine(machineId),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["settings"] });
|
|
queryClient.invalidateQueries({ queryKey: ["monitoring"] });
|
|
},
|
|
});
|
|
}
|
|
|
|
export function useResetLocalDatabase() {
|
|
const queryClient = useQueryClient();
|
|
return useMutation({
|
|
mutationFn: (payload: ResetLocalDatabaseInput) =>
|
|
resetLocalDatabase(payload),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({ queryKey: ["settings"] });
|
|
queryClient.invalidateQueries({ queryKey: ["monitoring"] });
|
|
queryClient.invalidateQueries({ queryKey: ["media"] });
|
|
queryClient.invalidateQueries({ queryKey: ["dashboard"] });
|
|
},
|
|
});
|
|
}
|