shared/
The app/shared/ folder holds the types, constants, enums, status lifecycles and pure functions that both pages and the server need, written once.
app/
├── shared/
│ └── order.ts # OrderStatus, OrderFlow
├── orders/
│ ├── repository.ts # OrderFlow.assert(...)
│ ├── get.ts # z.enum(OrderStatus.values)
│ └── page.tsx # valueEnum: OrderStatus.valueEnumWhat it does
Pages (.tsx) and the server (.ts) never import each other’s files. app/shared/ is the one exception: both sides import it. Write a status list here and the API validator, table columns, selects and e-mails all read the same list.
Exports
Anything, as long as it is pure:
- TypeScript types, constants,
as constlists - enums made with
defineEnumfrommanh/enum:values,label(v),color(v),valueEnum,options - lifecycles made with
defineFlow(def, Enum)frommanh/flow:assert(user, from, to),next(status, role),can(role, from, to),isFinal(status) - pure functions, such as money formatting
Example
app/shared/order.ts
import { defineEnum } from "manh/enum";
import { defineFlow } from "manh/flow";
export const OrderStatus = defineEnum({
new: ["New", "processing"], // [label, color]
done: ["Done", "success"],
cancelled: ["Cancelled", "default"],
});
export type OrderStatus = (typeof OrderStatus.values)[number];
// status → next status → roles allowed to make the move
export const OrderFlow = defineFlow({
new: { done: ["warehouse", "admin"], cancelled: ["sales", "admin"] },
done: {},
cancelled: {},
}, OrderStatus); // pass the enum so a refused move reads "Mới" → "Hoàn thành", not the keys- Server:
z.enum(OrderStatus.values)in aschema;OrderFlow.assert(user, row.status, to)in the repository answers 409 for a move that doesn’t exist and 403 for the wrong role. - Pages:
valueEnum: OrderStatus.valueEnumon a column; status buttons come fromOrderFlow.next(row.status, user.role).
Good to know
- Files in
app/shared/import onlyapp/shared/*,manh/enum,manh/flowandmanh/vn. Nozod,app/db,manh/env,hono,drizzle-ormorreact.checkreportsshared.impure-import. - Files are
.tswith no JSX (shared.not-tsx) and nowindow,documentorlocalStorage. - Don’t declare the same list again in a page or a handler.
- With
app/auth.ts, manh.run generatesapp/shared/auth-roles.tswithROLES,ADMIN_ROLE,DEFAULT_ROLE. Use it for menus and role pickers.
Next steps
- repository.ts — call
OrderFlow.assertbefore changing a status. <resource>-columns.tsx— use the enum’svalueEnumon the status column.
Last updated