From 7eba0668131452d382e72ea33924f2352c1fd1e3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Sat, 17 Oct 2026 00:16:09 +0000 Subject: [PATCH] refactor(ui): reuse pathLeaf instead of local basename copies App, Review, and Commits each inlined the same last-segment helper. Call the existing pathLeaf from repoIdentity and pin it against the deleted split/filter algorithm. Co-authored-by: Daniels --- ui/src/App.tsx | 18 +++++-------- ui/src/lib/repoIdentity.test.ts | 45 +++++++++++++++++++++++++++++++++ ui/src/views/Commits.tsx | 6 ++--- ui/src/views/Review.tsx | 7 ++--- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/ui/src/App.tsx b/ui/src/App.tsx index 24dc242e..bcefe234 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -29,7 +29,7 @@ import { DEFAULT_WORKSPACE_ID, useWorkspaces } from './stores/workspaces'; import { useWorkspaceReview } from './stores/workspaceReview'; import { useUpdates } from './stores/updates'; import { UPDATES_MANAGED_BY_STORE } from './lib/distribution'; -import { accentHueForColor, groupTabs, pathKey, repoFamilyName, workspaceMemberSet } from './lib/repoIdentity'; +import { accentHueForColor, groupTabs, pathKey, pathLeaf, repoFamilyName, workspaceMemberSet } from './lib/repoIdentity'; import { buildContentReportUrl, buildCrashIssueUrl } from './lib/crashReport'; import { pickCodeWorkspaceFile, pickRepoDirectories } from './lib/dialog'; import { editorTemplate, osType, terminalTemplate } from './lib/integrations'; @@ -204,12 +204,6 @@ interface OpProgress { error?: string | null; } -/** Last path segment of a filesystem path (handles `/` and `\`). */ -function basename(p: string): string { - const parts = p.split(/[\\/]/).filter(Boolean); - return parts[parts.length - 1] ?? p; -} - const MAIN_SURFACE_BY_VIEW: Partial> = { local: BUILT_IN_SURFACE_IDS.localChanges, review: BUILT_IN_SURFACE_IDS.review, @@ -805,7 +799,7 @@ export function App() { id, kind: 'open', title: 'Opening', - subject: basename(path), + subject: pathLeaf(path), percent: null, detail: 'Reading repository…', eta: null, @@ -848,7 +842,7 @@ export function App() { id, kind: 'clone', title: 'Cloning', - subject: basename(dest), + subject: pathLeaf(dest), percent: null, detail: 'Starting…', eta: null, @@ -892,7 +886,7 @@ export function App() { // open the freshly cloned repo. setOpProgress((cur) => cur && cur.id === id - ? { id, kind: 'open', title: 'Opening', subject: basename(clonedPath!), percent: null, detail: 'Reading repository…', eta: null, startedAt: Date.now() } + ? { id, kind: 'open', title: 'Opening', subject: pathLeaf(clonedPath!), percent: null, detail: 'Reading repository…', eta: null, startedAt: Date.now() } : cur, ); try { @@ -918,7 +912,7 @@ export function App() { request.createInitialCommit, ); await useWorkspaces.getState().openRepoInActive(outcome.path); - showToast(`Initialized ${basename(outcome.path)} on ${outcome.initial_branch}`); + showToast(`Initialized ${pathLeaf(outcome.path)} on ${outcome.initial_branch}`); }, [showToast]); // Open a batch of repos as tabs, one after another. Sequential (not @@ -956,7 +950,7 @@ export function App() { // rail and the toolbar tab strip (whichever `repoNav` selects). const openIconDialog = useCallback((path: string) => { const tab = useRepo.getState().tabs.find((t) => t.path === path); - setIconDialog({ path, name: tab ? repoFamilyName(tab.meta) : basename(path) }); + setIconDialog({ path, name: tab ? repoFamilyName(tab.meta) : pathLeaf(path) }); }, []); // Switch the active repository to the next (+1) / previous (-1) open one, diff --git a/ui/src/lib/repoIdentity.test.ts b/ui/src/lib/repoIdentity.test.ts index 67d7201e..83039958 100644 --- a/ui/src/lib/repoIdentity.test.ts +++ b/ui/src/lib/repoIdentity.test.ts @@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest'; import { mainPathFromCommonDir, pathKey, + pathLeaf, repoFamilyName, repoTabLabel, tabWorktreeName, @@ -86,6 +87,50 @@ describe('repo identity labels', () => { }); }); +describe('pathLeaf', () => { + /** Local copies deleted from App/Review/Commits: `split(/[\\/]/).filter(Boolean).pop() ?? p`. */ + function legacyBasename(p: string): string { + return p.split(/[\\/]/).filter(Boolean).pop() ?? p; + } + + it('returns the last segment of unix and windows paths', () => { + expect(pathLeaf('/src/strand')).toBe('strand'); + expect(pathLeaf('C:\\dev\\portal')).toBe('portal'); + expect(pathLeaf('C:/dev/portal')).toBe('portal'); + expect(pathLeaf('strand')).toBe('strand'); + }); + + it('ignores trailing and repeated separators', () => { + expect(pathLeaf('/src/strand/')).toBe('strand'); + expect(pathLeaf('C:\\dev\\portal\\')).toBe('portal'); + expect(pathLeaf('/src//strand')).toBe('strand'); + }); + + it('returns the original string when there is no usable segment', () => { + expect(pathLeaf('')).toBe(''); + expect(pathLeaf('/')).toBe('/'); + expect(pathLeaf('\\\\')).toBe('\\\\'); + }); + + it('matches the deleted split/filter basename copies', () => { + const samples = [ + '/src/strand', + '/src/strand/', + '/src//strand', + 'C:\\dev\\portal', + 'C:\\dev\\portal\\', + 'C:/dev/portal', + 'strand', + '', + '/', + '\\\\', + ]; + for (const sample of samples) { + expect(pathLeaf(sample)).toBe(legacyBasename(sample)); + } + }); +}); + describe('path identity', () => { it('maps every spelling of a directory to one key', () => { expect(pathKey('D:\\GitSources\\strand')).toBe('D:/GitSources/strand'); diff --git a/ui/src/views/Commits.tsx b/ui/src/views/Commits.tsx index 733fd4ef..3cf02a50 100644 --- a/ui/src/views/Commits.tsx +++ b/ui/src/views/Commits.tsx @@ -15,7 +15,7 @@ import { type LocalGitOp, } from '../lib/localGitOp'; import { providerMergedBranchNames } from '../lib/branchIntegration'; -import { pathKey } from '../lib/repoIdentity'; +import { pathKey, pathLeaf } from '../lib/repoIdentity'; import { errMessage, tauri } from '../lib/tauri'; import type { Commit, Refs, Stash } from '../lib/types'; import { useRepo } from '../stores/repo'; @@ -932,7 +932,7 @@ export function Commits({ onCreateTag, onInteractiveRebase, onResetTo, onCreateW title={`Back to ${fileReturn}`} > - Back to {fileReturn.split(/[\\/]/).filter(Boolean).pop() ?? fileReturn} + Back to {pathLeaf(fileReturn)} )}
@@ -1009,7 +1009,7 @@ export function Commits({ onCreateTag, onInteractiveRebase, onResetTo, onCreateW title={`Back to ${workFileReturn.path}`} > - Back to {workFileReturn.path.split(/[\\/]/).filter(Boolean).pop() ?? workFileReturn.path} + Back to {pathLeaf(workFileReturn.path)} )}