import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { ActivityIndicator, Animated, BackHandler, Easing, Pressable, ScrollView, StyleSheet, Text, View, } from 'react-native'; import type { DebugQuickAction } from '../../types/config'; import { t } from '../../i18n'; import { Colors } from '../theme/colors'; import { ACTION_ORB_SIZE, ACTION_ORB_TOP, layoutRadialActions } from './radialActions'; interface QuickActionsMenuProps { open: boolean; active: boolean; actions: readonly DebugQuickAction[]; origin: { x: number; y: number }; viewport: { width: number; height: number }; onClose: () => void; onExited: () => void; reduceMotion: boolean; } interface Feedback { id: string; title: string; message: string; success: boolean } export function QuickActionsMenu({ open, active, actions, origin, viewport, onClose, onExited, reduceMotion }: QuickActionsMenuProps) { const mounted = useRef(false); const [present, setPresent] = useState(open); const values = useMemo(() => Array.from({ length: actions.length }, () => new Animated.Value(0)), [actions.length]); const backdrop = useRef(new Animated.Value(0)).current; const busyRef = useRef(new Set()); const [busy, setBusy] = useState>(new Set()); const [feedback, setFeedback] = useState(null); const [selectedActionId, setSelectedActionId] = useState(null); const pending = useRef<(() => void) | null>(null); const current = useRef({ active, open }); current.current = { active, open }; const layout = useMemo(() => layoutRadialActions({ origin: { x: origin.x, y: origin.y }, viewport: { width: viewport.width, height: viewport.height }, count: actions.length, }), [origin.x, origin.y, viewport.width, viewport.height, actions.length]); const grid = layout[0]?.mode === 'grid'; useEffect(() => { mounted.current = true; return () => { mounted.current = false; pending.current = null; }; }, []); useEffect(() => { if (open && active) setPresent(true); const entering = open && active; if (entering) setSelectedActionId(null); const finish = () => { if (mounted.current && !entering) setPresent(false); }; if (reduceMotion || !active) { backdrop.setValue(entering ? 1 : 0); values.forEach(value => value.setValue(entering ? 1 : 0)); finish(); return; } const animatedValues = entering ? values : [...values].reverse(); const animation = Animated.parallel([ Animated.timing(backdrop, { toValue: entering ? 1 : 0, duration: entering ? 180 : 160, useNativeDriver: true }), ...animatedValues.map((value, index) => Animated.timing(value, { toValue: entering ? 1 : 0, duration: entering ? 260 : 180, delay: index * (entering ? 20 : 16), // The slight overshoot gives the radial fan a spring-like arrival while // retaining native-driver support in older React Native versions. easing: entering ? Easing.bezier(0.2, 0.9, 0.25, 1.06) : Easing.bezier(0.4, 0, 1, 1), useNativeDriver: true, })), ]); animation.start(({ finished }) => { if (finished) finish(); }); return () => animation.stop(); }, [open, active, reduceMotion, values, backdrop]); // Flush only after the closing layer has been removed in a committed render. useEffect(() => { if (present || open) return; onExited(); const invoke = pending.current; pending.current = null; invoke?.(); }, [present, open, onExited]); useEffect(() => { if (!open || !active) return; const subscription = BackHandler?.addEventListener('hardwareBackPress', () => { onClose(); return true; }); return () => subscription?.remove(); }, [open, active, onClose]); useEffect(() => { if (!feedback) return; const timer = setTimeout(() => setFeedback(null), 3000); return () => clearTimeout(timer); }, [feedback]); const run = useCallback((action: DebugQuickAction) => { if (!current.current.active || !current.current.open || action.disabled || pending.current || busyRef.current.has(action.id)) return; busyRef.current.add(action.id); setBusy(new Set(busyRef.current)); setSelectedActionId(action.id); setFeedback(null); const invoke = () => { if (!mounted.current) return; void Promise.resolve().then(() => action.onPress()).then(() => { if (mounted.current) setFeedback({ id: action.id, title: action.title, message: t('quickActions.success'), success: true }); }, (error: unknown) => { const detail = error instanceof Error ? error.message.slice(0, 160) : ''; if (mounted.current) setFeedback({ id: action.id, title: action.title, message: detail ? t('quickActions.error') + ': ' + detail : t('quickActions.error'), success: false }); }).finally(() => { busyRef.current.delete(action.id); if (mounted.current) setBusy(new Set(busyRef.current)); }); }; if (action.closeOnPress !== false) { pending.current = invoke; onClose(); } else invoke(); }, [onClose]); const cards = actions.map((action, index) => { const card = layout[index]; if (!card) return null; const progress = values[index]!; const icon = typeof action.icon === 'string' || typeof action.icon === 'number' ? {action.icon} : action.icon; const isBusy = busy.has(action.id); const success = feedback?.id === action.id && feedback.success; const selected = selectedActionId === action.id; const receded = selectedActionId !== null && !selected; return ( run(action)} style={[styles.hitTarget, action.disabled && styles.disabled]}> {({ pressed }) => <> {isBusy ? : success ? ✓ : icon ?? {action.title.slice(0, 1)}} {action.title} } ); }); const showLayer = active && (open || present) && actions.length > 0; return ( <> {showLayer && ( <> {grid ? ( {t('quickActions.close')} × card.bottom), 0) + 8 }}> {cards} ) : {cards}} )} {feedback && active && ( {feedback.title + ' · ' + feedback.message} )} ); } const styles = StyleSheet.create({ layer: { ...StyleSheet.absoluteFillObject, zIndex: 998 }, backdrop: { ...StyleSheet.absoluteFillObject, backgroundColor: 'rgba(7,16,24,0.24)' }, position: { position: 'absolute' }, hitTarget: { width: '100%', height: '100%', alignItems: 'center', justifyContent: 'flex-start', paddingTop: ACTION_ORB_TOP, gap: 4 }, disabled: { opacity: 0.4 }, orb: { width: ACTION_ORB_SIZE, height: ACTION_ORB_SIZE, borderRadius: ACTION_ORB_SIZE / 2, borderWidth: 1, borderColor: 'rgba(255,255,255,0.14)', backgroundColor: '#202B3A', alignItems: 'center', justifyContent: 'center', shadowColor: '#000', shadowOffset: { width: 0, height: 4 }, shadowOpacity: 0.16, shadowRadius: 10, elevation: 8 }, selectedOrb: { borderColor: Colors.primary, backgroundColor: Colors.primaryDim }, successOrb: { borderColor: Colors.success }, iconText: { color: Colors.primaryLight, fontSize: 20, lineHeight: 25 }, label: { maxWidth: 64, color: Colors.text, backgroundColor: 'rgba(15,17,20,0.90)', borderRadius: 8, paddingHorizontal: 4, paddingVertical: 2, fontSize: 10, lineHeight: 14, fontWeight: '500', textAlign: 'center', overflow: 'hidden' }, feedback: { position: 'absolute', left: 16, right: 16, bottom: 16, alignItems: 'center', zIndex: 1001 }, feedbackText: { color: Colors.text, backgroundColor: Colors.surfaceElevated, borderWidth: 1, borderColor: Colors.border, borderRadius: 12, paddingHorizontal: 16, paddingVertical: 10, fontSize: 12 }, gridPanel: { ...StyleSheet.absoluteFillObject, backgroundColor: Colors.background }, gridClose: { minHeight: 44, justifyContent: 'center', alignItems: 'center' }, closeText: { color: Colors.primaryLight, fontSize: 12 }, });