mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* Allow interleaving projects and groups in sidebar * Fix drag refs during render
195 lines
6.5 KiB
TypeScript
195 lines
6.5 KiB
TypeScript
import { normalizeExecutionHostId } from './execution-host'
|
||
import type { ProjectGroup, ProjectGroupCreatedFrom } from './project-group-types'
|
||
import type { Repo } from './repo-types'
|
||
import { createNonSecureContextUuid } from './non-secure-context-uuid'
|
||
|
||
export const UNGROUPED_PROJECT_GROUP_KEY = 'project-group:ungrouped'
|
||
|
||
export function normalizeProjectGroupName(name: string, fallback = 'Untitled group'): string {
|
||
const trimmed = name.trim()
|
||
return trimmed.length > 0 ? trimmed : fallback
|
||
}
|
||
|
||
export function createProjectGroup(input: {
|
||
name: string
|
||
parentPath?: string | null
|
||
connectionId?: string | null
|
||
parentGroupId?: string | null
|
||
createdFrom: ProjectGroupCreatedFrom
|
||
tabOrder: number
|
||
now?: number
|
||
}): ProjectGroup {
|
||
const now = input.now ?? Date.now()
|
||
return {
|
||
id: createNonSecureContextUuid(),
|
||
name: normalizeProjectGroupName(input.name),
|
||
parentPath: input.parentPath ?? null,
|
||
connectionId: input.connectionId ?? null,
|
||
parentGroupId: input.parentGroupId ?? null,
|
||
createdFrom: input.createdFrom,
|
||
tabOrder: input.tabOrder,
|
||
isCollapsed: false,
|
||
color: null,
|
||
createdAt: now,
|
||
updatedAt: now
|
||
}
|
||
}
|
||
|
||
export function normalizeProjectGroups(value: unknown): ProjectGroup[] {
|
||
if (!Array.isArray(value)) {
|
||
return []
|
||
}
|
||
const groups: ProjectGroup[] = []
|
||
const seen = new Set<string>()
|
||
for (const candidate of value) {
|
||
if (!candidate || typeof candidate !== 'object') {
|
||
continue
|
||
}
|
||
const raw = candidate as Partial<ProjectGroup>
|
||
if (typeof raw.id !== 'string' || seen.has(raw.id)) {
|
||
continue
|
||
}
|
||
seen.add(raw.id)
|
||
const now = Date.now()
|
||
const executionHostId = normalizeExecutionHostId(raw.executionHostId)
|
||
groups.push({
|
||
id: raw.id,
|
||
name: normalizeProjectGroupName(typeof raw.name === 'string' ? raw.name : ''),
|
||
parentPath: typeof raw.parentPath === 'string' ? raw.parentPath : null,
|
||
connectionId:
|
||
typeof raw.connectionId === 'string'
|
||
? raw.connectionId
|
||
: raw.connectionId === null
|
||
? null
|
||
: null,
|
||
parentGroupId: typeof raw.parentGroupId === 'string' ? raw.parentGroupId : null,
|
||
createdFrom:
|
||
raw.createdFrom === 'manual' ||
|
||
raw.createdFrom === 'folder-scan' ||
|
||
raw.createdFrom === 'migration'
|
||
? raw.createdFrom
|
||
: 'manual',
|
||
tabOrder:
|
||
typeof raw.tabOrder === 'number' && Number.isFinite(raw.tabOrder) ? raw.tabOrder : 0,
|
||
...(typeof raw.rootProjectOrder === 'number' && Number.isFinite(raw.rootProjectOrder)
|
||
? { rootProjectOrder: raw.rootProjectOrder }
|
||
: {}),
|
||
isCollapsed: raw.isCollapsed === true,
|
||
color: typeof raw.color === 'string' ? raw.color : null,
|
||
createdAt:
|
||
typeof raw.createdAt === 'number' && Number.isFinite(raw.createdAt) ? raw.createdAt : now,
|
||
updatedAt:
|
||
typeof raw.updatedAt === 'number' && Number.isFinite(raw.updatedAt) ? raw.updatedAt : now,
|
||
// Why: runtime-owned groups otherwise look local after persistence reload.
|
||
...(executionHostId ? { executionHostId } : {})
|
||
})
|
||
}
|
||
groups.sort(
|
||
(left, right) => left.tabOrder - right.tabOrder || left.name.localeCompare(right.name)
|
||
)
|
||
for (const group of groups) {
|
||
if (group.parentGroupId === group.id || !seen.has(group.parentGroupId ?? '')) {
|
||
group.parentGroupId = null
|
||
}
|
||
}
|
||
return groups
|
||
}
|
||
|
||
export function clearMissingProjectGroupMemberships(repos: Repo[], groups: ProjectGroup[]): Repo[] {
|
||
const groupIds = new Set(groups.map((group) => group.id))
|
||
return repos.map((repo) =>
|
||
repo.projectGroupId && !groupIds.has(repo.projectGroupId)
|
||
? { ...repo, projectGroupId: null }
|
||
: repo
|
||
)
|
||
}
|
||
|
||
export type ProjectGroupChildIndex = ReadonlyMap<string, string[]>
|
||
|
||
/** Build once and reuse when collecting subtrees for more than one root. */
|
||
export function buildProjectGroupChildIndex(
|
||
groups: readonly Pick<ProjectGroup, 'id' | 'parentGroupId'>[]
|
||
): ProjectGroupChildIndex {
|
||
const childGroupsByParentId = new Map<string, string[]>()
|
||
for (const group of groups) {
|
||
if (!group.parentGroupId) {
|
||
continue
|
||
}
|
||
const children = childGroupsByParentId.get(group.parentGroupId) ?? []
|
||
children.push(group.id)
|
||
childGroupsByParentId.set(group.parentGroupId, children)
|
||
}
|
||
return childGroupsByParentId
|
||
}
|
||
|
||
export function getProjectGroupSubtreeIds(
|
||
groups: readonly Pick<ProjectGroup, 'id' | 'parentGroupId'>[],
|
||
rootGroupId: string
|
||
): Set<string> {
|
||
return collectProjectGroupSubtreeIds(buildProjectGroupChildIndex(groups), rootGroupId)
|
||
}
|
||
|
||
export function collectProjectGroupSubtreeIds(
|
||
childGroupsByParentId: ProjectGroupChildIndex,
|
||
rootGroupId: string
|
||
): Set<string> {
|
||
const subtreeIds = new Set<string>()
|
||
const pending = [rootGroupId]
|
||
while (pending.length > 0) {
|
||
const groupId = pending.pop()!
|
||
if (subtreeIds.has(groupId)) {
|
||
continue
|
||
}
|
||
subtreeIds.add(groupId)
|
||
// Wide imported trees can exceed V8’s spread argument limit; append children individually.
|
||
for (const childGroupId of childGroupsByParentId.get(groupId) ?? []) {
|
||
pending.push(childGroupId)
|
||
}
|
||
}
|
||
return subtreeIds
|
||
}
|
||
|
||
/** Use explicit group rank, else global repo rank, so drag midpoint math and sidebar order agree. */
|
||
export function getEffectiveProjectGroupManualRank(
|
||
repo: Pick<Repo, 'id' | 'projectGroupOrder'> | undefined,
|
||
repoOrderRankById?: ReadonlyMap<string, number>,
|
||
siblingFallbackIndex?: number
|
||
): number {
|
||
if (!repo) {
|
||
return Number.POSITIVE_INFINITY
|
||
}
|
||
const order = repo.projectGroupOrder
|
||
if (typeof order === 'number' && Number.isFinite(order)) {
|
||
return order
|
||
}
|
||
const repoRank = repoOrderRankById?.get(repo.id)
|
||
if (repoRank !== undefined) {
|
||
return repoRank * 1000
|
||
}
|
||
if (siblingFallbackIndex !== undefined) {
|
||
return siblingFallbackIndex * 1000
|
||
}
|
||
return Number.POSITIVE_INFINITY
|
||
}
|
||
|
||
export function getNextProjectGroupOrder(repos: readonly Repo[], groupId: string | null): number {
|
||
let max = -1
|
||
for (const repo of repos) {
|
||
if ((repo.projectGroupId ?? null) !== groupId) {
|
||
continue
|
||
}
|
||
const order = repo.projectGroupOrder
|
||
if (typeof order === 'number' && Number.isFinite(order)) {
|
||
max = Math.max(max, order)
|
||
}
|
||
}
|
||
return max + 1
|
||
}
|
||
|
||
/** Deduplicate group IDs in first-seen order with last-copy values, matching id-keyed lookup behavior across host copies (#30045). */
|
||
export function getDistinctProjectGroups<T extends Pick<ProjectGroup, 'id'>>(
|
||
projectGroups: readonly T[]
|
||
): T[] {
|
||
return [...new Map(projectGroups.map((group) => [group.id, group])).values()]
|
||
}
|