diff --git a/docs/notifications.md b/docs/notifications.md index 495de60edd..01c99c265a 100644 --- a/docs/notifications.md +++ b/docs/notifications.md @@ -63,6 +63,8 @@ An arbitrary Activity launch with data extras does not exercise Expo's FCM path. Foreground pushes remain in Notification Center without a banner or sound. This preserves notifications sent with stale presence information. +Native clients have no DOM events, so presence comes from `@/hooks/native-user-activity`: touches on the root `GestureHandlerRootView` and edits in the native `EditingTextInput` refresh `lastActivityAt`. Touches inside a native `Modal` open a separate window and do not reach the root. + ## Startup and verification Wait for the root navigator, host registry, remembered selection, and startup diff --git a/packages/app/src/app/_layout.tsx b/packages/app/src/app/_layout.tsx index 4a4ebc96bc..6151ea6edc 100644 --- a/packages/app/src/app/_layout.tsx +++ b/packages/app/src/app/_layout.tsx @@ -108,6 +108,7 @@ import { useGlobalNewWorkspaceAction } from "@/hooks/use-global-new-workspace-ac import { useLatchedBoolean } from "@/hooks/use-latched-boolean"; import { useFaviconStatus } from "@/hooks/use-favicon-status"; import { useKeyboardShortcuts } from "@/keyboard/use-shortcuts"; +import { reportNativeUserActivity } from "@/hooks/native-user-activity"; import { KeyboardShiftProvider } from "@/keyboard/shift"; import { useCompactWebViewportZoomLock } from "@/hooks/use-compact-web-viewport-zoom-lock"; import { useOpenProject } from "@/hooks/use-open-project"; @@ -1026,7 +1027,11 @@ function RootProviders({ children }: { children: ReactNode }) { function RootAppTree() { return ( - + diff --git a/packages/app/src/components/ui/text-input/text-input.native.tsx b/packages/app/src/components/ui/text-input/text-input.native.tsx index 4ff773cefa..f7bf4b511c 100644 --- a/packages/app/src/components/ui/text-input/text-input.native.tsx +++ b/packages/app/src/components/ui/text-input/text-input.native.tsx @@ -13,6 +13,7 @@ import PasteInput, { type PasteTextInputInstance, } from "@mattermost/react-native-paste-input"; import { useIsInsideBottomSheet } from "@/components/ui/bottom-sheet-scope"; +import { reportNativeUserActivity } from "@/hooks/native-user-activity"; import type { EditingTextInputHandle, EditingTextInputProps } from "./types"; type NativeInput = (TextInput | PasteTextInputInstance) & { @@ -130,6 +131,8 @@ export const EditingTextInput = forwardRef { textRef.current = nextText; + // Soft and hardware keyboards type without touching the app's root view. + reportNativeUserActivity(); onChangeText?.(nextText); bumpTextRevision(); }, diff --git a/packages/app/src/hooks/native-user-activity.ts b/packages/app/src/hooks/native-user-activity.ts new file mode 100644 index 0000000000..ac8c84cc31 --- /dev/null +++ b/packages/app/src/hooks/native-user-activity.ts @@ -0,0 +1,24 @@ +/** + * App-wide signal for user input that native clients cannot observe through DOM + * events: touches on the app root and edits in native text inputs. + * + * Every host session subscribes, so one touch keeps the device present for all + * connected daemons. + */ + +type Listener = () => void; + +const listeners = new Set(); + +export function reportNativeUserActivity(): void { + for (const listener of listeners) { + listener(); + } +} + +export function subscribeNativeUserActivity(listener: Listener): () => void { + listeners.add(listener); + return () => { + listeners.delete(listener); + }; +} diff --git a/packages/app/src/hooks/use-client-activity.test.tsx b/packages/app/src/hooks/use-client-activity.test.tsx new file mode 100644 index 0000000000..8eae4d4c50 --- /dev/null +++ b/packages/app/src/hooks/use-client-activity.test.tsx @@ -0,0 +1,71 @@ +/** + * @vitest-environment jsdom + */ +import { renderHook } from "@testing-library/react"; +import type { DaemonClient } from "@getpaseo/client/internal/daemon-client"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import type { HeartbeatPayload } from "./client-activity-tracker"; +import { reportNativeUserActivity } from "./native-user-activity"; +import { useClientActivity } from "./use-client-activity"; + +vi.mock("@/constants/platform", () => ({ + isWeb: false, + isNative: true, + getIsElectron: () => false, +})); + +const START_MS = new Date("2026-10-28T10:00:00.000Z").getTime(); +const READING_WITHOUT_NAVIGATING_MS = 4 * 60_000; + +function createConnectedClient() { + const heartbeats: HeartbeatPayload[] = []; + const client = { + isConnected: true, + sendHeartbeat: (payload: HeartbeatPayload) => heartbeats.push(payload), + subscribeConnectionStatus: (listener: (state: { status: string }) => void) => { + listener({ status: "connected" }); + return () => undefined; + }, + } as unknown as DaemonClient; + return { client, heartbeats }; +} + +describe("useClientActivity on native", () => { + beforeEach(() => { + vi.useFakeTimers({ now: START_MS }); + }); + + afterEach(() => { + vi.useRealTimers(); + }); + + it("keeps a client reading one agent present while the user touches or types", () => { + const { client, heartbeats } = createConnectedClient(); + renderHook(() => + useClientActivity({ client, focusedAgentId: "agent-1", focusedTerminalId: null }), + ); + + vi.advanceTimersByTime(READING_WITHOUT_NAVIGATING_MS); + reportNativeUserActivity(); + + expect(heartbeats.at(-1)).toMatchObject({ + deviceType: "mobile", + focusedAgentId: "agent-1", + lastActivityAt: new Date(START_MS + READING_WITHOUT_NAVIGATING_MS).toISOString(), + }); + }); + + it("stops reporting activity for a session after it unmounts", () => { + const { client, heartbeats } = createConnectedClient(); + const { unmount } = renderHook(() => + useClientActivity({ client, focusedAgentId: "agent-1", focusedTerminalId: null }), + ); + unmount(); + const sentBeforeActivity = heartbeats.length; + + vi.advanceTimersByTime(READING_WITHOUT_NAVIGATING_MS); + reportNativeUserActivity(); + + expect(heartbeats).toHaveLength(sentBeforeActivity); + }); +}); diff --git a/packages/app/src/hooks/use-client-activity.ts b/packages/app/src/hooks/use-client-activity.ts index 65d8bf4067..fc49529a83 100644 --- a/packages/app/src/hooks/use-client-activity.ts +++ b/packages/app/src/hooks/use-client-activity.ts @@ -10,6 +10,7 @@ import { DESKTOP_IDLE_POLL_INTERVAL_MS, HEARTBEAT_INTERVAL_MS, } from "./client-activity-tracker"; +import { subscribeNativeUserActivity } from "./native-user-activity"; interface ClientActivityOptions { client: DaemonClient; @@ -56,6 +57,15 @@ export function useClientActivity({ return () => subscription.remove(); }, [tracker]); + // Track touches and typing on native, where the DOM listeners below don't exist. + useEffect(() => { + if (!isNative) return; + return subscribeNativeUserActivity(() => { + tracker.recordUserActivity(); + tracker.maybeSendImmediateHeartbeat(); + }); + }, [tracker]); + // Track user activity and visibility on web. useEffect(() => { if (isNative) return;