refactor(core): move app state ownership into the Rust core (#320)

* refactor(core): integrate CoreStateProvider and streamline state management

- Replaced UserProvider with CoreStateProvider in App component to centralize state management.
- Updated various components to utilize the new CoreStateProvider for accessing session tokens and user data.
- Refactored hooks and components to eliminate direct Redux store dependencies, enhancing modularity and maintainability.
- Introduced a new core state management structure to improve the handling of user authentication and onboarding states.

This refactor aims to simplify state access across the application and improve overall code clarity.

* fix: restore polyfills import and clean up component imports

- Reintroduced the import of polyfills in main.tsx to ensure compatibility.
- Adjusted import statements in PrivacyPanel and SocketProvider for consistency.
- Simplified conditional expressions in TeamInvitesPanel and TeamMembersPanel for better readability.
- Refactored CoreStateProvider and store.ts to streamline state initialization.
- Enhanced formatting in various files for improved code clarity and maintainability.

* refactor(tests): streamline onboarding and protected route tests

- Updated OnboardingOverlay tests to utilize mock state management and simplify assertions.
- Refactored ProtectedRoute tests to improve readability and ensure consistent use of mock state.
- Enhanced teamApi tests to replace direct API calls with core RPC method calls, improving test isolation and clarity.
- Adjusted socketSelectors tests to utilize a centralized core state snapshot for better state management during tests.

* refactor(onboarding): simplify onboarding state management and improve loading logic

- Removed unnecessary local state for onboarding completion in OnboardingOverlay, directly utilizing snapshot data.
- Enhanced loading logic to prevent unnecessary renders and improve user experience during onboarding.
- Updated PrivacyPanel to handle analytics consent persistence with error handling.
- Refactored TeamManagementPanel and TeamPanel to improve team data fetching logic and loading states.
- Streamlined CoreStateProvider to manage session token synchronization and state updates more effectively.

* chore(deps): update tempfile dependency and refactor app state management

- Added `tempfile` dependency to Cargo.toml for improved temporary file handling.
- Refactored app state loading and saving functions to enhance error handling and ensure data integrity.
- Introduced quarantine mechanism for corrupted app state files to prevent application crashes.
- Updated URL parsing logic to ensure proper formatting of API URLs.
- Adjusted type definitions in schemas for better clarity and consistency.

* refactor(tests): simplify mock state usage in onboarding and protected route tests

- Consolidated mock state management in OnboardingOverlay and ProtectedRoute tests for improved readability.
- Streamlined assertions by reducing unnecessary lines in test setups.
- Enhanced consistency in mock return values across tests to ensure clarity and maintainability.

* refactor(tests): enhance PublicRoute tests with mock state and routing

- Updated PublicRoute tests to utilize MemoryRouter for improved routing simulation.
- Simplified mock state management by integrating `useCoreState` for user authentication scenarios.
- Streamlined test assertions and removed redundant preloaded state configurations for clarity and maintainability.

* refactor(tests): streamline mock state usage in PublicRoute and Mnemonic tests

- Simplified mock state management in PublicRoute and Mnemonic tests for improved readability.
- Consolidated mock return values to reduce redundancy and enhance clarity in test setups.
- Improved consistency in the usage of `useCoreState` across test files.

* Refactor billing components for improved readability

- Cleaned up import statements in BillingPanel.tsx for better organization.
- Enhanced formatting of billing plan descriptions in billingHelpers.ts for consistency.
- Improved readability of conditional checks in BillingPanel component.

* Refactor API endpoint handling for clarity and consistency

- Updated references from `/settings` to `/auth/me` in various modules to standardize user authentication flows.
- Renamed `parse_settings_response_json` to `parse_api_response_json` for improved clarity in response handling.
- Adjusted user ID extraction functions to reflect the new endpoint structure, enhancing maintainability across the codebase.
- Updated test cases to align with the new endpoint naming conventions, ensuring consistency in API interactions.
This commit is contained in:
Steven Enamakel
2026-04-04 03:20:18 -07:00
committed by GitHub
parent 24876497df
commit ef708bdd20
60 changed files with 1890 additions and 1437 deletions
+2 -2
View File
@@ -2,7 +2,7 @@
//!
//! Use [`crate::api::config`] for default base URL and env normalization,
//! [`crate::api::jwt`] for session token retrieval and bearer formatting,
//! [`crate::api::rest`] for authenticated REST calls (`/auth/...`, `GET /settings`, etc.),
//! [`crate::api::rest`] for authenticated REST calls (`/auth/...`, `GET /auth/me`, etc.),
//! and [`crate::api::socket`] for Socket.IO WebSocket URLs.
//! [`crate::api::models`] holds shared DTOs for auth and realtime (server-adjacent).
@@ -17,7 +17,7 @@ pub use config::{
};
pub use jwt::{bearer_authorization_value, get_session_token};
pub use rest::{
decrypt_handoff_blob, user_id_from_profile_payload, user_id_from_settings_payload,
decrypt_handoff_blob, user_id_from_auth_me_payload, user_id_from_profile_payload,
BackendOAuthClient, ConnectResponse, IntegrationSummary, IntegrationTokensHandoff,
};
pub use socket::websocket_url;
+8 -14
View File
@@ -21,9 +21,8 @@ fn build_backend_reqwest_client() -> Result<Client> {
.map_err(|e| anyhow::anyhow!("failed to build HTTP client: {e}"))
}
fn parse_settings_response_json(text: &str) -> Result<Value> {
let v: Value =
serde_json::from_str(text).with_context(|| format!("parse /settings JSON: {text}"))?;
fn parse_api_response_json(text: &str) -> Result<Value> {
let v: Value = serde_json::from_str(text).with_context(|| format!("parse API JSON: {text}"))?;
let Some(obj) = v.as_object() else {
return Ok(v);
};
@@ -34,7 +33,7 @@ fn parse_settings_response_json(text: &str) -> Result<Value> {
.or_else(|| obj.get("error"))
.and_then(|x| x.as_str())
.unwrap_or("request unsuccessful");
anyhow::bail!("/settings failed: {msg}");
anyhow::bail!("API request failed: {msg}");
}
if let Some(data) = obj.get("data") {
if !data.is_null() {
@@ -86,8 +85,8 @@ pub fn user_id_from_profile_payload(payload: &Value) -> Option<String> {
})
}
pub fn user_id_from_settings_payload(settings: &Value) -> Option<String> {
user_id_from_profile_payload(settings)
pub fn user_id_from_auth_me_payload(payload: &Value) -> Option<String> {
user_id_from_profile_payload(payload)
}
/// JSON body returned by the backend after OAuth connect starts.
@@ -249,12 +248,7 @@ impl BackendOAuthClient {
if !status.is_success() {
anyhow::bail!("GET /auth/me failed ({status}): {text}");
}
parse_settings_response_json(&text)
}
/// Backward-compatible alias retained while older call sites are migrated.
pub async fn fetch_settings(&self, bearer_jwt: &str) -> Result<Value> {
self.fetch_current_user(bearer_jwt).await
parse_api_response_json(&text)
}
/// `POST /telegram/login-tokens/:token/consume` — exchange a one-time login token for a JWT.
@@ -332,7 +326,7 @@ impl BackendOAuthClient {
anyhow::bail!("create channel link token failed ({status}): {text}");
}
parse_settings_response_json(&text)
parse_api_response_json(&text)
}
/// Generic authenticated JSON request helper for backend API routes that
@@ -373,7 +367,7 @@ impl BackendOAuthClient {
);
}
parse_settings_response_json(&text)
parse_api_response_json(&text)
}
/// `GET /auth/integrations`