|
| 1 | +import React, { useState, useMemo } from "react"; |
| 2 | +import { useWorkspaceContext } from "@/browser/contexts/WorkspaceContext"; |
| 3 | +import { useWorkspaceStoreRaw } from "@/browser/stores/WorkspaceStore"; |
| 4 | +import { isLocalProjectRuntime } from "@/common/types/runtime"; |
| 5 | +import type { RuntimeConfig } from "@/common/types/runtime"; |
| 6 | +import { useSyncExternalStore } from "react"; |
| 7 | + |
| 8 | +/** |
| 9 | + * Warning shown when a local project-dir workspace has another workspace |
| 10 | + * for the same project that is currently streaming. |
| 11 | + * |
| 12 | + * This warns users that agents may interfere with each other when |
| 13 | + * working on the same directory without isolation. |
| 14 | + */ |
| 15 | +export const ConcurrentLocalWarning: React.FC<{ |
| 16 | + workspaceId: string; |
| 17 | + projectPath: string; |
| 18 | + runtimeConfig?: RuntimeConfig; |
| 19 | +}> = (props) => { |
| 20 | + const [dismissed, setDismissed] = useState(false); |
| 21 | + |
| 22 | + // Only show for local project-dir runtimes (not worktree or SSH) |
| 23 | + const isLocalProject = isLocalProjectRuntime(props.runtimeConfig); |
| 24 | + |
| 25 | + const { workspaceMetadata } = useWorkspaceContext(); |
| 26 | + const store = useWorkspaceStoreRaw(); |
| 27 | + |
| 28 | + // Find other local project-dir workspaces for the same project |
| 29 | + const otherLocalWorkspaceIds = useMemo(() => { |
| 30 | + if (!isLocalProject) return []; |
| 31 | + |
| 32 | + const result: string[] = []; |
| 33 | + for (const [id, meta] of workspaceMetadata) { |
| 34 | + // Skip current workspace |
| 35 | + if (id === props.workspaceId) continue; |
| 36 | + // Must be same project |
| 37 | + if (meta.projectPath !== props.projectPath) continue; |
| 38 | + // Must also be local project-dir runtime |
| 39 | + if (!isLocalProjectRuntime(meta.runtimeConfig)) continue; |
| 40 | + result.push(id); |
| 41 | + } |
| 42 | + return result; |
| 43 | + }, [isLocalProject, workspaceMetadata, props.workspaceId, props.projectPath]); |
| 44 | + |
| 45 | + // Subscribe to streaming state of other local workspaces |
| 46 | + // We need to check if any of them have canInterrupt === true |
| 47 | + const streamingWorkspaceName = useSyncExternalStore( |
| 48 | + (listener) => { |
| 49 | + // Subscribe to all other local workspaces |
| 50 | + const unsubscribers = otherLocalWorkspaceIds.map((id) => store.subscribeKey(id, listener)); |
| 51 | + return () => unsubscribers.forEach((unsub) => unsub()); |
| 52 | + }, |
| 53 | + () => { |
| 54 | + // Find first streaming workspace |
| 55 | + for (const id of otherLocalWorkspaceIds) { |
| 56 | + try { |
| 57 | + const state = store.getWorkspaceSidebarState(id); |
| 58 | + if (state.canInterrupt) { |
| 59 | + const meta = workspaceMetadata.get(id); |
| 60 | + return meta?.name ?? id; |
| 61 | + } |
| 62 | + } catch { |
| 63 | + // Workspace may not be registered yet, skip |
| 64 | + } |
| 65 | + } |
| 66 | + return null; |
| 67 | + } |
| 68 | + ); |
| 69 | + |
| 70 | + // Don't show if: |
| 71 | + // - Not a local project-dir runtime |
| 72 | + // - No other local workspaces are streaming |
| 73 | + // - User dismissed the warning |
| 74 | + if (!isLocalProject || !streamingWorkspaceName || dismissed) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + return ( |
| 79 | + <div className="mx-4 mt-2 mb-1 flex items-center justify-between gap-2 rounded bg-yellow-900/30 px-3 py-1.5 text-xs text-yellow-200"> |
| 80 | + <span> |
| 81 | + <strong>{streamingWorkspaceName}</strong> is also running in this project. Agents may |
| 82 | + interfere — consider using only one at a time. |
| 83 | + </span> |
| 84 | + <button |
| 85 | + type="button" |
| 86 | + onClick={() => setDismissed(true)} |
| 87 | + className="shrink-0 text-yellow-400 hover:text-yellow-200" |
| 88 | + title="Dismiss warning" |
| 89 | + > |
| 90 | + ✕ |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + ); |
| 94 | +}; |
0 commit comments