Skip to content

Commit

Permalink
Open respective page in editor from preview
Browse files Browse the repository at this point in the history
  • Loading branch information
apedroferreira committed Mar 29, 2023
1 parent 37c008c commit b811615
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions packages/toolpad-app/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const APP_PAGE_ROUTE = '/app/pages/:nodeId';
export const APP_API_ROUTE = '/app/apis/:nodeId';
export const APP_CODE_COMPONENT_ROUTE = '/app/codeComponents/:nodeId';
export const APP_CONNECTION_ROUTE = '/app/connections/:nodeId';

export const PREVIEW_PAGE_ROUTE = '/preview/pages/:nodeId';
8 changes: 4 additions & 4 deletions packages/toolpad-app/src/runtime/AppNavigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
import { useNavigate, useLocation, useHref } from 'react-router-dom';
import * as appDom from '../appDom';

const NAV_LIST_SUBHEADER_ID = 'nav-list-subheader';

const DRAWER_WIDTH = 250; // px

interface AppNavigationProps {
Expand All @@ -35,6 +33,8 @@ export default function AppNavigation({ pages, isPreview = false }: AppNavigatio

const activePagePath = location.pathname.replace(href, '');

const navListSubheaderId = React.useId();

return (
<Drawer
variant="permanent"
Expand All @@ -51,11 +51,11 @@ export default function AppNavigation({ pages, isPreview = false }: AppNavigatio
<List
component="nav"
subheader={
<ListSubheader id={NAV_LIST_SUBHEADER_ID} sx={{ px: 4 }}>
<ListSubheader id={navListSubheaderId} sx={{ px: 4 }}>
Pages
</ListSubheader>
}
aria-labelledby={NAV_LIST_SUBHEADER_ID}
aria-labelledby={navListSubheaderId}
>
{pages.map((page) => (
<ListItem key={page.id} onClick={handlePageClick(page)} disablePadding>
Expand Down
10 changes: 8 additions & 2 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import {
Navigate,
Location as RouterLocation,
useNavigate,
useHref,
matchPath,
} from 'react-router-dom';
import { ErrorBoundary, FallbackProps } from 'react-error-boundary';
import {
Expand Down Expand Up @@ -81,6 +83,7 @@ import Header from '../toolpad/ToolpadShell/Header';
import { ThemeProvider } from '../ThemeContext';
import { BridgeContext } from '../canvas/BridgeContext';
import AppNavigation from './AppNavigation';
import { PREVIEW_PAGE_ROUTE } from '../routes';

const ReactQueryDevtoolsProduction = React.lazy(() =>
import('@tanstack/react-query-devtools/build/lib/index.prod.js').then((d) => ({
Expand Down Expand Up @@ -1143,9 +1146,12 @@ function ToolpadAppLayout({ dom, version, hasShell: hasShellProp = true }: Toolp
const root = appDom.getApp(dom);
const { pages = [] } = appDom.getChildNodes(dom, root);

const { search } = useLocation();
const location = useLocation();
const { pathname, search } = location;
const urlParams = React.useMemo(() => new URLSearchParams(search), [search]);

const pageMatch = matchPath(PREVIEW_PAGE_ROUTE, `/preview${pathname}`);

const hasShell = hasShellProp && urlParams.get('toolpad-display') !== 'standalone';

const isPreview = version === 'preview';
Expand All @@ -1166,7 +1172,7 @@ function ToolpadAppLayout({ dom, version, hasShell: hasShellProp = true }: Toolp
endIcon={<EditIcon />}
color="primary"
component="a"
href={`/_toolpad/app`}
href={`/_toolpad/app/pages/${pageMatch?.params.nodeId}`}
>
Edit
</Button>
Expand Down

0 comments on commit b811615

Please sign in to comment.