# media-viewer — Agent Guide

> **Parent guide:** Always read [`../AGENTS.md`](../AGENTS.md) before working in this package — it
> contains workspace-wide rules covering platform guidelines, changeset workflow, accessibility
> standards, and the knowledge index protocol that applies to every Media package.

## Package identity

| Field    | Value                                                                                                                        |
| -------- | ---------------------------------------------------------------------------------------------------------------------------- |
| NPM name | `@atlaskit/media-viewer`                                                                                                     |
| Path     | `media-viewer/`                                                                                                              |
| Owner    | Media Exif                                                                                                                   |
| Purpose  | Full-screen media viewer — powerful and extendable viewer for images, video, audio, documents, SVG, archives, and code files |

## Knowledge index

> Read, search, edit, investigate, and validate the repository knowledge index at
> `.agents/knowledge-index/`. ALWAYS use this skill before making ANY repo change, or when exploring
> the codebase. The knowledge index is the canonical starting point for every coding task in this
> repo, trigger this skill aggressively whenever the user mentions architecture, conventions,
> packages, modules, services, build, coding, writing code, APIs, feature flags, testing, or
> anything similar. Also trigger when "is the knowledge index up to date?" / "update the knowledge
> index" / "what does the knowledge index say about Y?" — even if they don't say the words
> "knowledge index". Also use it (via the `validate` subcommand) to detect drift between the
> codebase and the docs after any code change. Use the `generate` subcommand to bootstrap the index
> in a new repository.

**Entry point:** `.agents/knowledge-index/INDEX.md` **Unit doc:**
`.agents/knowledge-index/domains/media/units/atlaskit-media-viewer.md` **CLI:**
`python3 .agents/skills/knowledge-index/scripts/kg.py {find,read,edit,investigate,validate,init,explore,generate} …`
**Refresh protocol:** never silently bump `Last verified` — always re-read the listed `Sources`
first, edit, then `kg.py edit <path> --message "<reason>"`.

## Key source files

- `src/index.ts` — public API entry point
- `src/media-viewer.tsx` — root `MediaViewer` component
- `src/viewers/` — per-format viewer implementations (image, video, doc, SVG, code, archive, custom)
- `src/components/` — shared viewer UI components
- `src/analytics/` — analytics event definitions and helpers
- `src/domain/` — domain types and state
- `src/utils/` — shared utilities
- `src/header.tsx` — viewer header bar
- `src/navigation.tsx` — multi-file navigation
- `src/content.tsx` — content area

## Public API (key exports)

`MediaViewer` (default), `MediaViewerExtensions`, `MediaViewerExtensionsActions`,
`MediaViewerProps`, `MediaMessage`, `ViewerOptionsProps`, `CustomRendererConfig`,
`CustomRendererStateProps`, `CustomRendererProps`, `ArchiveFileItem`

## Development notes

- React Compiler is enabled for this package
- Peer dependencies: `@emotion/react`, `react`, `react-dom`, `react-intl`
- Export subpaths: `.`, `./classnames`, `./media-viewer-loader`, `./preload`, `./types`,
  `./viewer-options`
- `./preload` exports `preloadMediaViewer`, called from hover/focus handlers in
  `@atlaskit/media-card` and `@atlaskit/editor-plugin-media` to warm the viewer chunks and the full
  resolution image ahead of the click. The in-flight request and its `AbortController` are held in
  `src/warmedImages.ts` (at most 2 entries, 30 s each). `ImageViewer` takes the entry and awaits the
  same request instead of issuing its own, so the image downloads once even when the click lands
  mid-download. Once taken, the viewer owns the download and closing it aborts the request. Entries
  nobody takes are aborted on eviction or expiry. Don't rely on the browser HTTP cache for this: the
  warm-up is usually still downloading when the user clicks. Behind the
  `platform_media_viewer_preload_on_hover` experiment. `preloadMediaViewer` is the only exposure
  point and fires in both arms on hover or focus. `ImageViewer` has no check of its own: warm-ups
  only exist in treatment. The store is on `globalThis` under
  `Symbol.for('@atlaskit/media-viewer/warmed-images/v1')`, because a bundler can copy the module
  into the card's bundle and the image viewer chunk separately. Bump the key if `Entry` changes.
  Entries are keyed by file, collection and the `wmv` from `MediaClient.getImageWatermarkVersion`,
  so a warm-up made before a watermark config change is never taken by a viewer opened after it. The
  warm-up resolves the version before it touches the store. `ImageViewer` resolves it only when the
  store holds a warm-up for the file, so control makes no extra auth call. The warm-up makes one
  attempt (`retryOptions.maxAttempts: 1`) and reports `mediaFile` `preloadFailed` and a sampled
  `preloadEnded` through the caller's `onAnalyticsEvent`. Aborts are not failures.
- Supports custom viewer via `CustomRendererConfig`
- Inset presentation: when `extensions.useInsetViewer` is set and the
  `cc_comments_inset_media_viewer` gate is on, `media-viewer.tsx` computes `isInsetViewer` once and
  provides it through `InsetViewerProvider` (`src/insetViewerContext.tsx`). Components read it with
  `useIsInsetViewer()` or, for class components, `withInsetViewer`; don't re-derive it from
  `extensions`.
  - Media header (`src/header.tsx`, rendered by `List` as in the overlay viewer): one-line file name
    and size, the consumer's extra actions, and, while the sidebar is closed, download, the sidebar
    toggle and close.
  - Sidebar column (`SidebarColumn` in `src/styleWrappers.tsx`): holds `extensions.sidebar` next to
    the media, with its own header row (`src/inset-sidebar-header.tsx`: title, download, sidebar
    toggle and close) above the body, in one width animation and one enter/exit motion; when closed
    it stays mounted but is `aria-hidden`.
  - Media footer (`MediaFooterBar`): registers its element through the footer context; zoom controls
    render into it with a portal (`withInsetViewerFooter`).
  - Video and audio (`src/viewers/inset-video-frame.tsx`, `src/viewers/inset-audio-frame.tsx`): the
    shared inset layouts used by the video and audio viewers and by video and audio inside a zip
    file. The video frame fits the video into the media stage by aspect ratio (`FittedVideoFrame`,
    refit by a `ResizeObserver` as the sidebar changes the available width). Both wrap the player in
    media-ui's `InsetViewerProvider` so it portals its controls into the media footer, and register
    them with `useRegisterMediaFooterPlayerControls` so the footer makes room.
- Large test suite: 50+ test files under `src/__tests__/`
- All new behaviour changes must be behind a feature gate (`fg()` from
  `@atlaskit/platform-feature-flags`)
