import React, { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; import PropTypes from "prop-types"; import { Button } from "react-bootstrap"; import { Link } from "react-router-dom"; import { FaSort, FaSortUp, FaSortDown } from "react-icons/fa"; import "./server-dashboard.css"; import { timeSince } from "../../util/timeSince"; import PaginationFooter from "../PaginationFooter/PaginationFooter"; const ServerDashboard = (props) => { // sort methods var usernameDesc = (e) => e.sort((a, b) => (a.name > b.name ? 1 : -1)), usernameAsc = (e) => e.sort((a, b) => (a.name < b.name ? 1 : -1)), adminDesc = (e) => e.sort((a) => (a.admin ? -1 : 1)), adminAsc = (e) => e.sort((a) => (a.admin ? 1 : -1)), dateDesc = (e) => e.sort((a, b) => new Date(a.last_activity) - new Date(b.last_activity) > 0 ? -1 : 1 ), dateAsc = (e) => e.sort((a, b) => new Date(a.last_activity) - new Date(b.last_activity) > 0 ? 1 : -1 ), runningAsc = (e) => e.sort((a) => (a.server == null ? -1 : 1)), runningDesc = (e) => e.sort((a) => (a.server == null ? 1 : -1)); var [errorAlert, setErrorAlert] = useState(null); var [sortMethod, setSortMethod] = useState(null); var user_data = useSelector((state) => state.user_data), user_page = useSelector((state) => state.user_page), limit = useSelector((state) => state.limit), page = parseInt(new URLSearchParams(props.location.search).get("page")); page = isNaN(page) ? 0 : page; var slice = [page * limit, limit]; const dispatch = useDispatch(); var { updateUsers, shutdownHub, startServer, stopServer, startAll, stopAll, history, } = props; var dispatchPageUpdate = (data, page) => { dispatch({ type: "USER_PAGE", value: { data: data, page: page, }, }); }; if (!user_data) { return
; } if (page != user_page) { updateUsers(...slice).then((data) => dispatchPageUpdate(data, page)); } if (sortMethod != null) { user_data = sortMethod(user_data); } return (
{errorAlert != null ? (
{errorAlert}
) : ( <> )}
{"> Manage Groups"}
{user_data.map((e, i) => ( ))}
User{" "} setSortMethod(() => method)} testid="user-sort" /> Admin{" "} setSortMethod(() => method)} testid="admin-sort" /> Last Activity{" "} setSortMethod(() => method)} testid="last-activity-sort" /> Running{" "} setSortMethod(() => method)} testid="running-status-sort" /> Actions
{/* Start all servers */} {/* Stop all servers */} {/* Shutdown Jupyterhub */}
{e.name} {e.admin ? "admin" : ""} {e.last_activity ? timeSince(e.last_activity) : "Never"} {e.server != null ? ( // Stop Single-user server ) : ( // Start Single-user server )} {/* Edit User */}


); }; ServerDashboard.propTypes = { user_data: PropTypes.array, updateUsers: PropTypes.func, shutdownHub: PropTypes.func, startServer: PropTypes.func, stopServer: PropTypes.func, startAll: PropTypes.func, stopAll: PropTypes.func, dispatch: PropTypes.func, history: PropTypes.shape({ push: PropTypes.func, }), location: PropTypes.shape({ search: PropTypes.string, }), }; const SortHandler = (props) => { var { sorts, callback, testid } = props; var [direction, setDirection] = useState(undefined); return (
{ if (!direction) { callback(sorts.desc); setDirection("desc"); } else if (direction == "asc") { callback(sorts.desc); setDirection("desc"); } else { callback(sorts.asc); setDirection("asc"); } }} > {!direction ? ( ) : direction == "asc" ? ( ) : ( )}
); }; SortHandler.propTypes = { sorts: PropTypes.object, callback: PropTypes.func, testid: PropTypes.string, }; export default ServerDashboard;