Skip to content
27 changes: 25 additions & 2 deletions apps/mobile/src/features/threads/LinkedThreadSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ 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 { resolveMarkdownLinkPresentation } from "../../lib/markdownLinks";
import { tryOpenExternalUrl } from "../../lib/openExternalUrl";
import { SelectableMarkdownText } from "../../native/SelectableMarkdownText";
import { useEnvironments } from "../../state/environments";
import {
Expand All @@ -44,6 +46,15 @@ import { ReadOnlyComposerBar } from "./ReadOnlyComposerBar";
import { ThreadInspectorHeader, type ThreadInspectorPresentation } from "./thread-inspector-header";
import { useOpenLinkedThread } from "./useOpenLinkedThread";

function openWebLink(href: string) {
const link = resolveMarkdownLinkPresentation(href);
if (link.kind === "external") void tryOpenExternalUrl(link.href, "markdown-link");
}

function canOpenWebLink(href: string) {
return resolveMarkdownLinkPresentation(href).kind === "external";
}

function ActivityRow(props: {
readonly activity: ThreadFeedActivity;
readonly target: LinkedThreadTarget;
Expand Down Expand Up @@ -163,7 +174,14 @@ function LinkedThreadEntry(props: {
}
// A message the agent sent with a tool reads as its own words, as on the thread screen.
if (entry.type === "sent-message") {
return <SelectableMarkdownText markdown={entry.text} textStyle={styles.nativeTextStyle} />;
return (
<SelectableMarkdownText
markdown={entry.text}
textStyle={styles.nativeTextStyle}
onLinkPress={openWebLink}
canOpenLinkFromMenu={canOpenWebLink}
/>
);
}
if (entry.type !== "message" || entry.message.text.trim() === "") return null;
return entry.message.role === "user" ? (
Expand All @@ -173,7 +191,12 @@ function LinkedThreadEntry(props: {
</Text>
</View>
) : (
<SelectableMarkdownText markdown={entry.message.text} textStyle={styles.nativeTextStyle} />
<SelectableMarkdownText
markdown={entry.message.text}
textStyle={styles.nativeTextStyle}
onLinkPress={openWebLink}
canOpenLinkFromMenu={canOpenWebLink}
/>
);
}

Expand Down
30 changes: 13 additions & 17 deletions apps/mobile/src/native/EnrichedMarkdownText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -346,19 +346,21 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
else if (actionId === "enriched-share-link") void Share.share({ message: url });
else onFileContextMenuAction?.(url, actionId);
};
const defaultLinkMenu = (url: string) => ({
title: url,
actions: [
...(props.canOpenLinkFromMenu?.(url) === false
? []
: [{ id: "enriched-open-link", title: gt("Open link") }]),
{ id: "enriched-copy-link", title: gt("Copy link") },
...(Platform.OS === "ios" ? [{ id: "enriched-share-link", title: gt("Share link") }] : []),
],
});
const linkContextMenuItems =
Platform.OS === "ios" && (fileContextMenu || props.onLinkPress || props.onImagePress)
? Object.fromEntries(
linkUrls.map((url) => {
const menu = modelContextMenu(url) ??
fileContextMenu?.(url) ?? {
title: url,
actions: [
{ id: "enriched-open-link", title: gt("Open link") },
{ id: "enriched-copy-link", title: gt("Copy link") },
{ id: "enriched-share-link", title: gt("Share link") },
],
};
const menu = modelContextMenu(url) ?? fileContextMenu?.(url) ?? defaultLinkMenu(url);
return [
enrichedLinkVariantPattern(url),
menu.actions.map((action) => ({
Expand Down Expand Up @@ -438,14 +440,8 @@ export function MobileEnrichedMarkdownText(props: SelectableMarkdownTextProps) {
const onLinkLongPress =
Platform.OS === "android" && props.fileContextMenu
? ({ url }: { url: string }) => {
const menu = modelContextMenu(url) ??
props.fileContextMenu?.(url) ?? {
title: url,
actions: [
{ id: "enriched-open-link", title: gt("Open link") },
{ id: "enriched-copy-link", title: gt("Copy link") },
],
};
const menu =
modelContextMenu(url) ?? props.fileContextMenu?.(url) ?? defaultLinkMenu(url);
setActiveMenu({ url, menu });
openAndroidMenu?.();
}
Expand Down
1 change: 1 addition & 0 deletions apps/mobile/src/native/SelectableMarkdownText.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export interface SelectableMarkdownTextProps {
readonly models?: ReadonlyArray<SelectableMarkdownModel>;
readonly preserveSoftBreaks?: boolean;
readonly onLinkPress?: (href: string) => void;
readonly canOpenLinkFromMenu?: (href: string) => boolean;
readonly onCiteText?: (text: string) => void;
readonly fileContextMenu?: (href: string) => MarkdownFileContextMenu | undefined;
readonly onFileContextMenuAction?: (href: string, actionId: string) => void;
Expand Down
Loading