--- name: kiranism-shadcn-dashboard description: | Guide for building features, pages, tables, forms, themes, and navigation in this Next.js 16 shadcn dashboard template. Use this skill whenever the user wants to add a new page, create a feature module, build a data table, add a form, configure navigation items, add a theme, set up RBAC access control, or work with the dashboard's patterns and conventions. Also triggers when adding routes under /dashboard, working with Clerk auth/orgs/billing, creating mock APIs, or modifying the sidebar. Even if the user doesn't mention "dashboard" explicitly — if they're adding UI, pages, or features to this project, use this skill. --- # Dashboard Development Guide This skill encodes the exact patterns and conventions used in this Next.js 16 + shadcn/ui admin dashboard template. Following these patterns ensures consistency across the codebase. ## Quick Reference: What Goes Where | Task | Location | | ---------------- | --------------------------------------------------- | | New page | `src/app/dashboard//page.tsx` | | New feature | `src/features//components/` | | Query options | `src/features//api/queries.ts` | | Nav item | `src/config/nav-config.ts` | | Types | `src/types/index.ts` | | Mock data | `src/constants/mock-api.ts` or `mock-api-.ts` | | Search params | `src/lib/searchparams.ts` | | Query client | `src/lib/query-client.ts` | | Theme CSS | `src/styles/themes/.css` | | Theme registry | `src/components/themes/theme.config.ts` | | Custom hook | `src/hooks/` | | Form components | `src/components/forms/` | | Table components | `src/components/data-table/` | | Icons registry | `src/components/icons.tsx` | --- ## Adding a New Feature (End-to-End) When a user asks to add a new feature (e.g., "add a users page", "create an orders section"), follow all these steps in order: 1. **Create mock API** in `src/constants/mock-api-.ts` 2. **Create query options** in `src/features//api/queries.ts` 3. **Create the feature module** in `src/features//components/` 4. **Create the page route** in `src/app/dashboard//page.tsx` 5. **Add search params** in `src/lib/searchparams.ts` (if table/filtering needed) 6. **Add navigation** in `src/config/nav-config.ts` 7. **Register icon** in `src/components/icons.tsx` (if new icon needed) --- ## 1. Data Fetching with React Query The project uses **TanStack React Query** for data fetching with server-side prefetching and client-side cache management. This is the default pattern for all new pages. ### Query Options (`api/queries.ts`) Define reusable query options shared between server prefetch and client hooks: ```tsx import { queryOptions } from '@tanstack/react-query'; import { fakeEntities, type Entity } from '@/constants/mock-api-entities'; export type { Entity }; export const entitiesQueryOptions = (filters: { page?: number; limit?: number; search?: string }) => queryOptions({ queryKey: ['entities', filters], queryFn: () => fakeEntities.getEntities(filters) }); ``` ### Server Prefetch + Client Hydration (Listing Component) ```tsx import { HydrationBoundary, dehydrate, noop } from '@tanstack/react-query'; import { getQueryClient } from '@/lib/query-client'; import { searchParamsCache } from '@/lib/searchparams'; import { entityQueries } from '../api/queries'; import { EntityTable, EntityTableSkeleton } from './entity-table'; import { Suspense } from 'react'; export default function EntityListingPage() { const page = searchParamsCache.get('page'); const search = searchParamsCache.get('name'); const pageLimit = searchParamsCache.get('perPage'); const filters = { page, limit: pageLimit, ...(search && { search }) }; const queryClient = getQueryClient(); void queryClient.query(entityQueries.list(filters)).catch(noop); return ( }> ); } ``` ### Client Table Component (`shallow: true` + `useQuery`) ```tsx 'use client'; import { useQuery } from '@tanstack/react-query'; import { parseAsInteger, useQueryState } from 'nuqs'; import { useDataTable } from '@/hooks/use-data-table'; import { entityQueries } from '../../api/queries'; import { columns } from './columns'; export function EntityTable() { const [page] = useQueryState('page', parseAsInteger.withDefault(1)); const [pageSize] = useQueryState('perPage', parseAsInteger.withDefault(10)); const [search] = useQueryState('name'); const filters = { page, limit: pageSize, ...(search && { search }) }; const { data, isLoading } = useQuery(entityQueries.list(filters)); const { table } = useDataTable({ data: data?.items ?? [], columns, pageCount: Math.ceil((data?.total_items ?? 0) / pageSize), shallow: true, // URL changes stay client-side — React Query handles fetching debounceMs: 500, initialState: { columnPinning: { start: [], end: ['actions'] } } }); if (isLoading) return ; return ( ); } ``` **Key points:** - `shallow: true` — URL changes stay client-side, React Query fetches on the client - `shallow: false` — triggers full RSC server navigation (legacy pattern, avoid for new pages) - First load uses hydrated server-prefetched data (no loading spinner) - Subsequent pagination/filter changes fetch on the client - Cached pages/filters load instantly (React Query cache) ### Mutations (Forms) ```tsx // api/queries.ts: shared mutationOptions hold only the mutationFn export const entityMutations = { create: () => mutationOptions({ mutationFn: (data: Payload) => createEntity(data) }) }; // component: everything after the mutation lives here, in one block const queryClient = useQueryClient(); const createMutation = useMutation({ ...entityMutations.create(), onSuccess: () => { toast.success('Created successfully'); router.push('/dashboard/entities'); }, onError: () => toast.error('Failed to create'), onSettled: () => queryClient.invalidateQueries({ queryKey: entityQueries.all() }) }); // In useAppForm onSubmit: onSubmit: async ({ value }) => { await createMutation.mutateAsync(payload); }; ``` --- ## 2. Page Structure Pages are **server components** by default. They use `PageContainer` and accept search params as a Promise (Next.js 16 pattern). ```tsx import PageContainer from '@/components/layout/page-container'; import EntityListingPage from '@/features/entities/components/entity-listing'; import { searchParamsCache } from '@/lib/searchparams'; import type { SearchParams } from 'nuqs/server'; export const metadata = { title: 'Dashboard: Entities' }; type PageProps = { searchParams: Promise }; export default async function Page(props: PageProps) { const searchParams = await props.searchParams; searchParamsCache.parse(searchParams); return ( ); } ``` --- ## 3. Data Tables Tables use TanStack Table v9 (DiceUI data table) with `useDataTable` hook and `nuqs` for URL state. Type columns with `DataTableFeatures` from `@/lib/data-table-features`. ### Column Definitions ```tsx export const columns: ColumnDef[] = [ { id: 'name', accessorKey: 'name', header: ({ column }) => , meta: { label: 'Name', placeholder: 'Search...', variant: 'text', icon: Icons.text }, enableColumnFilter: true }, { id: 'category', accessorKey: 'category', enableColumnFilter: true, meta: { label: 'Category', variant: 'multiSelect', options: CATEGORY_OPTIONS } }, { id: 'actions', cell: ({ row }) => } ]; ``` ### Column Pinning ```tsx initialState: { columnPinning: { start: [], end: ['actions'] } } ``` **Filter variants**: `text`, `number`, `range`, `date`, `dateRange`, `select`, `multiSelect`, `boolean` --- ## 4. Forms See `docs/forms.md` for the full guide. Forms use **TanStack Form + Zod** via `useAppForm` from `@/lib/form` (TanStack `createFormHook`). The shadcn TanStack Form doc anatomy lives once per widget in `src/components/forms/fields/`; pages render fields as one-liners: ```tsx import { useAppForm } from '@/lib/form'; import { FieldGroup } from '@/components/ui/field'; const form = useAppForm({ defaultValues: { email: '', role: '' }, validators: { onSubmit: schema }, // Zod schema — errors paint per field on submit onSubmit: async ({ value }) => { await createMutation.mutateAsync(value); // React Query mutation } });
{ e.preventDefault(); form.handleSubmit(); }} > } /> } /> Save
``` **Available components** (all take `label`, `description?`, `required?`): `TextField` (any input type; number converts at the edge, async-validator spinner built in), `TextareaField` (`showCount`), `SelectField`, `CheckboxField`, `SwitchField`, `RadioGroupField`, `SliderField`, `ComboboxField`, `DatePickerField`, `DateRangeField`, `OtpField`, `ColorField`, `FileUploadField`, `CheckboxGroupField`, `TagsField`, `ToggleGroupField`. **Rules:** - Array-valued components (`CheckboxGroupField`, `TagsField`, `ToggleGroupField`) need `mode='array'` on the `form.AppField`. - Field-level validators/listeners (async checks, `onChangeListenTo` linked fields) go on the `form.AppField` element; function validators return `{ message: '…' }` objects. - One-off custom fields (object-row arrays, bespoke UI) drop down to raw `form.Field` render props composing `Field`/`FieldLabel`/`FieldError` from `@/components/ui/field` — `data-invalid` on Field, `aria-invalid` on the control, `{isInvalid && }`. - Large forms split into typed sections with `withForm` from `@/lib/form` (section receives `form` as a prop; field names stay compile-checked). - Sheet/Dialog forms: submit button in the footer via the HTML `form` attribute (`