Skip to content
Closed
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
44 changes: 23 additions & 21 deletions apps/desktop/src/lib/trpc/routers/projects/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { publicProcedure, router } from "../..";
import {
activateProject,
getBranchWorkspace,
selectNextActiveWorkspace,
setLastActiveWorkspace,
touchWorkspace,
} from "../workspaces/utils/db-helpers";
Expand Down Expand Up @@ -941,48 +942,49 @@ export const createProjectsRouter = (getWindow: () => BrowserWindow | null) => {
.where(eq(workspaces.projectId, input.id))
.all();

let totalFailed = 0;
const registry = getWorkspaceRuntimeRegistry();
for (const workspace of projectWorkspaces) {
const terminal = registry.getForWorkspaceId(workspace.id).terminal;
const terminalResult = await terminal.killByWorkspaceId(workspace.id);
totalFailed += terminalResult.failed;
}

const closedWorkspaceIds = projectWorkspaces.map((w) => w.id);

// Must run before the async terminal kill so queries exclude
// these workspaces while terminals are still shutting down.
if (closedWorkspaceIds.length > 0) {
localDb
.delete(workspaces)
.update(workspaces)
.set({ deletingAt: Date.now() })
.where(inArray(workspaces.id, closedWorkspaceIds))
.run();
}

// Hide the project by setting tabOrder to null
localDb
.update(projects)
.set({ tabOrder: null })
.where(eq(projects.id, input.id))
.run();

// Update active workspace if it was in this project
const currentSettings = localDb.select().from(settings).get();
if (
currentSettings?.lastActiveWorkspaceId &&
closedWorkspaceIds.includes(currentSettings.lastActiveWorkspaceId)
) {
const remainingWorkspaces = localDb
.select()
.from(workspaces)
.orderBy(desc(workspaces.lastOpenedAt))
.all();
setLastActiveWorkspace(selectNextActiveWorkspace());
}

const registry = getWorkspaceRuntimeRegistry();
const terminalResults = await Promise.all(
projectWorkspaces.map((workspace) =>
registry
.getForWorkspaceId(workspace.id)
.terminal.killByWorkspaceId(workspace.id),
),
);
const totalFailed = terminalResults.reduce(
(sum, r) => sum + r.failed,
0,
);

if (closedWorkspaceIds.length > 0) {
localDb
.update(settings)
.set({
lastActiveWorkspaceId: remainingWorkspaces[0]?.id ?? null,
})
.where(eq(settings.id, 1))
.delete(workspaces)
.where(inArray(workspaces.id, closedWorkspaceIds))
.run();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,17 @@ export const createDeleteProcedures = () => {
throw new Error("Workspace not found");
}

// Must run before the async terminal kill so queries exclude
// this workspace while terminals are still shutting down.
markWorkspaceAsDeleting(input.id);
updateActiveWorkspaceIfRemoved(input.id);

const terminalResult = await getWorkspaceRuntimeRegistry()
.getForWorkspaceId(input.id)
.terminal.killByWorkspaceId(input.id);

deleteWorkspace(input.id);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
hideProjectIfNoWorkspaces(workspace.projectId);
updateActiveWorkspaceIfRemoved(input.id);

const terminalWarning =
terminalResult.failed > 0
Expand Down