Open source CapCut alternative.
TypeScript
fix(site): open a super-user section from the proxy
David Le1fdd22ce6d7e
diff --git a/site/src/app/su/SuHeader.tsx b/site/src/app/su/SuHeader.tsxindex 53063cf0..dc64f7ec 100644--- a/site/src/app/su/SuHeader.tsx+++ b/site/src/app/su/SuHeader.tsx@@ -1,16 +1,30 @@"use client";import { usePathname } from "next/navigation";+import type { ComponentType } from "react";-import { suSurfaceAt } from "@/app/su/nav";+import { RunAnalyticsButton } from "@/app/su/analytics/RunAnalyticsButton";+import { DrainNowButton } from "@/app/su/jobs/email/DrainNowButton";+import { suSurfaceAt, type SuPage } from "@/app/su/nav";+import { ScanOutreachButton } from "@/app/su/outreach/ScanOutreachButton";import { SidebarTrigger } from "@/components/ui/sidebar";+// The action a page carries in its header, keyed by the page's address. The+// rail's list (nav.ts) stays free of React so the proxy can read it, so the+// components live here.+const PAGE_ACTIONS: Partial<Record<SuPage["href"], ComponentType>> = {+ "/analytics/product": RunAnalyticsButton,+ "/jobs/email": DrainNowButton,+ "/outreach": ScanOutreachButton,+};+// The title is the surface's; the description and action belong to whichever-// tab is showing, since a section's tabs each drive their own work. On a+// page is showing, since a section's tabs each drive their own work. On a// phone the rail is off screen, so the header carries the button that opens it.export function SuHeader() {const pathname = usePathname();- const { surface, tab } = suSurfaceAt(pathname);+ const { surface, page } = suSurfaceAt(pathname);+ const Action = PAGE_ACTIONS[page.href];return (<div className="sticky top-0 z-20 mx-auto flex w-full max-w-6xl shrink-0 items-start justify-between gap-4 bg-background px-4 pt-4 pb-4 md:px-10 md:pt-9"><div className="flex min-w-0 items-start gap-2">@@ -19,14 +33,14 @@ export function SuHeader() {<h1 className="text-lg font-semibold tracking-tight">{surface.title}</h1>- {tab.description ? (+ {"description" in page && page.description ? (<p className="mt-1 text-sm text-muted-foreground">- {tab.description}+ {page.description}</p>) : null}</div></div>- {tab.Action ? <tab.Action /> : null}+ {Action ? <Action /> : null}</div>);}diff --git a/site/src/app/su/SuSidebar.tsx b/site/src/app/su/SuSidebar.tsxindex 029970f7..d70da15f 100644--- a/site/src/app/su/SuSidebar.tsx+++ b/site/src/app/su/SuSidebar.tsx@@ -2,10 +2,21 @@import Link, { useLinkStatus } from "next/link";import { usePathname, useRouter } from "next/navigation";-import { ArrowLeft, ChevronRight } from "lucide-react";+import {+ ArrowLeft,+ ChartColumn,+ ChevronRight,+ CreditCard,+ FlaskConical,+ ListChecks,+ Mail,+ Megaphone,+ Package,+ UserRound,+} from "lucide-react";import { useEffect } from "react";-import { SU_NAV, suSurfaceAt } from "@/app/su/nav";+import { SU_NAV, suSurfaceAt, suTabs, type SuSurface } from "@/app/su/nav";import {Collapsible,CollapsibleContent,@@ -27,11 +38,22 @@ import {} from "@/components/ui/sidebar";import { SU_APP_ORIGIN } from "@/cut/lib/hosts";-// Every page the rail can open. A section's own address redirects to its-// first tab, so the tabs are the pages.-const RAIL_PAGES = SU_NAV.flatMap((surface) =>- surface.tabs ? surface.tabs.map((tab) => tab.href) : [surface.href],-);+// Each surface's icon, keyed by address. The rail's list (nav.ts) stays free+// of React so the proxy can read it, so the icons live here.+const SURFACE_ICONS: Record<SuSurface["href"], typeof UserRound> = {+ "/analytics": ChartColumn,+ "/credits": CreditCard,+ "/experiments": FlaskConical,+ "/jobs": ListChecks,+ "/outreach": Mail,+ "/product": Package,+ "/promotions": Megaphone,+ "/users": UserRound,+};++// Every page the rail can open. A section's own address opens its first tab,+// so the tabs are the pages.+const RAIL_PAGES = SU_NAV.flatMap((surface) => suTabs(surface)?.map((tab) => tab.href) ?? [surface.href]);/** The clicked row's answer while its page is on the way: the accent fill,* breathing. A link only shows its pending state when the page was not@@ -99,7 +121,9 @@ export function SuSidebar() {<SidebarGroup><SidebarMenu className="gap-0.5">{SU_NAV.map((surface) => {- const { href, label, icon: Icon, tabs } = surface;+ const { href, label } = surface;+ const Icon = SURFACE_ICONS[href];+ const tabs = suTabs(surface);const active = here.surface === surface;if (!tabs) {return (@@ -135,7 +159,7 @@ export function SuSidebar() {{tabs.map((tab) => (<SidebarMenuSubItem key={tab.href}><SidebarMenuSubButton- isActive={active && here.tab === tab}+ isActive={active && here.page === tab}render={<Link href={tab.href} />}className="relative isolate">diff --git a/site/src/app/su/analytics/page.tsx b/site/src/app/su/analytics/page.tsxdeleted file mode 100644index 75d67c29..00000000--- a/site/src/app/su/analytics/page.tsx+++ /dev/null@@ -1,8 +0,0 @@-import { redirect } from "next/navigation";--import { SU_NAV } from "@/app/su/nav";--// The section has no surface of its own; it opens its first tab.-export default function SuAnalyticsPage() {- redirect(SU_NAV[0].tabs![0].href);-}diff --git a/site/src/app/su/experiments/page.tsx b/site/src/app/su/experiments/page.tsxdeleted file mode 100644index 478a85b6..00000000--- a/site/src/app/su/experiments/page.tsx+++ /dev/null@@ -1,9 +0,0 @@-import { redirect } from "next/navigation";--import { SU_NAV } from "@/app/su/nav";--// The section has no surface of its own; it opens its first tab.-export default function SuExperimentsPage() {- const section = SU_NAV.find((s) => s.href === "/experiments")!;- redirect(section.tabs![0].href);-}diff --git a/site/src/app/su/jobs/page.tsx b/site/src/app/su/jobs/page.tsxdeleted file mode 100644index 7484f9f4..00000000--- a/site/src/app/su/jobs/page.tsx+++ /dev/null@@ -1,8 +0,0 @@-import { redirect } from "next/navigation";--import { SU_NAV } from "@/app/su/nav";--// The section has no surface of its own; it opens its first tab.-export default function SuJobsPage() {- redirect(SU_NAV.find((s) => s.href === "/jobs")!.tabs![0].href);-}diff --git a/site/src/app/su/nav.ts b/site/src/app/su/nav.tsindex 50dc8048..fc94efb8 100644--- a/site/src/app/su/nav.ts+++ b/site/src/app/su/nav.ts@@ -1,91 +1,68 @@-import {- ChartColumn,- CreditCard,- FlaskConical,- ListChecks,- Mail,- Megaphone,- Package,- UserRound,-} from "lucide-react";-import type { ComponentType } from "react";--import { RunAnalyticsButton } from "@/app/su/analytics/RunAnalyticsButton";-import { DrainNowButton } from "@/app/su/jobs/email/DrainNowButton";-import { ScanOutreachButton } from "@/app/su/outreach/ScanOutreachButton";-// The super-user surfaces in rail order, addressed from the root of their own-// host. One entry carries everything a surface is drawn from — the rail's tab,-// the header's title and action — so adding a surface is one edit here and a-// page.tsx. The section root lands on the first entry, so reordering this list-// moves where the host opens.+// host. One entry carries what a surface is drawn from — the rail's tab, the+// header's title and description — so adding a surface is one edit here and a+// page.tsx; the rail keeps the icons and the header keeps the actions, keyed by+// address, because the proxy imports this file and it has to stay free of+// React. The host opens on the first entry, so reordering this list moves+// where it opens.//-// A surface with tabs is a section: its href is a folder, each tab is a page-// under it, and the section's own page redirects to the first tab. The rail-// opens the tabs beneath the section while any of them is showing.-export type SuTab = {+// A surface with tabs is a section: its href is a folder and each tab is a+// page under it. A section address opens its first tab, answered by the proxy+// before any route runs (suSectionHome). The rail opens the tabs beneath the+// section while any of them is showing.+type SuTabShape = {href: string;label: string;description?: string;- Action?: ComponentType;};-export type SuSurface = SuTab & {- icon: typeof UserRound;+type SuSurfaceShape = SuTabShape & {title: string;- tabs?: SuTab[];+ tabs?: readonly SuTabShape[];};-export const SU_NAV: SuSurface[] = [+export const SU_NAV = [{href: "/analytics",label: "Analytics",- icon: ChartColumn,title: "Analytics",tabs: [- { href: "/analytics/product", label: "Product", Action: RunAnalyticsButton },+ { href: "/analytics/product", label: "Product" },{ href: "/analytics/social", label: "Social" },],},{href: "/users",label: "Users",- icon: UserRound,title: "Users",description: "Account actions.",},{href: "/credits",label: "Credits",- icon: CreditCard,title: "Credits",description: "Offer credits to a user.",},{href: "/product",label: "Product",- icon: Package,title: "Product",description: "What a new account starts with.",},{href: "/outreach",label: "Outreach",- icon: Mail,title: "Outreach",- Action: ScanOutreachButton,},{href: "/promotions",label: "Promotions",- icon: Megaphone,title: "Promotions",description: "One email to a segment of accounts.",},{href: "/experiments",label: "Experiments",- icon: FlaskConical,title: "Experiments",tabs: [{@@ -103,7 +80,6 @@ export const SU_NAV: SuSurface[] = [{href: "/jobs",label: "Jobs",- icon: ListChecks,title: "Jobs",tabs: [{@@ -115,20 +91,38 @@ export const SU_NAV: SuSurface[] = [href: "/jobs/email",label: "Email",description: "The outbox: today's quota, what is queued by kind, and what failed.",- Action: DrainNowButton,},],},-];+] as const satisfies readonly SuSurfaceShape[];++export type SuSurface = (typeof SU_NAV)[number];+export type SuTab = Extract<SuSurface, { tabs: unknown }>["tabs"][number];+/** Every address the rail can open: a tab, or a surface without tabs. */+export type SuPage = SuTab | SuSurface;-// The surface an address belongs to, and the tab within it when the surface-// has tabs. A section address (`/analytics`) resolves to its first tab, the-// one its page redirects to; an address outside the rail falls back to the-// entry the section root opens.-export function suSurfaceAt(pathname: string): { surface: SuSurface; tab: SuTab } {+export const suTabs = (surface: SuSurface): readonly SuTab[] | undefined =>+ "tabs" in surface ? surface.tabs : undefined;++// 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 } {const surface =SU_NAV.find((s) => pathname === s.href || pathname.startsWith(`${s.href}/`)) ??SU_NAV[0];- const tab = surface.tabs?.find((t) => t.href === pathname) ?? surface.tabs?.[0] ?? surface;- return { surface, tab };+ const tabs = suTabs(surface);+ const page = tabs?.find((t) => t.href === pathname) ?? tabs?.[0] ?? surface;+ return { surface, page };+}++// Where an address that is not itself a page opens: the host root lands on+// the first entry, and a section on its first tab. Null for a page address.+export function suSectionHome(pathname: string): string | null {+ const surface = pathname === "/" ? SU_NAV[0] : SU_NAV.find((s) => s.href === pathname);+ if (!surface) return null;+ const tabs = suTabs(surface);+ if (tabs) return tabs[0].href;+ return pathname === "/" ? surface.href : null;}diff --git a/site/src/app/su/page.tsx b/site/src/app/su/page.tsxdeleted file mode 100644index d97ba919..00000000--- a/site/src/app/su/page.tsx+++ /dev/null@@ -1,8 +0,0 @@-import { redirect } from "next/navigation";--import { SU_NAV } from "./nav";--// The section root has no surface of its own; it opens the rail's first one.-export default function SuPage() {- redirect(SU_NAV[0].href);-}diff --git a/site/src/proxy.ts b/site/src/proxy.tsindex 7198e4c4..4169d348 100644--- a/site/src/proxy.ts+++ b/site/src/proxy.ts@@ -8,6 +8,7 @@ import {isDonkeycutHost,isSuHost,} from "@/cut/lib/hosts";+import { suSectionHome } from "@/app/su/nav";// Cut (the video editor, publicly "Donkey Cut") lives under /cut in this single// site app: the marketing landing at /cut and the app under /cut/app. The@@ -129,6 +130,17 @@ async function gateAnswer(cookie: string): Promise<{ status: number; superUser:async function suHost(req: NextRequest, pathname: string): Promise<NextResponse> {if (underPath(pathname, "/api")) return NextResponse.next();+ // The host root and a section address open a page, answered here so no+ // page ever renders a redirect. One thrown from a page reaches the browser+ // as a meta tag behind the streamed shell, and the page's slot paints empty+ // until the router follows it.+ const home = suSectionHome(pathname);+ if (home) {+ const url = req.nextUrl.clone();+ url.pathname = home;+ return NextResponse.redirect(url, 307);+ }+const account = await gateAnswer(req.headers.get("cookie") ?? "");if (account.status === 401) {// The callback has to carry the exact origin sign-in trusts (src/lib/auth.ts),