2025-09-10 09:59:17 +07:00
|
|
|
import { type ColumnDef } from "@tanstack/react-table";
|
|
|
|
|
import {
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardDescription,
|
|
|
|
|
CardFooter,
|
|
|
|
|
CardHeader,
|
|
|
|
|
CardTitle,
|
|
|
|
|
} from "@/components/ui/card";
|
2025-11-19 14:55:14 +07:00
|
|
|
import { FileText, Building2 } from "lucide-react";
|
|
|
|
|
import { FormDialog } from "@/components/dialogs/form-dialog";
|
|
|
|
|
import { VersionTable } from "@/components/tables/version-table";
|
|
|
|
|
import { RequestUpdateMenu } from "@/components/menu/request-update-menu";
|
2025-09-24 16:13:57 +07:00
|
|
|
import type { AxiosProgressEvent } from "axios";
|
2025-09-26 17:56:55 +07:00
|
|
|
import { useState } from "react";
|
2025-11-19 14:55:14 +07:00
|
|
|
import { SelectDialog } from "@/components/dialogs/select-dialog";
|
|
|
|
|
import { DeviceSearchDialog } from "@/components/bars/device-searchbar";
|
|
|
|
|
import { UploadVersionForm } from "@/components/forms/upload-file-form";
|
|
|
|
|
import type { Room } from "@/types/room";
|
|
|
|
|
import { mapRoomsToSelectItems } from "@/helpers/mapRoomToSelectItems";
|
|
|
|
|
import { fetchDevicesFromRoom } from "@/services/device.service";
|
2025-09-10 09:59:17 +07:00
|
|
|
|
|
|
|
|
interface AppManagerTemplateProps<TData> {
|
|
|
|
|
title: string;
|
|
|
|
|
description: string;
|
|
|
|
|
data: TData[];
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
columns: ColumnDef<TData, any>[];
|
2025-09-26 17:56:55 +07:00
|
|
|
onUpload: (
|
|
|
|
|
fd: FormData,
|
|
|
|
|
config?: { onUploadProgress?: (e: AxiosProgressEvent) => void }
|
|
|
|
|
) => Promise<void>;
|
2025-10-31 16:52:56 +07:00
|
|
|
onUpdate?: (targetNames: string[]) => Promise<void> | void;
|
2025-09-10 09:59:17 +07:00
|
|
|
updateLoading?: boolean;
|
2025-11-26 13:16:32 +07:00
|
|
|
onDownload?: (targetNames: string[]) => Promise<void> | void;
|
|
|
|
|
downloadLoading?: boolean;
|
2025-09-10 09:59:17 +07:00
|
|
|
onTableInit?: (table: any) => void;
|
2025-11-19 14:55:14 +07:00
|
|
|
rooms?: Room[];
|
2025-10-31 16:52:56 +07:00
|
|
|
devices?: string[];
|
2025-09-10 09:59:17 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function AppManagerTemplate<TData>({
|
|
|
|
|
title,
|
|
|
|
|
description,
|
|
|
|
|
data,
|
|
|
|
|
isLoading,
|
|
|
|
|
columns,
|
|
|
|
|
onUpload,
|
|
|
|
|
onUpdate,
|
|
|
|
|
updateLoading,
|
2025-11-26 13:16:32 +07:00
|
|
|
onDownload,
|
|
|
|
|
downloadLoading,
|
2025-09-10 09:59:17 +07:00
|
|
|
onTableInit,
|
2025-10-31 16:52:56 +07:00
|
|
|
rooms = [],
|
|
|
|
|
devices = [],
|
2025-09-10 09:59:17 +07:00
|
|
|
}: AppManagerTemplateProps<TData>) {
|
2025-09-26 17:56:55 +07:00
|
|
|
const [dialogOpen, setDialogOpen] = useState(false);
|
2025-11-26 13:16:32 +07:00
|
|
|
const [dialogType, setDialogType] = useState<"room" | "device" | "download-room" | "download-device" | null>(null);
|
2025-10-31 16:52:56 +07:00
|
|
|
|
|
|
|
|
const openRoomDialog = () => {
|
|
|
|
|
if (rooms.length > 0 && onUpdate) {
|
|
|
|
|
setDialogType("room");
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openDeviceDialog = () => {
|
2025-11-19 14:55:14 +07:00
|
|
|
if (onUpdate) {
|
2025-10-31 16:52:56 +07:00
|
|
|
setDialogType("device");
|
2025-09-26 17:56:55 +07:00
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-10-31 16:52:56 +07:00
|
|
|
|
2025-11-26 13:16:32 +07:00
|
|
|
const openDownloadRoomDialog = () => {
|
|
|
|
|
if (rooms.length > 0 && onDownload) {
|
|
|
|
|
setDialogType("download-room");
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const openDownloadDeviceDialog = () => {
|
|
|
|
|
if (onDownload) {
|
|
|
|
|
setDialogType("download-device");
|
|
|
|
|
setDialogOpen(true);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-10-31 16:52:56 +07:00
|
|
|
const handleUpdateAll = async () => {
|
|
|
|
|
if (!onUpdate) return;
|
2025-11-26 13:16:32 +07:00
|
|
|
try {
|
|
|
|
|
const roomIds = rooms.map((room) =>
|
|
|
|
|
typeof room === "string" ? room : room.name
|
|
|
|
|
);
|
|
|
|
|
const allTargets = [...roomIds, ...devices];
|
|
|
|
|
await onUpdate(allTargets);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Update error:", e);
|
|
|
|
|
}
|
2025-10-31 16:52:56 +07:00
|
|
|
};
|
|
|
|
|
|
2025-09-10 09:59:17 +07:00
|
|
|
return (
|
|
|
|
|
<div className="w-full px-6 space-y-4">
|
2025-10-31 16:52:56 +07:00
|
|
|
{/* Header */}
|
2025-09-10 09:59:17 +07:00
|
|
|
<div className="flex items-center justify-between">
|
|
|
|
|
<div>
|
|
|
|
|
<h1 className="text-3xl font-bold">{title}</h1>
|
|
|
|
|
<p className="text-muted-foreground mt-2">{description}</p>
|
|
|
|
|
</div>
|
2025-11-19 14:55:14 +07:00
|
|
|
<FormDialog
|
|
|
|
|
triggerLabel="Tải lên phiên bản mới"
|
|
|
|
|
title="Cập nhật phiên bản"
|
|
|
|
|
>
|
|
|
|
|
{(closeDialog) => (
|
|
|
|
|
<UploadVersionForm onSubmit={onUpload} closeDialog={closeDialog} />
|
|
|
|
|
)}
|
|
|
|
|
</FormDialog>
|
2025-09-10 09:59:17 +07:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Card className="w-full">
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<CardTitle className="flex items-center gap-2">
|
|
|
|
|
<FileText className="h-5 w-5" /> Lịch sử phiên bản
|
|
|
|
|
</CardTitle>
|
|
|
|
|
<CardDescription>Tất cả các phiên bản đã tải lên</CardDescription>
|
|
|
|
|
</CardHeader>
|
2025-10-31 16:52:56 +07:00
|
|
|
|
2025-09-10 09:59:17 +07:00
|
|
|
<CardContent>
|
|
|
|
|
<VersionTable
|
|
|
|
|
data={data}
|
|
|
|
|
isLoading={isLoading}
|
|
|
|
|
columns={columns}
|
|
|
|
|
onTableInit={onTableInit}
|
|
|
|
|
/>
|
|
|
|
|
</CardContent>
|
2025-10-31 16:52:56 +07:00
|
|
|
|
2025-09-10 09:59:17 +07:00
|
|
|
{onUpdate && (
|
2025-11-26 13:16:32 +07:00
|
|
|
<CardFooter className="gap-2">
|
2025-10-31 16:52:56 +07:00
|
|
|
<RequestUpdateMenu
|
|
|
|
|
onUpdateDevice={openDeviceDialog}
|
|
|
|
|
onUpdateRoom={openRoomDialog}
|
|
|
|
|
onUpdateAll={handleUpdateAll}
|
2025-09-26 17:56:55 +07:00
|
|
|
loading={updateLoading}
|
|
|
|
|
/>
|
2025-11-26 13:16:32 +07:00
|
|
|
{onDownload && (
|
|
|
|
|
<RequestUpdateMenu
|
|
|
|
|
onUpdateDevice={openDownloadDeviceDialog}
|
|
|
|
|
onUpdateRoom={openDownloadRoomDialog}
|
|
|
|
|
onUpdateAll={() => {
|
|
|
|
|
if (!onDownload) return;
|
|
|
|
|
const roomIds = rooms.map((room) =>
|
|
|
|
|
typeof room === "string" ? room : room.name
|
|
|
|
|
);
|
|
|
|
|
const allTargets = [...roomIds, ...devices];
|
|
|
|
|
onDownload(allTargets);
|
|
|
|
|
}}
|
|
|
|
|
loading={downloadLoading}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-09-10 09:59:17 +07:00
|
|
|
</CardFooter>
|
|
|
|
|
)}
|
|
|
|
|
</Card>
|
2025-10-31 16:52:56 +07:00
|
|
|
|
2025-11-19 14:55:14 +07:00
|
|
|
{/* Dialog chọn phòng */}
|
|
|
|
|
{dialogType === "room" && (
|
2025-10-31 16:52:56 +07:00
|
|
|
<SelectDialog
|
|
|
|
|
open={dialogOpen}
|
2025-11-26 13:16:32 +07:00
|
|
|
onClose={() => {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
}}
|
2025-11-19 14:55:14 +07:00
|
|
|
title="Chọn phòng"
|
|
|
|
|
description="Chọn các phòng cần cập nhật"
|
|
|
|
|
icon={<Building2 className="w-6 h-6 text-primary" />}
|
|
|
|
|
items={mapRoomsToSelectItems(rooms)}
|
2025-10-31 16:52:56 +07:00
|
|
|
onConfirm={async (selectedItems) => {
|
|
|
|
|
if (!onUpdate) return;
|
2025-11-26 13:16:32 +07:00
|
|
|
try {
|
|
|
|
|
await onUpdate(selectedItems);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Update error:", e);
|
|
|
|
|
} finally {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
setTimeout(() => window.location.reload(), 500);
|
|
|
|
|
}
|
2025-10-31 16:52:56 +07:00
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-11-19 14:55:14 +07:00
|
|
|
|
|
|
|
|
{/* Dialog tìm thiết bị */}
|
|
|
|
|
{dialogType === "device" && (
|
|
|
|
|
<DeviceSearchDialog
|
|
|
|
|
open={dialogOpen && dialogType === "device"}
|
2025-11-26 13:16:32 +07:00
|
|
|
onClose={() => {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
}}
|
2025-11-19 14:55:14 +07:00
|
|
|
rooms={rooms}
|
2025-11-26 13:16:32 +07:00
|
|
|
fetchDevices={fetchDevicesFromRoom}
|
|
|
|
|
onSelect={async (deviceIds) => {
|
|
|
|
|
if (!onUpdate) {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await onUpdate(deviceIds);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Update error:", e);
|
|
|
|
|
} finally {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
setTimeout(() => window.location.reload(), 500);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Dialog tải file - chọn phòng */}
|
|
|
|
|
{dialogType === "download-room" && (
|
|
|
|
|
<SelectDialog
|
|
|
|
|
open={dialogOpen}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
}}
|
|
|
|
|
title="Chọn phòng"
|
|
|
|
|
description="Chọn các phòng để tải file xuống"
|
|
|
|
|
icon={<Building2 className="w-6 h-6 text-primary" />}
|
|
|
|
|
items={mapRoomsToSelectItems(rooms)}
|
|
|
|
|
onConfirm={async (selectedItems) => {
|
|
|
|
|
if (!onDownload) return;
|
|
|
|
|
try {
|
|
|
|
|
await onDownload(selectedItems);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Download error:", e);
|
|
|
|
|
} finally {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
setTimeout(() => window.location.reload(), 500);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{/* Dialog tải file - tìm thiết bị */}
|
|
|
|
|
{dialogType === "download-device" && (
|
|
|
|
|
<DeviceSearchDialog
|
|
|
|
|
open={dialogOpen && dialogType === "download-device"}
|
|
|
|
|
onClose={() => {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
}}
|
|
|
|
|
rooms={rooms}
|
|
|
|
|
fetchDevices={fetchDevicesFromRoom}
|
|
|
|
|
onSelect={async (deviceIds) => {
|
|
|
|
|
if (!onDownload) {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await onDownload(deviceIds);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error("Download error:", e);
|
|
|
|
|
} finally {
|
|
|
|
|
setDialogOpen(false);
|
|
|
|
|
setDialogType(null);
|
|
|
|
|
setTimeout(() => window.location.reload(), 500);
|
|
|
|
|
}
|
|
|
|
|
}}
|
2025-11-19 14:55:14 +07:00
|
|
|
/>
|
|
|
|
|
)}
|
2025-09-10 09:59:17 +07:00
|
|
|
</div>
|
|
|
|
|
);
|
2025-09-26 17:56:55 +07:00
|
|
|
}
|