Refactor: enhance dashboard layout and experiment management features
- Added functionality to save and retrieve the current dashboard view in localStorage for improved user experience. - Updated DashboardLayout component to handle view changes with access control based on user roles. - Renamed Experiments component to ExperimentManagement for clarity. - Introduced new ExperimentPhase interface and related utility functions for managing experiment phases. - Updated seed data to include initial roles and experiment phases for testing. - Cleaned up unnecessary blank lines in various files for better code readability.
This commit is contained in:
@@ -3,9 +3,10 @@ import { Sidebar } from './Sidebar'
|
||||
import { TopNavbar } from './TopNavbar'
|
||||
import { DashboardHome } from './DashboardHome'
|
||||
import { UserManagement } from './UserManagement'
|
||||
import { Experiments } from './Experiments'
|
||||
import { ExperimentManagement } from './ExperimentManagement'
|
||||
import { DataEntry } from './DataEntry'
|
||||
import { VisionSystem } from './VisionSystem'
|
||||
import { Scheduling } from './Scheduling'
|
||||
import { VideoStreamingPage } from '../features/video-streaming'
|
||||
import { userManagement, type User } from '../lib/supabase'
|
||||
|
||||
@@ -22,10 +23,68 @@ export function DashboardLayout({ onLogout }: DashboardLayoutProps) {
|
||||
const [isMobileOpen, setIsMobileOpen] = useState(false)
|
||||
const [isHovered, setIsHovered] = useState(false)
|
||||
|
||||
// Valid dashboard views
|
||||
const validViews = ['dashboard', 'user-management', 'experiments', 'analytics', 'data-entry', 'vision-system', 'scheduling', 'video-library']
|
||||
|
||||
// Save current view to localStorage
|
||||
const saveCurrentView = (view: string) => {
|
||||
try {
|
||||
localStorage.setItem('dashboard-current-view', view)
|
||||
} catch (error) {
|
||||
console.warn('Failed to save current view to localStorage:', error)
|
||||
}
|
||||
}
|
||||
|
||||
// Get saved view from localStorage
|
||||
const getSavedView = (): string => {
|
||||
try {
|
||||
const savedView = localStorage.getItem('dashboard-current-view')
|
||||
return savedView && validViews.includes(savedView) ? savedView : 'dashboard'
|
||||
} catch (error) {
|
||||
console.warn('Failed to get saved view from localStorage:', error)
|
||||
return 'dashboard'
|
||||
}
|
||||
}
|
||||
|
||||
// Check if user has access to a specific view
|
||||
const hasAccessToView = (view: string): boolean => {
|
||||
if (!user) return false
|
||||
|
||||
// Admin-only views
|
||||
if (view === 'user-management') {
|
||||
return user.roles.includes('admin')
|
||||
}
|
||||
|
||||
// All other views are accessible to authenticated users
|
||||
return true
|
||||
}
|
||||
|
||||
// Handle view change with persistence
|
||||
const handleViewChange = (view: string) => {
|
||||
if (validViews.includes(view) && hasAccessToView(view)) {
|
||||
setCurrentView(view)
|
||||
saveCurrentView(view)
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
fetchUserProfile()
|
||||
}, [])
|
||||
|
||||
// Restore saved view when user is loaded
|
||||
useEffect(() => {
|
||||
if (user) {
|
||||
const savedView = getSavedView()
|
||||
if (hasAccessToView(savedView)) {
|
||||
setCurrentView(savedView)
|
||||
} else {
|
||||
// If user doesn't have access to saved view, default to dashboard
|
||||
setCurrentView('dashboard')
|
||||
saveCurrentView('dashboard')
|
||||
}
|
||||
}
|
||||
}, [user])
|
||||
|
||||
const fetchUserProfile = async () => {
|
||||
try {
|
||||
setLoading(true)
|
||||
@@ -46,9 +105,8 @@ export function DashboardLayout({ onLogout }: DashboardLayoutProps) {
|
||||
}
|
||||
|
||||
const handleLogout = async () => {
|
||||
// Navigate to signout route which will handle the actual logout
|
||||
window.history.pushState({}, '', '/signout')
|
||||
window.dispatchEvent(new PopStateEvent('popstate'))
|
||||
// Call the logout function passed from parent
|
||||
onLogout()
|
||||
}
|
||||
|
||||
const toggleSidebar = () => {
|
||||
@@ -88,7 +146,7 @@ export function DashboardLayout({ onLogout }: DashboardLayoutProps) {
|
||||
)
|
||||
}
|
||||
case 'experiments':
|
||||
return <Experiments />
|
||||
return <ExperimentManagement />
|
||||
case 'analytics':
|
||||
return (
|
||||
<div className="p-6">
|
||||
@@ -104,6 +162,8 @@ export function DashboardLayout({ onLogout }: DashboardLayoutProps) {
|
||||
return <DataEntry />
|
||||
case 'vision-system':
|
||||
return <VisionSystem />
|
||||
case 'scheduling':
|
||||
return <Scheduling user={user} />
|
||||
case 'video-library':
|
||||
return <VideoStreamingPage />
|
||||
default:
|
||||
@@ -162,7 +222,7 @@ export function DashboardLayout({ onLogout }: DashboardLayoutProps) {
|
||||
<Sidebar
|
||||
user={user}
|
||||
currentView={currentView}
|
||||
onViewChange={setCurrentView}
|
||||
onViewChange={handleViewChange}
|
||||
isExpanded={isExpanded}
|
||||
isMobileOpen={isMobileOpen}
|
||||
isHovered={isHovered}
|
||||
|
||||
Reference in New Issue
Block a user