wcz-layout
Version:
90 lines (79 loc) • 4.14 kB
Markdown
---
name: routing
description: "Use when adding or changing a page or route file in app: list, detail, create and edit routes, loaders and preloading, pending skeletons, missing rows (not found), search params, route access (requireAuth), navigation entries and LayoutProvider options in __root.tsx, or in-app links (RouterLink, RouterButton, RouterIconButton)."
metadata:
type: convention
library: wcz-layout
---
Route mechanics (file routes, loaders, search params, navigation, not-found, type safety): load
`/router-core#router-core` and the sub-skills the task needs (`data-loading`,
`search-params`, `navigation`, `not-found-and-errors`).
## Rules
- Access: `beforeLoad: requireAuth("<key>")` with a key from `src/lib/auth/permissions.ts`, or
`requireAuth()` for any signed-in user; omit it only on intentionally public pages. It hides
the page; server functions guard themselves (server-functions). On a guarded route
`Route.useRouteContext().user` is a `User`, elsewhere `User | null`. `requireAuth` and
`hasPermission` come from `wcz-layout/utils`.
- Data: the loader returns `context.dbClient.preloadLiveQuery(…)` (client-db). A route
reading a suspense query sets `pendingComponent: <Entity>Skeleton`, MUI `Skeleton`s in the
page's own shape, from its `-components`. A missing row renders
`<RouterNotFound />` from the component.
- Names: feature folders and literal segments are kebab-case, entity features plural. One page
is `src/routes/<entities>.tsx`; several live in `src/routes/<entities>/` as `index.tsx`,
`$id.tsx`, `create.tsx`, `edit.$id.tsx`.
- Colocation: a feature's components and hooks go in `<entities>/-components` and `-hooks`.
Code only one of its routes uses goes in a group named after that route:
`<entities>/(index)/index.tsx` with `(index)/-components`. `src/components` and `src/hooks`
hold only code shared across features.
- One page per feature: narrow rows with filters or search params.
- Every new page gets a navigation item in `__root.tsx`: its own `/icons-material` icon,
`title` through `t`, and `hidden: !hasPermission(user, "<key>")` with the route's key.
Kinds: `item` (`title`, `icon`, `to` with `params`/`search`, or `href` for an external URL),
`group` (`title`, `icon`, `children`), `header` (`title`), `divider`; all accept `hidden`.
- `LayoutProvider` in `__root.tsx` takes `theme`, `navigation` and `options`: `appBarActions`
(nodes between the app title and the account menu), `showShell` (app bar and navigation) and
`chat` (a chat docked beside every page; see the ai skill).
- Links a user clicks are `RouterLink`, `RouterButton`, `RouterIconButton`, `RouterFab`,
`RouterTab`, `RouterListItemButton` from `wcz-layout/components`: typed `to`/`params`/
`search`, MUI props, and hover preloads the target's loader. `useNavigate()` only after an
action, e.g. a save.
## Examples
```tsx
// src/routes/features/$id.tsx — detail page; edit.$id.tsx has the same route options (forms)
export const Route = createFileRoute("/features/$id")({
component: RouteComponent,
pendingComponent: FeatureSkeleton,
beforeLoad: requireAuth("admin"),
loader: ({ context, params }) =>
context.dbClient.preloadLiveQuery(featureByIdQueryOptions(params.id)),
});
function RouteComponent() {
const { id } = Route.useParams();
const { t } = useTranslation();
const { data } = useLiveSuspenseQuery(featureByIdQueryOptions(id));
if (!data) return <RouterNotFound />;
return (
<>
<Typography>{data.name}</Typography>
<RouterButton to="/features/edit/$id" params={{ id }} startIcon={<Edit />}>
{t("Edit")}
</RouterButton>
{/* Delete: confirm(), optimistic delete, when("settled"), navigate({ to: "/features" }) */}
</>
);
}
```
```tsx
// src/routes/__root.tsx, inside RootDocument
const { user } = Route.useRouteContext();
const navigation: Navigation = [
{ kind: "item", to: "/", title: t("Homepage"), icon: <Home /> },
{
kind: "item",
to: "/features",
title: t("Feature.Features"),
icon: <Widgets />,
hidden: !hasPermission(user, "admin"),
},
];
```