import { useEffect, useRef, useState, type RefObject } from "react"; import { createPortal } from "react-dom"; import { Settings2 } from "lucide-react"; import { Button } from "antd"; import { VideoSettingsPanel, videoResolutionLabel, videoSecondsLabel, videoSizeLabel } from "@/components/video-settings-panel"; import { canvasThemes } from "@/lib/canvas-theme"; import { modelCapabilityConfigFor, resolveVideoRatioValue, resolveVideoResolutionValue } from "@/lib/model-capabilities"; import { useThemeStore } from "@/stores/use-theme-store"; import type { AiConfig } from "@/stores/use-config-store"; type CanvasVideoSettingsPopoverProps = { config: AiConfig; onConfigChange: (key: keyof AiConfig, value: string) => void; buttonClassName?: string; placement?: "topLeft" | "top" | "topRight" | "bottomLeft" | "bottom" | "bottomRight"; }; export function CanvasVideoSettingsPopover({ config, onConfigChange, buttonClassName, placement = "topLeft" }: CanvasVideoSettingsPopoverProps) { const theme = canvasThemes[useThemeStore((state) => state.theme)]; const buttonRef = useRef(null); const panelRef = useRef(null); const [open, setOpen] = useState(false); const [buttonRect, setButtonRect] = useState(null); const videoProfile = modelCapabilityConfigFor(config, config.model).video; const resolutionSupported = Boolean(videoProfile?.resolutions.length); const sizeSupported = Boolean(videoProfile?.ratios.length); const resolution = videoProfile ? resolveVideoResolutionValue(videoProfile, config.vquality) : ""; const size = videoProfile ? resolveVideoRatioValue(videoProfile, config.size) : ""; const summary = [ ...(resolutionSupported ? [videoResolutionLabel(resolution)] : []), ...(sizeSupported ? [videoSizeLabel(size)] : []), videoSecondsLabel(config.videoSeconds), ].join(" · "); useEffect(() => { if (!open) return; const syncPosition = () => setButtonRect(buttonRef.current?.getBoundingClientRect() || null); const closeOnOutsidePointer = (event: PointerEvent) => { const target = event.target; if (!(target instanceof Node)) return; if (buttonRef.current?.contains(target) || panelRef.current?.contains(target)) return; setOpen(false); }; syncPosition(); window.addEventListener("resize", syncPosition); window.addEventListener("scroll", syncPosition, true); window.addEventListener("pointerdown", closeOnOutsidePointer, true); return () => { window.removeEventListener("resize", syncPosition); window.removeEventListener("scroll", syncPosition, true); window.removeEventListener("pointerdown", closeOnOutsidePointer, true); }; }, [open]); const panel = open && buttonRect ? : null; return ( <> {panel} ); } function VideoSettingsPortal({ buttonRect, panelRef, placement, theme, config, onConfigChange, }: { buttonRect: DOMRect; panelRef: RefObject; placement: CanvasVideoSettingsPopoverProps["placement"]; theme: (typeof canvasThemes)[keyof typeof canvasThemes]; config: AiConfig; onConfigChange: (key: keyof AiConfig, value: string) => void; }) { const gap = 8; const margin = 12; const width = Math.min(356, window.innerWidth - margin * 2); const alignRight = placement?.endsWith("Right"); const alignCenter = placement === "top" || placement === "bottom"; const left = alignCenter ? buttonRect.left + buttonRect.width / 2 - width / 2 : alignRight ? buttonRect.right - width : buttonRect.left; const topPlacement = placement?.startsWith("top"); const estimatedHeight = 370; const topSpace = buttonRect.top - gap - margin; const bottomSpace = window.innerHeight - buttonRect.bottom - gap - margin; const placeAbove = topPlacement ? topSpace >= estimatedHeight || topSpace >= bottomSpace : bottomSpace < estimatedHeight && topSpace > bottomSpace; const style = { position: "fixed", zIndex: "var(--z-dialog-popover)", width, left: Math.max(margin, Math.min(window.innerWidth - width - margin, left)), ...(placeAbove ? { bottom: window.innerHeight - buttonRect.top + gap, maxHeight: Math.max(260, topSpace) } : { top: buttonRect.bottom + gap, maxHeight: Math.max(260, bottomSpace) }), background: theme.canvas.background, border: `1px solid ${theme.toolbar.border}`, borderRadius: 10, boxShadow: `0 24px 72px ${theme.spatial.shadow}`, padding: 12, overflowY: "auto", color: theme.node.text, } as const; return createPortal(
event.stopPropagation()} onMouseDown={(event) => event.stopPropagation()} onClick={(event) => event.stopPropagation()} > onConfigChange(key, value)} theme={theme} className="space-y-3" />
, document.body, ); }