"use client"; import { LayoutDrawer, LayoutModalViewFile, LayoutNavbarNew, TEMA, } from "@/module/_global"; import { ActionIcon, Anchor, Box, Breadcrumbs, Button, Checkbox, Divider, Flex, Grid, Group, Indicator, Menu, Modal, rem, Select, SimpleGrid, Skeleton, Text, TextInput, } from "@mantine/core"; import React, { useState } from "react"; import { HiMenu } from "react-icons/hi"; import { FcDocument, FcFolder, FcImageFile } from "react-icons/fc"; import { BsDownload, BsListCheck } from "react-icons/bs"; import { AiOutlineDelete } from "react-icons/ai"; import { CgRename } from "react-icons/cg"; import { LuShare2 } from "react-icons/lu"; import { MdClose, MdOutlineMoreHoriz } from "react-icons/md"; import LayoutModal from "@/module/_global/layout/layout_modal"; import toast from "react-hot-toast"; import { useParams, useRouter, useSearchParams } from "next/navigation"; import DrawerMenuDocumentDivision from "./drawer_menu_document_division"; import DrawerMore from "./drawer_more"; import { funGetDivisionById } from "@/module/division_new"; import { useMediaQuery, useShallowEffect } from "@mantine/hooks"; import { funDeleteDocument, funGetAllDocument, funRenameDocument, } from "../lib/api_document"; import { IDataDocument, IJalurItem } from "../lib/type_document"; import { useHookstate } from "@hookstate/core"; import { globalRefreshDocument } from "../lib/val_document"; import { RiListCheck } from "react-icons/ri"; import { GoChevronRight } from "react-icons/go"; import DrawerShareDocument from "./drawer_share_document"; import { FaShare } from "react-icons/fa6"; export default function NavbarDocumentDivision() { const router = useRouter(); const param = useParams<{ id: string }>(); const [isOpenModalView, setOpenModalView] = useState(false); const [isExtension, setExtension] = useState(""); const [idStorage, setIdStorage] = useState(""); const [name, setName] = useState(""); const [isOpen, setOpen] = useState(false); const [isDelete, setIsDelete] = useState(false); const [rename, setRename] = useState(false); const [share, setShare] = useState(false); const [more, setMore] = useState(false); const [shareSelected, setShareSelected] = useState(false); const [copyAllowed, setCopyAllowed] = useState(true); const searchParams = useSearchParams(); const path = searchParams.get("path"); const [dataDocument, setDataDocument] = useState([]); const [dataJalur, setDataJalur] = useState([]); const refresh = useHookstate(globalRefreshDocument); const [selectedFiles, setSelectedFiles] = useState([]); const [selectAll, setSelectAll] = useState(false); const [dariSelectAll, setDariSelectAll] = useState(false); const isMobile = useMediaQuery("(max-width: 369px)"); const isMobile2 = useMediaQuery("(max-width: 496px)"); const tema = useHookstate(TEMA); const [loading, setLoading] = useState(true); const [bodyRename, setBodyRename] = useState({ id: "", name: "", path: "", idDivision: param.id, extension: "", }); const handleCheckboxChange = (index: number) => { setDariSelectAll(false); if (selectedFiles.some((i: any) => i.id == dataDocument[index].id)) { setSelectedFiles( selectedFiles.filter((i: any) => i.id != dataDocument[index].id) ); } else { setSelectedFiles([ ...selectedFiles, { id: dataDocument[index].id, name: dataDocument[index].name, path: dataDocument[index].path, extension: dataDocument[index].extension, category: dataDocument[index].category, share: dataDocument[index].share, idStorage: dataDocument[index].idStorage, }, ]); } }; function cek() { if (selectedFiles.length == dataDocument.length) { setSelectAll(true); } else { setSelectAll(false); } const shareSelected = selectedFiles.some((i: any) => i?.share == true); if (shareSelected) { setShareSelected(true); } else { setShareSelected(false); } const cek = selectedFiles.some((i: any) => i?.category == "FOLDER"); if (cek || shareSelected || selectedFiles.length > 1) { setCopyAllowed(false); } else { setCopyAllowed(true); } } const handleSelectAll = () => { if (!selectAll) { setDariSelectAll(false); for (let index = 0; index < dataDocument.length; index++) { if (!selectedFiles.some((i: any) => i.id == dataDocument[index].id)) { const newArr = { id: dataDocument[index].id, name: dataDocument[index].name, path: dataDocument[index].path, extension: dataDocument[index].extension, category: dataDocument[index].category, share: dataDocument[index].share, idStorage: dataDocument[index].idStorage, }; setSelectedFiles((selectedFiles: any) => [...selectedFiles, newArr]); } } } else { setDariSelectAll(true); setSelectedFiles([]); } }; const handleBatal = () => { setSelectedFiles([]); setSelectAll(false); setDariSelectAll(false); }; async function onConfirmDelete(val: boolean) { if (val) { try { const respon = await funDeleteDocument(selectedFiles); if (respon.success) { getOneData(); } else { toast.error(respon.message); } } catch (error) { console.error(error); toast.error("Gagal menghapus item, coba lagi nanti"); } handleBatal(); } setIsDelete(false); } async function onRenameSubmit() { try { const res = await funRenameDocument(bodyRename); if (res.success) { getOneData(); } else { toast.error(res.message); } } catch (error) { console.error(error); toast.error("Gagal mengganti nama item, coba lagi nanti"); } setSelectedFiles([]); setDariSelectAll(false); setRename(false); } async function getOneData() { try { setLoading(true); const respon = await funGetAllDocument( "?division=" + param.id + "&path=" + path ); if (respon.success) { setDataDocument(respon.data); setDataJalur(respon.jalur); setLoading(false); } else { toast.error(respon.message); setDataDocument([]); setDataJalur([]); setLoading(false); } const res = await funGetDivisionById(param.id); if (res.success) { setName(res.data.division.name); } else { toast.error(res.message); } } catch (error) { console.error(error); toast.error("Gagal mendapatkan item, coba lagi nanti"); } finally { setLoading(false); } } function resetRefresh() { refresh.set(false); setOpen(false); setMore(false); setShare(false); handleBatal(); } useShallowEffect(() => { cek(); }, [selectedFiles]); useShallowEffect(() => { getOneData(); resetRefresh(); }, [param.id, path, refresh.get()]); function onChooseRename() { setBodyRename({ ...bodyRename, id: selectedFiles[0].id, name: selectedFiles[0].name, path: selectedFiles[0].path, extension: selectedFiles[0].extension, }); setRename(true); } const onDownload = async () => { try { const fileUrl = `https://wibu-storage.wibudev.com/api/files/${selectedFiles[0].idStorage}`; const response = await fetch(fileUrl); const blob = await response.blob(); // Create a link element, use Blob URL const link = document.createElement("a"); const url = window.URL.createObjectURL(blob); link.href = url; link.download = `${selectedFiles[0].name}.${selectedFiles[0].extension}`; // Nama file yang akan diunduh document.body.appendChild(link); link.click(); // Cleanup window.URL.revokeObjectURL(url); document.body.removeChild(link); } catch (error) { alert(error); } }; return ( {(selectedFiles.length > 0 || dariSelectAll) && ( <> handleBatal()} > {selectedFiles.length > 0 ? selectedFiles.length + " item terpilih" : "Pilih Item"} handleSelectAll()} > {selectAll ? ( ) : ( )} { if (selectedFiles.length > 0 && copyAllowed) { onDownload(); } }} > 0 && copyAllowed ? "white" : "#656060" } /> 0 && copyAllowed ? "white" : "#656060" } > Unduh 0 && !shareSelected ? () => setIsDelete(true) : undefined } > 0 && !shareSelected ? "white" : "#656060" } /> 0 && !shareSelected ? "white" : "#656060" } > Hapus onChooseRename() : undefined } > Ganti Nama 0 && !shareSelected ? () => setShare(true) : undefined } > 0 && !shareSelected ? "white" : "#656060" } /> 0 && !shareSelected ? "white" : "#656060" } > Bagikan 0 && !shareSelected ? () => setMore(true) : undefined } > 0 && !shareSelected ? "white" : "#656060" } /> 0 && !shareSelected ? "white" : "#656060" } > Lainnya )} setOpen(true)} variant="light" bg={tema.get().bgIcon} size="lg" radius="lg" aria-label="Settings" > } /> {isMobile2 ? ( } my="xs" style={{ cursor: "pointer" }} > {dataJalur.slice(0, 3).map((v, i) => { return ( router.push("?path=" + v.id)} key={i} style={{ overflow: "hidden", textOverflow: "ellipsis", whiteSpace: "nowrap", maxWidth: "100%", }} truncate={"end"} > {v.name} ); })} {dataJalur.length > 3 && ( {dataJalur.slice(3).map((v, i) => { return ( router.push("?path=" + v.id)} > {v.name} ); })} )} ) : ( } my="xs" style={{ cursor: "pointer" }} > {dataJalur.slice(0, 4).map((v, i) => { return ( router.push("?path=" + v.id)} key={i} truncate="end" > {v.name} ); })} {dataJalur.length > 4 && ( {dataJalur.slice(4).map((v, i) => { return ( router.push("?path=" + v.id)} > {v.name} ); })} )} )} {loading ? ( ) : null} {loading ? ( Array(6) .fill(null) .map((_, i) => ( )) ) : ( {dataDocument.length == 0 ? ( Tidak ada Dokumen ) : ( dataDocument.map((v, i) => { const isSelected = selectedFiles.some( (i: any) => i?.id == v.id ); return ( { if ( v.category == "FOLDER" && selectedFiles.length == 0 && !dariSelectAll ) { router.push("?path=" + v.id); } else if ( v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll ) { setExtension(v.extension); setIdStorage(v.idStorage); setOpenModalView(true); } }} > {v.share ? ( } size={25} > {v.category == "FOLDER" ? ( ) : v.extension == "pdf" || v.extension == "csv" ? ( ) : ( )} ) : ( <> {v.category == "FOLDER" ? ( ) : v.extension == "pdf" || v.extension == "csv" ? ( ) : ( )} )} { if ( v.category == "FOLDER" && selectedFiles.length == 0 && !dariSelectAll ) { router.push("?path=" + v.id); } else if ( v.category == "FILE" && selectedFiles.length == 0 && !dariSelectAll ) { setExtension(v.extension); setIdStorage(v.idStorage); setOpenModalView(true); } }} > {v.category == "FOLDER" ? v.name : v.name + "." + v.extension} {v.updatedAt} handleCheckboxChange(i)} /> ); }) )} )} setOpen(false)} > {/* MODAL KONFIRMASI DELETE */} setIsDelete(false)} description="Apakah Anda yakin ingin menghapus item?" onYes={(val) => { onConfirmDelete(val); }} /> {/* MODAL RENAME */} setRename(false)} centered withCloseButton={false} > Ganti Nama Item setBodyRename({ ...bodyRename, name: e.target.value }) } /> setShare(false)} size="lg" > setMore(false)}> setOpenModalView(false)} file={idStorage} extension={isExtension} fitur="dokumen" /> ); }