Skip to content
Merged
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
23 changes: 20 additions & 3 deletions apps/desktop/src/lib/trpc/routers/workspaces/procedures/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { getWorkspace } from "../utils/db-helpers";
import { detectBaseBranch, hasOriginRemote } from "../utils/git";
import { getWorkspacePath } from "../utils/worktree";

type WorktreePathMap = Map<string, string>;

export const createQueryProcedures = () => {
return router({
get: publicProcedure
Expand Down Expand Up @@ -34,6 +36,12 @@ export const createQueryProcedures = () => {
.where(isNotNull(projects.tabOrder))
.all();

// Preload all worktrees once to avoid N+1 queries in the loop below
const allWorktrees = localDb.select().from(worktrees).all();
const worktreePathMap: WorktreePathMap = new Map(
allWorktrees.map((wt) => [wt.id, wt.path]),
);

const groupsMap = new Map<
string,
{
Expand Down Expand Up @@ -84,11 +92,20 @@ export const createQueryProcedures = () => {
.sort((a, b) => a.tabOrder - b.tabOrder);

for (const workspace of allWorkspaces) {
if (groupsMap.has(workspace.projectId)) {
groupsMap.get(workspace.projectId)?.workspaces.push({
const group = groupsMap.get(workspace.projectId);
if (group) {
// Resolve path from preloaded data instead of per-workspace DB queries
let worktreePath = "";
if (workspace.type === "worktree" && workspace.worktreeId) {
worktreePath = worktreePathMap.get(workspace.worktreeId) ?? "";
} else if (workspace.type === "branch") {
worktreePath = group.project.mainRepoPath;
}

group.workspaces.push({
...workspace,
type: workspace.type as "worktree" | "branch",
worktreePath: getWorkspacePath(workspace) ?? "",
worktreePath,
isUnread: workspace.isUnread ?? false,
});
}
Expand Down
Loading