import * as React from 'react'; import { createRouter, createRootRoute, createRoute, RouterProvider, Outlet, Link as TanStackLink, } from '@tanstack/react-router'; import { createHashHistory } from '@tanstack/history'; import fakeRestDataProvider from 'ra-data-fakerest'; import { tanStackRouterProvider } from 'ra-router-tanstack'; import { CustomRoutes, useGetList, Resource, LinkBase } from 'ra-core'; import { List, Datagrid, TextField, DateField, ReferenceField, EditButton, ShowButton, Edit, SimpleForm, TabbedForm, TextInput, DateInput, ReferenceInput, Show, SimpleShowLayout, Create, ReferenceManyField, TabbedShowLayout, } from 'ra-ui-materialui'; import { Box, Card, CardContent, Typography, Button } from '@mui/material'; import { Admin } from './Admin'; const { Route } = tanStackRouterProvider; export default { title: 'react-admin/Frameworks/TanStack', }; // Fake data provider with posts and comments const dataProvider = fakeRestDataProvider( { posts: [ { id: 1, title: 'Hello World', body: 'Welcome to react-admin with TanStack Router!', created_at: '2024-01-15', }, { id: 2, title: 'Getting Started', body: 'This is a guide to get you started with react-admin.', created_at: '2024-01-20', }, { id: 3, title: 'Advanced Features', body: 'Learn about advanced features in react-admin.', created_at: '2024-02-01', }, { id: 4, title: 'Custom Components', body: 'How to create custom components in react-admin.', created_at: '2024-02-10', }, { id: 5, title: 'Data Providers', body: 'Understanding data providers in react-admin.', created_at: '2024-02-15', }, ], comments: [ { id: 1, post_id: 1, author: 'Alice', body: 'Great post!', created_at: '2024-01-16', }, { id: 2, post_id: 1, author: 'Bob', body: 'Very helpful, thanks!', created_at: '2024-01-17', }, { id: 3, post_id: 2, author: 'Charlie', body: 'This is exactly what I needed.', created_at: '2024-01-21', }, { id: 4, post_id: 3, author: 'Diana', body: 'Can you explain more about this?', created_at: '2024-02-02', }, { id: '類/衣', post_id: 4, author: 'Eve', body: 'Awesome tutorial!', created_at: '2024-02-11', }, ], }, process.env.NODE_ENV === 'development' ); // Post List component const PostList = () => ( ); // Post Edit component const PostEdit = () => ( ); // Post Show component const PostShow = () => ( ); // Post Create component const PostCreate = () => ( ); // Comment List component const CommentList = () => ( ); const CommentShow = () => ( ); // Comment Edit component const CommentEdit = () => ( ); // Comment Create component const CommentCreate = () => ( ); // Custom Dashboard page const Dashboard = () => { const { data: posts, total: totalPosts } = useGetList('posts', { pagination: { page: 1, perPage: 5 }, sort: { field: 'created_at', order: 'DESC' }, }); const { total: totalComments } = useGetList('comments', { pagination: { page: 1, perPage: 5 }, sort: { field: 'created_at', order: 'DESC' }, }); return ( Dashboard Total Posts {totalPosts ?? 0} Total Comments {totalComments ?? 0} Configuration Recent Posts {posts?.map(post => ( {post.title} {post.created_at} ))} ); }; // Custom Settings page (custom route example) const SettingsPage = () => ( Settings Application Settings This is a custom page demonstrating custom routes with TanStack Router. Theme: Light Language: English Notifications: Enabled ); /** * Basic: Standalone TanStack Router Admin * Admin creates its own TanStack Router instance. */ export const FullApp = () => ( } /> `Comment by ${record.author}`} /> ); /** * Embedded in existing TanStack Router app * Admin is mounted under /admin in an existing TanStack Router application. */ const AppNav = () => ( Home About Admin ); const HomePage = () => ( Welcome to the App This is a TanStack Router application with an embedded react-admin panel. ); const AboutPage = () => ( About This demo shows how to embed react-admin inside an existing TanStack Router application. The admin panel is mounted at /admin and uses its own routing while integrating seamlessly with the parent application. ); const EmbeddedAdmin = () => ( } /> `Comment by ${record.author}`} /> ); // Create route tree for embedded mode // The frontend app has its own layout with AppNav // The admin app has its own layout (provided by react-admin) const rootRoute = createRootRoute({ component: () => , }); // Frontend layout with navigation - only for non-admin pages const FrontendLayout = () => ( ); const frontendLayoutRoute = createRoute({ getParentRoute: () => rootRoute, id: 'frontend', component: FrontendLayout, }); const homeRoute = createRoute({ getParentRoute: () => frontendLayoutRoute, path: '/', component: HomePage, }); const aboutRoute = createRoute({ getParentRoute: () => frontendLayoutRoute, path: '/about', component: AboutPage, }); // Admin routes - no frontend layout, admin provides its own const adminRoute = createRoute({ getParentRoute: () => rootRoute, path: '/admin', component: EmbeddedAdmin, }); const adminSplatRoute = createRoute({ getParentRoute: () => rootRoute, path: '/admin/$', component: EmbeddedAdmin, }); const routeTree = rootRoute.addChildren([ frontendLayoutRoute.addChildren([homeRoute, aboutRoute]), adminRoute, adminSplatRoute, ]); export const Embedded = () => { const router = React.useMemo( () => createRouter({ routeTree, history: createHashHistory(), } as any), [] ); return ; };