Spaces:
Runtime error
Runtime error
Update components/Sidebar.tsx
Browse files- components/Sidebar.tsx +15 -8
components/Sidebar.tsx
CHANGED
|
@@ -25,7 +25,8 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 25 |
const canSeeAI = userRole === UserRole.ADMIN || (userRole === UserRole.TEACHER && currentUser?.aiAccess);
|
| 26 |
const isHomeroom = userRole === UserRole.TEACHER && !!currentUser?.homeroomClass;
|
| 27 |
|
| 28 |
-
// Default Items
|
|
|
|
| 29 |
const defaultItems: MenuItem[] = [
|
| 30 |
{ id: 'dashboard', label: '工作台', icon: LayoutDashboard, roles: [UserRole.ADMIN, UserRole.PRINCIPAL, UserRole.TEACHER, UserRole.STUDENT] },
|
| 31 |
{ id: 'my-class', label: '我的班级', icon: UserCheck, roles: isHomeroom ? [UserRole.TEACHER] : [] },
|
|
@@ -48,10 +49,11 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 48 |
const [menuItems, setMenuItems] = useState<MenuItem[]>(defaultItems);
|
| 49 |
const [isEditing, setIsEditing] = useState(false);
|
| 50 |
|
|
|
|
| 51 |
useEffect(() => {
|
| 52 |
-
|
|
|
|
| 53 |
if (currentUser?.menuOrder && currentUser.menuOrder.length > 0) {
|
| 54 |
-
const ordered: MenuItem[] = [];
|
| 55 |
const map = new Map(defaultItems.map(i => [i.id, i]));
|
| 56 |
// Add saved items in order
|
| 57 |
currentUser.menuOrder.forEach(id => {
|
|
@@ -60,11 +62,13 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 60 |
map.delete(id);
|
| 61 |
}
|
| 62 |
});
|
| 63 |
-
// Append any new/remaining items
|
| 64 |
map.forEach(item => ordered.push(item));
|
| 65 |
-
|
|
|
|
| 66 |
}
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
const handleMove = (index: number, direction: -1 | 1) => {
|
| 70 |
const newItems = [...menuItems];
|
|
@@ -83,6 +87,7 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 83 |
// Update local user object
|
| 84 |
const updatedUser = { ...currentUser, menuOrder: orderIds };
|
| 85 |
localStorage.setItem('user', JSON.stringify(updatedUser));
|
|
|
|
| 86 |
} catch(e) { console.error("Failed to save menu order"); }
|
| 87 |
}
|
| 88 |
};
|
|
@@ -120,9 +125,11 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 120 |
</div>
|
| 121 |
<nav className="space-y-1 px-2">
|
| 122 |
{menuItems.map((item, idx) => {
|
|
|
|
| 123 |
if (item.roles.length > 0 && !item.roles.includes(userRole)) return null;
|
|
|
|
|
|
|
| 124 |
if (item.id === 'ai-assistant' && !canSeeAI) return null;
|
| 125 |
-
// Special check for 'my-class': only homeroom teachers
|
| 126 |
if (item.id === 'my-class' && !isHomeroom) return null;
|
| 127 |
|
| 128 |
const Icon = item.icon;
|
|
@@ -171,4 +178,4 @@ export const Sidebar: React.FC<SidebarProps> = ({ currentView, onChangeView, use
|
|
| 171 |
</div>
|
| 172 |
</>
|
| 173 |
);
|
| 174 |
-
};
|
|
|
|
| 25 |
const canSeeAI = userRole === UserRole.ADMIN || (userRole === UserRole.TEACHER && currentUser?.aiAccess);
|
| 26 |
const isHomeroom = userRole === UserRole.TEACHER && !!currentUser?.homeroomClass;
|
| 27 |
|
| 28 |
+
// Default Items Definition
|
| 29 |
+
// We define this inside render or memoize it, but it needs to react to isHomeroom
|
| 30 |
const defaultItems: MenuItem[] = [
|
| 31 |
{ id: 'dashboard', label: '工作台', icon: LayoutDashboard, roles: [UserRole.ADMIN, UserRole.PRINCIPAL, UserRole.TEACHER, UserRole.STUDENT] },
|
| 32 |
{ id: 'my-class', label: '我的班级', icon: UserCheck, roles: isHomeroom ? [UserRole.TEACHER] : [] },
|
|
|
|
| 49 |
const [menuItems, setMenuItems] = useState<MenuItem[]>(defaultItems);
|
| 50 |
const [isEditing, setIsEditing] = useState(false);
|
| 51 |
|
| 52 |
+
// Sync menu items when currentUser (and thus isHomeroom/permissions) changes
|
| 53 |
useEffect(() => {
|
| 54 |
+
let ordered: MenuItem[] = [];
|
| 55 |
+
|
| 56 |
if (currentUser?.menuOrder && currentUser.menuOrder.length > 0) {
|
|
|
|
| 57 |
const map = new Map(defaultItems.map(i => [i.id, i]));
|
| 58 |
// Add saved items in order
|
| 59 |
currentUser.menuOrder.forEach(id => {
|
|
|
|
| 62 |
map.delete(id);
|
| 63 |
}
|
| 64 |
});
|
| 65 |
+
// Append any new/remaining items (crucial for new features like 'my-class')
|
| 66 |
map.forEach(item => ordered.push(item));
|
| 67 |
+
} else {
|
| 68 |
+
ordered = defaultItems;
|
| 69 |
}
|
| 70 |
+
setMenuItems(ordered);
|
| 71 |
+
}, [currentUser?.menuOrder, isHomeroom, canSeeAI]); // Re-run if order, homeroom status, or AI access changes
|
| 72 |
|
| 73 |
const handleMove = (index: number, direction: -1 | 1) => {
|
| 74 |
const newItems = [...menuItems];
|
|
|
|
| 87 |
// Update local user object
|
| 88 |
const updatedUser = { ...currentUser, menuOrder: orderIds };
|
| 89 |
localStorage.setItem('user', JSON.stringify(updatedUser));
|
| 90 |
+
// Force update logic (though App.tsx usually handles user state, this helps local consistency)
|
| 91 |
} catch(e) { console.error("Failed to save menu order"); }
|
| 92 |
}
|
| 93 |
};
|
|
|
|
| 125 |
</div>
|
| 126 |
<nav className="space-y-1 px-2">
|
| 127 |
{menuItems.map((item, idx) => {
|
| 128 |
+
// Filtering Logic
|
| 129 |
if (item.roles.length > 0 && !item.roles.includes(userRole)) return null;
|
| 130 |
+
|
| 131 |
+
// Special feature toggles
|
| 132 |
if (item.id === 'ai-assistant' && !canSeeAI) return null;
|
|
|
|
| 133 |
if (item.id === 'my-class' && !isHomeroom) return null;
|
| 134 |
|
| 135 |
const Icon = item.icon;
|
|
|
|
| 178 |
</div>
|
| 179 |
</>
|
| 180 |
);
|
| 181 |
+
};
|