173 lines
4.0 KiB
TypeScript
173 lines
4.0 KiB
TypeScript
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
import * as deviceCommService from "@/services/device-comm.service";
|
|
import type { DeviceHealthCheck } from "@/types/device";
|
|
|
|
const DEVICE_COMM_QUERY_KEYS = {
|
|
all: ["device-comm"] as const,
|
|
allDevices: () => [...DEVICE_COMM_QUERY_KEYS.all, "all"] as const,
|
|
roomList: () => [...DEVICE_COMM_QUERY_KEYS.all, "rooms"] as const,
|
|
devicesInRoom: (roomName: string) =>
|
|
[...DEVICE_COMM_QUERY_KEYS.all, "room", roomName] as const,
|
|
clientFolderStatus: (roomName: string) =>
|
|
[...DEVICE_COMM_QUERY_KEYS.all, "folder-status", roomName] as const,
|
|
};
|
|
|
|
/**
|
|
* Hook để lấy tất cả thiết bị
|
|
*/
|
|
export function useGetAllDevices(enabled = true) {
|
|
return useQuery({
|
|
queryKey: DEVICE_COMM_QUERY_KEYS.allDevices(),
|
|
queryFn: () => deviceCommService.getAllDevices(),
|
|
enabled,
|
|
staleTime: 60 * 1000,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để lấy danh sách phòng
|
|
*/
|
|
export function useGetRoomList(enabled = true) {
|
|
return useQuery({
|
|
queryKey: DEVICE_COMM_QUERY_KEYS.roomList(),
|
|
queryFn: () => deviceCommService.getRoomList(),
|
|
enabled,
|
|
staleTime: 5 * 60 * 1000, // 5 minutes
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để lấy danh sách thiết bị trong phòng
|
|
*/
|
|
export function useGetDeviceFromRoom(roomName?: string, enabled = true) {
|
|
return useQuery<DeviceHealthCheck[]>({
|
|
queryKey: roomName ? DEVICE_COMM_QUERY_KEYS.devicesInRoom(roomName) : ["disabled"],
|
|
queryFn: () =>
|
|
roomName ? deviceCommService.getDeviceFromRoom(roomName) : Promise.reject("No room"),
|
|
enabled: enabled && !!roomName,
|
|
staleTime: 30 * 1000, // 30 seconds
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để tải file
|
|
*/
|
|
export function useDownloadFiles() {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
roomName,
|
|
data,
|
|
}: {
|
|
roomName: string;
|
|
data: any;
|
|
}) => deviceCommService.downloadFiles(roomName, data),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để cài đặt MSI
|
|
*/
|
|
export function useInstallMsi() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
roomName,
|
|
data,
|
|
}: {
|
|
roomName: string;
|
|
data: any;
|
|
}) => deviceCommService.installMsi(roomName, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: DEVICE_COMM_QUERY_KEYS.all,
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để cập nhật agent
|
|
*/
|
|
export function useUpdateAgent() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: ({
|
|
roomName,
|
|
data,
|
|
}: {
|
|
roomName: string;
|
|
data: any;
|
|
}) => deviceCommService.updateAgent(roomName, data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: DEVICE_COMM_QUERY_KEYS.all,
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để cập nhật blacklist
|
|
*/
|
|
export function useUpdateDeviceBlacklist() {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
roomName,
|
|
data,
|
|
}: {
|
|
roomName: string;
|
|
data: any;
|
|
}) => deviceCommService.updateBlacklist(roomName, data),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để gửi lệnh shell
|
|
*/
|
|
export function useSendCommand() {
|
|
return useMutation({
|
|
mutationFn: ({
|
|
roomName,
|
|
data,
|
|
}: {
|
|
roomName: string;
|
|
data: any;
|
|
}) => deviceCommService.sendCommand(roomName, data),
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để thay đổi phòng của thiết bị
|
|
*/
|
|
export function useChangeDeviceRoom() {
|
|
const queryClient = useQueryClient();
|
|
|
|
return useMutation({
|
|
mutationFn: (data: any) => deviceCommService.changeDeviceRoom(data),
|
|
onSuccess: () => {
|
|
queryClient.invalidateQueries({
|
|
queryKey: DEVICE_COMM_QUERY_KEYS.all,
|
|
});
|
|
},
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Hook để lấy trạng thái folder client
|
|
*/
|
|
export function useGetClientFolderStatus(roomName?: string, enabled = true) {
|
|
return useQuery({
|
|
queryKey: roomName
|
|
? DEVICE_COMM_QUERY_KEYS.clientFolderStatus(roomName)
|
|
: ["disabled"],
|
|
queryFn: () =>
|
|
roomName
|
|
? deviceCommService.getClientFolderStatus(roomName)
|
|
: Promise.reject("No room"),
|
|
enabled: enabled && !!roomName,
|
|
staleTime: 30 * 1000,
|
|
});
|
|
}
|