Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

new-task-project-selection.ts

92 lines3.6 KB

1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2import type { EnvironmentId } from "@t3tools/contracts";
3​
4import { scopedProjectKey } from "../../lib/scopedEntities";
5import type { HomeProjectScope } from "../home/homeThreadList";
6​
7type DraftProjectSelectionResolution =
8 | { readonly kind: "preserve" }
9 | { readonly kind: "select"; readonly project: EnvironmentProject }
10 | { readonly kind: "pick" };
11​
12export function getProjectScopeSelectionTarget(
13 scope: HomeProjectScope,
14 preferredEnvironmentId: EnvironmentId | null,
15): EnvironmentProject {
16 return (
17 scope.projects.find((project) => project.environmentId === preferredEnvironmentId) ??
18 scope.representative
19 );
21​
22function getOnlySelectableProject(
23 projectScopes: ReadonlyArray<HomeProjectScope>,
24): EnvironmentProject | null {
25 const onlyScope = projectScopes.length === 1 ? projectScopes[0] : null;
26 return onlyScope?.representative ?? null;
28​
29/**
30 * Picks the project on a target environment that corresponds to the project
31 * currently selected in the new-task flow, so switching computers follows the
32 * same repo. Repository identity is preferred; projects without one (e.g. not
33 * yet indexed) fall back to workspace basename, then title. When nothing
34 * matches, the first project on the target stands in — the same fallback the
35 * render path applies when no key is selected — so the draft always has a
36 * concrete key to carry over to.
37 */
38export function resolveEnvironmentProjectMatch(
39 projectsOnTarget: ReadonlyArray<EnvironmentProject>,
40 selectedProject: EnvironmentProject | null,
41): EnvironmentProject | null {
42 const repositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null;
43 // `|| null` (not `??`): a pending-task placeholder project can have an empty
44 // workspaceRoot, and an "" basename would match nothing meaningful.
45 const workspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null;
46 // The weaker signals only apply where identity is unknown on at least one
47 // side; two known, different repositories never match on a shared basename
48 // or title (mirrors the environment list filter in the new-task flow).
49 const isKnownMismatch = (project: EnvironmentProject) => {
50 const projectKey = project.repositoryIdentity?.canonicalKey ?? null;
51 return repositoryKey !== null && projectKey !== null && projectKey !== repositoryKey;
52 };
53 return (
54 (repositoryKey !== null
55 ? projectsOnTarget.find(
56 (project) => (project.repositoryIdentity?.canonicalKey ?? null) === repositoryKey,
57 )
58 : undefined) ??
59 (workspaceBasename !== null
60 ? projectsOnTarget.find(
61 (project) =>
62 !isKnownMismatch(project) &&
63 project.workspaceRoot.split("/").at(-1) === workspaceBasename,
64 )
65 : undefined) ??
66 (selectedProject !== null
67 ? projectsOnTarget.find(
68 (project) => !isKnownMismatch(project) && project.title === selectedProject.title,
69 )
70 : undefined) ??
71 projectsOnTarget[0] ??
72 null
73 );
75​
76export function resolveDraftProjectSelection(
77 selectedProjectKey: string | null,
78 projects: ReadonlyArray<EnvironmentProject>,
79 projectScopes: ReadonlyArray<HomeProjectScope>,
80): DraftProjectSelectionResolution {
81 const hasExplicitProjectSelection =
82 selectedProjectKey !== null &&
83 projects.some(
84 (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey,
85 );
86 if (hasExplicitProjectSelection) {
87 return { kind: "preserve" };
88 }
89​
90 const onlyProject = getOnlySelectableProject(projectScopes);
91 return onlyProject ? { kind: "select", project: onlyProject } : { kind: "pick" };