Skip to content

Commit

Permalink
feat: Open assistant-generated links in a browser
Browse files Browse the repository at this point in the history
Enable users to navigate with links included in assistant responses,
opening the URL in the user's default browser.
  • Loading branch information
dcalhoun committed Jun 17, 2024
1 parent c1148f1 commit 1992253
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/components/content-tab-assistant.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from '@sentry/react';
import { Spinner } from '@wordpress/components';
import { createInterpolateElement } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
Expand All @@ -18,6 +19,7 @@ import { AIInput } from './ai-input';
import { MessageThinking } from './assistant-thinking';
import Button from './button';
import WelcomeComponent from './welcome-message-prompt';

interface ContentTabAssistantProps {
selectedSite: SiteDetails;
}
Expand Down Expand Up @@ -168,7 +170,7 @@ export const Message = ( { children, isUser, className }: MessageProps ) => {
>
{ typeof children === 'string' ? (
<div className="assistant-markdown">
<Markdown components={ { code: CodeBlock } } remarkPlugins={ [ remarkGfm ] }>
<Markdown components={ { a: Anchor, code: CodeBlock } } remarkPlugins={ [ remarkGfm ] }>
{ children }
</Markdown>
</div>
Expand All @@ -180,6 +182,34 @@ export const Message = ( { children, isUser, className }: MessageProps ) => {
);
};

function Anchor( props: JSX.IntrinsicElements[ 'a' ] & ExtraProps ) {
const { href } = props;

return (
<a
{ ...props }
onClick={ ( e ) => {
if ( ! href ) {
return;
}

e.preventDefault();
try {
getIpcApi().openURL( href );
} catch ( error ) {
getIpcApi().showMessageBox( {
type: 'error',
message: __( 'Failed to open link' ),
detail: __( 'We were unable to open the link. Please try again.' ),
buttons: [ __( 'OK' ) ],
} );
Sentry.captureException( error );
}
} }
/>
);
}

const AuthenticatedView = memo(
( {
messages,
Expand Down

0 comments on commit 1992253

Please sign in to comment.