/* eslint-disable react-hooks/exhaustive-deps */ import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; import { faBookOpen, faGear, faKey, faMobile, faPlug, faUser, } from "@fortawesome/free-solid-svg-icons"; import { faPlus } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import getOrganizations from "~/pages/api/organization/getOrgs"; import getOrganizationUserProjects from "~/pages/api/organization/GetOrgUserProjects"; import getOrganizationUsers from "~/pages/api/organization/GetOrgUsers"; import addUserToWorkspace from "~/pages/api/workspace/addUserToWorkspace"; import createWorkspace from "~/pages/api/workspace/createWorkspace"; import getWorkspaces from "~/pages/api/workspace/getWorkspaces"; import uploadKeys from "~/pages/api/workspace/uploadKeys"; import NavBarDashboard from "../navigation/NavBarDashboard"; import { decryptAssymmetric, encryptAssymmetric, } from "../utilities/cryptography/crypto"; import Button from "./buttons/Button"; import AddWorkspaceDialog from "./dialog/AddWorkspaceDialog"; import Listbox from "./Listbox"; export default function Layout({ children }) { const router = useRouter(); const [workspaceList, setWorkspaceList] = useState([]); const [workspaceMapping, setWorkspaceMapping] = useState([{ 1: 2 }]); const [workspaceSelected, setWorkspaceSelected] = useState("∞"); let [newWorkspaceName, setNewWorkspaceName] = useState(""); let [isOpen, setIsOpen] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(false); function closeModal() { setIsOpen(false); } // TODO: what to do about the fact that 2ids can have the same name /** * When a user creates a new workspace, redirect them to the page of the new workspace. * @param {*} workspaceName */ async function submitModal(workspaceName, addAllUsers) { setLoading(true); setTimeout(() => setLoading(false), 1500); const workspaces = await getWorkspaces(); const currentWorkspaces = workspaces.map((workspace) => workspace.name); if (!currentWorkspaces.includes(workspaceName)) { const newWorkspace = await createWorkspace( workspaceName, localStorage.getItem("orgData.id") ); let newWorkspaceId; try { newWorkspaceId = newWorkspace._id; } catch (error) { console.log(error); } if (addAllUsers) { let orgUsers = await getOrganizationUsers({ orgId: localStorage.getItem("orgData.id"), }); orgUsers.map(async (user) => { if (user.status == "accepted") { let result = await addUserToWorkspace( user.user.email, newWorkspaceId ); if (result?.invitee && result?.latestKey) { const PRIVATE_KEY = localStorage.getItem("PRIVATE_KEY"); // assymmetrically decrypt symmetric key with local private key const key = decryptAssymmetric({ ciphertext: result.latestKey.encryptedKey, nonce: result.latestKey.nonce, publicKey: result.latestKey.sender.publicKey, privateKey: PRIVATE_KEY, }); const { ciphertext, nonce } = encryptAssymmetric({ plaintext: key, publicKey: result.invitee.publicKey, privateKey: PRIVATE_KEY, }); uploadKeys(newWorkspaceId, result.invitee._id, ciphertext, nonce); } } }); } router.push("/dashboard/" + newWorkspaceId + "?Development"); setIsOpen(false); setNewWorkspaceName(""); } else { setError("A project with this name already exists."); setLoading(false); } } function openModal() { setIsOpen(true); } const menuItems = [ { href: "/dashboard/" + workspaceMapping[workspaceSelected] + "?Development", title: "Secrets", emoji: , }, { href: "/users/" + workspaceMapping[workspaceSelected], title: "Members", emoji: , }, { href: "/integrations/" + workspaceMapping[workspaceSelected], title: "Integrations", emoji: , }, { href: "/settings/project/" + workspaceMapping[workspaceSelected], title: "Project Settings", emoji: , }, ]; useEffect(async () => { // Put a user in a workspace if they're not in one yet if ( localStorage.getItem("orgData.id") == null || localStorage.getItem("orgData.id") == "" ) { const userOrgs = await getOrganizations(); localStorage.setItem("orgData.id", userOrgs[0]._id); } let orgUserProjects = await getOrganizationUserProjects({ orgId: localStorage.getItem("orgData.id"), }); let userWorkspaces = orgUserProjects; if ( userWorkspaces.length == 0 && router.asPath != "/noprojects" && !router.asPath.includes("settings") ) { router.push("/noprojects"); } else if (router.asPath != "/noprojects") { const intendedWorkspaceId = router.asPath .split("/")[router.asPath.split("/").length - 1].split("?")[0]; // If a user is not a member of a workspace they are trying to access, just push them to one of theirs if ( intendedWorkspaceId != "heroku" && !userWorkspaces .map((workspace) => workspace._id) .includes(intendedWorkspaceId) ) { router.push("/dashboard/" + userWorkspaces[0]._id + "?Development"); } else { setWorkspaceList(userWorkspaces.map((workspace) => workspace.name)); setWorkspaceMapping( Object.fromEntries( userWorkspaces.map((workspace) => [workspace.name, workspace._id]) ) ); setWorkspaceSelected( Object.fromEntries( userWorkspaces.map((workspace) => [workspace._id, workspace.name]) )[ router.asPath .split("/")[router.asPath.split("/").length - 1].split("?")[0]] ); } } }, []); useEffect(() => { try { if ( workspaceMapping[workspaceSelected] && workspaceMapping[workspaceSelected] !== router.asPath .split("/")[router.asPath.split("/").length - 1].split("?")[0] ) { router.push("/dashboard/" + workspaceMapping[workspaceSelected] + "?Development"); localStorage.setItem( "projectData.id", workspaceMapping[workspaceSelected] ); } } catch (error) { console.log(error); } }, [workspaceSelected]); return ( <>
{children}

{" "} To use Infisical, please log in through a device with larger dimensions.{" "}

); }