* Extract client SDK package * Polish SDK client identity defaults * Build client before dependent CI jobs * Restore daemon client server export * Extract protocol and client SDK packages * Fix provider override schema validation * Fix app test daemon client imports * Simplify workspace build targets * Fix CLI test server build bootstrap * Run SDK package tests in CI * Fix rebase package split drift * Restore lockfile registry metadata * Update SDK config test for prompt default * Move terminal stream router test to client package * Fix rebase drift for protocol imports * Fix SDK agent capability fixture * Restore legacy server client exports * Fix server export compatibility test * Advertise custom mode icon client capability * Remove server daemon-client exports * Format rebased mode control import * Fix rebase drift for protocol imports Files added by upstream PRs (#893, #1147, #1154) referenced the pre-split shared/ paths that this branch moves into @getpaseo/protocol. Redirect those imports to the protocol package so typecheck stays green after the rebase.
112 lines
3.6 KiB
TypeScript
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 "@getpaseo/client/internal/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]);
|
|
}
|