# Notification list

> The caller's latest notifications with an unread marker, loaded again on every change.

- Block: [notifications](https://bettersupabase.com/blocks/notifications.md)
- Kind: component
- Hook: `useNotifications` from `@/hooks/use-notifications`
- Page: https://bettersupabase.com/recipes/notification-list

## Install the block

```bash
npx better-supabase add notifications
```

## Web (Next.js)

Add the shadcn components:

```bash
npx shadcn add badge empty skeleton
```

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-notifications @better-supabase/notifications-actions-next
```

From `apps/examples/nextjs/src/features/notifications/components/notification-list.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/nextjs/src/features/notifications/components/notification-list.tsx):

```tsx
"use client";

import { BellIcon } from "lucide-react";
import { useExtracted, useFormatter } from "next-intl";

import { Badge } from "@/components/ui/badge";
import {
  Empty,
  EmptyDescription,
  EmptyHeader,
  EmptyMedia,
  EmptyTitle,
} from "@/components/ui/empty";
import { Skeleton } from "@/components/ui/skeleton";
import { useNotifications } from "@/hooks/use-notifications";
import { useAuth } from "@/lib/hooks";

import type { NotificationRow } from "../notification-types";

import { loadNotifications } from "../notifications-actions";

async function getRows(): Promise<readonly NotificationRow[]> {
  const result = await loadNotifications({});

  return result.ok ? result.data : [];
}

/**
 * The caller's latest notifications, loaded again after each broadcast on their
 * private topic `notifications:<user id>`: a new one, or one marked read.
 */
export function NotificationList() {
  const t = useExtracted("notifications");
  const format = useFormatter();
  const auth = useAuth();
  const userId = auth.user?.id;

  const { items } = useNotifications({
    topic: userId ? `notifications:${userId}` : null,
    load: getRows,
  });

  if (items === undefined) {
    return <NotificationListSkeleton />;
  }

  if (items.length === 0) {
    return (
      <Empty className="border">
        <EmptyHeader>
          <EmptyMedia variant="icon">
            <BellIcon />
          </EmptyMedia>
          <EmptyTitle>{t("Nothing here yet")}</EmptyTitle>
          <EmptyDescription>
            {t("Send yourself a notification to see it arrive.")}
          </EmptyDescription>
        </EmptyHeader>
      </Empty>
    );
  }

  return (
    <ul className="divide-y rounded-xl border">
      {items.map((notification) => (
        <li
          key={notification.id}
          className="flex items-center gap-3 px-4 py-3 text-sm"
        >
          <span className="flex-1">{notification.title}</span>
          {notification.readAt ? null : <Badge>{t("New")}</Badge>}
          <span className="text-xs text-muted-foreground tabular-nums">
            {format.dateTime(new Date(notification.createdAt), {
              dateStyle: "medium",
              timeStyle: "short",
            })}
          </span>
        </li>
      ))}
    </ul>
  );
}

function NotificationListSkeleton() {
  return (
    <div className="space-y-2" aria-busy="true">
      {Array.from({ length: 4 }, (_, index) => (
        <Skeleton key={index} className="h-11 w-full" />
      ))}
    </div>
  );
}
```

## Native (Expo)

The snippet uses `ActivityIndicator`, `FlatList`, `Text`, `View` from `react-native`, so it needs no styling library.

Add the glue from the better-supabase registry:

```bash
npx shadcn add @better-supabase/use-notifications @better-supabase/blocks
```

From `apps/examples/expo-powersync/src/components/notification-list.tsx` (https://github.com/ScaleDockHQ/better-supabase/blob/main/apps/examples/expo-powersync/src/components/notification-list.tsx):

```tsx
import { tenantOf, useAuth, useSupabase } from "better-supabase/react";
import { ActivityIndicator, FlatList, Text, View } from "react-native";

import { useNotifications } from "../hooks/use-notifications";
import { blocks } from "../lib/blocks";

/**
 * The caller's latest notifications, loaded again after each broadcast on their
 * private topic `notifications:<user id>`: a new one, or one marked read.
 */
export function NotificationList() {
  const auth = useAuth();
  const supabase = useSupabase();
  const tenant = tenantOf(auth);
  const userId = auth.user?.id;

  const { items } = useNotifications({
    topic: userId ? `notifications:${userId}` : null,
    load: async () => {
      if (!tenant) {
        return [];
      }

      const result = await blocks(supabase).notifications.list({
        tenant,
        limit: 20,
      });

      return result.ok ? result.data : [];
    },
  });

  if (items === undefined) {
    return <ActivityIndicator />;
  }

  return (
    <FlatList
      data={items}
      keyExtractor={(item) => item.id}
      ListEmptyComponent={<Text>Nothing here yet</Text>}
      renderItem={({ item }) => (
        <View style={{ flexDirection: "row", gap: 8, paddingVertical: 12 }}>
          <Text style={{ flex: 1 }}>{item.data.title}</Text>
          {item.readAt ? null : <Text style={{ fontWeight: "600" }}>New</Text>}
          <Text style={{ color: "#737373" }}>
            {item.createdAt.toLocaleString(undefined, {
              dateStyle: "medium",
              timeStyle: "short",
            })}
          </Text>
        </View>
      )}
    />
  );
}
```

## Prompt

Paste one of these into a coding agent to build the recipe in your app.

### Web

```text
Build "Notification list" in a React web app that uses shadcn/ui components. The caller's latest notifications with an unread marker, loaded again on every change.

It uses the better-supabase `notifications` block (Notifications). It calls the hook `useNotifications` from `@/hooks/use-notifications`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add notifications` and apply the migration.
2. Add the shadcn components the recipe uses: `npx shadcn add badge empty skeleton`.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-notifications @better-supabase/notifications-actions-next`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useNotifications` from `@/hooks/use-notifications`, starting from the recipe's snippet (`apps/examples/nextjs/src/features/notifications/components/notification-list.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/notification-list.md
- Block docs: https://bettersupabase.com/docs/blocks/notifications
```

### Native

```text
Build "Notification list" in a React Native app built with Expo. The caller's latest notifications with an unread marker, loaded again on every change.

It uses the better-supabase `notifications` block (Notifications). It calls the hook `useNotifications` from `@/hooks/use-notifications`.

Steps:

1. If the app doesn't have the block yet, add the SQL with `npx better-supabase add notifications` and apply the migration.
2. The recipe uses `ActivityIndicator`, `FlatList`, `Text`, `View` from `react-native`, so it needs no extra UI library. Use the app's own components if it has them.
3. Add the glue from the better-supabase registry: `npx shadcn add @better-supabase/use-notifications @better-supabase/blocks`. The items are copied into the app, so edit them as needed.
4. Write the component. Call `useNotifications` from `@/hooks/use-notifications`, starting from the recipe's snippet (`apps/examples/expo-powersync/src/components/notification-list.tsx`). Adapt it to the app's existing components, styling and data loading instead of copying it as is.
5. Check the result against the recipe and the block's docs: it typechecks, it calls the same hook, and it handles the loading and empty states the snippet handles.

Read the full code first:

- Recipe: https://bettersupabase.com/recipes/notification-list.md
- Block docs: https://bettersupabase.com/docs/blocks/notifications
```
