Skip to content

Commit

Permalink
🐛 Next.js Bugs - Fix useLocation + Custom Hook (#67)
Browse files Browse the repository at this point in the history
* Added improved useLocation for use with Next.js

* Added better logic to avoid Tina rerenders

* Removed dependency on react-use

* Reduced bundle size by using automatic detection of dependencies

* Removed sourcemap

* added update to package lock

* Removed debug logs
  • Loading branch information
Harry-Ross authored Feb 13, 2024
1 parent 959207e commit 68905cd
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 238 deletions.
22 changes: 15 additions & 7 deletions lib/components/CountryDropdown/CountryDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use client";
import { Popover, Transition } from "@headlessui/react";
import { Fragment, useEffect, useState } from "react";
import { useLocation } from "react-use";
import { Countries } from "../../types/country";
import { cx } from "../../util/cx";
import { CustomLink } from "../CustomLink";
Expand All @@ -22,17 +21,26 @@ const websites: { country: Countries; url: string }[] = [
},
] as const;

const CountryDropdown = () => {
const { host } = useLocation();
type CountryDropdownProps = {
url?: string;
};

const CountryDropdown = ({ url }: CountryDropdownProps) => {
const [isOpened, setIsOpened] = useState(false);
const [currentCountry, setCurrentCountry] = useState<Countries>("Australia");

useEffect(() => {
const website = websites.find((w) => host?.endsWith(w.url));
if (website) {
setCurrentCountry(website.country);
try {
const { hostname } = new URL(url || "");

const website = websites.find((w) => hostname?.endsWith(w.url));
if (website) {
setCurrentCountry(website.country);
}
} catch (err) {
console.error(err);
}
}, [host]);
}, [url]);

return (
<Popover>
Expand Down
6 changes: 3 additions & 3 deletions lib/components/DesktopMenu/DesktopMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface DesktopMenuProps {
menuGroups: NavMenuGroup[];
sideActionsOverride?: () => JSX.Element;
hidePhone?: boolean;
searchUrl?: string;
searchUrl: string;
}

export const ClosePopoverContext = createContext<(() => void) | null>(null);
Expand Down Expand Up @@ -84,7 +84,7 @@ const DesktopMenu: React.FC<DesktopMenuProps> = ({

type DefaultSideActionsProps = {
hidePhone?: boolean;
searchUrl?: string;
searchUrl: string;
};

const DefaultSideActions = ({
Expand All @@ -96,7 +96,7 @@ const DefaultSideActions = ({
{!hidePhone && <PhoneButton hideMobile />}
<Search url={searchUrl} />
<Divider />
<CountryDropdown />
<CountryDropdown url={searchUrl} />
</>
);
};
Expand Down
3 changes: 2 additions & 1 deletion lib/components/MegaMenuLayout/MegaMenuLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Bars3Icon } from "@heroicons/react/24/outline";
import React, { useState } from "react";
import { useMenuItems } from "../../hooks/useMenuItems";
import { NavMenuGroup } from "../../types/megamenu";
import { DEFAULT_URL } from "../../util/constants";
import { CustomLink } from "../CustomLink";
import DesktopMenu from "../DesktopMenu/DesktopMenu";
import { Logo } from "../Logo";
Expand Down Expand Up @@ -33,7 +34,7 @@ const MegaMenuLayout: React.FC<MegaMenuWrapperProps> = ({
title,
url,
subtitle,
searchUrl,
searchUrl = DEFAULT_URL,
menuBarItems,
rightSideActionsOverride,
}) => {
Expand Down
8 changes: 3 additions & 5 deletions lib/components/Search/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Dialog, Transition } from "@headlessui/react";
import React, { Fragment, useEffect, useRef, useState } from "react";
import { useHotkeys } from "react-hotkeys-hook";
import { useLocation } from "react-use";
import { MegaIcon } from "../MegaIcon";

export interface SearchProps {
Expand All @@ -10,7 +9,6 @@ export interface SearchProps {

export const Search: React.FC<SearchProps> = ({ url }) => {
const searchRef = useRef(null);
const location = useLocation();
const [isOpen, setIsOpen] = useState(false);
const [searchTerm, setSearchTerm] = useState("");

Expand All @@ -33,9 +31,9 @@ export const Search: React.FC<SearchProps> = ({ url }) => {

const performSearch = () => {
if (searchTerm) {
const searchUrl = `https://www.google.com.au/search?q=site:${
url || location.hostname
}%20${encodeURIComponent(searchTerm)}`;
const searchUrl = `https://www.google.com.au/search?q=site:${url}%20${encodeURIComponent(
searchTerm,
)}`;
window.open(searchUrl, "_blank");
}
};
Expand Down
11 changes: 8 additions & 3 deletions lib/hooks/useMenuItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ const refreshData = async () => {
export const useMenuItems = (
menuBarItems?: NavMenuGroup[],
): { menuItems: NavMenuGroup[] } => {
const [menuItems, setMenuItems] = useState<NavMenuGroup[]>(
menuBarItems || [],
const [menuItems, setMenuItems] = useState<NavMenuGroup[] | undefined>(
menuBarItems,
);

useEffect(() => {
Expand All @@ -25,9 +25,14 @@ export const useMenuItems = (
setMenuItems(data);
})
.catch((err) => {
setMenuItems(menuBarItems || []);
console.error(err);
});
}, []);

useEffect(() => {
if (menuBarItems) {
setMenuItems(menuBarItems);
}
}, [menuBarItems]);

return { menuItems: menuItems || [] };
Expand Down
1 change: 1 addition & 0 deletions lib/util/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_URL = "https://www.ssw.com.au";
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ssw.megamenu",
"version": "4.1.0",
"version": "4.1.1",
"type": "module",
"main": "dist/index.js",
"module": "dist/index.es.js",
Expand Down Expand Up @@ -30,7 +30,6 @@
"react-device-detect": "^2.2.3",
"react-hotkeys-hook": "^4.4.1",
"react-icons": "^4.12.0",
"react-use": "^17.4.2",
"tailwind-merge": "^2.1.0"
},
"peerDependencies": {
Expand Down Expand Up @@ -59,6 +58,7 @@
"postcss": "^8.4.32",
"prettier": "^3.1.1",
"prettier-plugin-tailwindcss": "^0.5.9",
"rollup-plugin-node-externals": "^7.0.1",
"source-map-explorer": "^2.5.3",
"tailwindcss": "^3.3.6",
"typescript": "^5.2.2",
Expand Down
Loading

0 comments on commit 68905cd

Please sign in to comment.