import { useEffect, useState, useRef } from 'react' // material-ui import { Box, Stack, ButtonGroup, Skeleton, ToggleButtonGroup, ToggleButton } from '@mui/material' import { useTheme } from '@mui/material/styles' // project imports import MainCard from '@/ui-component/cards/MainCard' import ItemCard from '@/ui-component/cards/ItemCard' import ToolDialog from './ToolDialog' import ViewHeader from '@/layout/MainLayout/ViewHeader' import ErrorBoundary from '@/ErrorBoundary' import { ToolsTable } from '@/ui-component/table/ToolsListTable' import { PermissionButton, StyledPermissionButton } from '@/ui-component/button/RBACButtons' import TablePagination, { DEFAULT_ITEMS_PER_PAGE } from '@/ui-component/pagination/TablePagination' // API import toolsApi from '@/api/tools' // Hooks import useApi from '@/hooks/useApi' import { useError } from '@/store/context/ErrorContext' import { gridSpacing } from '@/store/constant' // icons import { IconPlus, IconFileUpload, IconLayoutGrid, IconList } from '@tabler/icons-react' import ToolEmptySVG from '@/assets/images/tools_empty.svg' // ==============================|| TOOLS ||============================== // const Tools = () => { const theme = useTheme() const getAllToolsApi = useApi(toolsApi.getAllTools) const { error, setError } = useError() const [isLoading, setLoading] = useState(true) const [showDialog, setShowDialog] = useState(false) const [dialogProps, setDialogProps] = useState({}) const [view, setView] = useState(localStorage.getItem('toolsDisplayStyle') || 'card') const inputRef = useRef(null) /* Table Pagination */ const [currentPage, setCurrentPage] = useState(1) const [pageLimit, setPageLimit] = useState(DEFAULT_ITEMS_PER_PAGE) const [total, setTotal] = useState(0) const onChange = (page, pageLimit) => { setCurrentPage(page) setPageLimit(pageLimit) refresh(page, pageLimit) } const refresh = (page, limit) => { const params = { page: page || currentPage, limit: limit || pageLimit } getAllToolsApi.request(params) } const handleChange = (event, nextView) => { if (nextView === null) return localStorage.setItem('toolsDisplayStyle', nextView) setView(nextView) } const onUploadFile = (file) => { try { const dialogProp = { title: 'Add New Tool', type: 'IMPORT', cancelButtonName: 'Cancel', confirmButtonName: 'Save', data: JSON.parse(file) } setDialogProps(dialogProp) setShowDialog(true) } catch (e) { console.error(e) } } const handleFileUpload = (e) => { if (!e.target.files) return const file = e.target.files[0] const reader = new FileReader() reader.onload = (evt) => { if (!evt?.target?.result) { return } const { result } = evt.target onUploadFile(result) } reader.readAsText(file) } const addNew = () => { const dialogProp = { title: 'Add New Tool', type: 'ADD', cancelButtonName: 'Cancel', confirmButtonName: 'Add' } setDialogProps(dialogProp) setShowDialog(true) } const edit = (selectedTool) => { const dialogProp = { title: 'Edit Tool', type: 'EDIT', cancelButtonName: 'Cancel', confirmButtonName: 'Save', data: selectedTool } setDialogProps(dialogProp) setShowDialog(true) } const onConfirm = () => { setShowDialog(false) refresh(currentPage, pageLimit) } const [search, setSearch] = useState('') const onSearchChange = (event) => { setSearch(event.target.value) } function filterTools(data) { return ( data.name.toLowerCase().indexOf(search.toLowerCase()) > -1 || data.description.toLowerCase().indexOf(search.toLowerCase()) > -1 ) } useEffect(() => { refresh(currentPage, pageLimit) // eslint-disable-next-line react-hooks/exhaustive-deps }, []) useEffect(() => { setLoading(getAllToolsApi.loading) }, [getAllToolsApi.loading]) useEffect(() => { if (getAllToolsApi.data) { setTotal(getAllToolsApi.data.total) } }, [getAllToolsApi.data]) return ( <> {error ? ( ) : ( inputRef.current.click()} startIcon={} sx={{ borderRadius: 2, height: 40 }} > Load handleFileUpload(e)} /> } sx={{ borderRadius: 2, height: 40 }} > Create {isLoading && ( )} {!isLoading && total > 0 && ( <> {!view || view === 'card' ? ( {getAllToolsApi.data?.data?.filter(filterTools).map((data, index) => ( edit(data)} /> ))} ) : ( )} {/* Pagination and Page Size Controls */} )} {!isLoading && total === 0 && ( ToolEmptySVG
No Tools Created Yet
)}
)}
setShowDialog(false)} onConfirm={onConfirm} setError={setError} > ) } export default Tools