> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# Recipe — Adding a new feature

A short, opinionated playbook for adding a brand-new domain to the app. No feature in the repo currently ships the full `api` → `mappers` → `queries` → `store` → `screens` stack (the former reference, [`features/tracks`](/docs/features/tracks.md), kept only its `types.ts` and ephemeral `store.ts`), so the layering below and [data-fetching.md](/docs/data-fetching.md) are the reference.

## Decision tree

```mermaid
flowchart TB
  start([New thing to build]) --> q1{New user-facing domain?}
  q1 -->|no, it's a one-off screen| useFolder["Drop it inside an existing feature's screens/"]
  q1 -->|yes| q2{Has server data?}
  q2 -->|no| skipServer["Skip api.ts / mappers.ts / queries.ts"]
  q2 -->|yes| fullShape[Use the full feature shape]
  q1 --> q3{Belongs to many features?}
  q3 -->|yes| shared["Put in src/components or src/core/*"]
```

## Step-by-step

### 1. Scaffold the folder

```
src/features/<name>/
  api.ts          # optional: server data
  mappers.ts      # optional: DTO -> domain
  types.ts        # always
  queries.ts      # optional: react-query hooks
  store.ts        # optional: zustand
  screens/
    <Name>Screen.tsx
  components/
```

Naming: kebab-case folder, PascalCase screens/components.

### 2. Define types first

In `types.ts`:

```ts
export interface ThingDto {
  id: string;
  title: string;
  created_at: string;
}

export interface Thing {
  id: string;
  title: string;
  createdAt: string;
}
```

Snake_case for DTO, camelCase for domain. Always two distinct interfaces.

### 3. Write the api + mapper

```ts
// api.ts
import { apiClient } from '@services/apiClient';
import type { ThingDto } from './types';

export const fetchThings = async () => {
  const { data } = await apiClient.get<ThingDto[]>('/things');
  return data;
};

export const fetchThing = async (id: string) => {
  const { data } = await apiClient.get<ThingDto>(`/things/${id}`);
  return data;
};
```

```ts
// mappers.ts
import type { Thing, ThingDto } from './types';

export const toThing = (dto: ThingDto): Thing => ({
  id: dto.id,
  title: dto.title,
  createdAt: dto.created_at,
});
```

### 4. Wrap them in queries

```ts
// queries.ts
import { useQuery } from '@tanstack/react-query';
import { fetchThing, fetchThings } from './api';
import { toThing } from './mappers';
import type { Thing } from './types';

export const thingsKeys = {
  all: ['things'] as const,
  list: () => [...thingsKeys.all, 'list'] as const,
  detail: (id: string) => [...thingsKeys.all, 'detail', id] as const,
};

export const useThingsQuery = () =>
  useQuery<Thing[]>({
    queryKey: thingsKeys.list(),
    queryFn: async () => {
      const dtos = await fetchThings();
      return dtos.map(toThing);
    },
  });

export const useThingQuery = (id: string | undefined) =>
  useQuery<Thing>({
    queryKey: id ? thingsKeys.detail(id) : ['things', 'detail', 'none'],
    queryFn: async () => {
      if (!id) throw new Error('Missing id');
      return toThing(await fetchThing(id));
    },
    enabled: Boolean(id),
  });
```

### 5. Add client state (if needed)

Only if the screen has filters, selected items, or other UI state that should outlive a single component:

```ts
// store.ts
import { create } from 'zustand';

interface ThingsUiState {
  filter: string;
  setFilter: (v: string) => void;
}

export const useThingsStore = create<ThingsUiState>((set) => ({
  filter: '',
  setFilter: (filter) => set({ filter }),
}));
```

Persist only if a returning user should see the value (rare for UI filters; common for tutorials, user prefs).

### 6. Build the screen

```tsx
// screens/ThingsScreen.tsx
import { LegendList } from '@legendapp/list/react-native';
import { Screen } from '@components/Screen';
import { useThingsQuery } from '../queries';

export const ThingsScreen = () => {
  const { data, isLoading } = useThingsQuery();
  if (isLoading || !data) return <Screen center>Loading...</Screen>;
  return (
    <Screen>
      {/* A list that can outgrow a screen recycles its cells (docs/architecture.md#lists). */}
      <LegendList recycleItems data={data} keyExtractor={(t) => t.id} renderItem={({ item }) => ...} />
    </Screen>
  );
};
```

### 7. Wire the route

```tsx
// app/(app)/(tabs)/things.tsx
import { ThingsScreen } from '@features/things/screens/ThingsScreen';
export default ThingsScreen;
```

If it's a tab, also add it to `(tabs)/_layout.tsx`. If it's a detail under another tab, add `app/(app)/things/[id].tsx`.

## Checklist before merging

- [ ] Feature-scoped tests pass: `bun test src/features/<name>` (or `cd packages/<pkg> && bun test` for package work).
- [ ] Global suite passes: `bun run test` (packages **+** app shards in parallel), `bun run typecheck` (`tsc --noEmit`), `bun run lint` (`biome check`) — all three required. Requires Bun ≥ 1.3.13.
- [ ] No hardcoded colors — use `theme.colors.*`.
- [ ] No `useEffect`+`useState` to fetch — use `useQuery`.
- [ ] No imports of other features' `screens/` or `components/`.
- [ ] DTO and domain types are separate.
- [ ] Mutations invalidate the right query keys.

## Visual recap

```mermaid
flowchart TB
  step1["1. types.ts"] --> step2["2. api.ts + mappers.ts"]
  step2 --> step3["3. queries.ts"]
  step3 --> step4["4. store.ts (optional)"]
  step4 --> step5["5. screens/ + components/"]
  step5 --> step6["6. app/.../route.tsx (re-export)"]
```

***

<small>Source: [`docs/adding-a-feature.md`](https://github.com/leapgame/leap-sim-piano/blob/f794b594016038c483e2d20b6af56aea0ae1b982/docs/adding-a-feature.md)</small>
