"use client"; import { useAuthSession } from "@/shared/hooks/use-session"; import { DeleteDialog } from "@/app/(modules)/admin/_components/delete-dialog"; import { formatIndonesianDate } from "@/shared/utils/date"; import mapsetApi from "@/shared/services/mapset"; import { Button } from "@/shared/components/ds/button"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; import { Check, PencilLineIcon, DownloadIcon, UnlinkIcon, Trash2Icon, ChevronLeft, } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; import { VerifyMapsetDialog } from "../verify-mapset-dialog"; import MapsetClassificationSection from "./mapset-classification-section"; import MapsetInfoSection from "./mapset-info-section"; import MapsetMetadataSection from "./mapset-metadata-section"; import MapsetOrganizationSection from "./mapset-organization-section"; import { MapsetStatus } from "./mapset-status"; import MapsetVersionSection from "./mapset-version-section"; import PreviewMap from "@/shared/components/preview-map"; import MapsetHistory from "./mapset-history"; import { featureFlags } from "@/shared/config/feature-flag"; interface MapsetDetailProps { id: string; } export function MapsetDetail({ id }: MapsetDetailProps) { const [isVerifyDialogOpen, setIsVerifyDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const queryClient = useQueryClient(); const router = useRouter(); const { checkPermission } = useAuthSession(); const { data: mapset } = useQuery({ queryKey: ["mapset", id], queryFn: () => mapsetApi.getMapsetById(id), }); const updateStatusMutation = useMutation({ mutationFn: (updateData: { status: string; notes: string }) => mapsetApi.updateMapsetStatus( id, updateData.status, updateData.notes, mapset?.layer_url ?? "" ), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["mapset", id] }); toast.success("Status mapset berhasil diperbarui"); setIsVerifyDialogOpen(false); }, onError: (error) => { console.error(error); toast.error("Gagal memperbarui status mapset"); }, }); const deleteMutation = useMutation({ mutationFn: () => mapsetApi.deleteMapset(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["mapsets"] }); toast.success("Mapset berhasil dihapus"); router.push("/admin/mapset"); }, onError: (error) => { console.error(error); toast.error("Gagal menghapus mapset"); }, }); const handleEdit = () => { router.push(`/admin/mapset/edit/${id}`); }; const handleDownload = async () => { if (!mapset?.layer_url) { toast.error("URL layer tidak tersedia"); return; } try { toast.loading("Mempersiapkan download..."); const response = await fetch(`/fe-api/mapset/download/geojson/${id}`); if (!response.ok) { const error = await response.json(); throw new Error(error.message || "Gagal mengunduh data"); } // Get the blob from the response const blob = await response.blob(); // Create download link const url = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = url; link.download = `${mapset.name}.geojson`; document.body.appendChild(link); link.click(); // Cleanup window.URL.revokeObjectURL(url); document.body.removeChild(link); toast.success("Download GeoJSON dimulai"); } catch (error) { console.error("Download error:", error); toast.error( error instanceof Error ? error.message : "Gagal mengunduh data" ); } finally { toast.dismiss(); } }; const handleUnlink = () => { if (!mapset?.layer_url) { toast.error("URL layer tidak tersedia"); return; } navigator.clipboard.writeText(mapset.layer_url); toast.success("URL layer berhasil disalin"); }; const handleDelete = () => { setIsDeleteDialogOpen(true); }; const handleDeleteConfirm = () => { deleteMutation.mutate(); }; if (!mapset) return null; const handleVerifyClick = () => { setIsVerifyDialogOpen(true); }; const handleVerificationAction = ( action: "approve" | "reject", notes: string ) => { const newStatus = action === "approve" ? "approved" : "rejected"; updateStatusMutation.mutate({ status: newStatus, notes }); }; const canVerify = mapset?.status_validation === "on_verification" && checkPermission("mapset", "verify"); const canEdit = checkPermission("mapset", "update"); const canDownload = checkPermission("mapset", "read"); const canUnlink = checkPermission("mapset", "update"); const canDelete = checkPermission("mapset", "delete"); return (

Mapset Detail

Diperbarui {formatIndonesianDate(mapset.updated_at)}
{canVerify && ( )}
{mapset.is_popular && (
Mapset Populer
)}
{canEdit && ( )} {canDownload && ( )} {canUnlink && ( )}
{canDelete && ( <>
)}
{canEdit && featureFlags.mapsetHistory.isActive && (
)} {mapset && ( setIsVerifyDialogOpen(false)} open={isVerifyDialogOpen} /> )} setIsDeleteDialogOpen(false)} open={isDeleteDialogOpen} />
); }