Files
paseo/packages/app/src/hooks/use-push-token-registration.ts
T
Mohamed BoudraandGitHub 60531b5e1b refactor: long-tail type-aware sweep (T3.c) (#759)
* refactor: long-tail type-aware sweep (T3.c)

Cluster T3.c — 15 files, 24 errors cleared.

Rules fixed:
- no-useless-default-assignment: 6 → 0 (mcp-server.ts — Zod .default() makes destructuring defaults redundant)
- no-unnecessary-type-assertion: 1 → 0 (workspace-registry-model.ts — remove redundant ! on string[])
- no-unsafe-enum-comparison: 3 → 0 (use-push-token-registration.ts, use-image-attachment-picker.ts — use PermissionStatus enum values)
- no-redundant-type-constituents: 6 → 0 (workspace-setup.ts, tool-call-detail-state.ts, workspace-draft-agent-tab.tsx, stream.test.ts, desktop-settings-commands.ts — unknown | T → unknown)
- no-unnecessary-type-parameters: 4 → 0 (indexeddb-attachment-store.test.ts, use-archive-agent.ts, class-mocks.ts, agent-stream-coalescer.ts — narrow interface / remove unused generics)
- no-unnecessary-type-conversion: 4 → 0 (session-context.tsx — remove String() on already-string values; class-mocks.ts — remove String(prop) after symbol guard)
- no-implied-eval: 1 → 0 (agent-stream-coalescer.test.ts — narrow timer interface to function-only, eliminating string overload)

Deferred (need caller updates or extra files beyond 15-file cap):
- no-unnecessary-type-parameters: host.ts:onResized/onDragDropEvent, electron/events.ts (callers pass typed handlers; removing generic breaks contravariance without cast)
- no-base-to-string: ~21 errors, 18 files
- restrict-template-expressions: ~11 errors, 9 files
- await-thenable: ~7 errors, 4 files
- no-unnecessary-type-conversion: ~9 remaining errors, 9 files

* fix(server): restore MCP tool handler defaults that the linter mis-flagged

The `no-useless-default-assignment` rule fires because Zod's `.default()`
chain makes the TypeScript type non-optional, so the linter treats the
destructuring defaults as redundant. But the MCP framework invokes
handlers with raw (untransformed) params — Zod defaults are not applied
before the call. The destructuring defaults are the runtime safety net.

Reverts the three handler changes from T3.c:
- send_prompt: background = false, notifyOnFinish = false
- list_agents: includeArchived = false, sinceHours = 48, limit = 50
- get_terminal_lines: stripAnsi = true

The no-useless-default-assignment errors on these lines remain and are
deferred to a future cluster that addresses the MCP framework integration.
2026-05-05 22:38:55 +08:00

112 lines
3.6 KiB
TypeScript

import { useCallback, useEffect, useRef } from "react";
import { Platform } from "react-native";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as Notifications from "expo-notifications";
import Constants from "expo-constants";
import type { DaemonClient } from "@server/client/daemon-client";
import { isWeb } from "@/constants/platform";
const STORAGE_PREFIX = "@paseo:expo-push-token:";
function getExpoProjectId(): string | null {
const constants = Constants as unknown as {
easConfig?: { projectId?: unknown };
expoConfig?: { extra?: { eas?: { projectId?: unknown } } };
};
const fromEas = constants?.easConfig?.projectId;
if (typeof fromEas === "string" && fromEas.trim()) return fromEas.trim();
const fromExtra = constants?.expoConfig?.extra?.eas?.projectId;
if (typeof fromExtra === "string" && fromExtra.trim()) return fromExtra.trim();
return null;
}
async function ensurePushPermission(): Promise<boolean> {
const existing = await Notifications.getPermissionsAsync();
if (existing.status === Notifications.PermissionStatus.GRANTED) return true;
if (!existing.canAskAgain) return false;
const requested = await Notifications.requestPermissionsAsync();
return requested.status === Notifications.PermissionStatus.GRANTED;
}
export function usePushTokenRegistration(params: { client: DaemonClient; serverId: string }): void {
const { client, serverId } = params;
const tokenRef = useRef<string | null>(null);
const lastSentTokenRef = useRef<string | null>(null);
const registerIfPossible = useCallback(async () => {
if (isWeb) return;
if (!client.isConnected) return;
const token = tokenRef.current;
if (!token) return;
if (lastSentTokenRef.current === token) return;
lastSentTokenRef.current = token;
client.registerPushToken(token);
}, [client]);
useEffect(() => {
if (isWeb) return;
const storageKey = `${STORAGE_PREFIX}${serverId}`;
let cancelled = false;
const run = async () => {
const cached = await AsyncStorage.getItem(storageKey);
if (cancelled) return;
if (cached && typeof cached === "string") {
tokenRef.current = cached;
}
const granted = await ensurePushPermission();
if (!granted || cancelled) return;
if (Platform.OS === "android") {
await Notifications.setNotificationChannelAsync("default", {
name: "default",
importance: Notifications.AndroidImportance.DEFAULT,
});
}
const projectId = getExpoProjectId();
if (!projectId) {
console.warn("[PushToken] Missing EAS projectId; cannot fetch Expo push token");
return;
}
const result = await Notifications.getExpoPushTokenAsync({ projectId });
if (cancelled) return;
const token = result.data;
if (typeof token !== "string" || !token.trim()) return;
tokenRef.current = token;
await AsyncStorage.setItem(storageKey, token);
await registerIfPossible();
};
void run().catch((error) => {
console.warn("[PushToken] Failed to register push token", error);
});
return () => {
cancelled = true;
};
}, [registerIfPossible, serverId]);
useEffect(() => {
const unsubscribe = client.subscribeConnectionStatus((state) => {
if (state.status === "connected") {
void registerIfPossible();
} else {
// Re-register on the next successful connect.
lastSentTokenRef.current = null;
}
});
if (client.isConnected) {
void registerIfPossible();
}
return unsubscribe;
}, [client, registerIfPossible]);
}