Files
buzz/desktop/src/features/projects/lib/projectHomeSelection.ts
thomaspblock 9733863516 perf(desktop): restore project context during startup (#6939)
## Summary
Project channels previously appeared as ordinary channels until the
community-wide project enumeration completed, delaying the contextual
right rail most noticeably on larger relays.

- Restore the last fully validated, relay-and-identity-scoped project
collection immediately while keeping live relay data authoritative.
- Resolve the active channel's project home through scoped
`#buzz-channel` queries instead of waiting for the complete project
scan.
- Keep snapshots aligned with community removal and relay reconnect
invalidation.

### Related issue
None found.

### Testing
- Pre-push `file-size-check`, `desktop-check`, `desktop-typecheck`, and
`desktop-test`
- Project enumeration, snapshot persistence, and relay invalidation unit
coverage
- Targeted Playwright scenario: project sidebar rows open the home
channel and nest extra channels

No numeric startup benchmark was captured; this draft validates the
cache and scoped-query behavior while leaving timing measurement for
review.

---------

Signed-off-by: Thomas Petersen <thomasp@squareup.com>
2026-08-27 22:36:26 -04:00

61 lines
1.7 KiB
TypeScript

import type { Project } from "@/features/projects/projectModels";
/** Resolves the canonical visible project home for a channel. */
export function findProjectHomeByChannelId(
channelId: string | null | undefined,
projects: readonly Project[],
): Project | null {
if (!channelId) return null;
const matching = projects
.filter(
(project) =>
!project.legacy &&
project.projectChannelId === channelId &&
hasAuthoritativeHomeBinding(project),
)
.sort((left, right) => left.createdAt - right.createdAt);
return (
matching.find((project) => project.visibility !== "unlisted") ??
matching[0] ??
null
);
}
export type ProjectHomeCandidate = {
owner: string;
projectChannelId: string | null;
repositories: ReadonlyArray<{
channelId?: string | null;
maintainers?: ReadonlyArray<string>;
owner: string;
}>;
};
export function hasAuthoritativeHomeBinding(
project: ProjectHomeCandidate,
): boolean {
const channelId = project.projectChannelId;
if (!channelId) return false;
const projectOwner = project.owner.toLowerCase();
return project.repositories.some((repository) => {
if (repository.channelId !== channelId) return false;
if (repository.owner.toLowerCase() === projectOwner) return true;
return repository.maintainers?.some(
(maintainer) => maintainer.toLowerCase() === projectOwner,
);
});
}
export function isProjectHomeChannel(
channelId: string | null | undefined,
projects: ReadonlyArray<ProjectHomeCandidate>,
): boolean {
if (!channelId) return false;
return projects.some(
(project) =>
project.projectChannelId === channelId &&
hasAuthoritativeHomeBinding(project),
);
}