Skip to content

Commit

Permalink
Make page title optional in the yml files (#2948)
Browse files Browse the repository at this point in the history
  • Loading branch information
Janpot authored Nov 29, 2023
1 parent 8d5dc73 commit 8d8cff0
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 25 deletions.
6 changes: 5 additions & 1 deletion packages/toolpad-app/src/appDom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type PageDisplayMode = 'standalone' | 'shell';
export interface PageNode extends AppDomNodeBase {
readonly type: 'page';
readonly attributes: {
readonly title: string;
readonly title?: string;
readonly parameters?: [string, string][];
readonly module?: string;
readonly display?: PageDisplayMode;
Expand Down Expand Up @@ -1207,3 +1207,7 @@ export function getRequiredEnvVars(dom: AppDom): Set<string> {

return new Set(allVars);
}

export function getPageTitle(node: PageNode): string {
return node.attributes.title || node.name;
}
4 changes: 2 additions & 2 deletions packages/toolpad-app/src/runtime/ToolpadApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1379,7 +1379,7 @@ function RenderedLowCodePage({ page }: RenderedLowCodePageProps) {
const dom = useDomContext();
const { children = [], queries = [] } = appDom.getChildNodes(dom, page);

usePageTitle(page.attributes.title);
usePageTitle(appDom.getPageTitle(page));

const location = useLocation();
const components = useComponents();
Expand Down Expand Up @@ -1431,7 +1431,7 @@ export function RenderedPage({ nodeId }: RenderedNodeProps) {
const dom = useDomContext();
const page = appDom.getNode(dom, nodeId, 'page');

usePageTitle(page.attributes.title);
usePageTitle(appDom.getPageTitle(page));

if (page.attributes.codeFile) {
return <RenderedProCodePage page={page} />;
Expand Down
2 changes: 1 addition & 1 deletion packages/toolpad-app/src/server/localMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ function createPageDomFromPageFile(pageName: string, pageFile: Page): appDom.App
let fragment = appDom.createFragmentInternal(pageFileSpec.id as NodeId, 'page', {
name: pageName,
attributes: {
title: pageFileSpec.title || '',
title: pageFileSpec.title,
parameters: pageFileSpec.parameters?.map(({ name, value }) => [name, value]) || [],
display: pageFileSpec.display || undefined,
codeFile: pageFileSpec.unstable_codeFile || undefined,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { styled } from '@mui/material';
import { NodeId } from '@mui/toolpad-core';
import usePageTitle from '@mui/toolpad-utils/hooks/usePageTitle';
import { Panel, PanelGroup, PanelResizeHandle } from '../../../components/resizablePanels';
import RenderPanel from './RenderPanel';
import ComponentPanel from './ComponentPanel';
Expand All @@ -9,7 +10,6 @@ import { useAppState } from '../../AppState';
import * as appDom from '../../../appDom';
import ComponentCatalog from './ComponentCatalog';
import NotFoundEditor from '../NotFoundEditor';
import usePageTitle from '../../../utils/usePageTitle';
import useUndoRedo from '../../hooks/useUndoRedo';

const classes = {
Expand All @@ -32,7 +32,7 @@ interface PageEditorContentProps {
}

function PageEditorContent({ node }: PageEditorContentProps) {
usePageTitle(`${node.attributes.title} | Toolpad editor`);
usePageTitle(`${appDom.getPageTitle(node)} | Toolpad editor`);

return (
<PageEditorProvider key={node.id} nodeId={node.id}>
Expand Down
10 changes: 5 additions & 5 deletions packages/toolpad-app/src/toolpad/AppEditor/PageTitleEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { TextField } from '@mui/material';
import * as React from 'react';
import { AppDom, PageNode, setNodeNamespacedProp } from '../../appDom';
import * as appDom from '../../appDom';
import { useDomApi } from '../AppState';

interface PageTitleEditorProps {
node: PageNode;
node: appDom.PageNode;
}

function validateInput(input: string) {
Expand All @@ -16,16 +16,16 @@ function validateInput(input: string) {

export default function PageTitleEditor({ node }: PageTitleEditorProps) {
const domApi = useDomApi();
const [pageTitleInput, setPageTitleInput] = React.useState(node.attributes.title);
const [pageTitleInput, setPageTitleInput] = React.useState(appDom.getPageTitle(node));

const handlePageTitleChange = React.useCallback(
(event: React.ChangeEvent<HTMLInputElement>) => setPageTitleInput(event.target.value),
[],
);

const handleCommit = React.useCallback(() => {
domApi.update((appDom: AppDom) =>
setNodeNamespacedProp(appDom, node, 'attributes', 'title', pageTitleInput),
domApi.update((dom: appDom.AppDom) =>
appDom.setNodeNamespacedProp(dom, node, 'attributes', 'title', pageTitleInput),
);
}, [node, pageTitleInput, domApi]);

Expand Down
14 changes: 0 additions & 14 deletions packages/toolpad-app/src/utils/usePageTitle.ts

This file was deleted.

0 comments on commit 8d8cff0

Please sign in to comment.