Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(web-app): open an external link once on Linux

James Dama7752d5be16f
diff --git a/web-app/src/components/ExternalAnchor.tsx b/web-app/src/components/ExternalAnchor.tsx
index 8aec02dd4..37fb63d3e 100644
--- a/web-app/src/components/ExternalAnchor.tsx
+++ b/web-app/src/components/ExternalAnchor.tsx
@@ -14,7 +14,13 @@ export type ExternalAnchorProps = Omit<
export const ExternalAnchor = forwardRef<
HTMLAnchorElement,
ExternalAnchorProps
->(({ href, onClick, children, ...rest }, ref) => {
+>(({ href, onClick, children, ...props }, ref) => {
+ // Markdown renderers pass their own target="_blank" through; drop it (see
+ // below).
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
+ const { target: _target, ...rest } = props as typeof props & {
+ target?: string
+ }
const external = externalHref(href)
if (!external) {
return (
@@ -23,12 +29,15 @@ export const ExternalAnchor = forwardRef<
</a>
)
}
+ // No target="_blank": the click handler opens the URL itself, and WebKitGTK
+ // still raises a new-window request for a _blank link whose click was
+ // cancelled, which the navigation guard would hand to the browser a second
+ // time.
return (
<a
ref={ref}
{...rest}
href={external}
- target="_blank"
rel="noopener noreferrer"
onClick={(e) => {
onClick?.(e)
diff --git a/web-app/src/components/__tests__/ExternalAnchor.test.tsx b/web-app/src/components/__tests__/ExternalAnchor.test.tsx
index 99332f546..73d7badb9 100644
--- a/web-app/src/components/__tests__/ExternalAnchor.test.tsx
+++ b/web-app/src/components/__tests__/ExternalAnchor.test.tsx
@@ -19,10 +19,12 @@ describe('ExternalAnchor', () => {
)
const link = screen.getByRole('link', { name: 'Web' })
expect(link).toHaveAttribute('href', 'https://example.com/a?b=1')
- expect(link).toHaveAttribute('target', '_blank')
+ // No target: a _blank link would also raise a new-window request.
+ expect(link).not.toHaveAttribute('target')
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
const notPrevented = fireEvent.click(link)
expect(notPrevented).toBe(false)
+ expect(openExternalUrl).toHaveBeenCalledTimes(1)
expect(openExternalUrl).toHaveBeenCalledWith('https://example.com/a?b=1')
})
diff --git a/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx b/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx
index 7bb724d64..56e46a574 100644
--- a/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx
+++ b/web-app/src/components/ai-elements/__tests__/chain-of-thought.test.tsx
@@ -293,7 +293,7 @@ describe('ChainOfThoughtSearchResults', () => {
})
describe('ChainOfThoughtSearchResult', () => {
- it('renders as a link with href and target="_blank"', () => {
+ it('renders as an external link without a target', () => {
render(
<ChainOfThoughtSearchResult href="https://example.com">
Example
@@ -301,7 +301,7 @@ describe('ChainOfThoughtSearchResult', () => {
)
const link = screen.getByRole('link', { name: /Example/ })
expect(link).toHaveAttribute('href', 'https://example.com/')
- expect(link).toHaveAttribute('target', '_blank')
+ expect(link).not.toHaveAttribute('target')
expect(link).toHaveAttribute('rel', 'noopener noreferrer')
})
diff --git a/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx b/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
index 08149f647..3abbaf337 100644
--- a/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
+++ b/web-app/src/components/ai-elements/__tests__/reasoning.test.tsx
@@ -231,8 +231,9 @@ describe('ReasoningContent links', () => {
openExternalUrl.mockClear()
const link = renderLink('https://jan.ai/docs')
expect(link).toHaveAttribute('href', 'https://jan.ai/docs')
- expect(link).toHaveAttribute('target', '_blank')
+ expect(link).not.toHaveAttribute('target')
act(() => link.click())
+ expect(openExternalUrl).toHaveBeenCalledTimes(1)
expect(openExternalUrl).toHaveBeenCalledWith('https://jan.ai/docs')
})
diff --git a/web-app/src/containers/RenderMarkdown.tsx b/web-app/src/containers/RenderMarkdown.tsx
index 0ba2b016c..b2513b676 100644
--- a/web-app/src/containers/RenderMarkdown.tsx
+++ b/web-app/src/containers/RenderMarkdown.tsx
@@ -251,7 +251,7 @@ function RenderMarkdownComponent({
// navigate the app window: web and mail links open outside the app,
// in-page fragments stay, and anything else is inert text.
// eslint-disable-next-line @typescript-eslint/no-unused-vars
- const { node: _node, href: _href, ...rest } = props as typeof props & {
+ const { node: _node, href: _href, target: _target, ...rest } = props as typeof props & {
node?: unknown
}
const external = externalHref(href)
@@ -260,7 +260,7 @@ function RenderMarkdownComponent({
<a
{...rest}
href={external}
- target="_blank"
+ // No target="_blank": see ExternalAnchor for why.
rel="noopener noreferrer"
onClick={(e) => {
e.preventDefault()
diff --git a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
index e2c2606fb..be08d9b37 100644
--- a/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
+++ b/web-app/src/containers/__tests__/RenderMarkdown.test.tsx
@@ -551,9 +551,11 @@ describe('RenderMarkdown', () => {
)
const link = container.querySelector('a[href="https://example.com/a?b=1"]')
expect(link).toBeTruthy()
- expect(link?.getAttribute('target')).toBe('_blank')
+ // No target: a _blank link would also raise a new-window request.
+ expect(link?.hasAttribute('target')).toBe(false)
// fireEvent returns false when a handler called preventDefault.
expect(fireEvent.click(link!)).toBe(false)
+ expect(openExternalUrl).toHaveBeenCalledTimes(1)
expect(openExternalUrl).toHaveBeenCalledWith('https://example.com/a?b=1')
})