Skip to content

Commit

Permalink
Simplify demo titles
Browse files Browse the repository at this point in the history
  • Loading branch information
djhi committed Jan 22, 2025
1 parent a8beb34 commit cf47d5a
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 119 deletions.
23 changes: 6 additions & 17 deletions examples/demo/src/categories/CategoryEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,12 @@ import {
ReferenceManyField,
SimpleForm,
TextInput,
useTranslate,
useRecordContext,
useDefaultTitle,
useGetResourceLabel,
useEditContext,
} from 'react-admin';

import ThumbnailField from '../products/ThumbnailField';
import ProductRefField from '../products/ProductRefField';
import { Category } from '../types';

const CategoryEdit = () => (
<Edit title={<CategoryTitle />}>
Expand Down Expand Up @@ -62,21 +59,13 @@ const CategoryEdit = () => (

const CategoryTitle = () => {
const appTitle = useDefaultTitle();
const record = useRecordContext<Category>();
const translate = useTranslate();
const getResourceLabel = useGetResourceLabel();

const pageTitle = translate('ra.page.edit', {
name: getResourceLabel('categories', 1),
recordRepresentation: `"${record?.name}"`,
});

return record ? (
const { defaultTitle } = useEditContext();
return (
<>
<title>{`${appTitle} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
) : null;
);
};

export default CategoryEdit;
7 changes: 3 additions & 4 deletions examples/demo/src/categories/CategoryList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ import { humanize } from 'inflection';

import LinkToRelatedProducts from './LinkToRelatedProducts';
import { Category } from '../types';
import { usePageTitle } from '../usePageTitle';

const CategoriesTitle = () => {
const title = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'list' });
const { defaultTitle } = useListContext();
return (
<>
<title>{`${title} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${title} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/invoices/InvoiceList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import {
ReferenceInput,
FilterButton,
useDefaultTitle,
useListContext,
} from 'react-admin';

import FullNameField from '../visitors/FullNameField';
import AddressField from '../visitors/AddressField';
import InvoiceShow from './InvoiceShow';
import { usePageTitle } from '../usePageTitle';

const listFilters = [
<DateInput source="date_gte" alwaysOn />,
Expand All @@ -37,11 +37,11 @@ const ListActions = () => (

const InvoicesTitle = () => {
const title = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'list' });
const { defaultTitle } = useListContext();
return (
<>
<title>{`${title} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${title} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/orders/OrderEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import {
useRecordContext,
useTranslate,
useDefaultTitle,
useEditContext,
} from 'react-admin';
import { Card, CardContent, Box, Grid, Typography, Link } from '@mui/material';

import { Customer } from '../types';
import Basket from './Basket';
import Totals from './Totals';
import { usePageTitle } from '../usePageTitle';

const OrderEdit = () => (
<Edit title={<OrderTitle />} component="div">
Expand All @@ -30,11 +30,11 @@ const OrderEdit = () => (

const OrderTitle = () => {
const appTitle = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'edit' });
const { defaultTitle } = useEditContext();
return (
<>
<title>{`${appTitle} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
7 changes: 3 additions & 4 deletions examples/demo/src/orders/OrderList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import CustomerReferenceField from '../visitors/CustomerReferenceField';
import AddressField from '../visitors/AddressField';
import MobileGrid from './MobileGrid';
import { Customer } from '../types';
import { usePageTitle } from '../usePageTitle';

const ListActions = () => (
<TopToolbar>
Expand All @@ -40,11 +39,11 @@ const ListActions = () => (

const OrdersTitle = () => {
const title = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'list' });
const { defaultTitle } = useListContext();
return (
<>
<title>{`${title} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${title} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/products/ProductCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import {
TabbedForm,
TextInput,
required,
useCreateContext,
useDefaultTitle,
} from 'react-admin';
import { ProductEditDetails } from './ProductEditDetails';
import { usePageTitle } from '../usePageTitle';
const RichTextInput = React.lazy(() =>
import('ra-input-rich-text').then(module => ({
default: module.RichTextInput,
Expand All @@ -16,12 +16,12 @@ const RichTextInput = React.lazy(() =>

const ProductTitle = () => {
const appTitle = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'create' });
const { defaultTitle } = useCreateContext();

return (
<>
<title>{`${appTitle} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
22 changes: 6 additions & 16 deletions examples/demo/src/products/ProductEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import {
TabbedForm,
TextField,
TextInput,
useRecordContext,
useDefaultTitle,
useTranslate,
useGetResourceLabel,
useEditContext,
} from 'react-admin';
import PhotoCameraIcon from '@mui/icons-material/PhotoCamera';
import AspectRatioIcon from '@mui/icons-material/AspectRatio';
Expand All @@ -25,7 +23,6 @@ import { ProductEditDetails } from './ProductEditDetails';
import CustomerReferenceField from '../visitors/CustomerReferenceField';
import StarRatingField from '../reviews/StarRatingField';
import Poster from './Poster';
import { Product } from '../types';
import CreateRelatedReviewButton from './CreateRelatedReviewButton';

const RichTextInput = React.lazy(() =>
Expand All @@ -36,20 +33,13 @@ const RichTextInput = React.lazy(() =>

const ProductTitle = () => {
const appTitle = useDefaultTitle();
const translate = useTranslate();
const record = useRecordContext<Product>();
const getResourceLabel = useGetResourceLabel();

const pageTitle = translate('ra.page.edit', {
name: getResourceLabel('products', 1),
recordRepresentation: `"${record?.reference}"`,
});
return record ? (
const { defaultTitle } = useEditContext();
return (
<>
<title>{`${appTitle} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
) : null;
);
};

const ProductEdit = () => (
Expand Down
19 changes: 14 additions & 5 deletions examples/demo/src/products/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@ import {
TopToolbar,
useTranslate,
useDefaultTitle,
useListContext,
} from 'react-admin';

import ImageList from './GridList';
import Aside from './Aside';
import { usePageTitle } from '../usePageTitle';

const ProductList = () => {
const appTitle = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'list' });
const isSmall = useMediaQuery<Theme>(theme => theme.breakpoints.down('md'));
return (
<ListBase perPage={24} sort={{ field: 'reference', order: 'ASC' }}>
<title>{`${appTitle} - ${pageTitle}`}</title>
<Title defaultTitle={pageTitle} />
<ProductTitle />
<FilterContext.Provider value={productFilters}>
<ListActions isSmall={isSmall} />
{isSmall && (
Expand All @@ -51,6 +48,18 @@ const ProductList = () => {
);
};

const ProductTitle = () => {
const appTitle = useDefaultTitle();
const { defaultTitle } = useListContext();

return (
<>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<Title defaultTitle={defaultTitle} />
</>
);
};

const QuickFilter = ({ label }: InputProps) => {
const translate = useTranslate();
return <Chip sx={{ mb: 1 }} label={translate(label as string)} />;
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/reviews/ReviewCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ import {
useRedirect,
getRecordFromLocation,
useDefaultTitle,
useCreateContext,
} from 'react-admin';
import { useLocation } from 'react-router';

import StarRatingInput from './StarRatingInput';
import { usePageTitle } from '../usePageTitle';

const ReviewTitle = () => {
const appTitle = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'create' });
const { defaultTitle } = useCreateContext();
return (
<>
<title>{`${appTitle} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${appTitle} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
6 changes: 3 additions & 3 deletions examples/demo/src/reviews/ReviewEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
DateField,
Labeled,
useDefaultTitle,
useEditContext,
} from 'react-admin';
import { Box, Grid, Stack, IconButton, Typography } from '@mui/material';
import CloseIcon from '@mui/icons-material/Close';
Expand All @@ -16,7 +17,6 @@ import CustomerReferenceField from '../visitors/CustomerReferenceField';
import StarRatingField from './StarRatingField';
import ReviewEditToolbar from './ReviewEditToolbar';
import { Review } from '../types';
import { usePageTitle } from '../usePageTitle';

interface ReviewEditProps {
id: Review['id'];
Expand All @@ -25,8 +25,8 @@ interface ReviewEditProps {

const ReviewTitle = () => {
const appTitle = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'edit' });
return <title>{`${appTitle} - ${pageTitle}`}</title>;
const { defaultTitle } = useEditContext();
return <title>{`${appTitle} - ${defaultTitle}`}</title>;
};

const ReviewEdit = ({ id, onCancel }: ReviewEditProps) => {
Expand Down
8 changes: 4 additions & 4 deletions examples/demo/src/reviews/ReviewList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
SelectColumnsButton,
TopToolbar,
useDefaultTitle,
useListContext,
} from 'react-admin';
import { matchPath, useLocation, useNavigate } from 'react-router-dom';
import { Box, Drawer, useMediaQuery, Theme } from '@mui/material';
Expand All @@ -16,7 +17,6 @@ import ReviewListMobile from './ReviewListMobile';
import ReviewListDesktop from './ReviewListDesktop';
import reviewFilters from './reviewFilters';
import ReviewEdit from './ReviewEdit';
import { usePageTitle } from '../usePageTitle';

const ReviewListActions = () => (
<TopToolbar>
Expand All @@ -29,11 +29,11 @@ const ReviewListActions = () => (

const ReviewsTitle = () => {
const title = useDefaultTitle();
const pageTitle = usePageTitle({ view: 'list' });
const { defaultTitle } = useListContext();
return (
<>
<title>{`${title} - ${pageTitle}`}</title>
<span>{pageTitle}</span>
<title>{`${title} - ${defaultTitle}`}</title>
<span>{defaultTitle}</span>
</>
);
};
Expand Down
13 changes: 10 additions & 3 deletions examples/demo/src/segments/Segments.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,23 @@ import {
TableHead,
TableRow,
} from '@mui/material';
import { useTranslate, Title, useDefaultTitle } from 'react-admin';
import {
useTranslate,
Title,
useDefaultTitle,
useGetResourceLabel,
} from 'react-admin';

import LinkToRelatedCustomers from './LinkToRelatedCustomers';
import segments from './data';
import { usePageTitle } from '../usePageTitle';

const Segments = () => {
const appTitle = useDefaultTitle();
const translate = useTranslate();
const pageTitle = usePageTitle({ view: 'list', resource: 'segments' });
const getResourceLabel = useGetResourceLabel();
const pageTitle = translate(`ra.page.list`, {
name: getResourceLabel('segments', 2),
});

return (
<>
Expand Down
Loading

0 comments on commit cf47d5a

Please sign in to comment.