codeburn/app/renderer/lib/projectMatch.ts
Mathieu Stanowski 6cac9d929c feat(app): add a Projects pane that scopes the desktop app to chosen projects
Screen-sharing or demoing the app exposed every project on the machine,
with no way to narrow it. Settings now has a Projects pane: one switch
per detected project, plus a field for hiding a pattern that matches
nothing detected yet.

The filter lives in ~/.config/codeburn/app-filter.json rather than
renderer storage, because the main process is where the CLI argv is
assembled. No file, or empty lists, means no filter, so the default is
unchanged for everyone who never opens the pane. CODEBURN_APP_FILTER
overrides the path and an empty value disables it, which is what the app
test suite sets so a real filter file cannot reach the assertions.

Details worth knowing:

- The pane's own project list is fetched WITHOUT the filter. Scoping it
  would hide the projects the checklist exists to switch back on.
- Patterns are passed as --project=<value>, never as two argv entries. A
  Claude project directory is encoded as "-Users-me-Web-thing", and a
  leading dash in its own argv slot parses as a flag.
- Combined scope and a project filter cannot both hold. The CLI rejects
  --scope combined next to --project/--exclude because paired devices
  report their usage unfiltered, and dropping the filter instead would
  put the hidden projects back inside the combined total, which is the
  one thing the pane exists to prevent. So the filter wins: Settings
  offers Local only while something is hidden (and says why), the
  renderer derives its effective scope from the filter so even the first
  poll of a filtered session is local, and buildOverviewArgs keeps the
  same rule as a guard for a stale caller.
- A read that fails on an existing file keeps the last known filter
  instead of falling back to the empty one. Unhiding every project on a
  torn write or a typo mid-edit is the one failure mode this pane cannot
  have. Only a missing file means "show everything" again.
- The pane's matcher lives in renderer/lib/projectMatch.ts, and
  projectMatch.test.ts pins it against the CLI's matchesProjectPattern
  over every pattern shape. A switch that disagrees with the argv it
  writes would lie about what is on screen.
- Writes are serialized: every write is computed from the filter on
  screen, so a second click before the first landed would be built from
  the pre-click value and drop it.
- export carries the filter too, since it writes project names into a
  file that gets shared. getPlans stays unfiltered, like combined scope:
  a plan is billed on every project, so scoping it would misreport the
  cycle.
2026-09-12 05:38:52 -07:00

41 lines
2 KiB
TypeScript

/** The project filter rule, restated for the renderer: src/parser.ts reaches
* for node:fs, so the pane cannot import it. projectMatch.test.ts pins the two
* against each other, because a switch that disagrees with the CLI lies. */
type MatchTarget = { name: string; path?: string }
function isRooted(pattern: string): boolean {
const raw = pattern.trim().replace(/\\/g, '/')
return raw.startsWith('/') || /^[a-zA-Z]:\//.test(raw)
}
/** normalizeAbsProjectPathKey: identified Windows paths casefold, POSIX does not. */
export function absProjectPathKey(value: string): string | null {
const raw = value.trim().replace(/\\/g, '/')
if (!raw) return null
if (!raw.startsWith('/') && !/^[a-zA-Z]:\//.test(raw) && !(raw.includes('/') && !raw.startsWith('-'))) return null
const windows = /^[a-zA-Z]:(\/|$)/.test(raw) || raw.startsWith('//')
return (windows ? raw.toLowerCase() : raw).replace(/^\/+/, '').replace(/\/+$/, '') || null
}
export function projectMatches(project: MatchTarget, pattern: string): boolean {
const projectPath = project.path ?? ''
if (isRooted(pattern)) {
const anchor = absProjectPathKey(pattern)
const target = absProjectPathKey(projectPath)
return anchor !== null && target !== null && (target === anchor || target.startsWith(anchor + '/'))
}
const needle = pattern.toLowerCase()
return project.name.toLowerCase().includes(needle) || projectPath.toLowerCase().includes(needle)
}
/** Two rows can share a name (repo and subdirectory), so the path is the key. */
export function projectPattern(project: MatchTarget): string {
const raw = (project.path ?? '').trim()
if (!raw) return project.name
// A Codex-recorded path can arrive stripped of its leading slash. Rooting it
// is what routes the pattern through the anchored branch, instead of a
// substring that would hide the siblings sharing its prefix too.
const rooted = raw.startsWith('/') || /^[a-zA-Z]:[\\/]/.test(raw) || !raw.includes('/')
return rooted ? raw : `/${raw}`
}