-
Notifications
You must be signed in to change notification settings - Fork 122
FE-1500: Add the example embed route #9427
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
apps/petrinaut-website/src/examples/embedded-example-page.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { lazy, Suspense, type FunctionComponent } from "react"; | ||
|
|
||
| import { css } from "@hashintel/ds-helpers/css"; | ||
|
|
||
| import { getReadonlyExampleHandle } from "./readonly-example-handle"; | ||
| import { useSharedSearchNavigation } from "./use-shared-search-navigation"; | ||
|
|
||
| import type { LoadedExample } from "./catalog"; | ||
| import type { SharedExampleSearch } from "./example-search"; | ||
|
|
||
| const LazyPetrinaut = lazy(async () => { | ||
| const { Petrinaut } = await import("@hashintel/petrinaut/ui"); | ||
| return { default: Petrinaut }; | ||
| }); | ||
|
|
||
| // The page frame and the loading fallback render outside Petrinaut, and the | ||
| // design-system tokens are declared on `.petrinaut-root` (`cssVarRoot` in | ||
| // `scopedThemeConfig`), so token values do not resolve here. These use | ||
| // literals, as the site's other chrome does. | ||
| const pageStyle = css({ | ||
| width: "[100vw]", | ||
| height: "[100vh]", | ||
| minWidth: "0", | ||
| minHeight: "0", | ||
| overflow: "hidden", | ||
| backgroundColor: "[#f6f7f8]", | ||
| }); | ||
|
|
||
| const loadingStyle = css({ | ||
| display: "flex", | ||
| width: "[100%]", | ||
| height: "[100%]", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| color: "[#4b5563]", | ||
| fontSize: "[14px]", | ||
| }); | ||
|
|
||
| const embedTitleStyle = css({ | ||
| minWidth: "0", | ||
| overflow: "hidden", | ||
| color: "neutral.s90", | ||
| fontSize: "sm", | ||
| fontWeight: "medium", | ||
| textOverflow: "ellipsis", | ||
| whiteSpace: "nowrap", | ||
| }); | ||
|
|
||
| export type EmbeddedExamplePageProps = { | ||
| example: LoadedExample; | ||
| /** Writes the shared search subset back to the embed URL. */ | ||
| onSearchChange: ( | ||
| search: SharedExampleSearch, | ||
| history: "push" | "replace", | ||
| ) => void; | ||
| search: SharedExampleSearch; | ||
| }; | ||
|
|
||
| /** | ||
| * Embed of an example: the full Petrinaut component in its read-only | ||
| * presentation, navigated through the shared search contract. | ||
| */ | ||
| export const EmbeddedExamplePage: FunctionComponent< | ||
| EmbeddedExamplePageProps | ||
| > = ({ example, onSearchChange, search }) => { | ||
| const handle = getReadonlyExampleHandle(example); | ||
| const navigation = useSharedSearchNavigation(search, onSearchChange, { | ||
| // The embed lives in an iframe; it must not grow the host page's history. | ||
| historyPolicy: () => "replace", | ||
| }); | ||
|
|
||
| return ( | ||
| <main className={pageStyle}> | ||
| <Suspense | ||
| fallback={<div className={loadingStyle}>Loading Petrinautβ¦</div>} | ||
| > | ||
| <LazyPetrinaut | ||
| handle={handle} | ||
| hideNetManagementControls="all" | ||
| navigation={navigation} | ||
| readonly | ||
| slots={{ | ||
| topBarStart: ( | ||
| <span className={embedTitleStyle}>{example.catalog.title}</span> | ||
| ), | ||
| }} | ||
| title={example.catalog.title} | ||
| /> | ||
| </Suspense> | ||
| </main> | ||
| ); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import { css } from "@hashintel/ds-helpers/css"; | ||
|
|
||
| // This panel replaces the embed when there is no Petrinaut to render, so it | ||
| // sits outside `.petrinaut-root`, where the design-system tokens are declared | ||
| // (`cssVarRoot` in `scopedThemeConfig`). Token values would not resolve, so | ||
| // these are literals, matching the site's own not-found page. | ||
| const panelStyle = css({ | ||
| display: "flex", | ||
| width: "[100%]", | ||
| height: "[100%]", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| padding: "[24px]", | ||
| backgroundColor: "[#f6f7f8]", | ||
| fontFamily: "[Inter, system-ui, sans-serif]", | ||
| textAlign: "center", | ||
| }); | ||
|
|
||
| const titleStyle = css({ | ||
| color: "[#1f2933]", | ||
| fontSize: "[14px]", | ||
| fontWeight: "[600]", | ||
| }); | ||
|
|
||
| const bodyStyle = css({ | ||
| marginTop: "[4px]", | ||
| color: "[#4b5563]", | ||
| fontSize: "[12px]", | ||
| }); | ||
|
|
||
| /** | ||
| * Fallback for the embed route, sized for a frame rather than a page. The | ||
| * embed renders inside someone else's layout, so a failure stays small and | ||
| * carries no link: a link here would navigate the frame to this site. | ||
| */ | ||
| export const EmbedStatusPanel = ({ | ||
| body, | ||
| title, | ||
| }: { | ||
| body: string; | ||
| title: string; | ||
| }) => ( | ||
| <div className={panelStyle}> | ||
| <div> | ||
| <p className={titleStyle}>{title}</p> | ||
| <p className={bodyStyle}>{body}</p> | ||
| </div> | ||
| </div> | ||
| ); | ||
69 changes: 69 additions & 0 deletions
69
apps/petrinaut-website/src/routes/embed.examples.$slug.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { | ||
| createFileRoute, | ||
| notFound, | ||
| useLoaderData, | ||
| useNavigate, | ||
| useSearch, | ||
| } from "@tanstack/react-router"; | ||
|
|
||
| import { isExampleSlug, loadExample } from "../examples/catalog"; | ||
| import { EmbeddedExamplePage } from "../examples/embedded-example-page"; | ||
| import { validateSharedExampleSearch } from "../examples/example-search"; | ||
| import { EmbedStatusPanel } from "./-embed-status-panel"; | ||
|
|
||
| const routePath = "/embed/examples/$slug" as const; | ||
|
|
||
| function EmbeddedExampleRoute() { | ||
| const navigate = useNavigate({ from: routePath }); | ||
| const example = useLoaderData({ from: routePath }); | ||
|
|
||
| return ( | ||
| <EmbeddedExamplePage | ||
| // The page holds URL-unrepresentable location in state; remount per | ||
| // example so one model's state cannot leak into the next. | ||
| key={example.catalog.slug} | ||
| example={example} | ||
| onSearchChange={(search, history) => { | ||
| void navigate({ replace: history === "replace", search }); | ||
| }} | ||
| search={useSearch({ from: routePath })} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export const Route = createFileRoute("/embed/examples/$slug")({ | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| beforeLoad: ({ params }) => { | ||
| if (!isExampleSlug(params.slug)) { | ||
| throw notFound(); | ||
| } | ||
| }, | ||
| component: EmbeddedExampleRoute, | ||
| // The editor is imported lazily, so a chunk that fails to load throws after | ||
| // mount. Without a boundary here the root unmounts and the frame goes blank | ||
| // on someone else's page. | ||
| errorComponent: () => ( | ||
| <EmbedStatusPanel | ||
| body="Reload the page to try again." | ||
| title="This Petrinaut embed failed to load" | ||
| /> | ||
| ), | ||
| loader: ({ params }) => { | ||
| if (!isExampleSlug(params.slug)) { | ||
| throw notFound(); | ||
| } | ||
| return loadExample(params.slug); | ||
| }, | ||
| // The site-wide not-found page is a full viewport panel with a link that | ||
| // would navigate the embedder's frame, so the embed answers for itself. | ||
| notFoundComponent: () => ( | ||
| <EmbedStatusPanel | ||
| body="Check the example name in the embed URL." | ||
| title="Example not found" | ||
| /> | ||
| ), | ||
| // Unknown query params are dropped by `validateSearch` and left in the URL | ||
| // on purpose. Canonicalizing them with a `redirect({ href })` reloads the | ||
| // document inside the frame, and TanStack Router only rewrites the URL when | ||
| // the parsed search changes, so such a redirect re-triggers itself. | ||
| validateSearch: validateSharedExampleSearch, | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.