feat: add user selection dialog for admin users
- Import and integrate UserSelectionDialog component - Add state for user selection (showUserSelection, allUsers) - Add isSwitchedByAdmin state to track admin-switched sessions - Implement handleUserSelect and handleUserSelectionCancel handlers - Update login flow to show user selection when admin logs in - Add conditional logout button visibility based on user type - Update UI to include UserSelectionDialog component
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
|
|
||||||
import React, { useState, useEffect } from 'react'
|
import React, { useState, useEffect } from 'react'
|
||||||
import LoginDialog from './components/LoginDialog'
|
import LoginDialog from './components/LoginDialog'
|
||||||
|
import UserSelectionDialog, { type UserInfo as SelectedUserInfo } from './components/UserSelectionDialog'
|
||||||
import { ExtractorPage } from './pages/ExtractorPage'
|
import { ExtractorPage } from './pages/ExtractorPage'
|
||||||
import { CleanerPage } from './pages/CleanerPage'
|
import { CleanerPage } from './pages/CleanerPage'
|
||||||
|
|
||||||
@@ -29,8 +30,13 @@ function App(): React.JSX.Element {
|
|||||||
|
|
||||||
// Dialog state
|
// Dialog state
|
||||||
const [showLoginDialog, setShowLoginDialog] = useState(false)
|
const [showLoginDialog, setShowLoginDialog] = useState(false)
|
||||||
|
const [showUserSelection, setShowUserSelection] = useState(false)
|
||||||
|
const [allUsers, setAllUsers] = useState<SelectedUserInfo[]>([])
|
||||||
const [errorMessage, setErrorMessage] = useState('')
|
const [errorMessage, setErrorMessage] = useState('')
|
||||||
|
|
||||||
|
// Track if current session is switched by Admin
|
||||||
|
const [isSwitchedByAdmin, setIsSwitchedByAdmin] = useState(false)
|
||||||
|
|
||||||
// Navigation state
|
// Navigation state
|
||||||
const [currentPage, setCurrentPage] = useState<Page>('home')
|
const [currentPage, setCurrentPage] = useState<Page>('home')
|
||||||
|
|
||||||
@@ -69,9 +75,11 @@ function App(): React.JSX.Element {
|
|||||||
|
|
||||||
// Check if admin needs user selection
|
// Check if admin needs user selection
|
||||||
if (result.requiresUserSelection) {
|
if (result.requiresUserSelection) {
|
||||||
console.log('Admin user needs to select user - logging in anyway')
|
console.log('Admin user needs to select user')
|
||||||
// For now, just log in as the current user
|
// Load all users for selection
|
||||||
setIsAuthenticated(true)
|
const users = await window.electron.auth.getAllUsers()
|
||||||
|
setAllUsers(users)
|
||||||
|
setShowUserSelection(true)
|
||||||
} else {
|
} else {
|
||||||
console.log('Setting authenticated to true')
|
console.log('Setting authenticated to true')
|
||||||
setIsAuthenticated(true)
|
setIsAuthenticated(true)
|
||||||
@@ -105,8 +113,10 @@ function App(): React.JSX.Element {
|
|||||||
// Check if admin needs user selection
|
// Check if admin needs user selection
|
||||||
if (result.userInfo.userType === 'Admin') {
|
if (result.userInfo.userType === 'Admin') {
|
||||||
setShowLoginDialog(false)
|
setShowLoginDialog(false)
|
||||||
// TODO: Show user selection dialog
|
// Load all users for selection
|
||||||
console.log('Admin user needs to select user')
|
const users = await window.electron.auth.getAllUsers()
|
||||||
|
setAllUsers(users)
|
||||||
|
setShowUserSelection(true)
|
||||||
} else {
|
} else {
|
||||||
setIsAuthenticated(true)
|
setIsAuthenticated(true)
|
||||||
setShowLoginDialog(false)
|
setShowLoginDialog(false)
|
||||||
@@ -126,14 +136,47 @@ function App(): React.JSX.Element {
|
|||||||
setShowLoginDialog(false)
|
setShowLoginDialog(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle user selection
|
||||||
|
const handleUserSelect = async (user: SelectedUserInfo) => {
|
||||||
|
try {
|
||||||
|
const result = await window.electron.auth.switchUser(user)
|
||||||
|
if (result.success) {
|
||||||
|
setCurrentUser({
|
||||||
|
username: result.userInfo?.username || user.username,
|
||||||
|
userType: result.userInfo?.userType || user.userType
|
||||||
|
})
|
||||||
|
setIsAuthenticated(true)
|
||||||
|
setShowUserSelection(false)
|
||||||
|
// Mark as switched by Admin
|
||||||
|
setIsSwitchedByAdmin(true)
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('User selection error:', error)
|
||||||
|
showError('切换用户失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle user selection cancel
|
||||||
|
const handleUserSelectionCancel = () => {
|
||||||
|
// User cancelled selection, logout and show login dialog
|
||||||
|
window.electron.auth.logout()
|
||||||
|
setShowUserSelection(false)
|
||||||
|
setShowLoginDialog(true)
|
||||||
|
}
|
||||||
|
|
||||||
// Handle logout
|
// Handle logout
|
||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
await window.electron.auth.logout()
|
await window.electron.auth.logout()
|
||||||
setIsAuthenticated(false)
|
setIsAuthenticated(false)
|
||||||
setCurrentUser(null)
|
setCurrentUser(null)
|
||||||
|
setIsSwitchedByAdmin(false)
|
||||||
setShowLoginDialog(true)
|
setShowLoginDialog(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if should show logout button
|
||||||
|
// Show logout if: user is Admin, OR user was switched by Admin
|
||||||
|
const shouldShowLogout = currentUser?.userType === 'Admin' || isSwitchedByAdmin
|
||||||
|
|
||||||
// Show loading state during authentication
|
// Show loading state during authentication
|
||||||
if (isAuthenticating) {
|
if (isAuthenticating) {
|
||||||
return (
|
return (
|
||||||
@@ -183,12 +226,21 @@ function App(): React.JSX.Element {
|
|||||||
onError={showError}
|
onError={showError}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{/* User Selection Dialog for Admin */}
|
||||||
|
<UserSelectionDialog
|
||||||
|
isOpen={showUserSelection}
|
||||||
|
users={allUsers}
|
||||||
|
currentUsername={currentUser?.username || ''}
|
||||||
|
onSelectUser={handleUserSelect}
|
||||||
|
onCancel={handleUserSelectionCancel}
|
||||||
|
/>
|
||||||
|
|
||||||
{errorMessage && (
|
{errorMessage && (
|
||||||
<div className="error-toast">{errorMessage}</div>
|
<div className="error-toast">{errorMessage}</div>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{/* If showLoginDialog is false but not authenticated, show a message */}
|
{/* If showLoginDialog is false but not authenticated, show a message */}
|
||||||
{!showLoginDialog && (
|
{!showLoginDialog && !showUserSelection && (
|
||||||
<div style={{
|
<div style={{
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
alignItems: 'center',
|
alignItems: 'center',
|
||||||
@@ -257,17 +309,19 @@ function App(): React.JSX.Element {
|
|||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
<div>
|
<div>
|
||||||
<button onClick={handleLogout} style={{
|
{shouldShowLogout && (
|
||||||
padding: '8px 16px',
|
<button onClick={handleLogout} style={{
|
||||||
backgroundColor: '#ff4d4f',
|
padding: '8px 16px',
|
||||||
color: '#fff',
|
backgroundColor: '#ff4d4f',
|
||||||
border: 'none',
|
color: '#fff',
|
||||||
borderRadius: '6px',
|
border: 'none',
|
||||||
fontSize: '14px',
|
borderRadius: '6px',
|
||||||
cursor: 'pointer'
|
fontSize: '14px',
|
||||||
}}>
|
cursor: 'pointer'
|
||||||
退出登录
|
}}>
|
||||||
</button>
|
退出登录
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user