Skip to content

Commit

Permalink
feat: Open assistant-generated links in a browser (#261)
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 authored Jun 17, 2024
1 parent 09a575e commit 845f440
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 @@ -177,7 +179,7 @@ export const Message = ( { children, id, isUser, className }: MessageProps ) =>
</div>
{ 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 @@ -189,6 +191,34 @@ export const Message = ( { children, id, 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 845f440

Please sign in to comment.