Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

4.4 KB
Raw Blame
import { createContext, useRef, useContext, RefObject, ReactNode } from 'react';
import { toCanvas } from 'html-to-image';
import { completeProgressiveRowMounts } from '@librechat/chat/react';
import { ThemeContext, isDark, readThemeColor } from '@librechat/client';

type ScreenshotContextType = {
  ref?: RefObject<HTMLDivElement>;
};

/** Canvas area ceiling (~16.7 MP, 4096²) — WebKit rejects larger canvases and PNG encode cost grows linearly past it */
const MAX_CAPTURE_AREA = 16_777_216;
/** Chromium/Firefox cap a canvas edge at 32767px; beyond it drawing silently produces a blank canvas */
const MAX_CANVAS_EDGE = 32_767;
/** Below half-resolution the capture is illegible, so abort rather than degrade further */
const MIN_PIXEL_RATIO = 0.5;
/** html-to-image clones every node and copies ~340 computed styles each; cap the synchronous clone work */
const MAX_CAPTURE_ELEMENTS = 50_000;

export class ScreenshotLimitError extends Error {
  constructor(message: string) {
    super(message);
    this.name = 'ScreenshotLimitError';
  }
}

export class ScreenshotTargetError extends Error {
  constructor() {
    super('Screenshot target changed or is unavailable.');
    this.name = 'ScreenshotTargetError';
  }
}

const ScreenshotContext = createContext<ScreenshotContextType>({});

export const useScreenshot = () => {
  const { ref } = useContext(ScreenshotContext);
  const { theme } = useContext(ThemeContext);

  const takeScreenShot = async (node?: HTMLElement): Promise<Blob> => {
    if (!node) {
      throw new Error('You should provide correct html node.');
    }

    const width = node.scrollWidth;
    const height = node.scrollHeight;
    if (!width || !height) {
      throw new Error('Cannot capture an empty node.');
    }

    const elementCount = node.querySelectorAll('*').length;
    if (elementCount > MAX_CAPTURE_ELEMENTS) {
      throw new ScreenshotLimitError(
        `Screenshot capture aborted: ${elementCount} elements exceed the ${MAX_CAPTURE_ELEMENTS} limit`,
      );
    }

    const pixelRatio = Math.min(
      window.devicePixelRatio || 1,
      Math.sqrt(MAX_CAPTURE_AREA / (width * height)),
      MAX_CANVAS_EDGE / Math.max(width, height),
    );
    if (pixelRatio < MIN_PIXEL_RATIO) {
      throw new ScreenshotLimitError(
        `Screenshot capture aborted: ${width}x${height} CSS px cannot fit within ${MAX_CAPTURE_AREA} device px`,
      );
    }

    /** Read the canvas the app is actually painting rather than a fixed pair, so
     *  an export matches the selected appearance and the state colours keep the
     *  contrast they were calibrated against. The token is a channel triplet,
     *  not a colour, so it has to be wrapped before html-to-image sees it. */
    const fallbackBackground = isDark(theme) ? '#171717' : 'white';
    const backgroundColor = readThemeColor('--surface-primary') ?? fallbackBackground;
    const canvas = await toCanvas(node, {
      backgroundColor,
      pixelRatio,
      imagePlaceholder:
        'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+ip1sAAAAASUVORK5CYII=',
    });

    const blob = await new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, 'image/png'));
    if (!blob) {
      throw new Error('Failed to encode screenshot canvas.');
    }
    return blob;
  };

  const captureScreenshot = async (canCapture?: (node: HTMLElement) => boolean): Promise<Blob> => {
    if (ref instanceof Function) {
      throw new Error('Ref callback is not supported.');
    }
    const node = ref?.current;
    if (!node) {
      throw new ScreenshotTargetError();
    }
    const conversationId = node.dataset.conversationId;
    const assertTarget = () => {
      if (
        !conversationId ||
        !node.isConnected ||
        ref?.current !== node ||
        node.dataset.conversationId !== conversationId ||
        canCapture?.(node) === false
      ) {
        throw new ScreenshotTargetError();
      }
    };
    assertTarget();
    /** Pin the transcript before mounting or cloning can yield to navigation. */
    await completeProgressiveRowMounts();
    assertTarget();
    const image = await takeScreenShot(node);
    assertTarget();
    return image;
  };

  return { screenshotTargetRef: ref, captureScreenshot };
};

export const ScreenshotProvider = ({ children }: { children: ReactNode }) => {
  const ref = useRef<HTMLDivElement>(null);

  return <ScreenshotContext.Provider value={{ ref }}>{children}</ScreenshotContext.Provider>;
};