Skip to content

Commit 63ce507

Browse files
committed
feat: Open assistant-generated links in a browser
Enable users to navigate with links included in assistant responses, opening the URL in the user's default browser.
1 parent f431f55 commit 63ce507

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/components/content-tab-assistant.tsx

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as Sentry from '@sentry/react';
12
import { Spinner } from '@wordpress/components';
23
import { createInterpolateElement } from '@wordpress/element';
34
import { __ } from '@wordpress/i18n';
@@ -18,6 +19,7 @@ import { AIInput } from './ai-input';
1819
import { MessageThinking } from './assistant-thinking';
1920
import Button from './button';
2021
import WelcomeComponent from './welcome-message-prompt';
22+
2123
interface ContentTabAssistantProps {
2224
selectedSite: SiteDetails;
2325
}
@@ -168,7 +170,7 @@ export const Message = ( { children, isUser, className }: MessageProps ) => {
168170
>
169171
{ typeof children === 'string' ? (
170172
<div className="assistant-markdown">
171-
<Markdown components={ { code: CodeBlock } } remarkPlugins={ [ remarkGfm ] }>
173+
<Markdown components={ { a: Anchor, code: CodeBlock } } remarkPlugins={ [ remarkGfm ] }>
172174
{ children }
173175
</Markdown>
174176
</div>
@@ -180,6 +182,34 @@ export const Message = ( { children, isUser, className }: MessageProps ) => {
180182
);
181183
};
182184

185+
function Anchor( props: JSX.IntrinsicElements[ 'a' ] & ExtraProps ) {
186+
const { href } = props;
187+
188+
return (
189+
<a
190+
{ ...props }
191+
onClick={ ( e ) => {
192+
if ( ! href ) {
193+
return;
194+
}
195+
196+
e.preventDefault();
197+
try {
198+
getIpcApi().openURL( href );
199+
} catch ( error ) {
200+
getIpcApi().showMessageBox( {
201+
type: 'error',
202+
message: __( 'Failed to open link' ),
203+
detail: __( 'We were unable to open the link. Please try again.' ),
204+
buttons: [ __( 'OK' ) ],
205+
} );
206+
Sentry.captureException( error );
207+
}
208+
} }
209+
/>
210+
);
211+
}
212+
183213
const AuthenticatedView = memo(
184214
( {
185215
messages,

0 commit comments

Comments
 (0)