Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions apps/mobile/src/features/layout/AdaptiveWorkspaceLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import {
workspaceTabColumns,
workspaceTabNavigationState,
} from "../workspace/workspace-tab-navigation";
import type { WorkspaceTab } from "../workspace/workspace-tabs-state";
import { workspaceTabOf, type WorkspaceTab } from "../workspace/workspace-tabs-state";
import { workspaceRoutesOf } from "./workspace-overlay-routes";
import { SettingsWorkspaceLayout } from "../settings/SettingsWorkspaceLayout";

Expand Down Expand Up @@ -535,10 +535,7 @@ function AdaptiveWorkspaceLayoutContent(
if (Platform.OS === "android") Keyboard.dismiss();
setFileInspectorPreferredVisible(false);
if (layout.primarySidebarOverlaysContent) setPrimarySidebarVisible(false);
workspace?.dispatch({
type: "select",
tab: detail.name === "Thread" ? "threads" : "pullRequests",
});
workspace?.dispatch({ type: "select", tab: workspaceTabOf(detail.name) });
startOpeningDetail(detail);
navigation.dispatch(selectWorkspaceDetail(detail, sheet));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ function usePullRequestToolbarHeight(bottomInset: number) {
}

export function PullRequestRouteScreen({ route }: StaticScreenProps<PullRequestParams>) {
const returnToList = useAndroidWorkspaceBack("pullRequests");
const returnToList = useAndroidWorkspaceBack();
const { layout } = useAdaptiveWorkspaceLayout();
const sidebarHeaderItem = useWorkspaceSidebarHeaderItem();
const navigation = useNavigation();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { StackActions, useNavigation } from "@react-navigation/native";
import { useState } from "react";
import { Platform, View } from "react-native";
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
import { View } from "react-native";
import { AppText as Text } from "../../components/ui/AppText";
import { PullRequestCardGroup, PullRequestCardRow } from "./PullRequestControls";
import { pullRequestStatus } from "./pullRequestStatus";
Expand All @@ -14,7 +13,6 @@ export function PullRequestStackCard(props: {
readonly state: PullRequestStackState;
}) {
const navigation = useNavigation();
const selectDetail = useSelectWorkspaceDetail();
const [expanded, setExpanded] = useState(false);
const stack = props.state.stack;
if (!stack) {
Expand Down Expand Up @@ -67,9 +65,7 @@ export function PullRequestStackCard(props: {
? undefined
: () => {
const params = { ...props.params, number: layer.number };
if (Platform.OS === "android")
selectDetail({ name: "PullRequest", params });
else navigation.dispatch(StackActions.push("PullRequest", params));
navigation.dispatch(StackActions.push("PullRequest", params));
}
}
last={index === stack.layers.length - 1}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import { formatComposerContextReference } from "@t3tools/shared/composerContextR
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
import { normalizeThreadPullRequestKey } from "@t3tools/client-runtime/thread-pull-requests";
import { useRef, useState } from "react";
import { Platform } from "react-native";
import { useSelectWorkspaceDetail } from "../workspace/useSelectWorkspaceDetail";
import { pullRequestComposerContext } from "../../lib/composerContext";
import { scopedThreadKey } from "../../lib/scopedEntities";
import { uuidv4 } from "../../lib/uuid";
Expand Down Expand Up @@ -76,16 +74,13 @@ export function usePullRequestThreads(
const canAsk = !connected || canOperate;
const canCheckout = useEnvironmentScope(target.environmentId, AuthSourceControlWriteScope);
const navigation = useNavigation();
const selectDetail = useSelectWorkspaceDetail();
const pending = useRef(false);
const previousPrompts = useRef(new Map<string, string>());
const openThread = (thread: LinkedThread) => {
openThreadRoute({ environmentId: params.environmentId, threadId: thread.id });
};
// Android shows one page at a time; iOS opens the thread over the pull request, so Back returns.
const openThreadRoute = (destination: ReactNavigation.RootParamList["Thread"]) => {
if (Platform.OS === "android") selectDetail({ name: "Thread", params: destination });
else navigation.dispatch(StackActions.push("Thread", destination));
navigation.dispatch(StackActions.push("Thread", destination));
};
const [busy, setBusy] = useState(false);
const ask = async (thread?: LinkedThread, task: PullRequestAgentTask = "ask") => {
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/threads/NewTaskDraftScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export function NewTaskDraftScreen(props: {
const flow = useNewTaskFlow();
const navigation = useNavigation();
const isFocused = useIsFocused();
const goBack = useWorkspaceBackNavigation("threads");
const goBack = useWorkspaceBackNavigation();
const currentRoute = useRoute();
const flowKey = newTaskFlowKey(currentRoute);
const { getShare, isLoading: isIncomingShareInboxLoading } = useIncomingShare();
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/src/features/threads/ThreadHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function ThreadHeader(
readonly onOpenFilesInspector: () => void;
},
) {
const goBack = useWorkspaceBackNavigation("threads");
const goBack = useWorkspaceBackNavigation();
const { layout, panes, toggleAuxiliaryPane, composerKeyboardActive } =
useAdaptiveWorkspaceLayout();
const { onOpenTerminal, onMergeBack } = props.gitControls;
Expand Down
4 changes: 2 additions & 2 deletions apps/mobile/src/features/threads/ThreadRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function firstRouteParam(value: string | string[] | undefined): string | null {
}

function ThreadRouteFallbackLayout(props: { readonly children: ReactNode }) {
const goBack = useWorkspaceBackNavigation("threads");
const goBack = useWorkspaceBackNavigation();
const { layout } = useAdaptiveWorkspaceLayout();
const { themeVariables } = useAppearancePreferences();
if (Platform.OS !== "android")
Expand Down Expand Up @@ -239,7 +239,7 @@ export function ThreadRouteScreen(props: ThreadRouteScreenProps) {
}

function LiveThreadRouteController(props: ThreadRouteScreenProps) {
useAndroidWorkspaceBack("threads");
useAndroidWorkspaceBack();
const connectionsReady = useConnectionsReady();
const { connectionState } = useRemoteConnectionStatus();
const { selectedThread } = useThreadSelection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createContext, use, type ReactNode } from "react";
import { Platform } from "react-native";
import { androidWorkspaceSettingsOpen } from "./android-workspace-tabs";
import { WorkspaceTabsProvider } from "./WorkspaceTabsContext";
import { workspaceTabOf } from "./workspace-tabs-state";

interface AndroidWorkspaceRoot {
/**
Expand Down Expand Up @@ -35,7 +36,10 @@ export function AndroidWorkspaceTabsProvider(props: {
settingsIndex < 0 ? null : (props.state.routes[settingsIndex + 1]?.name ?? null);
const root = { key, settingsOpen, settingsRoute, settingsPage };
return Platform.OS === "android" ? (
<WorkspaceTabsProvider>
// A cold-start link lands on a page with no list selection; start on that page's list.
<WorkspaceTabsProvider
initialTab={workspaceTabOf(props.state.routes.find((route) => route.name !== "Home")?.name)}
>
<AndroidWorkspaceRootContext value={root}>{props.children}</AndroidWorkspaceRootContext>
</WorkspaceTabsProvider>
) : (
Expand Down
7 changes: 4 additions & 3 deletions apps/mobile/src/features/workspace/WorkspaceTabsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ function resolveWorkspaceTabsSelection(

// One model per provider. Handlers on frozen screens keep stale closures, so
// they read the current selection from the registry at call time.
function createWorkspaceTabsModel() {
const stored = Atom.make(initialWorkspaceTabsState());
function createWorkspaceTabsModel(selectedTab?: WorkspaceTab) {
const stored = Atom.make(initialWorkspaceTabsState(selectedTab));
const scrollOffsets: Record<WorkspaceSearchTab, number> = {
threads: 0,
pullRequests: 0,
Expand All @@ -75,9 +75,10 @@ function createWorkspaceTabsModel() {
export function WorkspaceTabsProvider(props: {
readonly children: ReactNode;
readonly settingsOpen?: boolean;
readonly initialTab?: WorkspaceTab;
}) {
const registry = use(RegistryContext);
const [model] = useState(createWorkspaceTabsModel);
const [model] = useState(() => createWorkspaceTabsModel(props.initialTab));
const state = useAtomValue(model.resolved);
const context = {
state,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,29 @@
import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native";
import { useCallback } from "react";
import { BackHandler, Keyboard, Platform } from "react-native";
import {
resetAndroidWorkspace,
returnToAndroidWorkspaceList,
} from "../../lib/android-workspace-navigation";
import { useWorkspaceTabs } from "./WorkspaceTabsContext";
import type { WorkspaceTab } from "./workspace-tabs-state";
import { returnToAndroidWorkspaceList } from "../../lib/android-workspace-navigation";

function useWorkspaceListNavigation(tab: WorkspaceTab) {
export function useWorkspaceBackNavigation() {
const navigation = useNavigation();
const dispatch = useWorkspaceTabs()?.dispatch;
return () => {
if (Platform.OS === "android") {
dispatch?.({ type: "select", tab });
resetAndroidWorkspace(navigation);
} else if (navigation.canGoBack()) navigation.goBack();
if (Platform.OS === "android") returnToAndroidWorkspaceList(navigation);
else if (navigation.canGoBack()) navigation.goBack();
else navigation.dispatch(StackActions.replace("Home"));
};
}

export function useWorkspaceBackNavigation(tab: WorkspaceTab) {
const navigation = useNavigation();
const dispatch = useWorkspaceTabs()?.dispatch;
const returnToList = useWorkspaceListNavigation(tab);
return () => {
if (Platform.OS === "android") {
returnToAndroidWorkspaceList(navigation, () => dispatch?.({ type: "select", tab }));
} else returnToList();
};
}

export function useAndroidWorkspaceBack(tab: WorkspaceTab) {
const dispatch = useWorkspaceTabs()?.dispatch;
const goBack = useWorkspaceBackNavigation(tab);
export function useAndroidWorkspaceBack() {
const goBack = useWorkspaceBackNavigation();
useFocusEffect(
useCallback(() => {
if (Platform.OS !== "android") return;
dispatch?.({ type: "select", tab });
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
if (Keyboard.isVisible()) Keyboard.dismiss();
else goBack();
return true;
});
return () => subscription.remove();
}, [goBack, dispatch, tab]),
}, [goBack]),
);
return goBack;
}
11 changes: 9 additions & 2 deletions apps/mobile/src/features/workspace/workspace-tabs-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,16 @@ function emptyTab(): WorkspaceTabState {
return { query: "", searchMode: "resting", scrollOffset: 0, searchEntryOffset: 0 };
}

export function initialWorkspaceTabsState(): WorkspaceTabsState {
/** The list a workspace page belongs to. */
export function workspaceTabOf(routeName: string | undefined): WorkspaceTab {
return routeName === "PullRequest" ? "pullRequests" : "threads";
}

export function initialWorkspaceTabsState(
selectedTab: WorkspaceTab = "threads",
): WorkspaceTabsState {
return {
selectedTab: "threads",
selectedTab,
selectionRevision: 0,
tabs: {
threads: emptyTab(),
Expand Down
31 changes: 16 additions & 15 deletions apps/mobile/src/lib/android-workspace-navigation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,12 @@ function names(state: ReturnType<typeof router.getInitialState>) {
}

describe("Android workspace navigation", () => {
it("returns to a distinct previous thread, otherwise to the workspace list", () => {
it("returns to the previous page, otherwise to the workspace list", () => {
const parent = withThread();
let state = apply(
parent,
StackActions.push("Thread", { ...thread.params, threadId: "child-a" }),
);
let listSelected = false;
const navigation = {
dispatch: (action: RootStackResetAction) => {
state = apply(state, action);
Expand All @@ -102,39 +101,41 @@ describe("Android workspace navigation", () => {
state = apply(state, StackActions.pop());
},
};
const back = () =>
returnToAndroidWorkspaceList(navigation, () => {
listSelected = true;
});
const back = () => returnToAndroidWorkspaceList(navigation);

back();
expect(state.routes).toEqual(parent.routes);
expect(state.index).toBe(parent.index);
expect(listSelected).toBe(false);

back();
expect(state.routes).toEqual([parent.routes[0]]);
expect(state.index).toBe(0);
expect(listSelected).toBe(true);

state = apply(
parent,
StackActions.push("Thread", { ...thread.params, environmentId: "environment-b" }),
);
listSelected = false;
back();
expect(state.routes).toEqual(parent.routes);
expect(state.index).toBe(parent.index);
expect(listSelected).toBe(false);

const pullRequestOverThread = legacyHistory();
state = pullRequestOverThread;
back();
expect(state.routes).toEqual(pullRequestOverThread.routes.slice(0, 2));

// Confirming a Git action from the inline inspector can duplicate the current thread.
state = apply(parent, StackActions.push("Thread", thread.params));
back();
expect({ routes: names(state), index: state.index, listSelected }).toEqual({
routes: ["Home"],
index: 0,
listSelected: true,
});
expect({ routes: names(state), index: state.index }).toEqual({ routes: ["Home"], index: 0 });

const child = { ...thread.params, threadId: "child-a" };
state = apply(
apply(parent, StackActions.push("Thread", child)),
StackActions.push("Thread", child),
);
back();
expect(state.routes).toEqual(parent.routes);
});

it("opens a linked thread above the current one so Back returns to it", () => {
Expand Down
25 changes: 12 additions & 13 deletions apps/mobile/src/lib/android-workspace-navigation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { NavigationState } from "@react-navigation/native";
import { resetRootStack, type RootStackNavigation } from "./root-stack-reset";

function androidWorkspaceBackReturnsToThread(state: NavigationState) {
const current = state.routes[state.index];
const params = current?.params as ReactNavigation.RootParamList["Thread"] | undefined;
return (
current?.name === "Thread" &&
state.routes[state.index - 1]?.name === "Thread" &&
params !== undefined &&
!parentThreadIsBelow(state, params)
);
// Back follows history; Git confirmations can stack copies of the current thread, so skip those too.
function androidWorkspaceBackTarget(state: NavigationState) {
let index = state.index;
const params = state.routes[index]?.params as ReactNavigation.RootParamList["Thread"] | undefined;
if (state.routes[index]?.name === "Thread" && params) {
while (parentThreadIsBelow({ ...state, index }, params)) index -= 1;
}
return index - 1;
}

/** The root stack with only the workspace list. */
Expand Down Expand Up @@ -59,12 +58,12 @@ export function linkedThreadStep(

export function returnToAndroidWorkspaceList(
navigation: RootStackNavigation & { readonly goBack: () => void },
selectTab: () => void,
) {
const state = navigation.getState();
if (state && androidWorkspaceBackReturnsToThread(state)) return navigation.goBack();
selectTab();
resetAndroidWorkspace(navigation);
const index = state ? androidWorkspaceBackTarget(state) : -1;
if (!state || index < 0) return resetAndroidWorkspace(navigation);
if (index === state.index - 1) return navigation.goBack();
resetRootStack(navigation, () => ({ ...state, index, routes: state.routes.slice(0, index + 1) }));
}

// In the split view these destinations swap the detail pane beside the sidebar instead of
Expand Down
7 changes: 1 addition & 6 deletions apps/mobile/src/native/useOpenPullRequest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { useNavigation, useRoute, StackActions } from "@react-navigation/native";
import { nativePullRequestDestination } from "../lib/pullRequestNavigation";
import { Platform } from "react-native";
import { useSelectWorkspaceDetail } from "../features/workspace/useSelectWorkspaceDetail";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { useProjects, useThreadShell } from "../state/entities";
import { tryOpenExternalUrl } from "../lib/openExternalUrl";
Expand All @@ -13,7 +11,6 @@ type PullRequestContext = { readonly environmentId: string; readonly projectId:
// Avoid useRoute so layouts without route context can open any thread's pull requests.
export function useNavigateToPullRequest(options?: { readonly replace?: boolean }) {
const navigation = useNavigation();
const selectDetail = useSelectWorkspaceDetail();
const projects = useProjects();
const replace = options?.replace;
return async (url: string, context: PullRequestContext | null) => {
Expand All @@ -23,9 +20,7 @@ export function useNavigateToPullRequest(options?: { readonly replace?: boolean
serverConfigs.has(environmentId),
);
if (!params) return tryOpenExternalUrl(url, "pull-request");
// Android shows one page at a time; iOS opens it over the current page, so Back returns.
if (Platform.OS === "android") selectDetail({ name: "PullRequest", params });
else if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
if (replace) navigation.dispatch(StackActions.replace("PullRequest", params));
else navigation.dispatch(StackActions.push("PullRequest", params));
return true;
};
Expand Down
Loading