From 8cf013e7e4561aa10ff679b5be8d79c17506dbc2 Mon Sep 17 00:00:00 2001 From: sam Date: Mon, 29 Sep 2025 21:36:50 +0700 Subject: [PATCH] wip drafts showing in sidebar --- src/components/ProjectSidebar.test.tsx | 3 + src/components/ProjectSidebar.tsx | 16 ++++- src/hooks/useDraftMessages.ts | 85 ++++++++++++++++++++------ 3 files changed, 82 insertions(+), 22 deletions(-) diff --git a/src/components/ProjectSidebar.test.tsx b/src/components/ProjectSidebar.test.tsx index c78e4ccb..9b74a453 100644 --- a/src/components/ProjectSidebar.test.tsx +++ b/src/components/ProjectSidebar.test.tsx @@ -11,6 +11,9 @@ vi.mock('@/hooks/useLocalStorage'); vi.mock('@/hooks/useProjectSessionStatus', () => ({ useProjectSessionStatus: () => ({ hasRunningSessions: false }) })); +vi.mock('@/hooks/useDraftMessages', () => ({ + useDraftMessages: () => ({ hasDraft: () => false }) +})); vi.mock('react-router-dom', async (importOriginal) => { const actual = await importOriginal() as Record; return { diff --git a/src/components/ProjectSidebar.tsx b/src/components/ProjectSidebar.tsx index ec800bc0..85ff4097 100644 --- a/src/components/ProjectSidebar.tsx +++ b/src/components/ProjectSidebar.tsx @@ -1,13 +1,14 @@ import React, { useEffect } from 'react'; import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; -import { Plus, Folder, GitBranch, Loader2, ChevronDown, Star, Columns2, X, Settings, HelpCircle } from 'lucide-react'; +import { Plus, Folder, GitBranch, Loader2, ChevronDown, Star, Columns2, X, Settings, HelpCircle, Pencil } 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 { useProjects } from '@/hooks/useProjects'; import { useProjectSessionStatus } from '@/hooks/useProjectSessionStatus'; import { useLocalStorage } from '@/hooks/useLocalStorage'; +import { useDraftMessages } from '@/hooks/useDraftMessages'; import { useIsMobile } from '@/hooks/useIsMobile'; import type { Project } from '@/lib/ProjectsManager'; import { cn } from '@/lib/utils'; @@ -19,9 +20,10 @@ interface ProjectItemProps { isSelected: boolean; onSelect: (project: Project) => void; isFavorite?: boolean; + hasDraft?: boolean; } -const ProjectItem: React.FC = ({ project, isSelected, onSelect, isFavorite }) => { +const ProjectItem: React.FC = ({ project, isSelected, onSelect, isFavorite, hasDraft }) => { const navigate = useNavigate(); const { hasRunningSessions } = useProjectSessionStatus(project.id); @@ -51,10 +53,16 @@ const ProjectItem: React.FC = ({ project, isSelected, onSelect )}
-
+

{project.name}

+ {/* Draft indicator - only shown when project has draft */} + {hasDraft && ( +
+ +
+ )}
{/* Star indicator - only shown when favorited */} @@ -93,6 +101,7 @@ export function ProjectSidebar({ const queryClient = useQueryClient(); const { data: projects = [], isLoading, error } = useProjects(); const [favorites] = useLocalStorage('project-favorites', []); + const { hasDraft } = useDraftMessages(); // Use without projectId for global access const isMobile = useIsMobile(); const navigate = useNavigate(); @@ -221,6 +230,7 @@ export function ProjectSidebar({ isSelected={selectedProject?.id === project.id} onSelect={onSelectProject} isFavorite={favorites.includes(project.id)} + hasDraft={hasDraft(project.id)} /> ))}
diff --git a/src/hooks/useDraftMessages.ts b/src/hooks/useDraftMessages.ts index c5429585..e64e25bd 100644 --- a/src/hooks/useDraftMessages.ts +++ b/src/hooks/useDraftMessages.ts @@ -1,44 +1,91 @@ -import { useCallback, useRef } from 'react'; +import { useCallback, useState, useEffect } from 'react'; + +// Global drafts storage shared across all hook instances +const globalDrafts = new Map(); + +// Global listeners for draft changes +const listeners = new Set<() => void>(); + +// Notify all listeners when drafts change +const notifyListeners = () => { + listeners.forEach(listener => listener()); +}; /** * Hook to manage draft messages per project. - * Drafts are stored in memory and persist when switching between projects. + * Drafts are stored in global memory and persist when switching between projects. * Drafts are cleared when messages are sent or queued. + * + * Can be used in two ways: + * 1. With projectId - for managing drafts for a specific project + * 2. Without projectId - for accessing global draft functionality (checking any project's drafts) */ -export function useDraftMessages(projectId: string) { - // Use ref to store drafts for all projects to persist across re-renders - const draftsRef = useRef>(new Map()); +export function useDraftMessages(projectId?: string) { + // Use state to trigger re-renders when drafts change + const [, forceUpdate] = useState(0); - // Get the current draft for this project - const getDraft = useCallback((): string => { - return draftsRef.current.get(projectId) || ''; + // Register listener for draft changes + useEffect(() => { + const listener = () => forceUpdate(prev => prev + 1); + listeners.add(listener); + return () => listeners.delete(listener); + }, []); + + // Get draft for a specific project (or current project if projectId provided) + const getDraft = useCallback((targetProjectId?: string): string => { + const id = targetProjectId || projectId; + if (!id) return ''; + return globalDrafts.get(id) || ''; }, [projectId]); - // Save draft for this project - const saveDraft = useCallback((content: string) => { + // Save draft for a specific project (or current project if projectId provided) + const saveDraft = useCallback((content: string, targetProjectId?: string) => { + const id = targetProjectId || projectId; + if (!id) return; + if (content.trim()) { - draftsRef.current.set(projectId, content); + globalDrafts.set(id, content); } else { // Clear draft if content is empty - draftsRef.current.delete(projectId); + globalDrafts.delete(id); } + notifyListeners(); }, [projectId]); - // Clear draft for this project - const clearDraft = useCallback(() => { - draftsRef.current.delete(projectId); + // Clear draft for a specific project (or current project if projectId provided) + const clearDraft = useCallback((targetProjectId?: string) => { + const id = targetProjectId || projectId; + if (!id) return; + + globalDrafts.delete(id); + notifyListeners(); }, [projectId]); - // Check if this project has a draft - const hasDraft = useCallback((): boolean => { - const draft = draftsRef.current.get(projectId); + // Check if a specific project has a draft (or current project if projectId provided) + const hasDraft = useCallback((targetProjectId?: string): boolean => { + const id = targetProjectId || projectId; + if (!id) return false; + + const draft = globalDrafts.get(id); return Boolean(draft && draft.trim()); }, [projectId]); + // Get all projects that have drafts (global functionality) + const getProjectsWithDrafts = useCallback((): string[] => { + const projectsWithDrafts: string[] = []; + for (const [id, draft] of globalDrafts.entries()) { + if (draft && draft.trim()) { + projectsWithDrafts.push(id); + } + } + return projectsWithDrafts; + }, []); + return { getDraft, saveDraft, clearDraft, - hasDraft + hasDraft, + getProjectsWithDrafts }; }