| import { useEffect, useRef } from 'react' |
| import { useNotifications } from '../context/notifications.js' |
| import { getShortcutDisplay } from '../keybindings/shortcutFormat.js' |
| import { hasImageInClipboard } from '../utils/imagePaste.js' |
|
|
| const NOTIFICATION_KEY = 'clipboard-image-hint' |
| |
| const FOCUS_CHECK_DEBOUNCE_MS = 1000 |
| |
| const HINT_COOLDOWN_MS = 30000 |
|
|
| |
| |
| |
| |
| |
| |
| |
| export function useClipboardImageHint( |
| isFocused: boolean, |
| enabled: boolean, |
| ): void { |
| const { addNotification } = useNotifications() |
| const lastFocusedRef = useRef(isFocused) |
| const lastHintTimeRef = useRef(0) |
| const checkTimeoutRef = useRef<NodeJS.Timeout | null>(null) |
|
|
| useEffect(() => { |
| |
| const wasFocused = lastFocusedRef.current |
| lastFocusedRef.current = isFocused |
|
|
| if (!enabled || !isFocused || wasFocused) { |
| return |
| } |
|
|
| |
| if (checkTimeoutRef.current) { |
| clearTimeout(checkTimeoutRef.current) |
| } |
|
|
| |
| checkTimeoutRef.current = setTimeout( |
| async (checkTimeoutRef, lastHintTimeRef, addNotification) => { |
| checkTimeoutRef.current = null |
|
|
| |
| const now = Date.now() |
| if (now - lastHintTimeRef.current < HINT_COOLDOWN_MS) { |
| return |
| } |
|
|
| |
| if (await hasImageInClipboard()) { |
| lastHintTimeRef.current = now |
| addNotification({ |
| key: NOTIFICATION_KEY, |
| text: `Image in clipboard · ${getShortcutDisplay('chat:imagePaste', 'Chat', 'ctrl+v')} to paste`, |
| priority: 'immediate', |
| timeoutMs: 8000, |
| }) |
| } |
| }, |
| FOCUS_CHECK_DEBOUNCE_MS, |
| checkTimeoutRef, |
| lastHintTimeRef, |
| addNotification, |
| ) |
|
|
| return () => { |
| if (checkTimeoutRef.current) { |
| clearTimeout(checkTimeoutRef.current) |
| checkTimeoutRef.current = null |
| } |
| } |
| }, [isFocused, enabled, addNotification]) |
| } |
|
|