feat: implement web URL constants and enhance navigation handling

- Introduced a centralized `WEB_URL` constant for managing application routes, improving maintainability and readability across components.
- Updated various components, including login, auth, and main app modules, to utilize the new `WEB_URL` constants for navigation, ensuring consistency in route management.
- Added a new `AppHomeRedirect` component to streamline user redirection based on privileges, enhancing user experience.
- Implemented client-side navigation functions to prevent full page reloads, improving performance and user interaction.
- Added tests for new functionalities, ensuring reliability in navigation and URL handling.

These changes significantly enhance the application's routing structure and navigation efficiency, providing a more cohesive user experience.
This commit is contained in:
shancheas
2026-09-02 11:17:51 +07:00
parent f5c1b7430a
commit b0090bc15f
18 changed files with 345 additions and 24 deletions
+12 -8
View File
@@ -1,6 +1,9 @@
import { appDatabase, AppDatabaseKey, appStorage, AppStorageKey } from '../storage/local';
import { API_URL } from '../constants/api-url';
import { WEB_URL } from '../constants/web-url';
import { clientReplace, notifySessionEnd, notifySessionStart } from './client-navigation';
import { mapUserPrivileges } from './map-user-privileges';
import { safeInternalPath } from './safe-internal-path';
import type { AuthUser, TokenPair } from './auth.types';
interface TerminateOptions {
@@ -39,16 +42,17 @@ export async function terminateAuthSession(options: TerminateOptions = {}): Prom
await appDatabase.removeItem(AppDatabaseKey.REFRESH_TOKEN);
await appStorage.removeItem(AppStorageKey.USER_ID);
let loginUrl = '/auth/login';
const isNotLoginPage = !window.location.pathname.includes('/auth/login');
let loginUrl = WEB_URL.LOGIN;
const isLoginPage =
window.location.pathname === WEB_URL.LOGIN || window.location.pathname.startsWith(`${WEB_URL.LOGIN}/`);
if (isNotLoginPage) {
if (!isLoginPage) {
if (preserveRedirect) {
const currentPath = window.location.pathname + window.location.search;
loginUrl += `?redirect=${encodeURIComponent(currentPath)}`;
loginUrl += `?redirect=${encodeURIComponent(window.location.pathname)}`;
}
window.location.replace(loginUrl);
notifySessionEnd();
clientReplace(loginUrl);
}
}
@@ -59,10 +63,10 @@ export async function initiateAuthSession(tokens: TokenPair, me: AuthUser): Prom
await persistTokenPair(tokens);
await persistUserSession(me);
notifySessionStart();
const params = new URLSearchParams(window.location.search);
const redirectTo = params.get('redirect');
window.location.replace(redirectTo ? decodeURIComponent(redirectTo) : '/app');
clientReplace(safeInternalPath(params.get('redirect'), WEB_URL.APP));
}
let refreshInFlight: Promise<TokenPair> | null = null;