Skip to content

The platform for continuously improving AI agents.

1.4 KB
Raw Blame
import { useEffect, useRef, useState } from "react";

import { ResizeHandle } from "@/components/ui/resize-handle";

export const WORKSHOP_PANEL_WIDTH = 416;

export function WorkshopPanelResize({
  panelId,
  width,
  onWidthChange,
  onResizingChange,
}: {
  panelId: string;
  width: number;
  onWidthChange: (width: number) => void;
  onResizingChange: (resizing: boolean) => void;
}) {
  const handle = useRef<HTMLDivElement>(null);
  const [maximum, setMaximum] = useState(800);
  const minimum = Math.min(320, maximum);
  const clamp = (value: number) => Math.min(maximum, Math.max(minimum, value));

  useEffect(() => {
    const container = handle.current?.parentElement?.parentElement;
    if (!container) return;
    const measure = () => {
      if (container.clientWidth) setMaximum(Math.max(0, Math.min(800, container.clientWidth - 24)));
    };
    measure();
    const observer = new ResizeObserver(measure);
    observer.observe(container);
    return () => observer.disconnect();
  }, []);

  useEffect(() => {
    if (width > maximum) onWidthChange(maximum);
  }, [maximum, onWidthChange, width]);

  return (
    <ResizeHandle
      className="resize-handle-edge"
      controls={panelId}
      label="Chat panel width"
      max={maximum}
      min={minimum}
      onReset={() => onWidthChange(clamp(WORKSHOP_PANEL_WIDTH))}
      onResize={onWidthChange}
      onResizingChange={onResizingChange}
      ref={handle}
      value={clamp(width)}
    />
  );
}