Files
Netcatty/components/host/HostDetailsScriptsSection.tsx
T
陈大猫andCursor 735c047a09 [Bug] Fix host connect script queue selection (#2705) (#2716)
* fix(hosts): keep draft connect scripts before onConnect promote

Host details queue editing pruned non-onConnect scripts immediately, so
default manual automation scripts could be selected but never appeared
in the queue. Keep an editable draft queue and only prune missing IDs
until save promotes triggers.

Closes #2705

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): promote persisted manual connect-queue scripts on save

Editable queue retention left already-saved manual script IDs unpromoted
when reopen/save compared only newly added IDs. Promote every queued
script that still needs onConnect + host targeting.

Co-authored-by: Cursor <cursoragent@cursor.com>

* chore(hosts): drop unused HostDetailsPanel snippet imports

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): do not demote global scripts during queue save sync

Promote-on-save must leave targetsAllHosts onConnect scripts unchanged
when they appear in a host connectScriptIds list.

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(hosts): cover global connect scripts during queue save sync

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): promote connect-queue scripts on new host save

New hosts have no initialData, so queue promote-on-save was skipped and
manual scripts stayed non-onConnect after the first save.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): harden connect-queue promote against Codex review

Preserve global scope when promoting manuals, skip re-promoting scripts
demoted while the host editor is open, sync peer host queues for retained
targets, and keep test fixtures ASCII-only.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): persist peer connect queues with edited host save

Peer queue sync via onHostsChange was overwritten by onSave rebuilding
from a stale hosts snapshot. Write peers+edited host together and make
save paths apply functional host updates.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): keep connect queues across hydration and concurrent edits

Skip queue rewrite while snippets are still empty, and do not promote
entries whose non-onConnect trigger changed while the host editor stayed
open.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): skip connect-queue rewrite before snippets hydrate

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): preserve concurrent target removals and merge peer deltas

Drop stale queue entries when targets were unlinked while editing, and
apply peer connectScriptIds updates via functional host state merges.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): hydrate connect-queue baseline and append peer deltas

Capture the first hydrated snippet catalog as the editor baseline, and
merge peer queue updates by appending only newly queued script IDs.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): snapshot opening queue and keep peer updates host-safe

Use the editor-opening connect queue as sync previous IDs, and never
overwrite the edited host inside peer delta merges.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): clear deleted queues after hydrate and honor manual unlinks

Treat a hydrated empty snippet catalog as ready for queue reconciliation,
and drop concurrently untargeted non-onConnect queue entries on save.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(hosts): clear loginScriptId when connect queue is emptied

sanitizeHost reconstitutes connectScriptIds from loginScriptId when the
explicit queue is absent, so clearing the last queued script must also
drop the legacy field.

Co-authored-by: Cursor <cursoragent@cursor.com>

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-05 09:53:37 +08:00

238 lines
9.0 KiB
TypeScript

