Skip to content

Commit

Permalink
v3 - fix sanity internal links (#763)
Browse files Browse the repository at this point in the history
* fix(link): broken links and root fix

* refactor(utils): rename get.tsx → link.ts
  • Loading branch information
mathiazom authored Oct 14, 2024
1 parent 3d7ecca commit 9b4b6a0
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/app/(main)/[lang]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const navigationManagerLink = {
_type: "link",
linkTitle: "Go to Navigation Manager",
linkType: LinkType.Internal,
internalLink: { _ref: "studio/structure/navigationManager" },
internalLink: { _ref: "studio/structure/siteSettings;navigationManager" },
};

const pagesLink = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/link/CustomLink.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from "next/link";
import React from "react";

import { getHref } from "src/utils/get";
import { getHref } from "src/utils/link";
import { ILink } from "studio/lib/interfaces/navigation";

import styles from "./link.module.css";
Expand Down
2 changes: 1 addition & 1 deletion src/components/linkButton/LinkButton.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";

import { getHref } from "src/utils/get";
import { getHref } from "src/utils/link";
import { ILink } from "studio/lib/interfaces/navigation";

import styles from "./linkButton.module.css";
Expand Down
2 changes: 1 addition & 1 deletion src/components/navigation/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { SanityImage } from "src/components/image/SanityImage";
import LanguageSwitcher from "src/components/languageSwitcher/LanguageSwitcher";
import CustomLink from "src/components/link/CustomLink";
import LinkButton from "src/components/linkButton/LinkButton";
import { getHref } from "src/utils/get";
import { getHref } from "src/utils/link";
import { BrandAssets } from "studio/lib/interfaces/brandAssets";
import { ILink, Navigation } from "studio/lib/interfaces/navigation";
import { callToActionFieldID } from "studio/schemas/fields/callToActionFields";
Expand Down
2 changes: 1 addition & 1 deletion src/components/utils/linkTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const homeLink = {
linkTitle: "Return to home",
linkType: LinkType.Internal,
internalLink: {
_ref: "/",
_ref: "",
},
};

Expand Down
13 changes: 3 additions & 10 deletions src/utils/get.tsx → src/utils/link.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,10 @@ import { ILink, LinkType } from "studio/lib/interfaces/navigation";
const hash = "#";

export const getHref = (link: ILink): string => {
switch (link?.linkType) {
switch (link.linkType) {
case LinkType.Internal:
if (link.internalLink?._ref) {
try {
return `${link.language ? `/${link.language}` : ""}/${link.internalLink._ref}${link.anchor ? `#${link.anchor}` : ""}`;
} catch (error) {
console.error("Error fetching page:", error);
return hash;
}
if (link.internalLink?._ref !== undefined) {
return `${link.language ? `/${link.language}` : ""}/${link.internalLink._ref}${link.anchor ? `#${link.anchor}` : ""}`;
}
return hash;
case LinkType.External:
Expand All @@ -20,7 +15,5 @@ export const getHref = (link: ILink): string => {
return `mailto:${link.email}`;
case LinkType.Phone:
return `tel:${link.phone}`;
default:
return hash;
}
};

0 comments on commit 9b4b6a0

Please sign in to comment.