import React, { useCallback, useContext, useEffect, useRef, useState } from 'react'; import { type StyleProp, StyleSheet, Text, type TextStyle, View, type ViewProps, type ViewStyle, } from 'react-native'; import { initialWindowMetrics, SafeAreaInsetsContext } from 'react-native-safe-area-context'; import { HUD_COLORS, HUD_NATIVE_FINISHED_FONT_SIZE, HUD_NATIVE_FINISHED_LINE_HEIGHT, HUD_NATIVE_INTENT_LINE_HEIGHT, HUD_NATIVE_INTENT_MAX_LINES, HUD_PANEL_BOUNDS, hudFinishedLine, hudIntentDisplay, type HudStepResult, nextHudFinishedStep, type RecipeHudPlacement, waitForHudPaint, } from '@farmslot/protocol/recipe'; export type FarmslotRecipeHudStatus = 'idle' | 'running' | 'pass' | 'fail'; export interface FarmslotRecipeHudState { status: FarmslotRecipeHudStatus; intent: string; currentStep?: number; totalSteps?: number; error?: string; position?: RecipeHudPlacement; /** The step that just finished; useRecipeHudState keeps it across updates. */ finished?: Pick; } export type RecipeHudStateController = readonly [ FarmslotRecipeHudState | null, (state: FarmslotRecipeHudState | null) => Promise, ]; interface HudAcknowledgement { resolve(): void; reject(error: unknown): void; } /** * Bridge acknowledgement follows the committed HUD rather than the state setter. * Updates carry the step that just finished, reset at each new run. */ export function useRecipeHudState(): RecipeHudStateController { const [state, setState] = useState(null); const received = useRef(null); const lastResult = useRef(null); const pending = useRef([]); const lifecycle = useRef(null); const update = useCallback((next: FarmslotRecipeHudState | null) => { if (next === received.current) return Promise.resolve(); received.current = next; const step = nextHudFinishedStep(lastResult.current, next); lastResult.current = step.last; const shown = next && next.finished === undefined && step.finished ? { ...next, finished: { intent: step.finished.intent, status: step.finished.status } } : next; return new Promise((resolve, reject) => { pending.current.push({ resolve, reject }); setState(shown); }); }, []); useEffect(() => { const controller = new AbortController(); lifecycle.current = controller; return () => { const error = new Error('HUD provider unmounted.'); controller.abort(error); for (const acknowledgement of pending.current.splice(0)) acknowledgement.reject(error); }; }, []); useEffect(() => { const acknowledgements = pending.current.splice(0); if (acknowledgements.length) void waitForHudPaint(requestAnimationFrame, lifecycle.current?.signal).then( () => { for (const acknowledgement of acknowledgements) acknowledgement.resolve(); }, (error: unknown) => { for (const acknowledgement of acknowledgements) acknowledgement.reject(error); }, ); }, [state]); return [state, update]; } export interface FarmslotRecipeHudText { badge?: (state: FarmslotRecipeHudState) => string; intent?: (state: FarmslotRecipeHudState) => string; error?: (state: FarmslotRecipeHudState) => string | undefined; } export interface FarmslotRecipeHudStyles { container?: StyleProp; line?: StyleProp; badgeText?: StyleProp; badgeTextRunning?: StyleProp; badgeTextPass?: StyleProp; badgeTextFail?: StyleProp; intent?: StyleProp; error?: StyleProp; } export interface RecipeHudOverlayProps { children: React.ReactNode; unstable_accessibilityContainerViewIsModal?: boolean; } export interface RecipeHudOptions { overlay?: React.ComponentType; text?: FarmslotRecipeHudText; styles?: FarmslotRecipeHudStyles; } export interface RecipeHudProps extends RecipeHudOptions { onLayout?: ViewProps['onLayout']; state: FarmslotRecipeHudState | null; } const PADDING_VERTICAL = 3; const ERROR_LINE_HEIGHT = 16; // The native panel never grows: the finished row shows only while two intent // lines, that row and an error line still fit its height. const FINISHED_ROW_FITS = PADDING_VERTICAL * 2 + HUD_NATIVE_INTENT_LINE_HEIGHT * HUD_NATIVE_INTENT_MAX_LINES + HUD_NATIVE_FINISHED_LINE_HEIGHT + ERROR_LINE_HEIGHT <= HUD_PANEL_BOUNDS.native.maxHeight; export function RecipeHud({ state, styles: customStyles, text, overlay: Overlay, onLayout, }: Readonly): React.ReactElement | null { const insets = useContext(SafeAreaInsetsContext) ?? initialWindowMetrics?.insets; if (!state || state.status === 'idle') return null; const badge = text?.badge?.(state); const intent = hudIntentDisplay(text?.intent?.(state) ?? state.intent, 'native'); const error = text?.error?.(state) ?? state.error; const badgeTone = badgeToneForStatus(state.status); const finished = FINISHED_ROW_FITS && state.finished ? hudFinishedLine({ nodeId: 'finished', ...state.finished }) : null; const progress = FINISHED_ROW_FITS ? formatProgress(state.currentStep, state.totalSteps) : ''; const overlay = ( {badge ? ( {badge} ) : null} {intent.text} {finished || progress ? ( {finished?.text} {progress ? ( {progress} ) : null} ) : null} {error ? ( {error} ) : null} ); return Overlay ? ( {overlay} ) : ( overlay ); } function formatProgress(currentStep?: number, totalSteps?: number): string { if (!currentStep || !totalSteps) return ''; return `${currentStep}/${totalSteps}`; } function badgeToneForStatus(status: FarmslotRecipeHudStatus): 'running' | 'pass' | 'fail' { if (status === 'fail') return 'fail'; if (status === 'pass') return 'pass'; return 'running'; } const styles = StyleSheet.create({ container: { position: 'absolute', left: 12, right: 12, bottom: 16, zIndex: 9999, backgroundColor: 'rgba(0, 0, 0, 0.58)', paddingHorizontal: 10, paddingVertical: PADDING_VERTICAL, }, line: { color: '#ffffff', fontSize: 11, fontWeight: '700', lineHeight: 14, }, badgeText: { fontSize: 9, fontWeight: '800', }, badgeTextRunning: { color: '#00ff88', }, badgeTextPass: { color: '#00ff88', }, badgeTextFail: { color: '#ff4d4f', }, finishedRow: { flexDirection: 'row', }, finished: { flex: 1, color: HUD_COLORS.muted, fontSize: HUD_NATIVE_FINISHED_FONT_SIZE, lineHeight: HUD_NATIVE_FINISHED_LINE_HEIGHT, }, failed: { color: HUD_COLORS.fail, }, progress: { marginLeft: 6, color: HUD_COLORS.muted, fontSize: HUD_NATIVE_FINISHED_FONT_SIZE, lineHeight: HUD_NATIVE_FINISHED_LINE_HEIGHT, }, error: { color: '#e6e6e6', fontSize: 10, fontWeight: '400', lineHeight: 12, }, });