Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useLiveQuery } from "@tanstack/react-db";
import { useCallback, useMemo } from "react";
import { useV2AgentConfigs } from "renderer/hooks/useV2AgentConfigs";
import { buildAgentLaunchCommand } from "renderer/lib/agent-launch-command";
import { getHostServiceClientByUrl } from "renderer/lib/host-service-client";
import { useWorkspace } from "renderer/routes/_authenticated/_dashboard/v2-workspace/providers/WorkspaceProvider";
import { useCollections } from "renderer/routes/_authenticated/providers/CollectionsProvider";
import type { V2TerminalPresetRow } from "renderer/routes/_authenticated/providers/CollectionsProvider/dashboardSidebarLocal";
Expand Down Expand Up @@ -74,31 +73,24 @@ export function useV2PresetExecution({
[allPresets, projectId],
);

// `useV2AgentConfigs` is the cached source of truth for agent configs
// (`staleTime: Infinity`, invalidated on every Settings → Agents mutation),
// so resolving against the in-memory `agents` array is correct and
// synchronous. Re-fetching via the host-service client on every call would
// duplicate that query and pin this function async, which forced the
// previous consumer (`useV2WorkspaceRun`) into a re-render cycle.
const resolvePresetCommands = useCallback(
async (preset: V2TerminalPresetRow): Promise<string[]> => {
(preset: V2TerminalPresetRow): string[] => {
if (!preset.agentId) return preset.commands;

let resolveAgents = agents;
if (activeHostUrl) {
try {
resolveAgents =
await getHostServiceClientByUrl(
activeHostUrl,
).settings.agentConfigs.list.query();
} catch {
resolveAgents = agents;
}
}

const linkedAgent = findLinkedAgent(resolveAgents, preset.agentId);
const linkedAgent = findLinkedAgent(agents, preset.agentId);
const live =
linkedAgent && linkedAgent.command.trim().length > 0
? buildAgentLaunchCommand(linkedAgent)
: undefined;
if (live) return [live];
return preset.commands;
},
[activeHostUrl, agents],
[agents],
);

const executePreset = useCallback(
Expand All @@ -107,7 +99,7 @@ export function useV2PresetExecution({
const activeTabId = state.activeTabId;
const target = resolveTarget(preset.executionMode);
const title = preset.name || undefined;
const commands = await resolvePresetCommands(preset);
const commands = resolvePresetCommands(preset);

const plan = getPresetLaunchPlan({
mode: preset.executionMode,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { toast } from "@superset/ui/sonner";
import { workspaceTrpc } from "@superset/workspace-client";
import { eq } from "@tanstack/db";
import { useLiveQuery } from "@tanstack/react-db";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useCallback, useMemo, useRef, useState } from "react";
import { useWorkspaceEvent } from "renderer/hooks/host-service/useWorkspaceEvent";
import { buildTerminalCommand } from "renderer/lib/terminal/launch-command";
import { useWorkspace } from "renderer/routes/_authenticated/_dashboard/v2-workspace/providers/WorkspaceProvider";
Expand Down Expand Up @@ -71,7 +71,7 @@ interface UseV2WorkspaceRunArgs {
store: StoreApi<WorkspaceStore<PaneViewerData>>;
launcher: TerminalLauncher;
matchedPresets: V2TerminalPresetRow[];
resolvePresetCommands: (preset: V2TerminalPresetRow) => Promise<string[]>;
resolvePresetCommands: (preset: V2TerminalPresetRow) => string[];
}

export function useV2WorkspaceRun({
Expand All @@ -89,10 +89,6 @@ export function useV2WorkspaceRun({
const utils = workspaceTrpc.useUtils();
const writeInputMutation = workspaceTrpc.terminal.writeInput.useMutation();
const killSessionMutation = workspaceTrpc.terminal.killSession.useMutation();
const [resolvedPresetCommandsById, setResolvedPresetCommandsById] = useState<
Record<string, string[]>
>({});

const { data: localWorkspaceRows = [] } = useLiveQuery(
(query) =>
query
Expand All @@ -111,44 +107,13 @@ export function useV2WorkspaceRun({
const { data: configRunDefinition } =
workspaceTrpc.config.getWorkspaceRunDefinition.useQuery({ projectId });

useEffect(() => {
let cancelled = false;

async function resolveCommands() {
const entries = await Promise.all(
matchedPresets.map(async (preset) => {
try {
return {
id: preset.id,
commands: await resolvePresetCommands(preset),
};
} catch {
return { id: preset.id, commands: preset.commands };
}
}),
);
if (cancelled) return;
const next: Record<string, string[]> = {};
for (const entry of entries) {
next[entry.id] = entry.commands;
}
setResolvedPresetCommandsById(next);
}

void resolveCommands();

return () => {
cancelled = true;
};
}, [matchedPresets, resolvePresetCommands]);

const resolvedMatchedPresets = useMemo(
() =>
matchedPresets.map((preset) => ({
...preset,
commands: resolvedPresetCommandsById[preset.id] ?? preset.commands,
commands: resolvePresetCommands(preset),
})),
[matchedPresets, resolvedPresetCommandsById],
[matchedPresets, resolvePresetCommands],
);
Comment thread
greptile-apps[bot] marked this conversation as resolved.

const definition = useMemo(
Expand Down
Loading