43 lines
1.2 KiB
TypeScript
43 lines
1.2 KiB
TypeScript
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) {
|
|
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);
|
|
}
|