Files
openhuman/src/store/socketSelectors.ts
T
Steven Enamakel 339334466a Refactor socket user ID selection and update QuickJS register function signatures
- Replaced the `selectCurrentUserId` function with `selectSocketUserId` to derive the socket user ID from the JWT token, improving clarity and functionality.
- Updated the `register` function signatures across multiple QuickJS operation files to use a generic lifetime parameter, enhancing type safety and consistency.
2026-02-05 19:45:35 +05:30

36 lines
1.1 KiB
TypeScript

import type { RootState } from './index';
const PENDING_USER = '__pending__';
/**
* Derive the socket user ID from the JWT token — must match the key used
* by tauriSocket.ts and socketService.ts when writing to byUser[].
*/
function selectSocketUserId(state: RootState): string {
const token = state.auth.token;
if (!token) return PENDING_USER;
try {
const parts = token.split('.');
if (parts.length !== 3) return PENDING_USER;
const payloadBase64 = parts[1].replace(/-/g, '+').replace(/_/g, '/');
const payloadJson = atob(payloadBase64);
const payload = JSON.parse(payloadJson);
return payload.tgUserId || payload.userId || payload.sub || PENDING_USER;
} catch {
return PENDING_USER;
}
}
export const selectSocketStatus = (state: RootState) => {
const userId = selectSocketUserId(state);
const userState = state.socket.byUser[userId];
return userState?.status ?? 'disconnected';
};
export const selectSocketId = (state: RootState): string | null => {
const userId = selectSocketUserId(state);
const userState = state.socket.byUser[userId];
return userState?.socketId ?? null;
};