import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { useAuthStore } from '../stores/authStore'; import { authApi } from '../api/auth'; import { feedsApi } from '../api/feeds'; import { articlesApi } from '../api/articles'; import { AddFeedModal } from './AddFeedModal'; import { ThemeToggle } from './ThemeToggle'; interface SidebarProps { onSelectFeed: (feedId: string | null) => void; selectedFeedId: string | null; onEnterFocus: () => void; isFocusMode: boolean; } export function Sidebar({ onSelectFeed, selectedFeedId, onEnterFocus, isFocusMode }: SidebarProps) { const [isAddModalOpen, setIsAddModalOpen] = useState(false); const queryClient = useQueryClient(); const user = useAuthStore((s) => s.user); const logoutStore = useAuthStore((s) => s.logout); const { data: feeds } = useQuery({ queryKey: ['feeds'], queryFn: () => feedsApi.list() }); const markAllReadMutation = useMutation({ mutationFn: () => (selectedFeedId ? articlesApi.markAllRead(selectedFeedId) : articlesApi.markAllReadGlobal()), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['feeds'] }); queryClient.invalidateQueries({ queryKey: ['articles'] }); }, }); const deleteFeedMutation = useMutation({ mutationFn: (id: string) => feedsApi.delete(id), onSuccess: (_, deletedId) => { queryClient.invalidateQueries({ queryKey: ['feeds'] }); if (selectedFeedId === deletedId) onSelectFeed(null); }, }); const updateFeedMutation = useMutation({ mutationFn: ({ id, title }: { id: string; title: string }) => feedsApi.update(id, title), onSuccess: () => queryClient.invalidateQueries({ queryKey: ['feeds'] }), }); const logoutMutation = useMutation({ mutationFn: () => authApi.logout(), onSuccess: () => { logoutStore(); window.location.href = '/login'; }, }); const handleDelete = (e: React.MouseEvent, id: string) => { e.stopPropagation(); if (window.confirm('Voulez-vous vraiment supprimer ce flux ?')) deleteFeedMutation.mutate(id); }; const handleRename = (e: React.MouseEvent, id: string, currentTitle: string) => { e.stopPropagation(); const newTitle = window.prompt('Renommer le flux :', currentTitle); if (newTitle && newTitle.trim() !== '' && newTitle !== currentTitle) { updateFeedMutation.mutate({ id, title: newTitle.trim() }); } }; const navItem = (active: boolean) => `w-full flex items-center justify-between px-4 py-2.5 rounded-xl text-left transition-all duration-300 ${ active ? 'bg-nature/10 text-nature border border-nature/20' : 'text-paper-muted border border-transparent hover:text-nature hover:bg-nature/5' }`; const dot = (active: boolean) => `w-1.5 h-1.5 rounded-full bg-nature mr-3 transition-opacity ${active ? 'opacity-100' : 'opacity-0 group-hover/item:opacity-100'}`; return ( <> setIsAddModalOpen(false)} /> > ); }