orca/src/shared/workspace-scope.ts

48 lines
1.6 KiB
TypeScript

import type { WorkspaceKey, WorkspaceScope } from './types'
export function worktreeWorkspaceKey(worktreeId: string): WorkspaceKey {
return `worktree:${worktreeId}`
}
export function folderWorkspaceKey(folderWorkspaceId: string): WorkspaceKey {
return `folder:${folderWorkspaceId}`
}
export function workspaceKeyFromScope(scope: WorkspaceScope): WorkspaceKey {
return scope.type === 'worktree'
? worktreeWorkspaceKey(scope.worktreeId)
: folderWorkspaceKey(scope.folderWorkspaceId)
}
export function parseWorkspaceKey(value: string): WorkspaceScope | null {
if (value.startsWith('worktree:')) {
const worktreeId = value.slice('worktree:'.length)
return worktreeId.length > 0 ? { type: 'worktree', worktreeId } : null
}
if (value.startsWith('folder:')) {
const folderWorkspaceId = value.slice('folder:'.length)
return folderWorkspaceId.length > 0 ? { type: 'folder', folderWorkspaceId } : null
}
return null
}
export function isWorkspaceKey(value: string): value is WorkspaceKey {
return parseWorkspaceKey(value) !== null
}
// Why: folder workspaces are tracked by the scoped active key, while older
// worktree-only paths still read activeWorktreeId.
export function getActiveSidebarWorkspaceId(
activeWorkspaceKey: string | null,
activeWorktreeId: string | null
): string | null {
const scope = activeWorkspaceKey ? parseWorkspaceKey(activeWorkspaceKey) : null
if (scope?.type === 'folder') {
return folderWorkspaceKey(scope.folderWorkspaceId)
}
if (scope?.type === 'worktree') {
return scope.worktreeId
}
return activeWorktreeId
}