Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Open assistant-generated links in a browser #261

Merged
merged 1 commit into from
Jun 17, 2024
Merged
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
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
Loading