Only display star button if project is favorited, refactor into useProjects hook

This commit is contained in:
Alex Gleason committed 2025-09-13 17:26:04 -05:00
1 parent bdcc030de6
commit b72baaeb48
4 files changed
+164 -59

No files matched your search

+9 -4
View File
@@ -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>
);