Skip to content
Merged
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
1 change: 1 addition & 0 deletions apps/desktop/src/settings/DesktopClientSettings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const clientSettings: ClientSettings = {
browserDefaultAppearance: "dark",
browserRecordingFrameRate: 60,
browserLinkTarget: "app",
sideThreadOpenTarget: "main",
browserAutoShowFloatingPreview: false,
browserProfiles: [{ id: "work", name: "Work", kind: "persistent" }],
browserDefaultProfileId: "work",
Expand Down
5 changes: 5 additions & 0 deletions apps/mobile/src/Stack.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,11 @@ const RootStackConfig = createWorkspaceStackNavigator({
screen: LinkedThreadSheet,
options: sheetRoute({ detent: 0.9, android: "fullScreen" }),
}),
// Maximized from the inspector, like ThreadInspectorFullscreen.
LinkedThreadFullscreen: createNativeStackScreen({
screen: LinkedThreadSheet,
options: modalRoute({ header: "glass", itemStyle: "editor", duoMinimize: "onScrollUp" }),
}),
ThreadGuestShare: createNativeStackScreen({
screen: ThreadGuestShareSheet,
options: sheetRoute({ detent: 0.5 }),
Expand Down
71 changes: 35 additions & 36 deletions apps/mobile/src/features/threads/LinkedThreadSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@t3tools/client-runtime/work-log/item-detail";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { T, useGT, useLocale, useMessages } from "gt-react-native";
import { useRef, useState, useSyncExternalStore } from "react";
import { useEffect, useRef, useState, useSyncExternalStore } from "react";
import type * as React from "react";
import { AppState, Pressable, ScrollView, View } from "react-native";
import { useSafeAreaInsets } from "react-native-safe-area-context";
Expand All @@ -26,7 +26,6 @@ import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator";
import { AppText as Text } from "../../components/ui/AppText";
import { Button } from "../../components/ui/Button";
import { ScreenContent } from "../../components/ui/ScreenContent";
import { SheetHeader } from "../../components/ui/SheetHeader";
import { SelectableMarkdownText } from "../../native/SelectableMarkdownText";
import { useEnvironments } from "../../state/environments";
import {
Expand All @@ -40,30 +39,11 @@ import { useConnectLinkedEnvironment, useLinkedThreadView } from "../../state/li
import { peerLinkEnvironment } from "../../state/peerLinks";
import { useEnvironmentQuery } from "../../state/query";
import { useMarkdownPreviewStyles } from "../files/FileMarkdownPreview";
import type { LinkedThreadRouteParams } from "./linkedThreadRoute";
import { ReadOnlyComposerBar } from "./ReadOnlyComposerBar";
import { ThreadInspectorHeader, type ThreadInspectorPresentation } from "./thread-inspector-header";
import { useOpenLinkedThread } from "./useOpenLinkedThread";

type LinkedThreadRouteParams = {
readonly viaEnvironmentId: string;
readonly environmentId: string;
readonly threadId: string;
readonly label: string;
readonly title?: string;
};

/** The route params that open a linked environment's thread read-only. */
export const linkedThreadRouteParams = (
thread: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId },
linked: { readonly viaEnvironmentId: EnvironmentId; readonly label: string },
title: string | null,
): LinkedThreadRouteParams => ({
viaEnvironmentId: linked.viaEnvironmentId,
environmentId: thread.environmentId,
threadId: thread.threadId,
label: linked.label,
...(title === null ? {} : { title }),
});

function ActivityRow(props: {
readonly activity: ThreadFeedActivity;
readonly target: LinkedThreadTarget;
Expand Down Expand Up @@ -263,12 +243,23 @@ const subscribeAppState = (onChange: () => void) => {
};
const appIsActive = () => AppState.currentState === "active";

function LinkedThreadView(props: { readonly target: LinkedThreadTarget }) {
/**
* A linked environment's thread, read through the environment this app is connected to. The app
* never connects to the linked one, so the view only reads. It shows in the inspector, where
* `presentation` maximizes it, or as its own screen, where `presentation` closes it.
*/
export function LinkedThreadView(props: {
readonly target: LinkedThreadTarget;
/** Closes the screen by going back when absent. */
readonly presentation?: ThreadInspectorPresentation;
}) {
const { target } = props;
const gt = useGT();
const m = useMessages();
const locale = useLocale();
const navigation = useNavigation();
const presentation = props.presentation ?? { onClose: () => navigation.goBack() };
const inInspector = "onMaximize" in presentation;
const insets = useSafeAreaInsets();
const via =
useEnvironments().environments.find(
Expand Down Expand Up @@ -299,23 +290,34 @@ function LinkedThreadView(props: { readonly target: LinkedThreadTarget }) {
const connection = useConnectLinkedEnvironment(target);
const connectState = connection.state;
const openThread = useOpenLinkedThread();
const shown = useRef(true);
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- records unmount, so a connect that resolves after the view is gone navigates nowhere
useEffect(() => {
shown.current = true;
return () => {
shown.current = false;
};
}, []);
const connect = async () => {
const outcome = await connection.connect();
if (outcome.type === "unreachable") return;
// This sheet closes either way: the thread opens where the app is now connected, or the
// add-environment screen takes its place.
if (outcome.type === "unreachable" || !shown.current) return;
// The read-only view gives way either way: the thread opens where the app is now connected,
// or the add-environment screen takes its place. In the inspector the thread replaces it there.
if (outcome.type === "connected") {
navigation.goBack();
if (!inInspector) navigation.goBack();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
openThread({ environmentId: target.environmentId, threadId: target.threadId });
return;
}
navigation.dispatch(StackActions.replace("ConnectionsNew", { linked: target.environmentId }));
if (inInspector) navigation.navigate("ConnectionsNew", { linked: target.environmentId });
else {
navigation.dispatch(StackActions.replace("ConnectionsNew", { linked: target.environmentId }));
}
};
return (
<View collapsable={false} className="flex-1 bg-sheet">
<SheetHeader
<View collapsable={false} className={inInspector ? "flex-1 bg-screen" : "flex-1 bg-sheet"}>
<ThreadInspectorHeader
{...presentation}
title={projection?.thread.title || target.title || gt("Thread on {label}", { label })}
onClose={() => navigation.goBack()}
/>
<ScreenContent>
<ScrollView
Expand Down Expand Up @@ -422,10 +424,7 @@ function LinkedThreadView(props: { readonly target: LinkedThreadTarget }) {
);
}

/**
* A linked environment's thread, read through the environment this app is connected to. The app
* never connects to the linked one, so the screen only reads.
*/
/** The read-only view as its own screen: a sheet, or the whole screen when maximized from the inspector. */
export function LinkedThreadSheet({ route }: StaticScreenProps<LinkedThreadRouteParams>) {
const { viaEnvironmentId, environmentId, threadId, label, title } = route.params;
return (
Expand Down
7 changes: 3 additions & 4 deletions apps/mobile/src/features/threads/ThreadAgentsSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import { SubagentStopButton } from "./SubagentStopButton";
import { ThreadWorkflowGroup } from "./ThreadWorkflowGroup";
import { ScreenContent } from "../../components/ui/ScreenContent";
import { subagentThreadTarget, type SubagentThreadTarget } from "./threadAgentsPresentation";
import { linkedThreadRouteParams } from "./LinkedThreadSheet";
import { useLinkedThreadReach } from "../../state/linkedThread";

import { useOpenThreadInspector } from "./thread-inspector-navigation";
import { useOpenLinkedThreadRead } from "./useOpenLinkedThread";

type AgentsTarget = { readonly environmentId: EnvironmentId; readonly threadId: ThreadId };

Expand All @@ -53,6 +53,7 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
const target = route.params;
const navigation = useNavigation();
const openInspector = useOpenThreadInspector();
const openLinkedRead = useOpenLinkedThreadRead();
const insets = useSafeAreaInsets();
const gt = useGT();
const messages = useMessages();
Expand All @@ -73,9 +74,7 @@ export function ThreadAgentsSheet({ route }: StaticScreenProps<AgentsTarget>) {
const openChildThread = (child: SubagentThreadTarget, title: string | null = null) => {
void Haptics.selectionAsync();
if (child.linked !== undefined) {
navigation.dispatch(
StackActions.replace("LinkedThread", linkedThreadRouteParams(child, child.linked, title)),
);
openLinkedRead(child, child.linked, title, { source: target, replace: true });
return;
}
if (
Expand Down
8 changes: 2 additions & 6 deletions apps/mobile/src/features/threads/ThreadRouteScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,8 @@ import {
useWorkspaceBackNavigation,
} from "../layout/workspace/useAndroidWorkspaceNavigation";
import { openSettings } from "../settings/settings-navigation";
import {
InspectorContentScope,
ThreadInspectorHeader,
ThreadInspectorPreview,
type ThreadInspectorPresentation,
} from "./thread-inspector-preview";
import { ThreadInspectorHeader, type ThreadInspectorPresentation } from "./thread-inspector-header";
import { InspectorContentScope, ThreadInspectorPreview } from "./thread-inspector-preview";
import { useStartSideChat } from "./useStartSideChat";
import { useThreadStatusAnnouncements } from "./useThreadStatusAnnouncements";
import type { ThreadInspectorMode, ThreadInspectorTarget } from "./thread-inspector-navigation";
Expand Down
22 changes: 22 additions & 0 deletions apps/mobile/src/features/threads/linkedThreadRoute.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";

export type LinkedThreadRouteParams = {
readonly viaEnvironmentId: string;
readonly environmentId: string;
readonly threadId: string;
readonly label: string;
readonly title?: string;
};

/** The route params that open a linked environment's thread read-only. */
export const linkedThreadRouteParams = (
thread: { readonly environmentId: EnvironmentId; readonly threadId: ThreadId },
linked: { readonly viaEnvironmentId: EnvironmentId; readonly label: string },
title: string | null,
): LinkedThreadRouteParams => ({
viaEnvironmentId: linked.viaEnvironmentId,
environmentId: thread.environmentId,
threadId: thread.threadId,
label: linked.label,
...(title === null ? {} : { title }),
});
38 changes: 38 additions & 0 deletions apps/mobile/src/features/threads/thread-inspector-header.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useGT } from "gt-react-native";

import { AppBar } from "../../components/ui/AppBar";
import { SheetHeader } from "../../components/ui/SheetHeader";

/** A thread shown outside its route: in the inspector pane, or presented full screen with a close. */
export type ThreadInspectorPresentation =
| { readonly onMaximize: () => void }
| { readonly onClose: () => void };

export function ThreadInspectorHeader(
props: ThreadInspectorPresentation & { readonly title: string; readonly subtitle?: string },
) {
const gt = useGT();
if ("onMaximize" in props) {
return (
<AppBar
title={props.title}
subtitle={props.subtitle}
actions={[
{
accessibilityLabel: gt("Maximize thread"),
icon: "arrow.up.left.and.arrow.down.right",
onPress: props.onMaximize,
},
]}
/>
);
}
return (
<SheetHeader
title={props.title}
subtitle={props.subtitle}
onClose={props.onClose}
accessibilityLabel={gt("Close thread")}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ import * as NodeModule from "node:module";
import * as React from "react";
import { act, useLayoutEffect } from "react";
import { createRoot } from "react-dom/client";
import { EnvironmentId, ThreadId } from "@t3tools/contracts";
import { expect, it, vi } from "vite-plus/test";

const harness = vi.hoisted(() => ({
dispatch: (_action: unknown) => {},
getState: (): unknown => undefined,
getRoute: (): unknown => undefined,
inspectorSupported: true,
}));

vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({
useWorkspacePaneState: () => ({ fileInspector: { supported: true } }),
useWorkspacePaneState: () => ({ fileInspector: { supported: harness.inspectorSupported } }),
}));
vi.mock("@react-navigation/native", async () => {
const { createRequire } = await import("node:module");
Expand All @@ -28,7 +30,7 @@ vi.mock("@react-navigation/native", async () => {
});

import { useOpenThreadInspector } from "./thread-inspector-navigation";
import { useOpenLinkedThread } from "./useOpenLinkedThread";
import { useOpenLinkedThread, useOpenLinkedThreadRead } from "./useOpenLinkedThread";

const requireNavigation = NodeModule.createRequire(
NodeModule.createRequire(import.meta.url).resolve("@react-navigation/native/package.json"),
Expand All @@ -38,11 +40,12 @@ const { StackRouter, StackActions } = requireNavigation("@react-navigation/route
StackActions: typeof import("@react-navigation/native").StackActions;
};

function setup(fromAgents: boolean, fullscreen = false) {
function setup(fromAgents: boolean, fullscreen = false, inspectorSupported = true) {
harness.inspectorSupported = inspectorSupported;
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
const router = StackRouter({});
const options = {
routeNames: ["Home", "Thread", "ThreadAgents", "ThreadInspectorFullscreen"],
routeNames: ["Home", "Thread", "ThreadAgents", "ThreadInspectorFullscreen", "LinkedThread"],
routeParamList: {},
routeGetIdList: {},
};
Expand All @@ -66,12 +69,15 @@ function setup(fromAgents: boolean, fullscreen = false) {
);
let openInspector: ReturnType<typeof useOpenThreadInspector>;
let openLinkedThread: ReturnType<typeof useOpenLinkedThread>;
let openLinkedThreadRead: ReturnType<typeof useOpenLinkedThreadRead>;
function Probe() {
const open = useOpenThreadInspector();
const openLinked = useOpenLinkedThread();
const openLinkedRead = useOpenLinkedThreadRead();
useLayoutEffect(() => {
openInspector = open;
openLinkedThread = openLinked;
openLinkedThreadRead = openLinkedRead;
});
return null;
}
Expand All @@ -94,6 +100,9 @@ function setup(fromAgents: boolean, fullscreen = false) {
openLinked(threadId: string) {
act(() => openLinkedThread({ environmentId: source.environmentId, threadId }));
},
openLinkedRead(...input: Parameters<typeof openLinkedThreadRead>) {
act(() => openLinkedThreadRead(...input));
},
[Symbol.dispose]() {
act(() => root.unmount());
},
Expand Down Expand Up @@ -134,6 +143,53 @@ it("opens a completion link in the parent inspector", () => {
});
});

it.each([
{ inspectorSupported: true, fromAgents: false },
{ inspectorSupported: true, fromAgents: true },
{ inspectorSupported: false, fromAgents: false },
{ inspectorSupported: false, fromAgents: true },
])(
"opens a linked environment's thread in the inspector where there is one ($inspectorSupported, from Agents $fromAgents)",
({ inspectorSupported, fromAgents }) => {
using screen = setup(fromAgents, false, inspectorSupported);
const thread = {
environmentId: EnvironmentId.make("linked-vm"),
threadId: ThreadId.make("there"),
};
const linked = { viaEnvironmentId: EnvironmentId.make("remote-environment"), label: "fs-17" };
screen.openLinkedRead(thread, linked, "Fix the build", {
source: screen.source,
replace: fromAgents,
});
if (inspectorSupported) {
// Beside the parent, like a subagent or a side chat; the Agents sheet gives way.
expect(screen.state.routes.map((route) => route.name)).toEqual(["Home", "Thread"]);
expect(screen.state.routes.at(-1)).toMatchObject({
key: screen.parentKey,
params: {
inspector: {
type: "linkedThread",
...thread,
linked: { ...linked, title: "Fix the build" },
},
},
});
return;
}
// A phone has no inspector: the read-only thread takes the screen, replacing the sheet.
expect(screen.state.routes.map((route) => route.name)).toEqual([
"Home",
"Thread",
"LinkedThread",
]);
expect(screen.state.routes.at(-1)?.params).toEqual({
...thread,
...linked,
title: "Fix the build",
});
},
);

it("switches between subagents and devices in the same parent route", () => {
using screen = setup(false);
screen.open({ type: "thread", environmentId: screen.source.environmentId, threadId: "child" });
Expand Down
Loading
Loading