diff --git a/src/hooks/use-keyboard-height/constants.ts b/src/hooks/use-keyboard-height/constants.ts index bb674bd0..e210445a 100644 --- a/src/hooks/use-keyboard-height/constants.ts +++ b/src/hooks/use-keyboard-height/constants.ts @@ -7,8 +7,6 @@ export const FALLBACK_KEYBOARD_RATIO = 0.42; export const FALLBACK_KEYBOARD_MIN = 260; export const FALLBACK_KEYBOARD_MAX = 380; export const FALLBACK_CLOSE_GRACE_MS = 900; -export const FALLBACK_WATCHDOG_INTERVAL_MS = 500; -export const FALLBACK_WATCHDOG_LOG_INTERVAL_MS = 1_500; export const RECHECK_DELAYS_MS = [0, 50, 120, 240, 420, 700]; export const KEYBOARD_RELEASE_EVENT_TYPES = new Set([ "resize", diff --git a/src/hooks/use-keyboard-height/index.ts b/src/hooks/use-keyboard-height/index.ts index 5715b1c7..256d9f95 100644 --- a/src/hooks/use-keyboard-height/index.ts +++ b/src/hooks/use-keyboard-height/index.ts @@ -34,7 +34,6 @@ import { isKeyboardTargetActive, } from "./keyboard-target"; import { addKeyboardRecheckListeners } from "./keyboard-event-listeners"; -import { createKeyboardWatchdog } from "./keyboard-watchdog"; import { isPossibleKeyboardCloseEvent } from "./release-events"; import type { UseKeyboardHeightOptions, ViewportMetrics } from "./types"; import { @@ -181,7 +180,6 @@ export function useKeyboardHeight({ metrics: formatMetrics(metrics), }); } - watchdog.update("apply"); }; const schedule = () => { @@ -234,24 +232,8 @@ export function useKeyboardHeight({ environment, metrics: formatMetrics(metrics), }); - watchdog.stop(reason); }; - const watchdog = createKeyboardWatchdog({ - environment, - log, - getTarget: () => targetRef?.current, - getReactActive: () => activeRef.current, - getFallbackAllowed: () => fallbackAllowedRef.current, - getFallbackWasApplied: () => fallbackWasApplied, - getLastInputLift: () => lastInputLift, - getFocusedAt: () => focusedAtRef.current, - onPossibleClose: () => { - lastEventType = "watchdog"; - lastMayBeKeyboardClose = true; - }, - schedule, - }); const removeKeyboardRecheckListeners = addKeyboardRecheckListeners(scheduleRechecks); @@ -259,7 +241,6 @@ export function useKeyboardHeight({ return () => { if (rafId != null) window.cancelAnimationFrame(rafId); - watchdog.cleanup(); timeoutIds.forEach((id) => window.clearTimeout(id)); removeKeyboardRecheckListeners(); resetKeyboardVars(); diff --git a/src/hooks/use-keyboard-height/keyboard-watchdog.ts b/src/hooks/use-keyboard-height/keyboard-watchdog.ts deleted file mode 100644 index 83d25139..00000000 --- a/src/hooks/use-keyboard-height/keyboard-watchdog.ts +++ /dev/null @@ -1,176 +0,0 @@ -import type { Logger } from "@/utils"; - -import { - FALLBACK_WATCHDOG_INTERVAL_MS, - FALLBACK_WATCHDOG_LOG_INTERVAL_MS, -} from "./constants"; -import type { KeyboardAdaptEnvironment, ViewportMetrics } from "./types"; -import { - didViewportMetricsChange, - formatMetrics, - getFocusedDurationMs, - hasDetectedKeyboard, - readViewportMetrics, -} from "./viewport-metrics"; -import { - getActiveElementTag, - isKeyboardTargetActive, -} from "./keyboard-target"; - -interface KeyboardWatchdogOptions { - environment: KeyboardAdaptEnvironment; - log: Logger; - getTarget: () => HTMLElement | null | undefined; - getReactActive: () => boolean; - getFallbackAllowed: () => boolean; - getFallbackWasApplied: () => boolean; - getLastInputLift: () => number; - getFocusedAt: () => number; - onPossibleClose: () => void; - schedule: () => void; -} - -export interface KeyboardWatchdog { - update: (reason: string) => void; - stop: (reason: string) => void; - cleanup: () => void; -} - -export function createKeyboardWatchdog({ - environment, - log, - getTarget, - getReactActive, - getFallbackAllowed, - getFallbackWasApplied, - getLastInputLift, - getFocusedAt, - onPossibleClose, - schedule, -}: KeyboardWatchdogOptions): KeyboardWatchdog { - let watchdogId: number | null = null; - let lastWatchdogLogAt = 0; - let lastWatchdogMetrics: ViewportMetrics | null = null; - let lastWatchdogKeyboardDetected = false; - - const shouldRun = (): boolean => { - if (!environment.usesXiaomiFacebookFallback) return false; - return isKeyboardTargetActive(getTarget(), getReactActive()); - }; - - const update = (reason: string): void => { - if (shouldRun()) { - start(reason); - return; - } - stop(reason); - }; - - const start = (reason: string): void => { - if (watchdogId != null) return; - const metrics = readViewportMetrics(); - watchdogId = window.setInterval( - run, - FALLBACK_WATCHDOG_INTERVAL_MS, - ); - lastWatchdogLogAt = performance.now(); - lastWatchdogMetrics = metrics; - lastWatchdogKeyboardDetected = hasDetectedKeyboard(metrics); - log.debug("[keyboard] watchdog started", { - reason, - environment, - active: getReactActive(), - fallbackAllowed: getFallbackAllowed(), - fallbackWasApplied: getFallbackWasApplied(), - lastInputLift: getLastInputLift(), - keyboardDetected: lastWatchdogKeyboardDetected, - metrics: formatMetrics(metrics), - }); - }; - - const stop = (reason: string): void => { - if (watchdogId == null) return; - window.clearInterval(watchdogId); - watchdogId = null; - lastWatchdogLogAt = 0; - lastWatchdogMetrics = null; - lastWatchdogKeyboardDetected = false; - log.debug("[keyboard] watchdog stopped", { - reason, - environment, - active: getReactActive(), - fallbackAllowed: getFallbackAllowed(), - fallbackWasApplied: getFallbackWasApplied(), - lastInputLift: getLastInputLift(), - }); - }; - - const cleanup = (): void => { - if (watchdogId == null) return; - window.clearInterval(watchdogId); - watchdogId = null; - }; - - const run = (): void => { - const metrics = readViewportMetrics(); - const activeNow = isKeyboardTargetActive(getTarget(), getReactActive()); - const keyboardDetected = hasDetectedKeyboard(metrics); - const metricsChanged = didViewportMetricsChange( - lastWatchdogMetrics, - metrics, - ); - const now = performance.now(); - const shouldLog = - metricsChanged || - now - lastWatchdogLogAt >= FALLBACK_WATCHDOG_LOG_INTERVAL_MS; - - if (!activeNow) { - log.debug("[keyboard] watchdog inactive target", { - environment, - reactActive: getReactActive(), - activeElement: getActiveElementTag(), - metrics: formatMetrics(metrics), - }); - stop("inactive-target"); - schedule(); - return; - } - - if (lastWatchdogKeyboardDetected && !keyboardDetected) { - onPossibleClose(); - log.debug("[keyboard] watchdog possible close detected", { - environment, - fallbackAllowed: getFallbackAllowed(), - fallbackWasApplied: getFallbackWasApplied(), - lastInputLift: getLastInputLift(), - previousMetrics: lastWatchdogMetrics - ? formatMetrics(lastWatchdogMetrics) - : null, - metrics: formatMetrics(metrics), - }); - schedule(); - } - - if (shouldLog) { - lastWatchdogLogAt = now; - log.debug("[keyboard] watchdog tick", { - environment, - active: activeNow, - reactActive: getReactActive(), - fallbackAllowed: getFallbackAllowed(), - fallbackWasApplied: getFallbackWasApplied(), - lastInputLift: getLastInputLift(), - keyboardDetected, - metricsChanged, - focusedForMs: getFocusedDurationMs(getFocusedAt()), - activeElement: getActiveElementTag(), - metrics: formatMetrics(metrics), - }); - } - - lastWatchdogMetrics = metrics; - lastWatchdogKeyboardDetected = keyboardDetected; - }; - - return { update, stop, cleanup }; -}