@@ -5,35 +5,52 @@ import { Search, Plus, Check, X, Pencil, Trash } from 'lucide-react'
55import { Input } from '@/components/ui/input'
66import { Button } from '@/components/ui/button'
77import { UserProfile } from './UserProfile'
8- import { getCategories , addCategory , updateCategory , deleteCategory } from '@/lib/client/api'
8+ import {
9+ getCategories ,
10+ addCategory ,
11+ updateCategory ,
12+ deleteCategory ,
13+ getTaskCategoryCount ,
14+ } from '@/lib/client/api'
915import type { Category , Task } from '@/lib/types'
10- import { useAuth } from '@/context/AuthContext'
1116
1217interface SidebarProps {
1318 onSelectCategory : ( category : Category ) => void
1419 selectedCategory : Category | null
1520 tasks : Task [ ]
1621}
1722
18- export function Sidebar ( { onSelectCategory, selectedCategory, tasks } : SidebarProps ) {
23+ export function Sidebar ( { onSelectCategory, selectedCategory, tasks } : Readonly < SidebarProps > ) {
1924 const [ categories , setCategories ] = useState < Category [ ] > ( [ ] )
2025 const [ searchTerm , setSearchTerm ] = useState ( '' )
2126 const [ newCategory , setNewCategory ] = useState ( '' )
22- const [ editingId , setEditingId ] = useState < string | null > ( null )
2327 const [ editingName , setEditingName ] = useState ( '' )
2428 const [ isLoading , setIsLoading ] = useState ( true )
2529 const [ editingCategoryId , setEditingCategoryId ] = useState < string | null > ( null )
26- const { user } = useAuth ( )
30+ const [ stats , setStats ] = useState < { category : string ; count : number } [ ] > ( [ ] )
2731
2832 useEffect ( ( ) => {
2933 fetchCategories ( )
34+ fetchTasksCount ( )
3035 } , [ ] )
3136
37+ useEffect ( ( ) => {
38+ fetchTasksCount ( )
39+ } , [ tasks ] )
40+
41+ const fetchTasksCount = async ( ) => {
42+ const fetchedTasks = await getTaskCategoryCount ( )
43+ setStats ( fetchedTasks . stats )
44+ }
45+
3246 const fetchCategories = async ( ) => {
3347 setIsLoading ( true )
3448 try {
3549 const fetchedCategories = await getCategories ( )
3650 setCategories ( fetchedCategories )
51+ if ( fetchedCategories . length > 0 ) {
52+ onSelectCategory ( fetchedCategories [ 0 ] )
53+ }
3754 } catch ( error ) {
3855 console . error ( 'Failed to fetch categories:' , error )
3956 } finally {
@@ -76,11 +93,11 @@ export function Sidebar({ onSelectCategory, selectedCategory, tasks }: SidebarPr
7693 if ( categoryId === 'all' ) {
7794 return tasks . length
7895 }
79- return tasks . filter ( ( task ) => task . categoryId === categoryId ) . length
96+ return stats . find ( ( stat ) => stat . category === categoryId ) ?. count ?? 0
8097 }
8198
8299 return (
83- < div className = "w-64 border-r h-screen flex flex-col bg-white" >
100+ < div className = "w-80 border-r h-screen flex flex-col bg-white" >
84101 < UserProfile />
85102
86103 < div className = "p-4" >
0 commit comments