forked from mui/toolpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_app.tsx
More file actions
195 lines (174 loc) · 4.88 KB
/
_app.tsx
File metadata and controls
195 lines (174 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import * as React from 'react';
import { useRouter } from 'next/router';
import { NextAppProvider } from '@toolpad/core/nextjs';
import { PageContainer } from '@toolpad/core/PageContainer';
import { DashboardLayout, ToolbarActions } from '@toolpad/core/DashboardLayout';
import Head from 'next/head';
import { AppCacheProvider } from '@mui/material-nextjs/v14-pagesRouter';
import DashboardIcon from '@mui/icons-material/Dashboard';
import ShoppingCartIcon from '@mui/icons-material/ShoppingCart';
import type { NextPage } from 'next';
import type { AppProps } from 'next/app';
import type { Navigation } from '@toolpad/core/AppProvider';
import { SessionProvider, signIn, signOut, useSession } from 'next-auth/react';
import LinearProgress from '@mui/material/LinearProgress';
import { Box, Button, IconButton, InputBase, Paper, Stack } from '@mui/material';
import SearchIcon from '@mui/icons-material/Search';
import { Account } from '@toolpad/core/Account';
export type NextPageWithLayout<P = {}, IP = P> = NextPage<P, IP> & {
getLayout?: (page: React.ReactElement<any>) => React.ReactNode;
requireAuth?: boolean;
};
type AppPropsWithLayout = AppProps & {
Component: NextPageWithLayout;
};
const NAVIGATION: Navigation = [
{
kind: 'header',
title: 'Main items',
},
{
title: 'Dashboard',
icon: <DashboardIcon />,
},
{
segment: 'orders',
title: 'Orders',
icon: <ShoppingCartIcon />,
pattern: 'orders{/:orderId}*',
},
];
const BRANDING = {
title: 'My Toolpad Core Next.js Pages App',
};
const AUTHENTICATION = {
signIn,
signOut,
};
function SearchBar() {
return (
<Paper
component="form"
elevation={0}
sx={{
alignItems: 'center',
width: { xl: 600, lg: 400, md: 'auto' },
height: 40,
px: 1.5,
borderRadius: 2,
backgroundColor: (theme) => theme.palette.action.hover,
}}
>
<IconButton type="submit" sx={{ p: '10px' }} aria-label="search" disableRipple>
<SearchIcon />
</IconButton>
<InputBase
sx={{ ml: 1, flex: 1 }}
placeholder="Search"
inputProps={{ 'aria-label': 'search' }}
/>
</Paper>
);
}
function CustomToolbar() {
return (
<Stack
direction="row"
spacing={2}
alignItems="center"
justifyContent="space-between"
className="custom-toolbar"
sx={{
flexGrow: 1,
px: 2,
py: 1,
}}
>
<Box sx={{ flexGrow: 1, display: 'flex', justifyContent: 'center' }}>
<Box sx={{ maxWidth: 400, width: '100%' }}>
<SearchBar />
</Box>
</Box>
<Stack direction="row" spacing={1} alignItems="center">
<ToolbarActions />
<Account />
<ToolbarCart />
</Stack>
</Stack>
);
}
function ToolbarCart() {
return (
<Button color="primary" aria-label="Shopping Cart">
<ShoppingCartIcon />
</Button>
);
}
function DefaultLayout({ page }: { page: React.ReactElement<any> }) {
const router = useRouter();
const { segments = [] } = router.query;
const [orderId] = segments;
const title = React.useMemo(() => {
if (router.asPath.split('?')[0] === '/orders/new') {
return 'New Order';
}
if (orderId && router.asPath.includes('/edit')) {
return `Order ${orderId} - Edit`;
}
if (orderId) {
return `Order ${orderId}`;
}
return undefined;
}, [orderId, router.asPath]);
return (
<DashboardLayout slots={{ toolbar: CustomToolbar }}>
<PageContainer title={title}>{page}</PageContainer>
</DashboardLayout>
);
}
function getDefaultLayout(page: React.ReactElement<any>) {
return <DefaultLayout page={page} />;
}
function RequireAuth({ children }: { children: React.ReactNode }) {
const { status } = useSession();
if (status === 'loading') {
return <LinearProgress />;
}
return children;
}
function AppLayout({ children }: { children: React.ReactNode }) {
const { data: session } = useSession();
return (
<React.Fragment>
<Head>
<meta name="viewport" content="initial-scale=1, width=device-width" />
</Head>
<NextAppProvider
navigation={NAVIGATION}
branding={BRANDING}
session={session}
authentication={AUTHENTICATION}
>
{children}
</NextAppProvider>
</React.Fragment>
);
}
export default function App(props: AppPropsWithLayout) {
const {
Component,
pageProps: { session, ...pageProps },
} = props;
const getLayout = Component.getLayout ?? getDefaultLayout;
const requireAuth = Component.requireAuth ?? true;
let pageContent = getLayout(<Component {...pageProps} />);
if (requireAuth) {
pageContent = <RequireAuth>{pageContent}</RequireAuth>;
}
pageContent = <AppLayout>{pageContent}</AppLayout>;
return (
<AppCacheProvider {...props}>
<SessionProvider session={session}>{pageContent}</SessionProvider>
</AppCacheProvider>
);
}