Resolve host new-worktree route action before commit (#3242)

This commit is contained in:
Neil
2026-05-30 19:15:42 -07:00
committed by GitHub
parent 67f74a29a8
commit c082f21d2e
3 changed files with 106 additions and 6 deletions
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest'
import {
createInitialHostRouteActionState,
resolveHostRouteActionState,
setHostRouteNewWorktreeVisible
} from './host-route-action-state'
describe('host route action state', () => {
it('opens new worktree modal on an initial newWorktree action', () => {
expect(createInitialHostRouteActionState('newWorktree')).toEqual({
routeAction: 'newWorktree',
showNewWorktree: true
})
})
it('keeps initial non-action routes closed', () => {
expect(createInitialHostRouteActionState(undefined)).toEqual({
routeAction: undefined,
showNewWorktree: false
})
})
it('opens once when route action changes to newWorktree', () => {
expect(
resolveHostRouteActionState({ routeAction: undefined, showNewWorktree: false }, 'newWorktree')
).toEqual({
routeAction: 'newWorktree',
showNewWorktree: true
})
})
it('does not reopen after user closes while route action is unchanged', () => {
const closed = setHostRouteNewWorktreeVisible(
{ routeAction: 'newWorktree', showNewWorktree: true },
false
)
expect(resolveHostRouteActionState(closed, 'newWorktree')).toBe(closed)
})
it('preserves an already-open modal when route action changes away', () => {
expect(
resolveHostRouteActionState({ routeAction: 'newWorktree', showNewWorktree: true }, undefined)
).toEqual({
routeAction: undefined,
showNewWorktree: true
})
})
})
+36
View File
@@ -0,0 +1,36 @@
export type HostRouteActionState = {
routeAction: string | undefined
showNewWorktree: boolean
}
export function createInitialHostRouteActionState(
routeAction: string | undefined
): HostRouteActionState {
return {
routeAction,
showNewWorktree: routeAction === 'newWorktree'
}
}
export function resolveHostRouteActionState(
current: HostRouteActionState,
routeAction: string | undefined
): HostRouteActionState {
if (current.routeAction === routeAction) {
return current
}
return {
routeAction,
showNewWorktree: current.showNewWorktree || routeAction === 'newWorktree'
}
}
export function setHostRouteNewWorktreeVisible(
current: HostRouteActionState,
showNewWorktree: boolean
): HostRouteActionState {
if (current.showNewWorktree === showNewWorktree) {
return current
}
return { ...current, showNewWorktree }
}