import { Globe, Link2, Play, Trash2 } from 'lucide-react';
import React, { useCallback, useMemo, useState, type DragEvent } from 'react';
import type { Host, Snippet } from '@/domain/models';
import {
appendHostConnectScript,
getEditableHostConnectScriptIds,
getGlobalConnectScripts,
removeHostConnectScript,
reorderHostConnectScript,
} from '@/domain/hostConnectScripts.ts';
import { isScriptSnippet } from '@/domain/snippetScript.ts';
import { cn } from '@/lib/utils';
import { getVaultDropPosition } from '@/components/vault/vaultReorderDrag';
import {
VaultEntityIcon,
vaultAutomationScriptIconClass,
vaultEntityIconSmClass,
} from '@/components/vault/VaultEntityIcon';
import { HostDetailsSection } from '../host-details';
import { Button } from '../ui/button';
import { Combobox } from '../ui/combobox';
const CONNECT_QUEUE_DRAG_TYPE = 'application/x-netcatty-connect-script-id';
export interface HostDetailsScriptsSectionProps {
host: Host;
onHostChange: (host: Host) => void;
snippets: Snippet[];
t: (key: string, params?: Record<string, unknown>) => string;
}
function triggerLabel(
snippet: Snippet,
t: HostDetailsScriptsSectionProps['t'],
): string {
if (snippet.trigger === 'onConnect') return t('scripts.trigger.onConnect');
if (snippet.trigger === 'onOutput') return t('scripts.trigger.onOutput');
return t('scripts.trigger.manual');
}
function scriptById(snippets: Snippet[], scriptId: string): Snippet | undefined {
return snippets.find((snippet) => snippet.id === scriptId && isScriptSnippet(snippet));
}
export const HostDetailsScriptsSection: React.FC<HostDetailsScriptsSectionProps> = ({
host,
onHostChange,
snippets,
t,
}) => {
const [draggingScriptId, setDraggingScriptId] = useState<string | null>(null);
const [dropIndicator, setDropIndicator] = useState<{ scriptId: string; position: 'before' | 'after' } | null>(null);
const scripts = useMemo(
() => snippets.filter(isScriptSnippet),
[snippets],
);
const globalScripts = useMemo(
() => getGlobalConnectScripts(snippets),
[snippets],
);
const queueIds = useMemo(
() => getEditableHostConnectScriptIds(host, snippets),
[host, snippets],
);
const queuedScripts = useMemo(
() => queueIds
.map((id) => scriptById(snippets, id))
.filter((snippet): snippet is Snippet => Boolean(snippet)),
[queueIds, snippets],
);
const linkableScripts = useMemo(
() => scripts.filter((script) => {
if (!script.id) return false;
if (script.targetsAllHosts) return false;
if (queueIds.includes(script.id)) return false;
return true;
}),
[queueIds, scripts],
);
const linkOptions = useMemo(
() => linkableScripts.map((script) => ({
value: script.id!,
label: script.label || t('scripts.running.unnamed'),
})),
[linkableScripts, t],
);
const clearDragState = useCallback(() => {
setDraggingScriptId(null);
setDropIndicator(null);
}, []);
if (scripts.length === 0) return null;
const handleAddToQueue = (scriptId: string) => {
if (!scriptId) return;
const snippet = scriptById(snippets, scriptId);
if (!snippet) return;
onHostChange(appendHostConnectScript(host, scriptId, snippets));
};
const handleRemoveFromQueue = (scriptId: string) => {
onHostChange(removeHostConnectScript(host, scriptId, snippets));
};
const handleDragStart = (event: DragEvent<HTMLDivElement>, scriptId: string) => {
event.dataTransfer.effectAllowed = 'move';
event.dataTransfer.setData(CONNECT_QUEUE_DRAG_TYPE, scriptId);
setDraggingScriptId(scriptId);
};
const handleDragOver = (event: DragEvent<HTMLDivElement>, scriptId: string) => {
if (!event.dataTransfer.types.includes(CONNECT_QUEUE_DRAG_TYPE)) return;
event.preventDefault();
event.dataTransfer.dropEffect = 'move';
const position = getVaultDropPosition(event.currentTarget, event.clientX, event.clientY, false);
setDropIndicator((prev) => (
prev?.scriptId === scriptId && prev.position === position
? prev
: { scriptId, position }
));
};
const handleDrop = (event: DragEvent<HTMLDivElement>, targetScriptId: string) => {
event.preventDefault();
const draggedScriptId = event.dataTransfer.getData(CONNECT_QUEUE_DRAG_TYPE);
if (!draggedScriptId || draggedScriptId === targetScriptId) {
clearDragState();
return;
}
const position = getVaultDropPosition(event.currentTarget, event.clientX, event.clientY, false);
onHostChange(reorderHostConnectScript(host, draggedScriptId, targetScriptId, position, snippets));
clearDragState();
};
return (
<HostDetailsSection
icon={<Play size={14} className="text-muted-foreground" />}
title={t('hostDetails.section.automation')}
hint={t('hostDetails.automation.queueHint')}
>
<div className="space-y-4">
{globalScripts.length > 0 ? (
<div className="space-y-2">
<div className="flex items-center gap-1.5">
<Globe size={12} className="text-muted-foreground shrink-0" />
<label className="text-xs text-muted-foreground">{t('hostDetails.automation.globalScripts')}</label>
</div>
<p className="text-[10px] text-muted-foreground leading-relaxed">
{t('hostDetails.automation.globalScriptsHint')}
</p>
<div className="space-y-1.5">
{globalScripts.map((script) => (
<div
key={script.id}
className="flex items-center gap-2.5 px-2.5 py-2 rounded-md border border-border/50 bg-muted/20"
>
<VaultEntityIcon
className={cn(vaultEntityIconSmClass, vaultAutomationScriptIconClass)}
icon={<Play size={14} />}
/>
<div className="min-w-0 flex-1">
<div className="text-sm font-medium truncate">{script.label || t('scripts.running.unnamed')}</div>
<div className="text-[10px] text-muted-foreground truncate">{triggerLabel(script, t)}</div>
</div>
</div>
))}
</div>
</div>
) : null}
<div className="space-y-2">
{queuedScripts.length === 0 ? (
<p className="text-[11px] text-muted-foreground">{t('hostDetails.automation.connectQueueEmpty')}</p>
) : (
<div className="space-y-1.5" onDragEnd={clearDragState}>
{queuedScripts.map((script) => {
const scriptId = script.id!;
const isDragging = draggingScriptId === scriptId;
const indicator = dropIndicator?.scriptId === scriptId ? dropIndicator.position : null;
return (
<div
key={scriptId}
draggable
data-vault-drop-position={indicator ?? undefined}
data-vault-drop-axis="y"
className={cn(
'vault-drop-indicator-row relative flex items-center gap-2.5 px-2.5 py-2 rounded-md border border-border/70 bg-background/60 transition-opacity cursor-grab active:cursor-grabbing',
isDragging && 'opacity-40',
)}
aria-label={t('hostDetails.automation.dragHandle')}
onDragStart={(event) => handleDragStart(event, scriptId)}
onDragOver={(event) => handleDragOver(event, scriptId)}
onDrop={(event) => handleDrop(event, scriptId)}
>
<VaultEntityIcon
className={cn(vaultEntityIconSmClass, vaultAutomationScriptIconClass)}
icon={<Play size={14} />}
/>
<div className="min-w-0 flex-1 select-none">
<div className="text-sm font-medium truncate">{script.label || t('scripts.running.unnamed')}</div>
<div className="text-[10px] text-muted-foreground truncate">{triggerLabel(script, t)}</div>
</div>
<Button
type="button"
variant="ghost"
size="icon"
className="h-7 w-7 shrink-0 text-muted-foreground hover:text-destructive"
onMouseDown={(event) => event.stopPropagation()}
onClick={() => handleRemoveFromQueue(scriptId)}
aria-label={t('hostDetails.automation.removeFromQueue')}
>
<Trash2 size={13} />
</Button>
</div>
);
})}
</div>
)}
{linkOptions.length > 0 ? (
<Combobox
options={linkOptions}
value=""
onValueChange={handleAddToQueue}
placeholder={t('hostDetails.automation.addToQueuePlaceholder')}
icon={<Link2 size={14} />}
triggerClassName="h-9"
/>
) : null}
</div>
</div>
</HostDetailsSection>
);
};