feat: add ops ai workspace dock control
This commit is contained in:
@@ -0,0 +1,170 @@
|
||||
/**
|
||||
* Copyright (c) 2023-present Plane Software, Inc. and contributors
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* See the LICENSE file for details.
|
||||
*/
|
||||
|
||||
import { useCallback, useEffect, useMemo, useState } from "react";
|
||||
import { createPortal } from "react-dom";
|
||||
import useSWR from "swr";
|
||||
import { CheckCircle2, MessageCircle, Monitor, RefreshCw, X } from "lucide-react";
|
||||
// plane imports
|
||||
import { Button } from "@plane/propel/button";
|
||||
import { Tooltip } from "@plane/propel/tooltip";
|
||||
import { EModalPosition, EModalWidth, ModalCore } from "@plane/ui";
|
||||
import { cn } from "@plane/utils";
|
||||
// services
|
||||
import { WorkspaceAIWorkspaceService } from "@/services/workspace-ai-workspace.service";
|
||||
|
||||
const workspaceAIWorkspaceService = new WorkspaceAIWorkspaceService();
|
||||
|
||||
const aiWorkspaceCloseButtonClassName =
|
||||
"absolute top-0.5 right-0.5 flex h-12 w-12 items-center justify-center rounded-full border-0 bg-[#17181B] text-white shadow-none ring-0 transition-transform outline-none hover:scale-[1.03] hover:bg-[#0F1012]";
|
||||
|
||||
type TAIWorkspaceGlobalControlProps = {
|
||||
workspaceSlug: string;
|
||||
};
|
||||
|
||||
export function AIWorkspaceGlobalControl({ workspaceSlug }: TAIWorkspaceGlobalControlProps) {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [dockSlot, setDockSlot] = useState<Element | null>(null);
|
||||
|
||||
const { data, error, isLoading, mutate } = useSWR(
|
||||
isOpen && workspaceSlug ? `AI_WORKSPACE_EXECUTORS_${workspaceSlug}` : null,
|
||||
() => workspaceAIWorkspaceService.listExecutors(workspaceSlug),
|
||||
{ refreshInterval: isOpen ? 10000 : 0 }
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof document === "undefined") return;
|
||||
|
||||
const updateDockSlot = () => {
|
||||
setDockSlot(
|
||||
document.querySelector("[data-nodedc-voice-task-toolbar-slot]") ??
|
||||
document.querySelector("[data-nodedc-voice-task-dock-slot]")
|
||||
);
|
||||
};
|
||||
|
||||
updateDockSlot();
|
||||
const observer = new MutationObserver(updateDockSlot);
|
||||
observer.observe(document.body, { childList: true, subtree: true });
|
||||
|
||||
return () => {
|
||||
observer.disconnect();
|
||||
};
|
||||
}, []);
|
||||
|
||||
const selectedExecutor = useMemo(
|
||||
() => data?.executors?.find((executor) => executor.id === data.selectedExecutorId) ?? data?.executors?.[0] ?? null,
|
||||
[data?.executors, data?.selectedExecutorId]
|
||||
);
|
||||
|
||||
const handleOpen = useCallback(() => {
|
||||
setIsOpen(true);
|
||||
}, []);
|
||||
|
||||
const handleClose = useCallback(() => {
|
||||
setIsOpen(false);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<>
|
||||
{dockSlot
|
||||
? createPortal(
|
||||
<Tooltip tooltipContent="AI Workspace" position="top">
|
||||
<button
|
||||
type="button"
|
||||
className="nodedc-bottom-dock-voice-button nodedc-bottom-dock-ai-workspace-button"
|
||||
onClick={handleOpen}
|
||||
aria-label="AI Workspace"
|
||||
>
|
||||
<MessageCircle className="size-4" />
|
||||
</button>
|
||||
</Tooltip>,
|
||||
dockSlot
|
||||
)
|
||||
: null}
|
||||
|
||||
<ModalCore
|
||||
isOpen={isOpen}
|
||||
handleClose={handleClose}
|
||||
position={EModalPosition.CENTER}
|
||||
width={EModalWidth.MD}
|
||||
className="overflow-visible"
|
||||
>
|
||||
<div className="relative p-5">
|
||||
<div className="flex items-start justify-between gap-4 pr-12">
|
||||
<div>
|
||||
<h3 className="text-18 font-medium text-primary">AI Workspace</h3>
|
||||
<div className="mt-1 text-12 text-tertiary">Ops</div>
|
||||
</div>
|
||||
<button type="button" className={aiWorkspaceCloseButtonClassName} onClick={handleClose}>
|
||||
<X className="size-5" />
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div className="mt-5 rounded-[28px] bg-white/[0.04] p-4 backdrop-blur-xl">
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="flex min-w-0 items-center gap-3">
|
||||
<span className="grid size-10 flex-shrink-0 place-items-center rounded-full bg-white/[0.06] text-[rgb(var(--nodedc-accent-rgb))]">
|
||||
<MessageCircle className="size-5" />
|
||||
</span>
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-15 font-medium text-primary">
|
||||
{selectedExecutor?.name ?? "Codex assistant"}
|
||||
</div>
|
||||
<div className="mt-0.5 truncate text-12 text-tertiary">
|
||||
{selectedExecutor?.model || selectedExecutor?.accountLabel || "AI Workspace"}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<Button variant="secondary" size="sm" onClick={() => mutate()} disabled={isLoading}>
|
||||
<RefreshCw className={cn("mr-2 size-3.5", isLoading && "animate-spin")} />
|
||||
Обновить
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
<div className="mt-4 grid grid-cols-1 gap-2">
|
||||
{error ? (
|
||||
<div className="border-red-500/25 bg-red-500/10 text-red-500 rounded-[20px] border px-4 py-3 text-12">
|
||||
{error?.message || error?.error || "AI Workspace недоступен"}
|
||||
</div>
|
||||
) : isLoading ? (
|
||||
<div className="rounded-[20px] bg-white/[0.035] px-4 py-3 text-12 text-tertiary">Загрузка</div>
|
||||
) : data?.executors?.length ? (
|
||||
data.executors.map((executor) => (
|
||||
<div key={executor.id} className="flex items-center justify-between gap-3 rounded-[20px] bg-white/[0.035] px-4 py-3">
|
||||
<div className="flex min-w-0 items-center gap-3">
|
||||
<Monitor className="size-4 flex-shrink-0 text-tertiary" />
|
||||
<div className="min-w-0">
|
||||
<div className="truncate text-13 font-medium text-primary">{executor.name}</div>
|
||||
<div className="mt-0.5 truncate text-11 text-tertiary">
|
||||
{executor.connectionMode === "hub" ? executor.pairingCode || "hub" : "direct"}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center gap-1.5 rounded-full px-2.5 py-1 text-11",
|
||||
executor.status === "online"
|
||||
? "bg-green-500/10 text-green-300"
|
||||
: "bg-white/[0.045] text-tertiary"
|
||||
)}
|
||||
>
|
||||
<CheckCircle2 className="size-3" />
|
||||
{executor.status}
|
||||
</span>
|
||||
</div>
|
||||
))
|
||||
) : (
|
||||
<div className="rounded-[20px] bg-white/[0.035] px-4 py-3 text-12 text-tertiary">
|
||||
Устройства не подключены
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</ModalCore>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user