docs: add comments to the src/routes & src/store & src/utils directory
This commit is contained in:
@@ -1,3 +1,22 @@
|
||||
/*
|
||||
* @Author: ZhaoYing
|
||||
* @Date: 2026-02-02 16:33:54
|
||||
* @Last Modified by: ZhaoYing
|
||||
* @Last Modified time: 2026-02-02 16:33:54
|
||||
*/
|
||||
/**
|
||||
* User Store
|
||||
*
|
||||
* Manages user authentication and profile with:
|
||||
* - User information storage
|
||||
* - Login/logout functionality
|
||||
* - Token management (access & refresh)
|
||||
* - Workspace storage type
|
||||
* - Navigation guards for workspace access
|
||||
*
|
||||
* @store
|
||||
*/
|
||||
|
||||
import { create } from 'zustand'
|
||||
import { clearAuthData } from '@/utils/auth';
|
||||
import type { User } from '@/views/UserManagement/types'
|
||||
@@ -5,6 +24,7 @@ import { getUsers, refreshToken, logout } from '@/api/user'
|
||||
import { getWorkspaceStorageType } from '@/api/workspaces';
|
||||
import { cookieUtils } from '@/utils/request'
|
||||
|
||||
/** Login information interface */
|
||||
export interface LoginInfo {
|
||||
access_token: string;
|
||||
expires_at: string;
|
||||
@@ -12,24 +32,37 @@ export interface LoginInfo {
|
||||
refresh_token: string;
|
||||
token_type: 'bearer'
|
||||
}
|
||||
|
||||
/** User state interface */
|
||||
export interface UserState {
|
||||
/** Current user information */
|
||||
user: User;
|
||||
/** Login token information */
|
||||
loginInfo: LoginInfo;
|
||||
/** Workspace storage type */
|
||||
storageType: string | null;
|
||||
/** Update login information */
|
||||
updateLoginInfo: (values: LoginInfo) => void;
|
||||
/** Get user information */
|
||||
getUserInfo: (flag?: boolean) => void;
|
||||
/** Clear user information */
|
||||
clearUserInfo: () => void;
|
||||
/** Logout user */
|
||||
logout: () => void;
|
||||
/** Get workspace storage type */
|
||||
getStorageType: () => void;
|
||||
/** Check and redirect if workspace not set */
|
||||
checkJump: () => void;
|
||||
}
|
||||
|
||||
/** Pages that don't require workspace */
|
||||
export const whitePage = [
|
||||
'/conversation',
|
||||
'/login',
|
||||
'/invite-register'
|
||||
]
|
||||
|
||||
/** User store */
|
||||
export const useUser = create<UserState>((set, get) => ({
|
||||
user: localStorage.getItem('user') ? JSON.parse(localStorage.getItem('user') || '{}') as User : {} as User,
|
||||
loginInfo: {} as LoginInfo,
|
||||
@@ -101,9 +134,10 @@ export const useUser = create<UserState>((set, get) => ({
|
||||
const localUser = JSON.parse(localStorage.getItem('user') || '{}') as User;
|
||||
const hash = window.location.hash;
|
||||
|
||||
/** Redirect to index if user has no workspace and not on whitelist page */
|
||||
if (localUser.id && (!localUser.current_workspace_id || localUser.current_workspace_id === '') && !whitePage.find(vo => hash.includes(vo))) {
|
||||
console.log('whitePage', whitePage.find(vo => hash.includes(vo)))
|
||||
window.location.href = '/#/index'
|
||||
}
|
||||
},
|
||||
}))
|
||||
}))
|
||||
|
||||
Reference in New Issue
Block a user