mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +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
@@ -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