mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-01 04:36:26 +00:00
fix(app): count native touches and typing as presence (#8070)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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 (
|
||||
<GestureHandlerRootView style={flexStyle}>
|
||||
<GestureHandlerRootView
|
||||
style={flexStyle}
|
||||
onTouchStart={reportNativeUserActivity}
|
||||
onTouchEnd={reportNativeUserActivity}
|
||||
>
|
||||
<View style={layoutStyles.surfaceFill}>
|
||||
<LucideProvider strokeWidth={ICON_STROKE_WIDTH}>
|
||||
<AddHostProvider>
|
||||
|
||||
@@ -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<EditingTextInputHandle, EditingTextIn
|
||||
const handleChangeText = useCallback(
|
||||
(nextText: string) => {
|
||||
textRef.current = nextText;
|
||||
// Soft and hardware keyboards type without touching the app's root view.
|
||||
reportNativeUserActivity();
|
||||
onChangeText?.(nextText);
|
||||
bumpTextRevision();
|
||||
},
|
||||
|
||||
@@ -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<Listener>();
|
||||
|
||||
export function reportNativeUserActivity(): void {
|
||||
for (const listener of listeners) {
|
||||
listener();
|
||||
}
|
||||
}
|
||||
|
||||
export function subscribeNativeUserActivity(listener: Listener): () => void {
|
||||
listeners.add(listener);
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user