# useObjectPreview (https://s3.dimah.dev/docs/react/hooks/object-preview)

`useObjectPreview` tells you how to show one stored file without saving it:
as an image, a video, audio, a PDF, or text, or not at all. It signs the
inline URL and reads the start of a text file. For a ready-made view, use
[`ObjectPreview`](https://s3.dimah.dev/docs/ui/components/object-preview).

```tsx title="components/file-preview.tsx"
"use client";

import { useObjectPreview } from "@dimah-s3/react";

export function FilePreview({ objectKey }: { objectKey: string }) {
  const preview = useObjectPreview({ route: "files", objectKey });

  if (preview.status === "loading") return <p>Loading…</p>;
  if (preview.status === "error") return <p>Could not load the preview.</p>;
  if (!preview.url) return <p>No preview</p>;

  switch (preview.kind) {
    case "image":
      return <img src={preview.url} alt="" />;
    case "video":
      return <video src={preview.url} controls preload="metadata" />;
    case "audio":
      return <audio src={preview.url} controls preload="metadata" />;
    case "pdf":
      return <iframe src={preview.url} title="Preview" />;
    case "text":
      return <pre>{preview.text?.value}</pre>;
    default:
      return <p>No preview</p>;
  }
}
```

## How it decides [#how-it-decides]

1. A type you pass in `contentType`, or one the file name gives (`fileName`, else the key's name), that has no preview, or whose kind is left out of `kinds`, ends here: `status` is `ready` with `kind: null`, and nothing is signed.
2. Otherwise it signs an inline URL with [`useObjectUrl`](https://s3.dimah.dev/docs/react/hooks/object-url).
3. The server keeps `inline` only for a type on the route's [`inlineTypes`](https://s3.dimah.dev/docs/server/routes#download), and reports the stored type. An attachment has no preview. The type picks the kind.
4. For text, it reads at most `maxTextBytes` (256 KiB) with one ranged `GET`. `text.truncated` says the file goes on.

| Kind    | Types             |
| ------- | ----------------- |
| `image` | `image/*`         |
| `video` | `video/*`         |
| `audio` | `audio/*`         |
| `pdf`   | `application/pdf` |
| `text`  | `text/*`, JSON    |

`resolvePreviewKind({ contentType, fileName })` from `@dimah-s3/react/engine`
maps a type to its kind, for a Preview button you show only when it applies.

<Callout type="warn">
Reading text uses `fetch`. In presign mode, the bucket's CORS rules must
allow `GET` from your app, as the [provider guides](https://s3.dimah.dev/docs/providers) set up.
</Callout>

## Refresh [#refresh]

The URL refreshes before it expires, as in `useObjectUrl`, and a text file is
not read again. `refresh()` tries again: it reads a text file again, and signs
any other file again. Call it when an `<img>` or `<video>` fails, such as
after the URL expired while the tab slept.

## API Reference [#api-reference]

### Options [#options]

| Prop           | Type                  | Default    | Description                                               |
| -------------- | --------------------- | ---------- | --------------------------------------------------------- |
| `route`        | `S3RouteName`         | required   | Named server route                                        |
| `objectKey`    | `string \| null`      | —          | Stored object key. `null`, `undefined`, or `""` is `idle` |
| `contentType`  | `string \| null`      | —          | The file's type. A type without a preview signs nothing   |
| `fileName`     | `string \| null`      | Key's name | Gives the type when nothing else does. Not sent           |
| `kinds`        | `ObjectPreviewKind[]` | Every kind | Kinds to preview. Others are `ready` with `kind: null`    |
| `maxTextBytes` | `number`              | `262144`   | Most bytes read from a text file                          |
| `api`          | `S3Api`               | —          | Replaces the `S3Provider` API                             |

### Return [#return]

| Property      | Type                                        | Description                                                       |
| ------------- | ------------------------------------------- | ----------------------------------------------------------------- |
| `status`      | `"idle" \| "loading" \| "ready" \| "error"` | Where the preview is                                              |
| `kind`        | `ObjectPreviewKind \| null`                 | How to show the file once `ready`. `null` for no preview          |
| `url`         | `string \| null`                            | The inline URL. `null` when nothing was signed                    |
| `contentType` | `string \| null`                            | The file's type, without parameters                               |
| `disposition` | `"inline" \| "attachment" \| null`          | The disposition `url` carries                                     |
| `expiresAt`   | `number \| null`                            | When `url` stops working, in ms since the epoch. `null` for proxy |
| `text`        | `{ value, truncated } \| null`              | The start of a text file, once read                               |
| `error`       | `APIError \| null`                          | Why `status` is `error`                                           |
| `objectKey`   | `string \| null`                            | The key this state belongs to                                     |
| `refresh`     | `() => void`                                | Reads text again, or signs again                                  |