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

new-task-project-selection.ts

34 lines1.3 KB

1import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
2​
3import { scopedProjectKey } from "../../lib/scopedEntities";
4import type { HomeProjectScope } from "../home/homeThreadList";
5​
6export type DraftProjectSelectionResolution =
7 | { readonly kind: "preserve" }
8 | { readonly kind: "select"; readonly project: EnvironmentProject }
9 | { readonly kind: "pick" };
10​
11export function getOnlySelectableProject(
12 projectScopes: ReadonlyArray<HomeProjectScope>,
13): EnvironmentProject | null {
14 const onlyScope = projectScopes.length === 1 ? projectScopes[0] : null;
15 return onlyScope?.projects.length === 1 ? (onlyScope.projects[0] ?? null) : null;
17​
18export function resolveDraftProjectSelection(
19 selectedProjectKey: string | null,
20 projects: ReadonlyArray<EnvironmentProject>,
21 projectScopes: ReadonlyArray<HomeProjectScope>,
22): DraftProjectSelectionResolution {
23 const hasExplicitProjectSelection =
24 selectedProjectKey !== null &&
25 projects.some(
26 (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey,
27 );
28 if (hasExplicitProjectSelection) {
29 return { kind: "preserve" };
30 }
31​
32 const onlyProject = getOnlySelectableProject(projectScopes);
33 return onlyProject ? { kind: "select", project: onlyProject } : { kind: "pick" };