Spaces:
Running
Running
File size: 6,074 Bytes
0dcbaee 874b177 de55c35 0dcbaee de55c35 94eb7aa de55c35 874b177 de55c35 94eb7aa 874b177 0dcbaee 874b177 94eb7aa 0dcbaee 94eb7aa 874b177 de55c35 0dcbaee de55c35 abb5ee1 de55c35 874b177 de55c35 385c360 874b177 385c360 0dcbaee 385c360 0dcbaee 385c360 874b177 385c360 0dcbaee 385c360 0dcbaee 874b177 385c360 874b177 0dcbaee 874b177 385c360 de55c35 385c360 de55c35 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { motion } from 'framer-motion';
import { useNavigate, useLocation } from 'react-router-dom';
import { useAuth } from '../context/AuthContext';
import { useTranslation } from 'react-i18next';
import { FREE_LIMITS, isPlus } from '../config/plans';
import {
LayoutDashboard,
PenTool,
Trophy,
Settings,
LogOut,
User
} from 'lucide-react';
const SidebarItem = ({ icon: Icon, label, active, onClick, badge }) => (
<motion.button
whileHover={{ x: 5 }}
whileTap={{ scale: 0.95 }}
onClick={onClick}
className={`w-full flex items-center justify-between px-6 py-4 rounded-xl transition-all group ${
active
? 'bg-primary text-on-primary rough-border shadow-[4px_4px_0px_0px_rgba(0,0,0,0.1)]'
: 'text-on-surface-variant hover:bg-surface-variant hover:text-primary'
}`}
>
<div className="flex items-center gap-4">
<Icon size={24} className={active ? 'text-secondary' : 'group-hover:text-primary'} />
<span className="text-xl font-bold font-display-lg tracking-wide">{label}</span>
</div>
{badge && (
<span className={`px-2 py-1 rounded-full text-xs font-bold ${active ? 'bg-secondary text-primary' : 'bg-primary/10 text-primary'}`}>
{badge}
</span>
)}
</motion.button>
);
let cachedProjectCount = null;
const Sidebar = () => {
const navigate = useNavigate();
const location = useLocation();
const { logout, user } = useAuth();
const { t } = useTranslation();
const [projectCount, setProjectCount] = useState(() => {
if (cachedProjectCount !== null) return cachedProjectCount;
const stored = localStorage.getItem('tf_project_count');
return stored ? parseInt(stored, 10) : 0;
});
const [ownedCount, setOwnedCount] = useState(0);
const userIsPlus = isPlus(user);
useEffect(() => {
if (!user) return;
const fetchProjectCount = async () => {
try {
const API_URL = import.meta.env.VITE_API_URL;
const res = await axios.get(`${API_URL}/graphs/user/${user.id}`);
if (Array.isArray(res.data)) {
const count = res.data.length;
setProjectCount(count);
setOwnedCount(res.data.filter(p => !p.isShared).length);
cachedProjectCount = count;
localStorage.setItem('tf_project_count', count.toString());
}
} catch (err) {
console.error('[Sidebar] Failed to fetch project count:', err);
}
};
fetchProjectCount();
const interval = setInterval(fetchProjectCount, 10000);
return () => clearInterval(interval);
}, [user]);
const handleLogout = () => {
logout();
navigate('/auth');
};
const menuItems = [
{ icon: LayoutDashboard, labelKey: 'projects', path: '/dashboard' },
{ icon: PenTool, labelKey: 'canvas', path: '/canvas' },
// Habits are hidden until they get their own space (see ROADMAP.md)
{ icon: User, labelKey: 'profile', path: '/profile' },
{ icon: Settings, labelKey: 'settings', path: '/settings' },
];
return (
<aside className="w-80 border-r-4 border-primary bg-surface-container-lowest p-6 flex flex-col gap-10 h-screen sticky top-0">
<div className="flex items-center gap-3 px-4 cursor-pointer" onClick={() => navigate('/')}>
<div className="w-12 h-12 bg-primary rounded-xl flex items-center justify-center text-on-primary">
<LayoutDashboard size={28} />
</div>
<span className="text-3xl font-display-lg text-primary">TaskFlow</span>
</div>
<nav className="flex flex-col gap-2 flex-grow">
<div className="px-4 mb-2 text-xs font-bold uppercase tracking-[0.2em] text-on-surface-variant/50">
{t('sidebar.main_menu', 'Main Menu')}
</div>
{menuItems.map((item) => (
<SidebarItem
key={item.labelKey}
icon={item.icon}
label={t(`sidebar.${item.labelKey}`)}
active={location.pathname === item.path}
onClick={() => navigate(item.path)}
badge={item.labelKey === 'projects' ? projectCount.toString() : null}
/>
))}
</nav>
<div className="flex flex-col gap-4">
<div className="p-4 bg-secondary/5 rough-border border-dashed border-secondary/30 flex flex-col gap-4">
<div className="flex flex-col gap-1">
<div className="flex items-center justify-between">
<span className="font-accent-note text-2xl text-secondary">
{userIsPlus ? 'Plus' : 'Free'}
</span>
{!userIsPlus && (
<span className="text-xs font-bold text-primary">
{t('sidebar.boards_used', { used: ownedCount, max: FREE_LIMITS.boards, defaultValue: `${ownedCount}/${FREE_LIMITS.boards} boards` })}
</span>
)}
</div>
<p className="text-[10px] text-on-surface-variant/70 font-semibold tracking-wide uppercase mt-1">
{userIsPlus
? t('sidebar.plus_active', 'No limits')
: t('sidebar.free_limits', { nodes: FREE_LIMITS.nodesPerBoard, defaultValue: `Up to ${FREE_LIMITS.nodesPerBoard} nodes per board` })}
</p>
</div>
{!userIsPlus && (
<button
onClick={() => navigate('/subscription')}
className="w-full py-2 bg-primary text-on-primary text-sm font-bold rounded-lg hover:bg-secondary hover:text-primary transition-all cursor-pointer"
>
{t('sidebar.get_plus', 'Get Plus')}
</button>
)}
</div>
<button
onClick={handleLogout}
className="w-full flex items-center gap-4 px-6 py-4 text-on-surface-variant hover:text-error transition-colors font-bold cursor-pointer"
>
<LogOut size={24} />
<span className="font-display-lg text-xl">{t('sidebar.logout')}</span>
</button>
</div>
</aside>
);
};
export default Sidebar;
|