import { useState, useRef } from "react"; import { Upload, Trash2 } from "lucide-react"; import { useServerMissions, useUploadMission, useDeleteMission } from "@/hooks/useServerDetail"; import { useAuthStore } from "@/store/auth.store"; import { useUIStore } from "@/store/ui.store"; import { logger } from "@/lib/logger"; interface MissionListProps { serverId: number; } export function MissionList({ serverId }: MissionListProps) { const isAdmin = useAuthStore((s) => s.user?.role === "admin"); const addNotification = useUIStore((s) => s.addNotification); const { data: missionsData, isLoading } = useServerMissions(serverId); const uploadMission = useUploadMission(serverId); const deleteMission = useDeleteMission(serverId); const fileInputRef = useRef(null); const handleUpload = async (e: React.ChangeEvent) => { const file = e.target.files?.[0]; if (!file) return; try { await uploadMission.mutateAsync(file); addNotification({ type: "success", message: `Mission ${file.name} uploaded` }); } catch (err) { logger.error("MissionList", "Failed to upload mission: %s", err); addNotification({ type: "error", message: "Failed to upload mission" }); } if (fileInputRef.current) fileInputRef.current.value = ""; }; const handleDelete = async (filename: string) => { try { await deleteMission.mutateAsync(filename); addNotification({ type: "info", message: `Mission ${filename} deleted` }); } catch (err) { logger.error("MissionList", "Failed to delete mission %s: %s", filename, err); addNotification({ type: "error", message: `Failed to delete ${filename}` }); } }; if (isLoading) { return
Loading missions...
; } const missions = missionsData?.missions ?? []; return (

Missions ({missionsData?.total ?? 0})

{isAdmin && ( )}
{uploadMission.isPending && (
Uploading mission...
)}
{isAdmin && ( )} {missions.length === 0 ? ( ) : ( missions.map((mission) => ( {isAdmin && ( )} )) )}
Filename Mission SizeActions
No missions uploaded
{mission.filename} {mission.name} {formatSize(mission.size_bytes)}
); } function formatSize(bytes: number): string { if (bytes >= 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; if (bytes >= 1024) return `${(bytes / 1024).toFixed(1)} KB`; return `${bytes} B`; }