UMBRA/frontend/src/hooks/useConfirmAction.ts
Kyle Pope 8aa662c096 Consolidate toast effects, extract useConfirmAction, extend index
- W3: Merge route-change and new-alert effects into single unified effect
- W6: Migration 018 extends due_lookup index with snoozed_until column
- S1: Extract useConfirmAction hook from TodoItem/ReminderItem
- S7: Update summary toast count on dismiss/snooze instead of dismissing

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-24 04:01:50 +08:00

27 lines
846 B
TypeScript

import { useState, useRef, useEffect, useCallback } from 'react';
/**
* Two-click confirmation pattern: first click shows "Sure?", second executes.
* Auto-resets after `timeoutMs` if the user doesn't confirm.
* Cleans up the timer on unmount.
*/
export function useConfirmAction(action: () => void, timeoutMs = 4000) {
const [confirming, setConfirming] = useState(false);
const timerRef = useRef<ReturnType<typeof setTimeout>>();
useEffect(() => () => clearTimeout(timerRef.current), []);
const handleClick = useCallback(() => {
if (!confirming) {
setConfirming(true);
timerRef.current = setTimeout(() => setConfirming(false), timeoutMs);
return;
}
clearTimeout(timerRef.current);
action();
setConfirming(false);
}, [confirming, action, timeoutMs]);
return { confirming, handleClick };
}