import React, { useState, useEffect } from "react"; import * as Skeleton from "react-loading-skeleton"; import "react-loading-skeleton/dist/skeleton.css"; import Workspace from "@/models/workspace"; import ManageWorkspace, { useManageWorkspaceModal, } from "../../Modals/ManageWorkspace"; import paths from "@/utils/paths"; import { useParams, useNavigate } from "react-router-dom"; import { GearSix, UploadSimple, DotsSixVertical } from "@phosphor-icons/react"; import useUser from "@/hooks/useUser"; import ThreadContainer from "./ThreadContainer"; import { useMatch } from "react-router-dom"; import { DragDropContext, Droppable, Draggable } from "react-beautiful-dnd"; import showToast from "@/utils/toast"; export default function ActiveWorkspaces() { const navigate = useNavigate(); const { slug } = useParams(); const [loading, setLoading] = useState(true); const [workspaces, setWorkspaces] = useState([]); const [selectedWs, setSelectedWs] = useState(null); const { showing, showModal, hideModal } = useManageWorkspaceModal(); const { user } = useUser(); const isInWorkspaceSettings = !!useMatch("/workspace/:slug/settings/:tab"); useEffect(() => { async function getWorkspaces() { const workspaces = await Workspace.all(); setLoading(false); setWorkspaces(Workspace.orderWorkspaces(workspaces)); } getWorkspaces(); }, []); if (loading) { return ( ); } /** * Reorders workspaces in the UI via localstorage on client side. * @param {number} startIndex - the index of the workspace to move * @param {number} endIndex - the index to move the workspace to */ function reorderWorkspaces(startIndex, endIndex) { const reorderedWorkspaces = Array.from(workspaces); const [removed] = reorderedWorkspaces.splice(startIndex, 1); reorderedWorkspaces.splice(endIndex, 0, removed); setWorkspaces(reorderedWorkspaces); const success = Workspace.storeWorkspaceOrder( reorderedWorkspaces.map((w) => w.id) ); if (!success) { showToast("Failed to reorder workspaces", "error"); Workspace.all().then((workspaces) => setWorkspaces(workspaces)); } } const onDragEnd = (result) => { if (!result.destination) return; reorderWorkspaces(result.source.index, result.destination.index); }; return ( {(provided) => ( {workspaces.map((workspace, index) => { const isActive = workspace.slug === slug; return ( {(provided, snapshot) => ( {workspace.name} {user?.role !== "default" && ( { e.preventDefault(); setSelectedWs(workspace); showModal(); }} className="border-none rounded-md flex items-center justify-center ml-auto p-[2px] hover:bg-[#646768] text-[#A7A8A9] hover:text-white" > { e.preventDefault(); e.stopPropagation(); navigate( isInWorkspaceSettings ? paths.workspace.chat(workspace.slug) : paths.workspace.settings.generalAppearance( workspace.slug ) ); }} className="rounded-md flex items-center justify-center text-[#A7A8A9] hover:text-white ml-auto p-[2px] hover:bg-[#646768]" aria-label="General appearance settings" > )} {isActive && ( )} )} ); })} {provided.placeholder} {showing && ( )} )} ); }
{workspace.name}