refactor: move workspace tab navigation logic to prepareWorkspaceTab

This commit is contained in:
Mohamed Boudra
2026-03-21 15:21:02 +07:00
parent 78fc1fe862
commit 48c3308c31
17 changed files with 184 additions and 390 deletions
@@ -0,0 +1,42 @@
import { useWorkspaceLayoutStore } from "@/stores/workspace-layout-store";
import { generateDraftId } from "@/stores/draft-keys";
import {
buildWorkspaceTabPersistenceKey,
type WorkspaceTabTarget,
} from "@/stores/workspace-tabs-store";
import { buildHostWorkspaceRoute } from "@/utils/host-routes";
interface PrepareWorkspaceTabInput {
serverId: string;
workspaceId: string;
target: WorkspaceTabTarget;
pin?: boolean;
}
function getPreparedTarget(target: WorkspaceTabTarget): WorkspaceTabTarget {
if (target.kind !== "draft" || target.draftId.trim() !== "new") {
return target;
}
return { kind: "draft", draftId: generateDraftId() };
}
export function prepareWorkspaceTab(input: PrepareWorkspaceTabInput): string {
const target = getPreparedTarget(input.target);
const key =
buildWorkspaceTabPersistenceKey({
serverId: input.serverId,
workspaceId: input.workspaceId,
}) ?? "";
const tabId = useWorkspaceLayoutStore.getState().openTab(key, target);
if (tabId) {
useWorkspaceLayoutStore.getState().focusTab(key, tabId);
}
if (input.pin && target.kind === "agent") {
useWorkspaceLayoutStore.getState().pinAgent(key, target.agentId);
}
return buildHostWorkspaceRoute(input.serverId, input.workspaceId);
}