import { useState } from "react"; import { Alert, Pressable, Text, View } from "react-native"; type AskAction = { id: string; label: string }; export function AskActions({ actions, disabled, onAnswer, }: { actions: AskAction[]; disabled?: boolean; onAnswer: (answer: string) => Promise; }) { const [pendingAction, setPendingAction] = useState(null); const submitting = pendingAction !== null; async function submit(answer: string) { if (disabled || submitting) return; setPendingAction(answer); try { await onAnswer(answer); } catch (error) { Alert.alert( "Could not submit answer", error instanceof Error ? error.message : "Please try again.", ); } finally { setPendingAction(null); } } return ( {actions.map((action) => ( void submit(action.id)} style={{ borderRadius: 11, paddingHorizontal: 14, paddingVertical: 8, backgroundColor: action.id === "allow" || action.id === "always" ? "#F1F1EF" : "transparent", borderWidth: action.id === "deny" ? 1 : 0, borderColor: "#26262A", opacity: disabled || submitting ? 0.5 : 1, }} > {pendingAction === action.id ? "Sending…" : action.label} ))} ); }