Skip to content

Commit

Permalink
Add page header to output pane
Browse files Browse the repository at this point in the history
  • Loading branch information
gramliu committed Nov 28, 2023
1 parent 0c491fe commit 2c97c66
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
44 changes: 36 additions & 8 deletions apps/web/app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"use client";
import { Editor } from "@tldraw/tldraw";
import { Editor, TLPageId } from "@tldraw/tldraw";
import "@tldraw/tldraw/tldraw.css";
import {
Button,
Expand All @@ -16,7 +16,7 @@ import IconLabel from "@ui/components/icon-label";
import NextJsIcon from "@ui/icons/nextjs";
import clsx from "clsx";
import { GitBranchIcon, GithubIcon } from "lucide-react";
import { useState } from "react";
import { useEffect, useState } from "react";
import { CopyBlock, nord } from "react-code-blocks";
import { convertEditorToCode } from "~/lib/editorToCode";

Expand Down Expand Up @@ -45,6 +45,26 @@ export default function IndexPage() {
const [loading, setLoading] = useState<boolean>(false);
const { toast } = useToast();

const [pageId, setPageId] = useState<TLPageId>();
const [page, setPage] = useState<string>("");

// Update page ID to trigger secondary effect
useEffect(() => {
if (editor != null) {
setPageId(editor.getCurrentPageId());
}
}, [editor]);

// Update page name in output pane
useEffect(() => {
if (pageId != null && editor != null) {
const page = editor.getPage(pageId);
if (page != null) {
setPage(page.name);
}
}
}, [pageId, editor]);

return (
<main className="h-full w-full flex flex-col p-5 pl-10 pt-10">
<h1 className="text-3xl font-bold">Web Spinner</h1>
Expand All @@ -58,7 +78,12 @@ export default function IndexPage() {
<section className="p-4 grid grid-cols-2 items-center justify-center">
{/* Editor */}
<div className="h-[70vh] w-[40vw] self-center justify-self-center bg-gray-100 rounded-md">
<Canvas setEditor={setEditor} />
<Canvas
setEditor={setEditor}
onPageChanged={(newPageId) => {
setPageId(newPageId);
}}
/>
</div>
{/* Output */}
<div
Expand All @@ -71,11 +96,14 @@ export default function IndexPage() {
defaultValue="preview"
className="w-full h-full grid grid-rows-[auto_1fr]"
>
<TabsList className="justify-start">
<TabsTrigger value="preview">Preview</TabsTrigger>
<TabsTrigger value="code_standalone">
Code (Standalone)
</TabsTrigger>
<TabsList className="justify-between">
<div className="pl-4">{page}</div>
<div>
<TabsTrigger value="preview">Preview</TabsTrigger>
<TabsTrigger value="code_standalone">
Code (Standalone)
</TabsTrigger>
</div>
</TabsList>
{loading ? (
<SkeletonPlaceholder />
Expand Down
4 changes: 2 additions & 2 deletions packages/ui/src/components/canvas/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"use client";
import { Editor, Tldraw } from "@tldraw/tldraw";
import { Editor, TLPageId, Tldraw } from "@tldraw/tldraw";
import "@tldraw/tldraw/tldraw.css";
import { Dispatch, SetStateAction, useCallback } from "react";
import initialState from "./initial-state.json";

interface CanvasProps {
onPageChanged?: (newPageId: string, oldPageId: string) => void;
onPageChanged?: (newPageId: TLPageId, oldPageId: TLPageId) => void;
setEditor?: Dispatch<SetStateAction<Editor>>;
}

Expand Down

0 comments on commit 2c97c66

Please sign in to comment.