import type { PluginTheme } from "@getpaseo/plugin"; import { Icon, Modal } from "@getpaseo/plugin/client/ui"; import { type RefObject, useEffect, useRef, useState } from "react"; import { Pressable, Text, View } from "react-native"; export interface ContextMenuItem { readonly id: string; readonly label: string; readonly icon: string; readonly tone?: "default" | "danger"; readonly disabled?: boolean; readonly separatorBefore?: boolean; onSelect(): void; } export interface ContextMenuRequest { readonly clientX: number; readonly clientY: number; readonly title?: string; readonly items: readonly ContextMenuItem[]; } interface DomRectLike { left: number; top: number; width: number; height: number; } interface DomEventLike { preventDefault(): void; stopPropagation(): void; clientX: number; clientY: number; target?: unknown; key?: string; } interface DomNodeLike { addEventListener?( type: string, listener: (event: DomEventLike) => void, capture?: boolean, ): void; removeEventListener?( type: string, listener: (event: DomEventLike) => void, capture?: boolean, ): void; getBoundingClientRect?(): DomRectLike; contains?(target: unknown): boolean; } function asDomNode(value: unknown): DomNodeLike | null { if (!value || typeof value !== "object") return null; return value as DomNodeLike; } function getDocumentNode(): DomNodeLike | null { const globalObj = globalThis as { document?: unknown }; return asDomNode(globalObj.document); } export function useContextMenuTarget( onOpen: (clientX: number, clientY: number) => void, ) { const [target, setTarget] = useState(null); const onOpenRef = useRef(onOpen); onOpenRef.current = onOpen; useEffect(() => { const node = asDomNode(target); if (!node || typeof node.addEventListener !== "function") { return; } const handleContextMenu = (event: DomEventLike) => { event.preventDefault(); event.stopPropagation(); onOpenRef.current(event.clientX, event.clientY); }; node.addEventListener("contextmenu", handleContextMenu); return () => { node.removeEventListener?.("contextmenu", handleContextMenu); }; }, [target]); return setTarget; } function clamp(value: number, min: number, max: number): number { if (max < min) return min; return Math.min(Math.max(value, min), max); } interface MenuItemListProps { items: readonly ContextMenuItem[]; theme: PluginTheme; compact: boolean; onSelectItem(item: ContextMenuItem): void; } function MenuItemList({ items, theme, compact, onSelectItem, }: MenuItemListProps) { const [hoveredId, setHoveredId] = useState(null); return ( {items.map((item) => { const isDanger = item.tone === "danger"; const isHovered = hoveredId === item.id; const foreground = isDanger ? theme.colors.statusDanger : theme.colors.foreground; const iconColor = isDanger ? theme.colors.statusDanger : theme.colors.foregroundMuted; return ( {item.separatorBefore ? ( ) : null} setHoveredId(item.id)} onHoverOut={() => setHoveredId((prev) => (prev === item.id ? null : prev)) } onPress={() => onSelectItem(item)} style={({ pressed }) => ({ flexDirection: "row", alignItems: "center", gap: compact ? 8 : 10, marginHorizontal: 4, paddingHorizontal: compact ? 8 : 10, paddingVertical: compact ? 6 : 9, borderRadius: 6, backgroundColor: pressed || isHovered ? theme.colors.surface2 : "transparent", opacity: item.disabled ? 0.5 : 1, })} > {item.label} ); })} ); } export interface ContextMenuOverlayProps { request: ContextMenuRequest | null; containerRef: RefObject; platform: "ios" | "android" | "web"; theme: PluginTheme; onClose(): void; } export function ContextMenuOverlay({ request, containerRef, platform, theme, onClose, }: ContextMenuOverlayProps) { const backdropRef = useRef(null); const menuRef = useRef(null); const [menuSize, setMenuSize] = useState<{ width: number; height: number; } | null>(null); useEffect(() => { setMenuSize(null); }, [request]); useEffect(() => { if (!request || platform !== "web") return; const doc = getDocumentNode(); if (!doc || typeof doc.addEventListener !== "function") return; const handleKeyDown = (event: DomEventLike) => { if (event.key === "Escape") { event.preventDefault(); onClose(); } }; const handlePointerDown = (event: DomEventLike) => { const menuNode = asDomNode(menuRef.current); if (menuNode?.contains?.(event.target)) { return; } const containerNode = asDomNode(containerRef.current); if (!containerNode?.contains?.(event.target)) { onClose(); } }; doc.addEventListener("keydown", handleKeyDown, true); doc.addEventListener("pointerdown", handlePointerDown, true); return () => { doc.removeEventListener?.("keydown", handleKeyDown, true); doc.removeEventListener?.("pointerdown", handlePointerDown, true); }; }, [containerRef, onClose, platform, request]); useEffect(() => { if (!request || platform !== "web") return; const backdropNode = asDomNode(backdropRef.current); if (!backdropNode || typeof backdropNode.addEventListener !== "function") { return; } const handleContextMenu = (event: DomEventLike) => { event.preventDefault(); event.stopPropagation(); onClose(); }; backdropNode.addEventListener("contextmenu", handleContextMenu); return () => { backdropNode.removeEventListener?.("contextmenu", handleContextMenu); }; }, [onClose, platform, request]); if (!request) return null; function handleSelectItem(item: ContextMenuItem) { onClose(); item.onSelect(); } const containerRect = platform === "web" ? asDomNode(containerRef.current)?.getBoundingClientRect?.() ?? null : null; if (!containerRect) { return ( { if (!nextOpen) onClose(); }} > ); } const padding = 8; const estimatedWidth = menuSize?.width ?? 192; const estimatedHeight = menuSize?.height ?? request.items.length * 32 + 36; const rawLeft = request.clientX - containerRect.left; const rawTop = request.clientY - containerRect.top; const left = clamp( rawLeft, padding, containerRect.width - estimatedWidth - padding, ); const top = clamp( rawTop, padding, containerRect.height - estimatedHeight - padding, ); return ( { const { width, height } = event.nativeEvent.layout; setMenuSize((prev) => prev && prev.width === width && prev.height === height ? prev : { width, height }, ); }} style={{ position: "absolute", left, top, minWidth: 184, maxWidth: Math.max(184, containerRect.width - padding * 2), paddingVertical: 4, borderRadius: 10, borderWidth: 1, borderColor: theme.colors.border, backgroundColor: theme.colors.surface1, shadowColor: "#000000", shadowOpacity: 0.24, shadowRadius: 12, shadowOffset: { width: 0, height: 6 }, elevation: 8, }} > {request.title ? ( {request.title} ) : null} ); }