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

Fix: make sure source link is configured and open in external browser #496

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions components/chat/messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Avatar, Button, Tooltip } from '@nextui-org/react';
import type { CallFrame } from '@gptscript-ai/gptscript';
import Calls from './messages/calls';
import { GoIssueReopened } from 'react-icons/go';
import { defaultUrlTransform } from 'react-markdown';

export enum MessageType {
Alert,
Expand All @@ -30,6 +31,14 @@ const abbreviate = (name: string) => {
return firstLetters.slice(0, 2).join('').toUpperCase();
};

// Allow links for file references in messages if it starts with file://, otherwise this will cause an empty href and cause app to reload when clicking on it
const urlTransformAllowFiles = (u: string) => {
if (u.startsWith('file://')) {
return u;
}
return defaultUrlTransform(u);
};

const Message = React.memo(
({
message,
Expand Down Expand Up @@ -88,6 +97,7 @@ const Message = React.memo(
rehypePlugins={[
[rehypeExternalLinks, { target: '_blank' }],
]}
urlTransform={urlTransformAllowFiles}
>
{message.message}
</Markdown>
Expand Down Expand Up @@ -156,6 +166,7 @@ const Message = React.memo(
rehypePlugins={[
[rehypeExternalLinks, { target: '_blank' }],
]}
urlTransform={urlTransformAllowFiles}
>
{message.message}
</Markdown>
Expand Down
7 changes: 7 additions & 0 deletions electron/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ function createWindow(url) {
// Allow navigation for internal URLs
return { action: 'allow' };
});

win.webContents.on('will-navigate', (event, url) => {
if (url.startsWith('file://')) {
event.preventDefault();
shell.openExternal(url);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to not just use the default of the system like the other button? Is it because this catches all navigation events?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested and the one above doesn't work but this works. I got this from chatgpt.

}
});
}

function ensureDirExists(dir) {
Expand Down
Loading