refactor: update Tauri socket event handling and improve Mnemonic component typing

- Revised the Tauri socket event contract to clarify that encryption key handling is managed via the API instead of the socket.
- Removed outdated socket event constants related to encryption key requests from `tauriSocket.ts`.
- Enhanced type definitions in the `Mnemonic` component by specifying the event type for keyboard events.
This commit is contained in:
M3gA-Mind
2026-02-20 21:21:18 +05:30
parent f628f870ad
commit 97d2460141
3 changed files with 3 additions and 28 deletions
+1 -8
View File
@@ -172,14 +172,7 @@ const socket = io(BACKEND_URL, {
### Socket event contract (Tauri)
In Tauri mode, the Rust socket forwards server events to the frontend via the `server:event` Tauri event. The frontend listens and can emit back via `emitViaRustSocket` (see `utils/tauriSocket.ts`).
| Direction | Event name | Payload | Description |
|------------|----------------------------|----------------------------------|-------------|
| Backend → | `request:encryption_key` | (any) | Backend requests the clients encryption key. |
| Frontend → | `encryption_key` | `{ encryptionKey: string \| null }` | Frontend sends `encryptionKeyByUser` for the current user from authSlice. |
Constants: `REQUEST_ENCRYPTION_KEY`, `ENCRYPTION_KEY_EVENT` (exported from `utils/tauriSocket.ts`).
In Tauri mode, the Rust socket forwards server events to the frontend via the `server:event` Tauri event. The frontend listens and can emit back via `emitViaRustSocket` (see `utils/tauriSocket.ts`). Encryption key is handled via the API, not the socket.
## MTProto Service (`services/mtprotoService.ts`)
+2 -2
View File
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useCallback, useEffect, useMemo, useRef, useState, type KeyboardEvent } from 'react';
import { useNavigate } from 'react-router-dom';
import LottieAnimation from '../components/LottieAnimation';
@@ -96,7 +96,7 @@ const Mnemonic = () => {
);
const handleImportKeyDown = useCallback(
(index: number, e: React.KeyboardEvent<HTMLInputElement>) => {
(index: number, e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Backspace' && !importWords[index] && index > 0) {
inputRefs.current[index - 1]?.focus();
}
-18
View File
@@ -12,10 +12,6 @@
* Legacy bridge (for backwards compatibility during migration):
* - socket:should_connect / socket:should_disconnect
* - report_socket_connected / disconnected / error
*
* Socket event contract (backend <-> frontend):
* - Backend emits: REQUEST_ENCRYPTION_KEY — frontend should reply with encryption key.
* - Frontend emits: ENCRYPTION_KEY_EVENT — payload { encryptionKey: string | null } from authSlice.
*/
import { isTauri as coreIsTauri, invoke } from '@tauri-apps/api/core';
import { listen, UnlistenFn } from '@tauri-apps/api/event';
@@ -25,12 +21,6 @@ import { store } from '../store';
import { setSocketIdForUser, setStatusForUser } from '../store/socketSlice';
import { BACKEND_URL } from './config';
/** Event name the backend emits to request the clients encryption key. */
export const REQUEST_ENCRYPTION_KEY = 'request:encryption_key';
/** Event name the frontend emits with encryptionKeyByUser (authSlice) for the current user. */
export const ENCRYPTION_KEY_EVENT = 'encryption_key';
// Check if we're running in Tauri
export const isTauri = (): boolean => {
return coreIsTauri();
@@ -171,14 +161,6 @@ export async function setupTauriSocketListeners(): Promise<void> {
unlistenServerEvent = await listen<{ event: string; data: unknown }>('server:event', event => {
const { event: eventName, data } = event.payload;
console.log('[TauriSocket] Server event:', eventName, data);
if (eventName === REQUEST_ENCRYPTION_KEY) {
const state = store.getState();
const userId =
state.user.user?._id ?? getSocketUserId();
const encryptionKey = state.auth.encryptionKeyByUser[userId] ?? null;
void emitViaRustSocket(ENCRYPTION_KEY_EVENT, { encryptionKey });
}
});
// Legacy: Listen for connect requests from Rust (backwards compat)