Skip to content

Commit 5226075

Browse files
committed
fix(mobile): preserve add project environment
1 parent 308dbd3 commit 5226075

3 files changed

Lines changed: 105 additions & 37 deletions

File tree

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2+
import { EnvironmentId } from "@t3tools/contracts";
3+
import { describe, expect, it } from "vite-plus/test";
4+
5+
import { resolveAddProjectEnvironment } from "./AddProjectScreen.logic";
6+
7+
const ENVIRONMENT_A = EnvironmentId.make("environment-a");
8+
const ENVIRONMENT_B = EnvironmentId.make("environment-b");
9+
10+
function environment(environmentId: EnvironmentId, connectionState: EnvironmentConnectionPhase) {
11+
return { environmentId, connectionState };
12+
}
13+
14+
describe("resolveAddProjectEnvironment", () => {
15+
it("does not redirect an explicit unavailable environment to another environment", () => {
16+
expect(
17+
resolveAddProjectEnvironment(
18+
[environment(ENVIRONMENT_A, "offline"), environment(ENVIRONMENT_B, "connected")],
19+
ENVIRONMENT_A,
20+
),
21+
).toBeNull();
22+
});
23+
24+
it("resolves an explicit connected environment", () => {
25+
expect(
26+
resolveAddProjectEnvironment(
27+
[environment(ENVIRONMENT_A, "connected"), environment(ENVIRONMENT_B, "connected")],
28+
ENVIRONMENT_A,
29+
)?.environmentId,
30+
).toBe(ENVIRONMENT_A);
31+
});
32+
33+
it("defaults to the first connected environment when no environment is requested", () => {
34+
expect(
35+
resolveAddProjectEnvironment(
36+
[environment(ENVIRONMENT_A, "offline"), environment(ENVIRONMENT_B, "connected")],
37+
null,
38+
)?.environmentId,
39+
).toBe(ENVIRONMENT_B);
40+
});
41+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects";
2+
import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
3+
import type { EnvironmentId } from "@t3tools/contracts";
4+
5+
export function resolveAddProjectEnvironment<
6+
T extends {
7+
readonly environmentId: EnvironmentId;
8+
readonly connectionState: EnvironmentConnectionPhase;
9+
},
10+
>(environmentOptions: ReadonlyArray<T>, requestedEnvironmentId: EnvironmentId | null): T | null {
11+
if (requestedEnvironmentId !== null) {
12+
return (
13+
environmentOptions.find(
14+
(environment) =>
15+
environment.environmentId === requestedEnvironmentId &&
16+
canCreateProjectInEnvironment(environment.connectionState),
17+
) ?? null
18+
);
19+
}
20+
21+
return (
22+
environmentOptions.find((environment) =>
23+
canCreateProjectInEnvironment(environment.connectionState),
24+
) ?? null
25+
);
26+
}

‎apps/mobile/src/features/projects/AddProjectScreen.tsx‎

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import { CommandId, type EnvironmentId, ProjectId } from "@t3tools/contracts";
3030
import { StackActions, useNavigation } from "@react-navigation/native";
3131
import { SymbolView } from "../../components/AppSymbol";
32-
import { useCallback, useEffect, useMemo, useState, type ReactNode } from "react";
32+
import { useCallback, useEffect, useMemo, useRef, useState, type ReactNode } from "react";
3333
import { ActivityIndicator, Alert, Pressable, ScrollView, View } from "react-native";
3434
import { useSafeAreaInsets } from "react-native-safe-area-context";
3535
import * as Arr from "effect/Array";
@@ -55,6 +55,7 @@ import {
5555
useRemoteEnvironmentRuntime,
5656
useSavedRemoteConnections,
5757
} from "../../state/use-remote-environment-registry";
58+
import { resolveAddProjectEnvironment } from "./AddProjectScreen.logic";
5859

5960
interface EnvironmentOption {
6061
readonly environmentId: EnvironmentId;
@@ -236,10 +237,13 @@ function ProjectPathInput(props: {
236237
}
237238

238239
function useBrowsePathInput(environment: EnvironmentOption | null) {
240+
const environmentId = environment?.environmentId ?? null;
241+
const environmentBaseDirectory = environment?.baseDirectory ?? null;
239242
const [pathInput, commitPathInput] = useState(() =>
240-
getAddProjectInitialQuery(environment?.baseDirectory),
243+
getAddProjectInitialQuery(environmentBaseDirectory),
241244
);
242-
const environmentRuntime = useRemoteEnvironmentRuntime(environment?.environmentId ?? null);
245+
const previousEnvironmentIdRef = useRef(environmentId);
246+
const environmentRuntime = useRemoteEnvironmentRuntime(environmentId);
243247
const loadBrowsePath = useAtomQueryRunner(filesystemEnvironment.browse, {
244248
reportFailure: false,
245249
reportDefect: false,
@@ -277,10 +281,11 @@ function useBrowsePathInput(environment: EnvironmentOption | null) {
277281
);
278282

279283
useEffect(() => {
280-
if (environment) {
281-
setPathInput(getAddProjectInitialQuery(environment.baseDirectory));
284+
if (environmentId !== null && environmentId !== previousEnvironmentIdRef.current) {
285+
previousEnvironmentIdRef.current = environmentId;
286+
setPathInput(getAddProjectInitialQuery(environmentBaseDirectory));
282287
}
283-
}, [environment, setPathInput]);
288+
}, [environmentBaseDirectory, environmentId, setPathInput]);
284289

285290
useEffect(
286291
() => () => {
@@ -583,17 +588,7 @@ function useEnvironmentFromParam(
583588
): EnvironmentOption | null {
584589
const environmentOptions = useEnvironmentOptions();
585590
const environmentId = stringParam(environmentIdParam) as EnvironmentId | null;
586-
return (
587-
environmentOptions.find(
588-
(environment) =>
589-
environment.environmentId === environmentId &&
590-
canCreateProjectInEnvironment(environment.connectionState),
591-
) ??
592-
environmentOptions.find((environment) =>
593-
canCreateProjectInEnvironment(environment.connectionState),
594-
) ??
595-
null
596-
);
591+
return resolveAddProjectEnvironment(environmentOptions, environmentId);
597592
}
598593

599594
export function AddProjectRepositoryScreen(props: {
@@ -657,26 +652,32 @@ export function AddProjectRepositoryScreen(props: {
657652
return (
658653
<AddProjectShell>
659654
{error ? <ErrorBanner message={error} /> : null}
660-
<TextInput
661-
className="h-12 min-h-12 rounded-[24px] px-4 py-0 text-base leading-snug"
662-
value={repositoryInput}
663-
onChangeText={setRepositoryInput}
664-
autoCapitalize="none"
665-
autoCorrect={false}
666-
placeholder={
667-
source === "url"
668-
? "https://github.com/org/repo.git"
669-
: addProjectRemoteSourcePathHint(source)
670-
}
671-
returnKeyType="next"
672-
onSubmitEditing={() => void lookupRepository()}
673-
/>
674-
<PrimaryActionButton
675-
label={source === "url" ? "Continue" : "Lookup repository"}
676-
disabled={isSubmitting || repositoryInput.trim().length === 0}
677-
onPress={() => void lookupRepository()}
678-
loading={isSubmitting}
679-
/>
655+
{environment ? (
656+
<>
657+
<TextInput
658+
className="h-12 min-h-12 rounded-[24px] px-4 py-0 text-base leading-snug"
659+
value={repositoryInput}
660+
onChangeText={setRepositoryInput}
661+
autoCapitalize="none"
662+
autoCorrect={false}
663+
placeholder={
664+
source === "url"
665+
? "https://github.com/org/repo.git"
666+
: addProjectRemoteSourcePathHint(source)
667+
}
668+
returnKeyType="next"
669+
onSubmitEditing={() => void lookupRepository()}
670+
/>
671+
<PrimaryActionButton
672+
label={source === "url" ? "Continue" : "Lookup repository"}
673+
disabled={isSubmitting || repositoryInput.trim().length === 0}
674+
onPress={() => void lookupRepository()}
675+
loading={isSubmitting}
676+
/>
677+
</>
678+
) : (
679+
<EmptyEnvironmentState />
680+
)}
680681
</AddProjectShell>
681682
);
682683
}

0 commit comments

Comments
 (0)