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>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user