AppShell
The staff app frame from manh/shell: a menu filtered by role, sign-out, Vietnamese for every component, the app’s theme and API errors shown by themselves. app/layout.tsx only declares the menu.
Usage
app/layout.tsx
import { AppShell } from "manh/shell";
import { ADMIN_ROLE, ROLE_LABELS } from "app/shared/auth-roles";
import { DashboardOutlined, ShoppingCartOutlined, TeamOutlined } from "@ant-design/icons";
import { getUser, signOut } from "app/hooks";
import { APP_THEME } from "app/theme";
const MENU = [
{ path: "/", name: "Tổng quan", icon: <DashboardOutlined /> },
{ path: "/orders", name: "Đơn hàng", icon: <ShoppingCartOutlined /> },
{ path: "/users", name: "Người dùng", icon: <TeamOutlined />, roles: [ADMIN_ROLE] },
];
export default function Layout() {
return <AppShell appName="Cửa hàng Minh Long" menu={MENU} roleLabels={ROLE_LABELS} user={getUser()} onSignOut={signOut} theme={APP_THEME} />;
}The auth starter writes this file. A new screen is one more line in MENU.
Signature
manh/shell
type MenuItem = { path?: string; name: string; icon?: ReactNode; roles?: string[]; children?: MenuItem[]; count?: () => Promise<number> | number };
function AppShell(props: {
appName: string;
logo?: ReactNode;
menu: MenuItem[];
roleLabels?: Record<string, string>;
user: { name: string; role: string; avatarUrl?: string | null } | null;
onSignOut: () => void;
theme?: { colorPrimary?: string; borderRadius?: number; dark?: boolean; compact?: boolean };
publicPaths?: string[];
isPublic?: (pathname: string) => boolean;
extra?: ReactNode;
}): JSX.Element;Parameters
appName,logo: the name and logo in the top corner.menu: the menu items.roles: only these roles see the item.children: groups several screens, such as a “Kho” (stock) group.count: returns the work waiting, shown as a badge next to the item and refreshed on every page change.
roleLabels: role names shown in the header, such as{ technician: "Kỹ thuật viên" }.user: the signed-in user, usuallygetUser().nullredirects to/login.onSignOut: runs on Đăng xuất (sign out), usuallysignOut; then goes to/login.theme: primary colour, radius, dark mode, compact mode. PassAPP_THEMEfromapp/theme.tsx.publicPaths: pages shown without the frame and without sign-in,["/login", "/register"]by default.isPublic: decides which pages are public, for a website with many pages.extra: an extra header button, such as a notification bell.
Returns
A layout around every child page:
- Vietnamese for antd and pro-components, and the app’s theme.
- The column types
vnd,vnDate,vnDateTime,vnDateRangeandphoneforProTableandProDescriptions. - The current menu item is highlighted and its group opens.
- Every failed API call (except 401) shows the server’s message by itself, so pages need no
try / catchwithmessage.error.
Examples
A menu with a group and a count of work waiting:
app/layout.tsx
const MENU = [
{ path: "/orders", name: "Chờ duyệt", count: () => api.orders.list({ status: "pending", pageSize: 1 }).then((r) => r.total) },
{ name: "Kho", icon: <InboxOutlined />, children: [
{ path: "/stock", name: "Tồn kho" },
{ path: "/stock/receipts", name: "Phiếu nhập" },
] },
];To show an error somewhere of your own, errorText(e) from manh/shell does the same as apiErrorText.
Next steps
- ResourceList: list screens inside this frame.
- App frame and page header: page layout inside the frame.
- useSession: hide buttons by role inside a page.
Last updated