Files
orca/src/shared/open-in-applications.ts
T
Jinjing e957b86197 Add configurable Open In applications (#2110)
* feat: add configurable Open In menu

Implements configurable Open In applications in worktree context menus with persisted settings, preload/API wiring, and renderer controls/tests.\n\nDesign doc: docs/configurable-open-in-menu.md

* fix: address review findings
2026-05-16 15:34:40 -07:00

59 lines
1.3 KiB
TypeScript

import type { OpenInApplication } from './types'
export const OPEN_IN_APPLICATIONS_MAX = 8
type NormalizeOpenInApplicationsOptions = {
createId?: () => string
}
function normalizeToken(value: unknown): string {
return typeof value === 'string' ? value.trim() : ''
}
function makeFallbackId(index: number): string {
return `open-in-${index + 1}`
}
export function normalizeOpenInApplications(
value: unknown,
options: NormalizeOpenInApplicationsOptions = {}
): OpenInApplication[] {
if (!Array.isArray(value)) {
return []
}
const normalized: OpenInApplication[] = []
const seenIds = new Set<string>()
for (const [index, row] of value.entries()) {
if (normalized.length >= OPEN_IN_APPLICATIONS_MAX) {
break
}
if (!row || typeof row !== 'object') {
continue
}
const label = normalizeToken((row as { label?: unknown }).label)
const command = normalizeToken((row as { command?: unknown }).command)
if (!label || !command) {
continue
}
let id = normalizeToken((row as { id?: unknown }).id)
if (!id) {
id = normalizeToken(options.createId?.())
if (!id) {
id = makeFallbackId(index)
}
}
if (seenIds.has(id)) {
continue
}
seenIds.add(id)
normalized.push({ id, label, command })
}
return normalized
}