Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

2.0 KB
Raw Blame
// Legacy order precedence: owning group tab order, tab-bar sortOrder, row sortOrder, then creation time. Do not merge disagreeing orders.

import type { TabGroup } from '../tab-types'
import type { LayoutGroup } from './workspace-layout-model'
import { rootGroupSeed } from './workspace-layout-minted-ids'
import { resolveTabGroupOwners } from './tab-group-owners'

export type OrderCandidate = {
  id: string
  /** The group the tab-bar entry names, if any. */
  groupId?: string
  tabBarSortOrder?: number
  rowSortOrder?: number
  createdAt: number
}

function compareUnplaced(left: OrderCandidate, right: OrderCandidate): number {
  const leftOrder = left.tabBarSortOrder ?? left.rowSortOrder ?? Number.POSITIVE_INFINITY
  const rightOrder = right.tabBarSortOrder ?? right.rowSortOrder ?? Number.POSITIVE_INFINITY
  return leftOrder - rightOrder || left.createdAt - right.createdAt
}

export function resolveGroupOrder(args: {
  storedGroups: readonly TabGroup[]
  candidates: readonly OrderCandidate[]
  workspaceKey: string
  mintId: (seed: string) => string
}): LayoutGroup[] {
  const owners = resolveTabGroupOwners(args.candidates, args.storedGroups)
  const placed = new Set<string>()
  const groups: LayoutGroup[] = []
  for (const stored of args.storedGroups) {
    if (groups.some((group) => group.id === stored.id)) {
      continue
    }
    const tabOrder: string[] = []
    for (const tabId of stored.tabOrder) {
      if (owners.get(tabId) === stored.id && !placed.has(tabId)) {
        placed.add(tabId)
        tabOrder.push(tabId)
      }
    }
    groups.push({ id: stored.id, tabOrder })
  }
  const unplaced = args.candidates.filter((candidate) => !placed.has(candidate.id))
  for (const candidate of [...unplaced].sort(compareUnplaced)) {
    let group = groups.find((entry) => entry.id === candidate.groupId) ?? groups[0]
    if (!group) {
      group = { id: args.mintId(rootGroupSeed(args.workspaceKey)), tabOrder: [] }
      groups.push(group)
    }
    group.tabOrder.push(candidate.id)
  }
  return groups.filter((group) => group.tabOrder.length > 0)
}