Skip to content

The agent that grows with you

5.3 KB
Raw Blame
import { useStore } from '@nanostores/react'
import { type CSSProperties, useEffect, useMemo, useRef, useState } from 'react'

import { useComposerScope } from '@/app/chat/composer/scope'
import {
  gatewayImageProxyDataUrl,
  getMediaImageDimensions,
  isInlineMediaSrc,
  isKnownBrokenMediaImage,
  type MediaImageDimensions,
  mediaImageKey,
  rememberMediaImageDimensions,
  rememberMediaImageFailure,
  resolveMediaDisplaySrc
} from '@/lib/media'
import { $connection } from '@/store/session'

/** Keep a frame for one source/owner for its mounted lifetime. Hints are not
 * intrinsic dimensions: a cold image is contained in that frame, not allowed
 * to resize it at decode. The next mount can use the measured dimensions.
 *
 * `preservePendingFrame` is for a caller whose content is all absolutely
 * positioned (a generated image): it always gets a frame, since without one a
 * successful retry renders into a zero-height box, and it keeps the frame
 * shown while its tool was pending. */
export function useMediaImage(
  path: string,
  fallbackRatio: number,
  intrinsic?: MediaImageDimensions,
  { preservePendingFrame = false }: { preservePendingFrame?: boolean } = {}
) {
  const connection = useStore($connection)
  const scope = useComposerScope()
  const connectionId = scope.connectionId || connection?.connectionId
  const profile = scope.profile ?? connection?.profile

  const owner = useMemo(
    () => (scope.connectionId || scope.profile ? { connectionId, profile } : undefined),
    [scope.connectionId, scope.profile, connectionId, profile]
  )

  const key = mediaImageKey(path, connection, owner)
  const ownerKey = mediaImageKey('', connection, owner)

  const initialState = () => {
    const dimensions = intrinsic ?? getMediaImageDimensions(key)
    const ratio = dimensions ? dimensions.width / dimensions.height : fallbackRatio

    return {
      key,
      ownerKey,
      path,
      fallbackRatio,
      // A source that just failed gets no frame: it would only collapse again.
      frameStyle:
        !dimensions && !preservePendingFrame && isKnownBrokenMediaImage(key)
          ? undefined
          : ({
              aspectRatio: ratio,
              width: `min(calc(var(--image-preview-height) * ${ratio}), var(--image-preview-max-width), 100%${dimensions ? `, ${dimensions.width}px` : ''})`
            } satisfies CSSProperties),
      src: path && isInlineMediaSrc(path) ? path : '',
      loaded: false,
      failed: false
    }
  }

  const [state, setState] = useState(initialState)

  // Source changes must not paint the previous image/geometry even for one
  // commit. React retries this component before committing its children.
  // With no source yet, nothing is painted, so a hint arriving late reshapes.
  if (state.key !== key || (!path && state.fallbackRatio !== fallbackRatio)) {
    const next = initialState()
    // A generated result fills the frame already visible while its tool was
    // pending. Media state still resets; geometry is never inherited across
    // owners or between two non-empty sources.
    const inheritsPendingFrame = preservePendingFrame && state.ownerKey === ownerKey && !state.path && Boolean(path)

    setState(inheritsPendingFrame ? { ...next, frameStyle: state.frameStyle } : next)
  }

  // One proxy retry per inline https source (#74564): when the direct load
  // fails (client blocked from the image CDN), ask the gateway — which
  // generated the image and can reach the CDN — to fetch the bytes through
  // its authenticated /api/media/proxy. Latched so a failing proxy data URL
  // (itself a data: src, which cannot fail with onError again) and a remount
  // never loop: the failed direct load is remembered via the broken-key
  // mechanism instead.
  const proxyRetriedRef = useRef(false)

  const retryThroughGatewayProxy = () => {
    if (proxyRetriedRef.current || !path || !isInlineMediaSrc(path) || !/^https?:/i.test(path)) {
      return
    }

    proxyRetriedRef.current = true

    void gatewayImageProxyDataUrl(path, owner).then(dataUrl => {
      if (dataUrl) {
        setState(current =>
          current.key === key && current.failed && !current.loaded
            ? { ...current, src: dataUrl, failed: false }
            : current
        )
      }
    })
  }

  useEffect(() => {
    let cancelled = false

    if (path && !isInlineMediaSrc(path)) {
      void resolveMediaDisplaySrc(path, owner).then(
        src => {
          if (!cancelled) {
            if (!src) {
              rememberMediaImageFailure(key)
            }

            setState(current => (current.key === key ? { ...current, src, failed: !src } : current))
          }
        },
        () => {
          if (!cancelled) {
            rememberMediaImageFailure(key)
            setState(current => (current.key === key ? { ...current, failed: true } : current))
          }
        }
      )
    }

    return () => {
      cancelled = true
    }
  }, [key, path, owner])

  const onLoad = (image: HTMLImageElement) => {
    rememberMediaImageDimensions(key, image.naturalWidth, image.naturalHeight)
    setState(current => ({ ...current, loaded: true }))
  }

  const onError = () => {
    rememberMediaImageFailure(key)
    setState(current => ({ ...current, failed: true, loaded: false }))
    retryThroughGatewayProxy()
  }

  return {
    ...state,
    onLoad,
    onError
  }
}