Skip to content

Open source CapCut alternative.

TypeScript

fix(site): normalize super-user paths during hydration

David Leee906ae0fbbd
diff --git a/site/src/app/su/blog/NewPostButton.tsx b/site/src/app/su/blog/NewPostButton.tsx
index 4afaa4a2..fbb1e8f4 100644
--- a/site/src/app/su/blog/NewPostButton.tsx
+++ b/site/src/app/su/blog/NewPostButton.tsx
@@ -1,6 +1,7 @@
"use client";
import { usePathname, useRouter } from "next/navigation";
+import { suPublicPath } from "@/app/su/nav";
import { Button } from "@/components/ui/button";
import { useCreateBlogPost } from "@/queries/blog";
@@ -9,7 +10,7 @@ import { useCreateBlogPost } from "@/queries/blog";
// editor has a post id to upload images under. The button belongs to the
// list; an open post carries its own actions.
export function NewPostButton() {
- const pathname = usePathname();
+ const pathname = suPublicPath(usePathname());
const router = useRouter();
const create = useCreateBlogPost();
if (pathname !== "/blog") return null;
diff --git a/site/src/app/su/nav.test.ts b/site/src/app/su/nav.test.ts
new file mode 100644
index 00000000..387cdb4c
--- /dev/null
+++ b/site/src/app/su/nav.test.ts
@@ -0,0 +1,21 @@
+import { expect, test } from "bun:test";
+import { SU_NAV, suPublicPath, suSurfaceAt, suTabs } from "./nav";
+
+test("server and browser paths render the same super-user navigation", () => {
+ for (const surface of SU_NAV) {
+ for (const page of suTabs(surface) ?? [surface]) {
+ expect(suSurfaceAt(`/su${page.href}`)).toEqual(suSurfaceAt(page.href));
+ expect(suSurfaceAt(page.href).surface).toBe(surface);
+ expect(suSurfaceAt(page.href).page).toBe(page);
+ }
+ }
+});
+
+test("nested blog pages keep their surface and normalize only the route prefix", () => {
+ expect(suSurfaceAt("/su/blog/post-id").surface.title).toBe("Blog");
+ expect(suPublicPath("/su/blog")).toBe("/blog");
+ expect(suPublicPath("/su/blog/post-id")).toBe("/blog/post-id");
+ expect(suPublicPath("/su")).toBe("/");
+ expect(suPublicPath("/support")).toBe("/support");
+ expect(suPublicPath("/blog/su/post-id")).toBe("/blog/su/post-id");
+});
diff --git a/site/src/app/su/nav.ts b/site/src/app/su/nav.ts
index 3d91ef57..be85faac 100644
--- a/site/src/app/su/nav.ts
+++ b/site/src/app/su/nav.ts
@@ -100,11 +100,18 @@ export type SuPage = SuTab | SuSurface;
export const suTabs = (surface: SuSurface): readonly SuTab[] | undefined =>
"tabs" in surface ? surface.tabs : undefined;
+/** The public address of a route rendered under the proxy's /su prefix. */
+export function suPublicPath(pathname: string): string {
+ if (pathname === "/su") return "/";
+ return pathname.startsWith("/su/") ? pathname.slice(3) : pathname;
+}
+
// The surface an address belongs to, and the page within it: the tab showing,
// or the surface itself when it has none. A section address resolves to its
// first tab, the one the proxy opens it on; an address outside the rail falls
// back to the entry the host opens on.
export function suSurfaceAt(pathname: string): { surface: SuSurface; page: SuPage } {
+ pathname = suPublicPath(pathname);
const surface =
SU_NAV.find((s) => pathname === s.href || pathname.startsWith(`${s.href}/`)) ??
SU_NAV[0];