({ card, ha }: Props)
| 17 | } |
| 18 | |
| 19 | export default function ScriptCardView({ card, ha }: Props) { |
| 20 | const [feedback, setFeedback] = useState<FeedbackState>('idle'); |
| 21 | const feedbackTimer = useRef<ReturnType<typeof setTimeout>>(); |
| 22 | const tapTimer = useRef<ReturnType<typeof setTimeout>>(); |
| 23 | const longPressTimer = useRef<ReturnType<typeof setTimeout>>(); |
| 24 | const tapCount = useRef(0); |
| 25 | const longPressFired = useRef(false); |
| 26 | |
| 27 | const callEntity = useCallback(async (entityId: string) => { |
| 28 | if (!ha || feedback === 'loading') return; |
| 29 | clearTimeout(feedbackTimer.current); |
| 30 | setFeedback('loading'); |
| 31 | const domain = entityId.split('.')[0]; |
| 32 | try { |
| 33 | await ha.callService(domain, 'turn_on', entityId); |
| 34 | setFeedback('success'); |
| 35 | } catch { |
| 36 | setFeedback('error'); |
| 37 | } |
| 38 | feedbackTimer.current = setTimeout(() => setFeedback('idle'), 1500); |
| 39 | }, [ha, feedback]); |
| 40 | |
| 41 | const handleTap = useCallback(() => { |
| 42 | callEntity(card.entityId); |
| 43 | }, [card.entityId, callEntity]); |
| 44 | |
| 45 | const handleDoubleTap = useCallback(() => { |
| 46 | if (card.doublePressEntityId) { |
| 47 | callEntity(card.doublePressEntityId); |
| 48 | } else { |
| 49 | handleTap(); |
| 50 | } |
| 51 | }, [card.doublePressEntityId, callEntity, handleTap]); |
| 52 | |
| 53 | const handleLongPress = useCallback(() => { |
| 54 | if (card.longPressEntityId) { |
| 55 | callEntity(card.longPressEntityId); |
| 56 | } |
| 57 | }, [card.longPressEntityId, callEntity]); |
| 58 | |
| 59 | /* ── Pointer events for combined gesture detection ── */ |
| 60 | |
| 61 | const handlePointerDown = useCallback((e: React.PointerEvent<HTMLButtonElement>) => { |
| 62 | e.currentTarget.setPointerCapture(e.pointerId); |
| 63 | longPressFired.current = false; |
| 64 | |
| 65 | longPressTimer.current = setTimeout(() => { |
| 66 | longPressFired.current = true; |
| 67 | handleLongPress(); |
| 68 | }, LONG_PRESS_DELAY); |
| 69 | }, [handleLongPress]); |
| 70 | |
| 71 | const handlePointerUp = useCallback((e: React.PointerEvent<HTMLButtonElement>) => { |
| 72 | e.currentTarget.blur(); |
| 73 | clearTimeout(longPressTimer.current); |
| 74 | |
| 75 | // If long-press already fired, skip tap handling |
| 76 | if (longPressFired.current) return; |
nothing calls this directly
no test coverage detected