mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
Only display star button if project is favorited, refactor into useProjects hook
This commit is contained in:
4 files changed
+164
-59
No files matched your search
@@ -1,12 +1,11 @@
|
||||
import React, { useState, memo } from 'react';
|
||||
import React from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { Plus, Folder, GitBranch, Settings, Loader2, ChevronDown } from 'lucide-react';
|
||||
import { Plus, Folder, GitBranch, Settings, Loader2, ChevronDown, Star } from 'lucide-react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from '@/components/ui/dropdown-menu';
|
||||
import { LoginArea } from '@/components/auth/LoginArea';
|
||||
import { StarButton } from '@/components/StarButton';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useProjects } from '@/hooks/useProjects';
|
||||
import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import type { Project } from '@/lib/ProjectsManager';
|
||||
@@ -16,9 +15,10 @@ interface ProjectItemProps {
|
||||
project: Project;
|
||||
isSelected: boolean;
|
||||
onSelect: (project: Project) => void;
|
||||
isFavorite?: boolean;
|
||||
}
|
||||
|
||||
const ProjectItem = memo(({ project, isSelected, onSelect }: ProjectItemProps) => {
|
||||
const ProjectItem: React.FC<ProjectItemProps> = ({ project, isSelected, onSelect, isFavorite }) => {
|
||||
const navigate = useNavigate();
|
||||
const { hasRunningSessions } = useProjectSessionStatus(project.id);
|
||||
|
||||
@@ -54,21 +54,20 @@ const ProjectItem = memo(({ project, isSelected, onSelect }: ProjectItemProps) =
|
||||
</h3>
|
||||
</div>
|
||||
|
||||
{/* Star Button */}
|
||||
<div className="flex-shrink-0 relative -mr-2">
|
||||
<StarButton
|
||||
projectId={project.id}
|
||||
projectName={project.name}
|
||||
showToast={false}
|
||||
className="h-6 w-6 hover:bg-primary/20"
|
||||
/>
|
||||
</div>
|
||||
{/* Star indicator - only shown when favorited */}
|
||||
{isFavorite && (
|
||||
<div className="flex-shrink-0 relative -mr-2">
|
||||
<div className="h-6 w-6 flex items-center justify-center">
|
||||
<Star className="h-4 w-4 text-yellow-500 fill-current" />
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
ProjectItem.displayName = 'ProjectItem';
|
||||
|
||||
@@ -83,46 +82,19 @@ export function ProjectSidebar({
|
||||
onSelectProject,
|
||||
className
|
||||
}: ProjectSidebarProps) {
|
||||
const [projects, setProjects] = useState<Project[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const { data: projects = [], isLoading, error } = useProjects();
|
||||
const [favorites] = useLocalStorage<string[]>('project-favorites', []);
|
||||
const projectsManager = useProjectsManager();
|
||||
|
||||
const navigate = useNavigate();
|
||||
|
||||
// Load projects on mount
|
||||
React.useEffect(() => {
|
||||
const loadProjects = async () => {
|
||||
try {
|
||||
await projectsManager.init();
|
||||
const projectList = await projectsManager.getProjects();
|
||||
setProjects(projectList);
|
||||
} catch (error) {
|
||||
console.error('Failed to load projects:', error);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadProjects();
|
||||
}, [projectsManager]);
|
||||
|
||||
const handleNewProject = () => {
|
||||
onSelectProject(null);
|
||||
navigate('/');
|
||||
};
|
||||
|
||||
const isFavorite = (projectId: string) => favorites.includes(projectId);
|
||||
|
||||
// Sort projects: favorites first, then by lastModified (newest first)
|
||||
const sortedProjects = projects.sort((a, b) => {
|
||||
const aIsFavorite = isFavorite(a.id);
|
||||
const bIsFavorite = isFavorite(b.id);
|
||||
|
||||
if (aIsFavorite && !bIsFavorite) return -1;
|
||||
if (!aIsFavorite && bIsFavorite) return 1;
|
||||
|
||||
return b.lastModified.getTime() - a.lastModified.getTime();
|
||||
});
|
||||
if (error) {
|
||||
console.error('Failed to load projects:', error);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className={cn("flex flex-col h-full bg-gradient-to-b from-sidebar to-sidebar/95", className)}>
|
||||
@@ -203,14 +175,15 @@ export function ProjectSidebar({
|
||||
</div>
|
||||
) : (
|
||||
<div className="space-y-1">
|
||||
{sortedProjects.map((project) => (
|
||||
<ProjectItem
|
||||
key={project.id}
|
||||
project={project}
|
||||
isSelected={selectedProject?.id === project.id}
|
||||
onSelect={onSelectProject}
|
||||
/>
|
||||
))}
|
||||
{projects.map((project) => (
|
||||
<ProjectItem
|
||||
key={project.id}
|
||||
project={project}
|
||||
isSelected={selectedProject?.id === project.id}
|
||||
onSelect={onSelectProject}
|
||||
isFavorite={favorites.includes(project.id)}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Button } from '@/components/ui/button';
|
||||
import { Star } from 'lucide-react';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import { useToast } from '@/hooks/useToast';
|
||||
import { useQueryClient } from '@tanstack/react-query';
|
||||
import { cn } from '@/lib/utils';
|
||||
|
||||
interface StarButtonProps {
|
||||
@@ -23,18 +24,22 @@ export function StarButton({
|
||||
}: StarButtonProps) {
|
||||
const [favorites, setFavorites] = useLocalStorage<string[]>('project-favorites', []);
|
||||
const { toast } = useToast();
|
||||
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const isFavorite = favorites.includes(projectId);
|
||||
|
||||
const handleToggleFavorite = (e: React.MouseEvent) => {
|
||||
e.stopPropagation(); // Prevent triggering parent click events
|
||||
|
||||
|
||||
const newFavorites = isFavorite
|
||||
? favorites.filter(id => id !== projectId)
|
||||
: [...favorites, projectId];
|
||||
|
||||
setFavorites(newFavorites);
|
||||
|
||||
// Invalidate the projects query to trigger a refetch with new sorting
|
||||
queryClient.invalidateQueries({ queryKey: ['projects'] });
|
||||
|
||||
if (showToast) {
|
||||
toast({
|
||||
title: isFavorite ? "Project Unfavorited" : "Project Favorited",
|
||||
@@ -54,11 +59,11 @@ export function StarButton({
|
||||
)}
|
||||
aria-label={isFavorite ? "Remove from favorites" : "Add to favorites"}
|
||||
>
|
||||
<Star
|
||||
<Star
|
||||
className={cn(
|
||||
"h-4 w-4",
|
||||
isFavorite ? "text-yellow-500 fill-current" : "text-muted-foreground hover:text-yellow-500"
|
||||
)}
|
||||
)}
|
||||
/>
|
||||
</Button>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { TestApp } from '@/test/TestApp';
|
||||
import { useProjects } from './useProjects';
|
||||
import type { Project } from '@/lib/ProjectsManager';
|
||||
|
||||
// Mock the useProjectsManager hook
|
||||
vi.mock('./useProjectsManager', () => ({
|
||||
useProjectsManager: () => ({
|
||||
init: vi.fn().mockResolvedValue(undefined),
|
||||
getProjects: vi.fn().mockResolvedValue([
|
||||
{
|
||||
id: 'project-1',
|
||||
name: 'Project 1',
|
||||
lastModified: new Date('2023-01-01'),
|
||||
},
|
||||
{
|
||||
id: 'project-2',
|
||||
name: 'Project 2',
|
||||
lastModified: new Date('2023-01-02'),
|
||||
},
|
||||
{
|
||||
id: 'project-3',
|
||||
name: 'Project 3',
|
||||
lastModified: new Date('2023-01-03'),
|
||||
},
|
||||
] as Project[]),
|
||||
}),
|
||||
}));
|
||||
|
||||
// Mock localStorage for favorites
|
||||
const mockLocalStorage = {
|
||||
getItem: vi.fn(),
|
||||
setItem: vi.fn(),
|
||||
removeItem: vi.fn(),
|
||||
clear: vi.fn(),
|
||||
};
|
||||
|
||||
Object.defineProperty(window, 'localStorage', {
|
||||
value: mockLocalStorage,
|
||||
});
|
||||
|
||||
describe('useProjects', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mockLocalStorage.getItem.mockReturnValue('[]');
|
||||
});
|
||||
|
||||
it('should load and return projects', async () => {
|
||||
const { result } = renderHook(() => useProjects(), {
|
||||
wrapper: TestApp,
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
});
|
||||
|
||||
expect(result.current.data).toHaveLength(3);
|
||||
expect(result.current.data?.[0].name).toBe('Project 3'); // Most recent first
|
||||
});
|
||||
|
||||
it('should sort favorites first, then by lastModified', async () => {
|
||||
// Mock favorites to include project-1
|
||||
mockLocalStorage.getItem.mockReturnValue('["project-1"]');
|
||||
|
||||
const { result } = renderHook(() => useProjects(), {
|
||||
wrapper: TestApp,
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
});
|
||||
|
||||
expect(result.current.data).toHaveLength(3);
|
||||
expect(result.current.data?.[0].id).toBe('project-1'); // Favorite first
|
||||
expect(result.current.data?.[1].id).toBe('project-3'); // Then most recent
|
||||
expect(result.current.data?.[2].id).toBe('project-2'); // Then older
|
||||
});
|
||||
|
||||
it('should handle multiple favorites correctly', async () => {
|
||||
// Mock favorites to include project-1 and project-2
|
||||
mockLocalStorage.getItem.mockReturnValue('["project-1", "project-2"]');
|
||||
|
||||
const { result } = renderHook(() => useProjects(), {
|
||||
wrapper: TestApp,
|
||||
});
|
||||
|
||||
await waitFor(() => {
|
||||
expect(result.current.isLoading).toBe(false);
|
||||
});
|
||||
|
||||
expect(result.current.data).toHaveLength(3);
|
||||
// Favorites should be sorted by lastModified among themselves
|
||||
expect(result.current.data?.[0].id).toBe('project-2'); // Favorite + more recent
|
||||
expect(result.current.data?.[1].id).toBe('project-1'); // Favorite + older
|
||||
expect(result.current.data?.[2].id).toBe('project-3'); // Not favorite
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
import { useProjectsManager } from '@/hooks/useProjectsManager';
|
||||
import { useLocalStorage } from '@/hooks/useLocalStorage';
|
||||
import type { Project } from '@/lib/ProjectsManager';
|
||||
|
||||
export function useProjects() {
|
||||
const projectsManager = useProjectsManager();
|
||||
const [favorites] = useLocalStorage<string[]>('project-favorites', []);
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['projects'],
|
||||
queryFn: async (): Promise<Project[]> => {
|
||||
await projectsManager.init();
|
||||
const projects = await projectsManager.getProjects();
|
||||
|
||||
// Sort projects: favorites first, then by lastModified (newest first)
|
||||
return projects.sort((a, b) => {
|
||||
const aIsFavorite = favorites.includes(a.id);
|
||||
const bIsFavorite = favorites.includes(b.id);
|
||||
|
||||
if (aIsFavorite && !bIsFavorite) return -1;
|
||||
if (!aIsFavorite && bIsFavorite) return 1;
|
||||
|
||||
return b.lastModified.getTime() - a.lastModified.getTime();
|
||||
});
|
||||
},
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user