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

AddProjectScreen.logic.ts

74 lines2.8 KB

1import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
2import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
3import { type EnvironmentId, FilesystemBrowseError } from "@t3tools/contracts";
4import * as Schema from "effect/Schema";
5​
6import { newTaskFlowKey } from "../threads/new-task-flow-state";
7import { isDraftThreadRoute, type NewTaskDraftRouteParams } from "../threads/thread-route-state";
8​
9const isFilesystemBrowseError = Schema.is(FilesystemBrowseError);
10​
11/** Clone destinations may name folders that do not exist yet; cloning creates them. */
12export function isMissingCloneParentBrowseFailure(input: {
13 readonly cloning: boolean;
14 readonly failure: unknown;
15}): boolean {
16 return (
17 input.cloning &&
18 isFilesystemBrowseError(input.failure) &&
19 input.failure.failure === "read_directory_failed" &&
20 input.failure.pathMissing === true
21 );
23​
24export function resolveAddProjectEnvironment<
25 T extends {
26 readonly environmentId: EnvironmentId;
27 readonly connectionState: EnvironmentConnectionPhase;
28 },
29>(environmentOptions: ReadonlyArray<T>, requestedEnvironmentId: EnvironmentId | null): T | null {
30 if (requestedEnvironmentId !== null) {
31 return (
32 environmentOptions.find(
33 (environment) =>
34 environment.environmentId === requestedEnvironmentId &&
35 canCreateProjectInEnvironment(environment.connectionState),
36 ) ?? null
37 );
38 }
39​
40 return (
41 environmentOptions.find((environment) =>
42 canCreateProjectInEnvironment(environment.connectionState),
43 ) ?? null
44 );
46​
47interface StackRoute {
48 readonly key: string;
49 readonly name: string;
50 readonly params?: object;
52​
53const firstParam = (value: string | string[] | undefined) =>
54 Array.isArray(value) ? value[0] : value;
55​
56/**
57 * The draft whose project chooser opened Add project, so the added project is picked for it as
58 * choosing one in the chooser is. Null when Add project opened from anywhere else.
59 */
60export function chooserDraftKey(
61 routes: ReadonlyArray<StackRoute>,
62 readFlowDraftKey: (flowKey: string) => string | null,
63): string | null {
64 const chooserIndex = routes.findLastIndex((route) => !route.name.startsWith("AddProject"));
65 const chooser = routes[chooserIndex];
66 if (chooser?.name !== "NewTaskProject") return null;
67 const savedDraftId = firstParam((chooser.params as { draftId?: string | string[] })?.draftId);
68 if (savedDraftId !== undefined) return savedDraftId;
69 const draftRoute = routes[chooserIndex - 1];
70 if (draftRoute === undefined || !isDraftThreadRoute(draftRoute)) return null;
71 const flowKey = newTaskFlowKey(draftRoute);
72 const draft = (draftRoute.params as { draft: NewTaskDraftRouteParams }).draft;
73 return (flowKey && readFlowDraftKey(flowKey)) ?? firstParam(draft.draftId) ?? null;