/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: Apache-2.0 */ import React, { useState } from "react"; import { useDashboards, useWindowSize } from "../hooks"; import { Dashboard, LocationState } from "../models"; import Tabs from "../components/Tabs"; import DraftsTab from "../components/DraftsTab"; import PublishedTab from "../components/PublishedTab"; import ArchivedTab from "../components/ArchivedTab"; import { useLocation, useHistory } from "react-router-dom"; import AlertContainer from "../containers/AlertContainer"; import BackendService from "../services/BackendService"; import Modal from "../components/Modal"; import Spinner from "../components/Spinner"; import { useTranslation } from "react-i18next"; function DashboardListing() { const { t } = useTranslation(); const { search } = useLocation(); const history = useHistory(); const windowSize = useWindowSize(); const { draftsDashboards, publishedDashboards, archivedDashboards, reloadDashboards, loading } = useDashboards(); const [isOpenArchiveModal, setIsOpenArchiveModal] = useState(false); const [isOpenDeleteModal, setIsOpenDeleteModal] = useState(false); const [isOpenCopyModal, setIsOpenCopyModal] = useState(false); const [selectedDashboards, setSelectedDashboards] = useState>([]); const closeArchiveModal = () => { setIsOpenArchiveModal(false); }; const closeDeleteModal = () => { setIsOpenDeleteModal(false); }; const closeCopyModal = () => { setIsOpenCopyModal(false); }; const onArchiveDashboards = (selected: Array) => { setSelectedDashboards(selected); setIsOpenArchiveModal(true); }; const onDeleteDashboards = (selected: Array) => { setSelectedDashboards(selected); setIsOpenDeleteModal(true); }; const onCopyDashboards = (selected: Array) => { setSelectedDashboards(selected); setIsOpenCopyModal(true); }; const archiveDashboards = async () => { closeArchiveModal(); if (selectedDashboards.length) { for (const dashboard of selectedDashboards) { await BackendService.archive(dashboard.id, dashboard.updatedAt); } history.replace("/admin/dashboards?tab=archived", { alert: { type: "success", message: `${ selectedDashboards.length > 1 ? selectedDashboards.length : selectedDashboards[0].name } ${ selectedDashboards.length > 1 ? t("DashboardListing.DashboardPlural") : t("DashboardListing.DashboardSingular") } ${t("DashboardListing.SuccessfullyArchived")}`, }, }); await reloadDashboards(); } }; const deleteDashboards = async () => { closeDeleteModal(); if (selectedDashboards.length) { await BackendService.deleteDashboards( selectedDashboards.map((dashboard) => dashboard.id), ); history.replace("/admin/dashboards", { alert: { type: "success", message: `${ selectedDashboards.length > 1 ? selectedDashboards.length : selectedDashboards[0].name } ${ selectedDashboards.length > 1 ? t("DashboardListing.DraftDashboardPlural") : t("DashboardListing.DraftDashboardSingular") } ${t("DashboardListing.SuccessfullyDeleted")}`, }, }); await reloadDashboards(); } }; const copyDashboards = async () => { closeCopyModal(); if (selectedDashboards.length) { for (let dashboard of selectedDashboards) { await BackendService.copyDashboard(dashboard.id); } history.replace("/admin/dashboards", { alert: { type: "success", message: `${ selectedDashboards.length > 1 ? selectedDashboards.length : selectedDashboards[0].name } ${ selectedDashboards.length > 1 ? t("DashboardListing.DashboardPlural") : t("DashboardListing.DashboardSingular") } ${t("DashboardListing.SuccessfullyCopied")}`, }, }); await reloadDashboards(); } }; let activeTab = "drafts"; const validTabs = ["drafts", "pending", "published", "archived"]; const queryString = search.split("tab="); if (queryString.length > 1 && validTabs.includes(queryString[1])) { activeTab = queryString[1]; } const dashboardLabel = `${ selectedDashboards.length !== 1 ? `${selectedDashboards.length} ${t("DashboardListing.SelectedDashboards")}` : `"${selectedDashboards[0].name}" ${t("DashboardListing.Dashboard")}` }`; return ( <>

{t("DashboardListing.Dashboards")}

{loading ? ( ) : ( <>
)} ); } export default DashboardListing;