Skip to content

Commit

Permalink
fix: clicking sidebar link should not open in a new tab if relative u…
Browse files Browse the repository at this point in the history
…rl (#1208)
  • Loading branch information
abvthecity authored Jul 26, 2024
1 parent 003c538 commit 4d7e236
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
11 changes: 8 additions & 3 deletions packages/ui/app/src/components/FernLink.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ExternalLinkIcon } from "@radix-ui/react-icons";
import { useAtomValue } from "jotai";
import { atom, useAtomValue } from "jotai";
import Link from "next/link";
import { ReactElement, forwardRef, useEffect, useState, type ComponentProps } from "react";
import { format, parse, resolve, type UrlObject } from "url";
import { useMemoOne } from "use-memo-one";
import { SLUG_ATOM, useDomain } from "../atoms";

interface FernLinkProps extends ComponentProps<typeof Link> {
Expand Down Expand Up @@ -34,8 +35,12 @@ export const FernLink = forwardRef<HTMLAnchorElement, FernLinkProps>(
FernLink.displayName = "FernLink";

const FernRelativeLink = forwardRef<HTMLAnchorElement, ComponentProps<typeof Link>>((props, ref) => {
const selectedSlug = useAtomValue(SLUG_ATOM);
const href = resolveRelativeUrl(`/${selectedSlug}`, formatUrlString(props.href));
const href = useAtomValue(
useMemoOne(
() => atom((get) => resolveRelativeUrl(`/${get(SLUG_ATOM)}`, formatUrlString(props.href))),
[props.href],
),
);
return <Link ref={ref} {...props} href={href} />;
});

Expand Down
3 changes: 1 addition & 2 deletions packages/ui/app/src/sidebar/nodes/SidebarLinkNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export function SidebarLinkNode({ node, depth, className }: SidebarLinkNodeProps
className={className}
depth={Math.max(depth - 1, 0)}
title={node.title}
rightElement={<ExternalLinkIcon />}
rightElement={node.url.startsWith("http") && <ExternalLinkIcon />}
href={node.url}
target={"_blank"}
hidden={false}
/>
);
Expand Down

0 comments on commit 4d7e236

Please sign in to comment.