Skip to content
TypeScript

fix(mobile): Add project path survives its host dropping out and returning (#25819)

Julius Marminge737eab244dc1
diff --git a/apps/mobile/src/features/projects/AddProjectScreen.test.ts b/apps/mobile/src/features/projects/AddProjectScreen.test.ts
index 3331f8e6440..91acc7a9bfe 100644
--- a/apps/mobile/src/features/projects/AddProjectScreen.test.ts
+++ b/apps/mobile/src/features/projects/AddProjectScreen.test.ts
@@ -686,3 +686,103 @@ describe("local project navigation", () => {
},
);
});
+
+describe("local folder path", () => {
+ function findPathInput(
+ node: ReactNode,
+ ): { value: string; onChangeText: (value: string) => void } | null {
+ if (Array.isArray(node)) {
+ for (const child of node) {
+ const input = findPathInput(child);
+ if (input) return input;
+ }
+ return null;
+ }
+ if (
+ !isValidElement<{
+ value?: string;
+ onChangeText?: (value: string) => void;
+ onSubmit?: () => void;
+ children?: ReactNode;
+ }>(node)
+ ) {
+ return null;
+ }
+ const { value, onChangeText, onSubmit } = node.props;
+ if (typeof value === "string" && onChangeText && onSubmit) return { value, onChangeText };
+ return findPathInput(node.props.children);
+ }
+
+ function mountLocalFolder() {
+ state.scopes = new Set([AuthOrchestrationOperateScope]);
+ state.baseDirectory = "/work";
+ state.connectionStates = {};
+ state.environments = [
+ { environmentId: "environment", environmentLabel: "Environment" },
+ { environmentId: "another-environment", environmentLabel: "Another environment" },
+ ];
+ (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
+ const root = createRoot(document.createElement("div"));
+ const renderedPaths: string[] = [];
+ let input: ReturnType<typeof findPathInput> = null;
+ function LocalFolderObserver(props: { environmentId?: string }) {
+ input = findPathInput(AddProjectLocalFolderScreen(props));
+ if (input) renderedPaths.push(input.value);
+ return null;
+ }
+ return {
+ renderedPaths,
+ get path() {
+ return input?.value;
+ },
+ type: (value: string) => act(() => input!.onChangeText(value)),
+ render: (environmentId?: string) =>
+ act(() => root.render(createElement(LocalFolderObserver, { environmentId }))),
+ [Symbol.asyncDispose]: async () => {
+ await act(() => root.unmount());
+ state.connectionStates = {};
+ state.environments = [{ environmentId: "environment", environmentLabel: "Environment" }];
+ },
+ };
+ }
+
+ it("shows the new host's default path on the first render after the host changes", async () => {
+ await using screen = mountLocalFolder();
+ await screen.render("environment");
+ await screen.type("/work/edited");
+ screen.renderedPaths.length = 0;
+
+ await screen.render("another-environment");
+
+ expect(screen.renderedPaths).not.toContain("/work/edited");
+ expect(screen.path).toBe("~/");
+ });
+
+ it("keeps each host's typed path when switching between hosts", async () => {
+ await using screen = mountLocalFolder();
+ await screen.render("environment");
+ await screen.type("/work/a");
+ await screen.render("another-environment");
+ await screen.type("~/b");
+
+ await screen.render("environment");
+ expect(screen.path).toBe("/work/a");
+ await screen.render("another-environment");
+ expect(screen.path).toBe("~/b");
+ });
+
+ it("keeps a typed path when the default host drops out and comes back", async () => {
+ await using screen = mountLocalFolder();
+ await screen.render();
+ expect(screen.path).toBe("~/");
+ await screen.type("~/code/app");
+
+ state.connectionStates = { "another-environment": "offline" };
+ await screen.render();
+ expect(screen.path).toBe("/work/");
+
+ state.connectionStates = {};
+ await screen.render();
+ expect(screen.path).toBe("~/code/app");
+ });
+});
diff --git a/apps/mobile/src/features/projects/AddProjectScreen.tsx b/apps/mobile/src/features/projects/AddProjectScreen.tsx
index 49de8d219ba..b03797cf358 100644
--- a/apps/mobile/src/features/projects/AddProjectScreen.tsx
+++ b/apps/mobile/src/features/projects/AddProjectScreen.tsx
@@ -292,23 +292,40 @@ function useBrowsePathInput(environment: EnvironmentOption | null, pinnedDirecto
const environmentId = environment?.environmentId ?? null;
const environmentBaseDirectory = environment?.baseDirectory ?? null;
const clonePathCaseSensitive = !isWindowsPlatform(environment?.platform ?? "");
- const [pathInput, commitPathInput] = useState(() =>
- getCloneDestinationPath(
- getAddProjectInitialQuery(environmentBaseDirectory),
- pinnedDirectoryName,
- ),
+ const seedPath = getCloneDestinationPath(
+ getAddProjectInitialQuery(environmentBaseDirectory),
+ pinnedDirectoryName,
);
- const previousEnvironmentIdRef = useRef(environmentId);
+ // Each host keeps the path last typed or browsed for it, and shows its own default until then,
+ // so switching hosts and back loses nothing. With no host selected (one dropping out briefly),
+ // the field keeps the last path typed anywhere.
+ const [paths, setPaths] = useState<{
+ readonly byHost: ReadonlyMap<EnvironmentId, string>;
+ readonly last: string;
+ }>(() => ({ byHost: new Map(), last: seedPath }));
+ const pathInput =
+ environmentId === null ? paths.last : (paths.byHost.get(environmentId) ?? seedPath);
+ const commitPathInput = (path: string) =>
+ setPaths((current) => {
+ const byHost = new Map(current.byHost);
+ if (environmentId !== null) {
+ byHost.set(environmentId, path);
+ }
+ return { byHost, last: path };
+ });
const environmentRuntime = useRemoteEnvironmentRuntime(environmentId);
const loadBrowsePath = useAtomQueryRunner(filesystemEnvironment.browse, {
reportFailure: false,
reportDefect: false,
});
const [browseNavigation] = useState(createBrowseNavigationCoordinator);
- const [isBrowseNavigating, setIsBrowseNavigating] = useState(false);
+ const [navigatingEnvironmentId, setNavigatingEnvironmentId] = useState<
+ EnvironmentId | null | undefined
+ >(undefined);
+ const isBrowseNavigating = navigatingEnvironmentId === environmentId;
const setPathInput = (path: string) => {
browseNavigation.invalidate();
- setIsBrowseNavigating(false);
+ setNavigatingEnvironmentId(undefined);
commitPathInput(path);
};
const navigateToBrowsePath = async (input: {
@@ -327,7 +344,7 @@ function useBrowsePathInput(environment: EnvironmentOption | null, pinnedDirecto
caseSensitive: clonePathCaseSensitive,
})
: getCloneDestinationPath(selectedDirectoryPath, pinnedDirectoryName);
- setIsBrowseNavigating(true);
+ setNavigatingEnvironmentId(environmentId);
const committed = await browseNavigation.run(
async () => {
if (
@@ -344,23 +361,11 @@ function useBrowsePathInput(environment: EnvironmentOption | null, pinnedDirecto
() => commitPathInput(nextPathInput),
);
if (committed) {
- setIsBrowseNavigating(false);
+ setNavigatingEnvironmentId(undefined);
}
return committed;
};
- useEffect(() => {
- if (environmentId !== null && environmentId !== previousEnvironmentIdRef.current) {
- previousEnvironmentIdRef.current = environmentId;
- setPathInput(
- getCloneDestinationPath(
- getAddProjectInitialQuery(environmentBaseDirectory),
- pinnedDirectoryName,
- ),
- );
- }
- }, [environmentBaseDirectory, environmentId, pinnedDirectoryName, setPathInput]);
-
useEffect(
() => () => {
browseNavigation.invalidate();