# Goals & tracking

### Description

As members chat and log, they create records such as meals. These components list, edit and summarize them, and show progress against the member's daily goals.

## MealList

### Description

An interactive list of meals.

### Basic usage

```tsx
import { useEffect, useState } from 'react';
import { Meal, MealList, useMiriApp } from '@miri-ai/miri-react-native';

function MealsForSelectedDay() {
  const { getMeals, selectedDate } = useMiriApp();
  const [meals, setMeals] = useState<Meal[]>([]);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    setIsLoading(true);
    // getMeals adjusts the time on the dates it is given, so pass a copy
    getMeals(new Date(selectedDate)).then((fetched) => {
      setMeals(fetched);
      setIsLoading(false);
    });
  }, [getMeals, selectedDate]);

  return <MealList meals={meals} isLoading={isLoading} />;
}
```

### Props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `meals` _(required)_ | `Meal[]` | The meals to show. |
| `isLoading` | `boolean` | If `true`, shows the loading state. Defaults to `false`. |
| `allowDelete` | `boolean` | If `true`, the user can swipe a meal to delete it. Defaults to `false`. |
| `onMealPress` | `(meal: Meal) => void` | Called when the user taps a meal. |
| `onDeleteMeal` | `(mealId: string) => void` | Called when the user deletes a meal. |
| `showTrackingItems` | `boolean` | If `true`, shows each meal's tracking item bubbles. |

## TrackingItemsSummary and StreakTracking

### Description

`TrackingItemsSummary` shows the member's progress on their daily tracking items (for example protein or water) for the selected day. `StreakTracking` shows their streak cards. Both take data from `getTrackingItemsProgress`.

### Basic usage

```tsx
import { useEffect, useState } from 'react';
import {
  Streak,
  StreakTracking,
  TrackingGroup,
  TrackingItemsSummary,
  useMiriApp,
} from '@miri-ai/miri-react-native';

function ProgressSummary() {
  const { getTrackingItemsProgress, selectedDate } = useMiriApp();
  const [progress, setProgress] = useState<{
    trackingItemsProgress: TrackingGroup[];
    streakCards: Streak[];
  } | null>(null);
  const [isLoading, setIsLoading] = useState(true);

  useEffect(() => {
    getTrackingItemsProgress().then((result) => {
      setProgress(result); // null if the request failed
      setIsLoading(false);
    });
  }, [getTrackingItemsProgress]);

  return (
    <>
      <TrackingItemsSummary
        trackingItemsProgress={progress?.trackingItemsProgress ?? []}
        selectedDate={selectedDate}
        isLoading={isLoading}
      />
      <StreakTracking streaks={progress?.streakCards ?? []} />
    </>
  );
}
```

### TrackingItemsSummary props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `trackingItemsProgress` _(required)_ | `TrackingGroup[]` | `trackingItemsProgress` from `getTrackingItemsProgress`. |
| `selectedDate` | `Date` | The day to show. |
| `isLoading` | `boolean` | If `true`, shows the loading state. |
| `hideTarget` | `boolean` | If `true`, hides each item's target. |

### StreakTracking props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `streaks` _(required)_ | `Streak[]` | `streakCards` from `getTrackingItemsProgress`. |
| `onReviewProgress` | `(streakType: string) => void` | Called when the user taps "Review your progress", which medication streaks show. |

## MealDetail

### Description

Shows one meal's details and lets the user edit its ingredients and portions, save it or delete it.

### Basic usage

```tsx
<MealDetail mealId={mealId} onDonePress={() => navigation.goBack()} />
```

### Props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `mealId` | `string` | The meal to load. Not needed when you pass `initialMeal`. |
| `initialMeal` | `Meal` | A meal you already have; it is shown without fetching. |
| `onDonePress` _(required)_ | `() => void` | Called when the user is done with the meal. |
| `onSave` | `(meal: Meal) => void` | Called with the saved meal after the user saves an edit. |
| `onDelete` | `(mealId: string) => void` | Called when the user deletes the meal. |
| `onNavigateToChat` | `(params: ChatSearchParams) => void` | Called when an edited meal should be re-analyzed in the meal-logging chat. Render `Chat` with these params (see **Chat & coach UI**). |
| `isReadOnly` | `boolean` | If `true`, the user cannot edit or delete the meal. Defaults to `false`. |
| `chatSessionId` | `string` | The chat session the meal belongs to. |
| `defaultExpanded` | `boolean` | If `true`, the meal details start expanded. Defaults to `false`. |
| `allowBarcodeScanning` | `boolean` | Lets the user scan a barcode when adding or editing an ingredient. Defaults to `true`. |
| `hideEditMenu` | `boolean` | Hides the edit menu (the three dots), e.g. for an unsaved draft. |

## LogPicker

### Description

A button that opens a sheet where the user chooses what to log: a meal, or weight and body metrics.

### Basic usage

```tsx
import { useCallback, useEffect, useState } from 'react';
import { BodyComposition, LogPicker, useMiriApp } from '@miri-ai/miri-react-native';

function LogButton() {
  const { getWeightProgress, getBodyComposition } = useMiriApp();
  const [weightProgress, setWeightProgress] = useState<
    Awaited<ReturnType<typeof getWeightProgress>>
  >({ weightGoal: null, weightTracking: null });
  const [bodyComposition, setBodyComposition] = useState<BodyComposition | null>(null);

  const refresh = useCallback(async () => {
    setWeightProgress(await getWeightProgress());
    setBodyComposition(await getBodyComposition());
  }, [getWeightProgress, getBodyComposition]);

  useEffect(() => {
    refresh();
  }, [refresh]);

  return (
    <LogPicker
      weightProgress={weightProgress}
      bodyComposition={bodyComposition}
      onUpdateTracking={refresh}
    />
  );
}
```

### Props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `weightProgress` _(required)_ | `{ weightGoal: PlanArtifactWeight \| null; weightTracking: WeightTracking \| null }` | The result of `getWeightProgress`. |
| `bodyComposition` | `BodyComposition \| null` | The result of `getBodyComposition`. |
| `onUpdateTracking` | `() => Promise<void>` | Called after the user saves a weight or body metrics entry, so you can refresh your data. |
| `onUpdateWeightGoal` | `() => void` | Called when the user taps the weight goal to edit it. |
| `onLogMeal` | `(meal: Meal) => void` | Called when a meal is logged. |
| `onMealDeleted` | `(mealId: string) => void` | Called after the user deletes a logged meal from the sheet. |
| `onPhotoCapture` | `(images: CameraImage[]) => void` | Called when the user takes photos with the Photo option. |
| `onChatTap` | `() => void` | Called when the user picks the Chat option. |

## FIOPicker

### Description

A button that opens a sheet of "Figure it out" chats, such as restaurants, recipes and cravings.

### Basic usage

```tsx
import { ChatSearchParams, FIOPicker } from '@miri-ai/miri-react-native';

function FigureItOutButton() {
  const handleNavigateToChat = (params: ChatSearchParams) => {
    // Open your chat screen and render <Chat {...params} />
  };

  return <FIOPicker onNavigateToChat={handleNavigateToChat} />;
}
```

### Props

| Prop | Type | Description |
| :--- | :--- | :--- |
| `onNavigateToChat` _(required)_ | `(params: ChatSearchParams) => void` | Called with the chosen module's chat params. |
