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

new-task-project-selection.ts

109 lines4.1 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​
22export function filterProjectScopes(
23 scopes: ReadonlyArray<HomeProjectScope>,
24 searchText: string,
25): ReadonlyArray<HomeProjectScope> {
26 const query = searchText.trim().toLowerCase();
27 if (!query) return scopes;
28 return scopes.filter(
29 (scope) =>
30 scope.title.toLowerCase().includes(query) ||
31 scope.projects.some(
32 (project) =>
33 project.title.toLowerCase().includes(query) ||
34 project.workspaceRoot.toLowerCase().includes(query),
35 ),
36 );
38​
39function getOnlySelectableProject(
40 projectScopes: ReadonlyArray<HomeProjectScope>,
41): EnvironmentProject | null {
42 const onlyScope = projectScopes.length === 1 ? projectScopes[0] : null;
43 return onlyScope?.representative ?? null;
45​
46/**
47 * Picks the project on a target environment that corresponds to the project
48 * currently selected in the new-task flow, so switching computers follows the
49 * same repo. Repository identity is preferred; projects without one (e.g. not
50 * yet indexed) fall back to workspace basename, then title. When nothing
51 * matches, the first project on the target stands in — the same fallback the
52 * render path applies when no key is selected — so the draft always has a
53 * concrete key to carry over to.
54 */
55export function resolveEnvironmentProjectMatch(
56 projectsOnTarget: ReadonlyArray<EnvironmentProject>,
57 selectedProject: EnvironmentProject | null,
58): EnvironmentProject | null {
59 const repositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null;
60 // `|| null` (not `??`): a pending-task placeholder project can have an empty
61 // workspaceRoot, and an "" basename would match nothing meaningful.
62 const workspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null;
63 // The weaker signals only apply where identity is unknown on at least one
64 // side; two known, different repositories never match on a shared basename
65 // or title (mirrors the environment list filter in the new-task flow).
66 const isKnownMismatch = (project: EnvironmentProject) => {
67 const projectKey = project.repositoryIdentity?.canonicalKey ?? null;
68 return repositoryKey !== null && projectKey !== null && projectKey !== repositoryKey;
69 };
70 return (
71 (repositoryKey !== null
72 ? projectsOnTarget.find(
73 (project) => (project.repositoryIdentity?.canonicalKey ?? null) === repositoryKey,
74 )
75 : undefined) ??
76 (workspaceBasename !== null
77 ? projectsOnTarget.find(
78 (project) =>
79 !isKnownMismatch(project) &&
80 project.workspaceRoot.split("/").at(-1) === workspaceBasename,
81 )
82 : undefined) ??
83 (selectedProject !== null
84 ? projectsOnTarget.find(
85 (project) => !isKnownMismatch(project) && project.title === selectedProject.title,
86 )
87 : undefined) ??
88 projectsOnTarget[0] ??
89 null
90 );
92​
93export function resolveDraftProjectSelection(
94 selectedProjectKey: string | null,
95 projects: ReadonlyArray<EnvironmentProject>,
96 projectScopes: ReadonlyArray<HomeProjectScope>,
97): DraftProjectSelectionResolution {
98 const hasExplicitProjectSelection =
99 selectedProjectKey !== null &&
100 projects.some(
101 (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey,
102 );
103 if (hasExplicitProjectSelection) {
104 return { kind: "preserve" };
105 }
106​
107 const onlyProject = getOnlySelectableProject(projectScopes);
108 return onlyProject ? { kind: "select", project: onlyProject } : { kind: "pick" };