Skip to content

Commit

Permalink
refactor: makes default pathname automatically set
Browse files Browse the repository at this point in the history
  • Loading branch information
kevin-hyperjump committed Jun 20, 2024
1 parent 95a5eae commit eef3f71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
16 changes: 7 additions & 9 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,20 @@ type NextPathnameContextType = {
nextPathname: string;
};

const defaultNextPathname = "/";

export const NextPathnameContext = createContext<NextPathnameContextType>({
nextPathname: "/",
nextPathname: defaultNextPathname,
});

export function NextPathnameProvider({
defaultPathname = "/",
children,
}: {
defaultPathname?: string;
children: ReactNode;
}) {
const [nextPathname, setNextPathname] = useState(defaultPathname);
export function NextPathnameProvider({ children }: { children: ReactNode }) {
const [nextPathname, setNextPathname] = useState(defaultNextPathname);

useEffect(() => {
if (typeof window === "undefined") return;

setNextPathname(window.location.pathname);

const handleClick = (event: MouseEvent) => {
const target = (event.target as HTMLElement).closest("a");

Expand Down
10 changes: 0 additions & 10 deletions test/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@ describe("NextPathnameProvider", () => {
expect(getByTestId("pathname").textContent).toBe("/");
});

it("provides the initial pathname from defaultPathname", () => {
const { getByTestId } = render(
<NextPathnameProvider defaultPathname="/initial-path">
<TestComponent />
</NextPathnameProvider>,
);

expect(getByTestId("pathname").textContent).toBe("/initial-path");
});

it("updates the pathname on link click", () => {
const { getByTestId } = render(
<NextPathnameProvider>
Expand Down

0 comments on commit eef3f71

Please sign in to comment.