import React, { useState, useEffect, useContext, memo } from "react"; import { toast } from "react-toastify"; import theme from "../theme.jsx"; import { v4 as uuidv4, v5 as uuidv5, validate as isUUID, } from "uuid"; import { Paper, Tooltip, Typography, Divider, Button, ButtonGroup, Grid, Card, Chip, Switch, Autocomplete, TextField, MenuItem, IconButton, } from "@mui/material"; import { OpenInNew as OpenInNewIcon, } from "@mui/icons-material"; import { makeStyles } from "@mui/styles"; import { Context } from "../context/ContextApi.jsx"; import { useNavigate, Link } from "react-router-dom"; import Priority from "../components/Priority.jsx"; import { constrainMatrix } from "reaviz"; //import { useAlert const useStyles = makeStyles({ notchedOutline: { borderColor: "#f85a3e !important", }, }); const Priorities = memo((props) => { const { globalUrl, userdata,clickedFromOrgTab,selectedOrganization, handleEditOrg, serverside, billingInfo, stripeKey, checkLogin, setAdminTab, setCurTab, notifications, setNotifications, } = props; const [showDismissed, setShowDismissed] = React.useState(false); const [showRead, setShowRead] = React.useState(false); const [appFramework, setAppFramework] = React.useState({}); const [selectedWorkflow, setSelectedWorkflow] = React.useState("NO HIGHLIGHT"); const [selectedExecutionId, setSelectedExecutionId] = React.useState("NO HIGHLIGHT"); const [highlightKMS, setHighlightKMS] = React.useState(false) const [workflows, setWorkflows] = React.useState([]) const [openNotification, setOpenNotification] = React.useState(false); const [workflow, setWorkflow] = React.useState({}) const [notificationWorkflow, setNotificationWorkflow] = React.useState( selectedOrganization.defaults === undefined ? "" : selectedOrganization.defaults.notification_workflow === undefined || selectedOrganization.defaults.notification_workflow.length === 0 ? "" : selectedOrganization.defaults.notification_workflow ); let navigate = useNavigate(); const classes = useStyles(); useEffect(() => { getFramework() // Check "workflow" and "execution_id" in URL const urlParams = new URLSearchParams(window.location.search) const workflow = urlParams.get("workflow") const execution_id = urlParams.get("execution_id") const kms = urlParams.get("kms") if (kms !== null && kms !== undefined && kms.length > 0 && kms === "true") { toast.info("KMS-related notifications are highlighted.") setHighlightKMS(true) } if (execution_id !== null) { setSelectedExecutionId(execution_id) //toast.info("Execution-related notifications are highlighted.") } if (workflow !== null) { setSelectedWorkflow(workflow) toast.info("Workflow-related notifications are highlighted.") } }, []) useEffect(() => { if (selectedOrganization === undefined || selectedOrganization === null || selectedOrganization?.id === undefined || selectedOrganization?.id === null || selectedOrganization?.id.length === 0) { return } if(workflows?.length === 0) { getAvailableWorkflows() } if (notificationWorkflow !== selectedOrganization?.defaults?.notification_workflow) { setNotificationWorkflow(selectedOrganization?.defaults?.notification_workflow) } }, [selectedOrganization]) if (userdata === undefined || userdata === null) { return } const getFramework = () => { fetch(globalUrl + "/api/v1/apps/frameworkConfiguration", { method: "GET", headers: { "Content-Type": "application/json", Accept: "application/json", }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for framework!"); } return response.json(); }) .then((responseJson) => { if (responseJson.success === false) { setAppFramework({}) if (responseJson.reason !== undefined) { //toast("Failed loading: " + responseJson.reason) } else { //toast("Failed to load framework for your org.") } } else { setAppFramework(responseJson) } }) .catch((error) => { console.log("err in framework: ", error.toString()); }) } const clearNotifications = () => { // Don't really care about the logout toast("Clearing notifications") fetch(`${globalUrl}/api/v1/notifications/clear`, { credentials: "include", method: "GET", headers: { "Content-Type": "application/json", }, }) .then(function (response) { if (response.status !== 200) { console.log("Error in response"); } return response.json(); }) .then(function (responseJson) { if (responseJson.success === true) { // Reload the UI const newNotifications = notifications.map((notification) => { notification.read = true return notification }) setNotifications(newNotifications) setShowRead(true) } else { toast("Failed dismissing notifications. Please try again later."); } }) .catch((error) => { console.log("error in notification dismissal: ", error); //removeCookie("session_token", {path: "/"}) }); }; const dismissNotification = (alert_id, disabled) => { var notificationurl = `${globalUrl}/api/v1/notifications/${alert_id}/markasread` if (disabled === true) { notificationurl += "?disabled=true" } else if (disabled === false) { notificationurl += "?disabled=false" } fetch(notificationurl , { credentials: "include", method: "GET", headers: { "Content-Type": "application/json", }, }) .then(function (response) { if (response.status !== 200) { console.log("Error in response"); } return response.json(); }) .then(function (responseJson) { if (responseJson.success === true) { // Mark current one as read var newNotifications = notifications.map((notification) => { if (notification.id === alert_id) { notification.read = true } return notification }) if (disabled === true) { toast("Notification disabled, and will not be shown again.") newNotifications = newNotifications.map((notification) => { if (notification.id === alert_id) { notification.ignored = true } return notification }) console.log("NEW NOTIFICATIONS: ", newNotifications); } else if (disabled === false) { toast("Notification re-enabled successfully") newNotifications = newNotifications.map((notification) => { if (notification.id === alert_id) { notification.ignored = false } return notification }) } else { toast("Notification dismissed successfully") } //const newNotifications = notifications.filter( // (data) => data.id !== alert_id //) //console.log("NEW NOTIFICATIONS: ", newNotifications); if (setNotifications !== undefined && newNotifications !== undefined) { setNotifications(newNotifications) } } else { toast("Failed dismissing notification. Please try again later."); } }) .catch((error) => { console.log("error in notification dismissal: ", error); //removeCookie("session_token", {path: "/"}) }) } const notificationWidth = "100%" const imagesize = 22 const boxColor = "#86c142" const getAvailableWorkflows = () => { fetch(globalUrl + "/api/v1/workflows", { method: "GET", headers: { "Content-Type": "application/json", Accept: "application/json", }, credentials: "include", }) .then((response) => { if (response.status !== 200) { console.log("Status not 200 for workflows :O!"); return; } return response.json(); }) .then((responseJson) => { if (responseJson !== undefined) { // Add parent notification workflow if it's a child org // selectedOrganization, if (selectedOrganization.creator_org !== undefined && selectedOrganization.creator_org !== null && selectedOrganization.creator_org.length > 0) { // Add to start of the list responseJson.unshift({ "name": "Parent-Org's Notification Workflow", "id": "parent", }) } setWorkflows(responseJson) if (selectedOrganization.defaults !== undefined && selectedOrganization.defaults.notification_workflow !== undefined) { const workflow = responseJson.find((workflow) => workflow.id === selectedOrganization.defaults.notification_workflow) if (workflow !== undefined && workflow !== null) { setWorkflow(workflow) } } } }) .catch((error) => { console.log("Error getting workflows: " + error); }) } const handleWorkflowSelectionUpdate = (e, isUserinput) => { if (e.target.value === undefined || e.target.value === null || e.target.value.id === undefined) { console.log("Returning as there's no id") return null } setOpenNotification(false) setWorkflow(e.target.value) setNotificationWorkflow(e.target.value.id) handleEditOrg( selectedOrganization?.name, selectedOrganization.description, selectedOrganization.id, selectedOrganization.image, { app_download_repo: selectedOrganization?.defaults?.app_download_repo, app_download_branch: selectedOrganization?.defaults?.app_download_branch, workflow_download_repo: selectedOrganization?.defaults?.workflow_download_repo, workflow_download_branch: selectedOrganization?.defaults?.workflow_download_branch, notification_workflow: e.target.value.id, documentation_reference: selectedOrganization?.defaults?.documentation_reference, workflow_upload_repo: selectedOrganization?.defaults?.workflow_upload_repo, workflow_upload_branch: selectedOrganization?.defaults?.workflow_upload_branch, workflow_upload_username: selectedOrganization?.defaults?.workflow_upload_username, workflow_upload_token: selectedOrganization?.defaults?.workflow_upload_token, newsletter: selectedOrganization?.defaults?.newsletter, weekly_recommendations: selectedOrganization?.defaults?.weekly_recommendations, }, { sso_entrypoint: selectedOrganization?.sso_config?.sso_entrypoint, sso_certificate: selectedOrganization?.sso_config?.sso_certificate, client_id: selectedOrganization?.sso_config?.client_id, client_secret: selectedOrganization?.sso_config?.client_secret, openid_authorization: selectedOrganization?.sso_config?.openid_authorization, openid_token: selectedOrganization?.sso_config?.openid_token, SSORequired: selectedOrganization?.sso_config?.SSORequired, auto_provision: selectedOrganization?.sso_config?.auto_provision, } ) } return (