Files
openchamber/packages/ui/src/lib/projectResolution.ts
T
00821700de chore: remove dead code (59 unused files + ~125 unused exports) (#1835)
* chore: remove dead/unreferenced files across ui, vscode

Remove 59 unused source files (components, hooks, lib utils, stores,
barrels, and orphaned vscode github modules) that are not imported by
any entry-reachable code. Also drop a stale test mock for the removed
execCommands module.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* refactor: remove unused exported symbols (types, functions, consts, hooks)

Remove exported symbols whose identifier is referenced nowhere in the
repository (verified via repo-wide search), across ui types/contracts,
lib utilities, sync layer, stores, and components. Also drop the few
imports/private helpers orphaned by these removals.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* refactor: remove more unused exports (desktop, shortcuts, worktree, vscode)

Continue removing repo-wide unreferenced exported functions, consts and
types across lib/desktop, shortcuts, worktreeSessionCreator, sync, and
vscode gitService, with cascading orphaned helpers/imports cleaned up.

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>

* chore: add dead-code cleanup tooling

* refactor: checkpoint dead-code cleanup

* refactor: remove dead-code suppressions

---------

Co-authored-by: Serhii Dziupin <makeittech@users.noreply.github.com>
Co-authored-by: Bohdan Triapitsyn <artmore@protonmail.com>
2026-06-26 19:27:53 +03:00

70 lines
2.5 KiB
TypeScript

import type { ProjectEntry } from "@/lib/api/types";
import type { WorktreeMetadata } from "@/types/worktree";
export const normalizeProjectPath = (value?: string | null): string | null => {
if (typeof value !== "string") return null;
const trimmed = value.trim();
if (!trimmed) return null;
const replaced = trimmed.replace(/\\/g, "/");
if (replaced === "/") return "/";
return replaced.length > 1 ? replaced.replace(/\/+$/, "") : replaced;
};
export const resolveProjectForDirectory = (
projects: ProjectEntry[],
directory: string | null,
): ProjectEntry | null => {
const nd = normalizeProjectPath(directory);
if (!nd) return null;
let best: ProjectEntry | null = null;
for (const p of projects) {
const pp = normalizeProjectPath(p.path);
if (!pp) continue;
if (nd !== pp && !nd.startsWith(`${pp}/`)) continue;
if (!best || pp.length > (normalizeProjectPath(best.path)?.length ?? 0)) best = p;
}
return best;
};
const resolveProjectFromWorktreeDirectory = (
projects: ProjectEntry[],
availableWorktreesByProject: Map<string, WorktreeMetadata[]>,
directory: string | null,
): ProjectEntry | null => {
const nd = normalizeProjectPath(directory);
if (!nd) return null;
let matchedWorktree: WorktreeMetadata | null = null;
let matchedProjectPath: string | null = null;
let bestLen = -1;
for (const [projectPath, worktrees] of availableWorktreesByProject.entries()) {
for (const wt of worktrees) {
const wp = normalizeProjectPath(wt.path);
if (!wp) continue;
if (nd !== wp && !nd.startsWith(`${wp}/`)) continue;
if (wp.length > bestLen) {
bestLen = wp.length;
matchedWorktree = wt;
matchedProjectPath = normalizeProjectPath(projectPath);
}
}
}
if (!matchedWorktree) return null;
const candidates = [normalizeProjectPath(matchedWorktree.projectDirectory), matchedProjectPath]
.filter((v): v is string => Boolean(v));
for (const c of candidates) {
const exact = projects.find((p) => normalizeProjectPath(p.path) === c) ?? null;
if (exact) return exact;
const nested = resolveProjectForDirectory(projects, c);
if (nested) return nested;
}
return null;
};
export const resolveProjectForSessionDirectory = (
projects: ProjectEntry[],
availableWorktreesByProject: Map<string, WorktreeMetadata[]>,
directory: string | null,
): ProjectEntry | null =>
resolveProjectFromWorktreeDirectory(projects, availableWorktreesByProject, directory) ??
resolveProjectForDirectory(projects, directory);