Skip to content
Source
Sign inSign up
fuko/trytanstackdb-v2
Open History to see the last commit

workspace.tsx

106 lines4.2 KB

1import { useLocation } from "@tanstack/react-router";
2import { useEffect, useState } from "react";
3import type { ReactNode } from "react";
4import { createPortal } from "react-dom";
5​
6import { BrowserFrame } from "@/components/browser/browser-frame";
7import { GuidePanel } from "@/components/guide/guide-panel";
8import { FixedNotesLayout } from "@/components/notes/notes-layout";
9import { usePageTitle } from "@/components/notes/use-page-title";
10import { ResizableHandle, ResizablePanel, ResizablePanelGroup } from "@/components/ui/resizable";
11import { Toaster } from "@/components/ui/sonner";
12import { exposeFakeApiOnWindow } from "@/api/dev-console";
13import { trackSectionProgress } from "@/guide/section-progress";
14import { useMediaQuery } from "@/hooks/use-media-query";
15​
16import { AppSlot } from "./app-slot";
17import { MobileWorkspace } from "./mobile-workspace";
18​
19/** At this width and up, the guide and the app sit side by side. Below it, the guide comes first. */
20export const SPLIT_LAYOUT_MIN_WIDTH = 768;
21​
22/**
23 * The page. Wide screens: the guide and the app (in a fake browser) as two
24 * framed panels with a resizable gap between them. Small screens: the guide
25 * full screen, with the app in a sheet that "Try it" buttons open.
26 *
27 * The app is rendered exactly once, here, and portalled into whichever
28 * layout is showing (see `AppSlot`), so resizing across the breakpoint never
29 * remounts it. Only its look changes: on small screens it's pinned to the
30 * iPhone layout.
31 */
32export function Workspace({ app }: { app: ReactNode }) {
33 const isSplit = useMediaQuery(`(min-width: ${SPLIT_LAYOUT_MIN_WIDTH}px)`);
34 // This route renders on the client only (`ssr: false`), so `document` exists.
35 const [appHost] = useState(() => {
36 const host = document.createElement("div");
37 host.className = "h-full";
38 return host;
39 });
40 const appSlot = <AppSlot host={appHost} />;
41​
42 // Doing a section's step in the app ticks it off in the guide.
43 useEffect(() => trackSectionProgress(), []);
44 useEffect(() => exposeFakeApiOnWindow(), []);
45​
46 return (
47 <>
48 {isSplit ? <SplitWorkspace appSlot={appSlot} /> : <MobileWorkspace appSlot={appSlot} />}
49 <Toaster position={isSplit ? "bottom-right" : "top-center"} closeButton />
50 {createPortal(
51 <FixedNotesLayout layout={isSplit ? null : "ios"}>{app}</FixedNotesLayout>,
52 appHost,
53 )}
54 </>
55 );
57​
58function SplitWorkspace({ appSlot }: { appSlot: ReactNode }) {
59 // Tablets and small laptop windows get a narrower guide and smaller minimums
60 // (the app switches to its iPhone layout when its pane gets narrow). Default
61 // sizes only apply on mount; crossing this width later only changes the limits.
62 const isWide = useMediaQuery("(min-width: 1024px)");
63 const title = usePageTitle();
64 const path = useLocation({ select: (location) => location.pathname });
65​
66 return (
67 <main className="h-dvh bg-muted p-2 lg:p-4 dark:bg-[#0c0c0d]">
68 <ResizablePanelGroup orientation="horizontal">
69 <ResizablePanel
70 id="guide"
71 defaultSize={isWide ? "34%" : "42%"}
72 minSize={isWide ? 320 : 280}
73 >
74 <Frame>
75 <GuidePanel />
76 </Frame>
77 </ResizablePanel>
78 <GapHandle />
79 <ResizablePanel id="app" defaultSize={isWide ? "66%" : "58%"} minSize={isWide ? 720 : 360}>
80 <Frame>
81 <BrowserFrame title={title} path={path}>
82 {appSlot}
83 </BrowserFrame>
84 </Frame>
85 </ResizablePanel>
86 </ResizablePanelGroup>
87 </main>
88 );
90​
91function Frame({ children }: { children: ReactNode }) {
92 return (
93 <div className="h-full overflow-hidden rounded-xl border bg-background shadow-sm lg:rounded-2xl">
94 {children}
95 </div>
96 );
98​
99/** The divider lives in the gap between the two panels. */
100function GapHandle() {
101 return (
102 <ResizableHandle className="group/handle w-3 bg-transparent after:hidden focus-visible:ring-0 lg:w-4">
103 <span className="h-10 w-1 rounded-full bg-foreground/15 transition-colors group-hover/handle:bg-foreground/40 group-data-[separator=hover]/handle:bg-foreground/40 group-data-[separator=active]/handle:bg-guide-accent group-focus-visible/handle:bg-guide-accent" />
104 </ResizableHandle>
105 );