Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(next): fix useSelectedLayoutSegment #464

Merged
merged 4 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions packages/react-server-next/src/compat/navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
routerRevalidate,
useParams as useParams_,
useRouter as useRouter_,
useSelectedParamEntries,
useSelectedParams,
useSelectedLayoutSegments,
} from "@hiogawa/react-server/client";
import React from "react";

Expand All @@ -27,14 +26,10 @@ export function useParams<T extends Params = Params>(): T {
return useParams_() as any;
}

export function useSelectedLayoutSegments(_todo?: string): string[] {
const selected = useSelectedParams();
return Object.values(selected);
}
export { useSelectedLayoutSegments };

export function useSelectedLayoutSegment(_todo?: string): string | null {
const [next] = useSelectedParamEntries();
return next?.[1] ?? null;
export function useSelectedLayoutSegment(): string | null {
return useSelectedLayoutSegments()[0] ?? null;
}

export function useRouter() {
Expand Down
26 changes: 13 additions & 13 deletions packages/react-server/examples/basic/e2e/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,39 +1087,39 @@ async function testCatchallRoute(page: Page, _options: { js: boolean }) {
await expect(page.getByLabel("test state")).not.toBeChecked();
}

test("useSelectedParams", async ({ page }) => {
test("useSelectedLayoutSegments", async ({ page }) => {
await page.goto("/test/dynamic/selected");
await page.getByText("/layout.tsx: {}").click();
await page.getByText("/page.tsx: {}").click();
await page.getByText("/layout.tsx: []").click();
await page.getByText("/page.tsx: []").click();

await page
.getByRole("link", { name: "• /test/dynamic/selected/x", exact: true })
.click();
await page.getByText('/layout.tsx: {"p1":"x"}').click();
await page.getByText("/[p1]/layout.tsx: {}").click();
await page.getByText('/layout.tsx: ["x"]').click();
await page.getByText("/[p1]/layout.tsx: []").click();

await page
.getByRole("link", { name: "• /test/dynamic/selected/x/y" })
.click();
await page.getByText('/layout.tsx: {"p1":"x","p2":"y"}').click();
await page.getByText('/[p1]/layout.tsx: {"p2":"y"}').click();
await page.getByText("/[p1]/[p2]/layout.tsx: {}").click();
await page.getByText('/layout.tsx: ["x","y"]').click();
await page.getByText('/[p1]/layout.tsx: ["y"]').click();
await page.getByText("/[p1]/[p2]/layout.tsx: []").click();

await page
.getByRole("link", {
name: "• /test/dynamic/selected/x/static",
exact: true,
})
.click();
await page.getByText('/layout.tsx: {"p1":"x"}').click();
await page.getByText("/[p1]/layout.tsx: {}").click();
await page.getByText('/layout.tsx: ["x","static"]').click();
await page.getByText('/[p1]/layout.tsx: ["static"]').click();

await page
.getByRole("link", { name: "• /test/dynamic/selected/x/static/y" })
.click();
await page.getByText('/layout.tsx: {"p1":"x","q1":"y"}').click();
await page.getByText('/[p1]/layout.tsx: {"q1":"y"}').click();
await page.getByText('/[p1]/static/layout.tsx: {"q1":"y"}').click();
await page.getByText('/layout.tsx: ["x","static","y"]').click();
await page.getByText('/[p1]/layout.tsx: ["static","y"]').click();
await page.getByText('/[p1]/static/layout.tsx: ["y"]').click();
});

test("full client route", async ({ page }) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LayoutProps } from "@hiogawa/react-server/server";
import { SelectedParams } from "../../_client";
import { Selected } from "../../_client";

export default function Page(props: LayoutProps) {
return (
<>
<pre>
/[p1]/[p2]/layout.tsx: <SelectedParams />
/[p1]/[p2]/layout.tsx: <Selected />
</pre>
{props.children}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SelectedParams } from "../../_client";
import { Selected } from "../../_client";

export default function Page() {
return (
<pre>
/[p1]/[p2]/page.tsx: <SelectedParams />
/[p1]/[p2]/page.tsx: <Selected />
</pre>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LayoutProps } from "@hiogawa/react-server/server";
import { SelectedParams } from "../_client";
import { Selected } from "../_client";

export default function Page(props: LayoutProps) {
return (
<>
<pre>
/[p1]/layout.tsx: <SelectedParams />
/[p1]/layout.tsx: <Selected />
</pre>
{props.children}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SelectedParams } from "../_client";
import { Selected } from "../_client";

export default function Page() {
return (
<pre>
/[p1]/page.tsx: <SelectedParams />
/[p1]/page.tsx: <Selected />
</pre>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LayoutProps } from "@hiogawa/react-server/server";
import { SelectedParams } from "../../../_client";
import { Selected } from "../../../_client";

export default function Page(props: LayoutProps) {
return (
<>
<pre>
/[p1]/static/[q1]/layout.tsx: <SelectedParams />
/[p1]/static/[q1]/layout.tsx: <Selected />
</pre>
{props.children}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SelectedParams } from "../../../_client";
import { Selected } from "../../../_client";

export default function Page() {
return (
<pre>
/[p1]/static/[q1]/page.tsx: <SelectedParams />
/[p1]/static/[q1]/page.tsx: <Selected />
</pre>
);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { LayoutProps } from "@hiogawa/react-server/server";
import { SelectedParams } from "../../_client";
import { Selected } from "../../_client";

export default function Page(props: LayoutProps) {
return (
<>
<pre>
/[p1]/static/layout.tsx: <SelectedParams />
/[p1]/static/layout.tsx: <Selected />
</pre>
{props.children}
</>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SelectedParams } from "../../_client";
import { Selected } from "../../_client";

export default function Page() {
return (
<pre>
/[p1]/static/page.tsx: <SelectedParams />
/[p1]/static/page.tsx: <Selected />
</pre>
);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"use client";

import { useSelectedParams } from "@hiogawa/react-server/client";
import { useSelectedLayoutSegments } from "@hiogawa/react-server/client";

export function SelectedParams() {
const params = useSelectedParams();
export function Selected() {
const params = useSelectedLayoutSegments();
return <>{JSON.stringify(params)}</>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { LayoutProps } from "@hiogawa/react-server/server";
import { NavMenu } from "../../../../components/nav-menu";
import { SelectedParams } from "./_client";
import { Selected } from "./_client";

export default function Page(props: LayoutProps) {
return (
Expand All @@ -17,7 +17,7 @@ export default function Page(props: LayoutProps) {
]}
/>
<pre>
/layout.tsx: <SelectedParams />
/layout.tsx: <Selected />
</pre>
{props.children}
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { SelectedParams } from "./_client";
import { Selected } from "./_client";

export default function Page() {
return (
<pre>
/page.tsx: <SelectedParams />
/page.tsx: <Selected />
</pre>
);
}
3 changes: 1 addition & 2 deletions packages/react-server/src/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ export { useRouter } from "./lib/client/router";
export {
routerRevalidate,
useParams,
useSelectedParams,
useSelectedParamEntries,
useSelectedLayoutSegments,
} from "./features/router/client";
10 changes: 5 additions & 5 deletions packages/react-server/src/features/router/client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,20 @@ export function LayoutMatchProvider(
return <LayoutMatchContext.Provider {...props} />;
}

export function useSelectedParamEntries() {
function useSelectedParamEntries() {
const all = useParamEntries();
const prefix = React.useContext(LayoutMatchContext).params;
return React.useMemo(() => all.slice(prefix.length), [all, prefix]);
}

export function useSelectedParams() {
export function useSelectedLayoutSegments(): string[] {
const entries = useSelectedParamEntries();
return React.useMemo(() => toMatchParamsObject(entries), [entries]);
return React.useMemo(() => entries.map(([_k, v]) => v), [entries]);
}

export function RemountRoute(props: React.PropsWithChildren) {
const [next] = useSelectedParamEntries();
return <React.Fragment key={next?.[1]}>{props.children}</React.Fragment>;
const key = useSelectedLayoutSegments()[0];
return <React.Fragment key={key}>{props.children}</React.Fragment>;
}

export const ROUTER_REVALIDATE_KEY = "__REVALIDATE";
Expand Down