Skip to Content

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.valueEnum

What 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 const lists
  • enums made with defineEnum from manh/enum: values, label(v), color(v), valueEnum, options
  • lifecycles made with defineFlow(def, Enum) from manh/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 a schema; 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.valueEnum on a column; status buttons come from OrderFlow.next(row.status, user.role).

Good to know

  • Files in app/shared/ import only app/shared/*, manh/enum, manh/flow and manh/vn. No zod, app/db, manh/env, hono, drizzle-orm or react. check reports shared.impure-import.
  • Files are .ts with no JSX (shared.not-tsx) and no window, document or localStorage.
  • Don’t declare the same list again in a page or a handler.
  • With app/auth.ts, manh.run generates app/shared/auth-roles.ts with ROLES, ADMIN_ROLE, DEFAULT_ROLE. Use it for menus and role pickers.

Next steps

Last updated