wip drafts showing in sidebar

This commit is contained in:
sam committed 2025-09-29 21:36:50 +07:00
1 parent 87cc79bda8
commit 8cf013e7e4
3 files changed
+82 -22

No files matched your search

+3
View File
@@ -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<string, unknown>;
return {
+13 -3
View File
@@ -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<ProjectItemProps> = ({ project, isSelected, onSelect, isFavorite }) => {
const ProjectItem: React.FC<ProjectItemProps> = ({ project, isSelected, onSelect, isFavorite, hasDraft }) => {
const navigate = useNavigate();
const { hasRunningSessions } = useProjectSessionStatus(project.id);
@@ -51,10 +53,16 @@ const ProjectItem: React.FC<ProjectItemProps> = ({ project, isSelected, onSelect
)}
</div>
<div className="flex justify-between gap-2 flex-1 min-w-0">
<div className="min-w-0 flex-1 flex items-center">
<div className="min-w-0 flex-1 flex items-center gap-2">
<h3 className="font-medium text-sm truncate">
{project.name}
</h3>
{/* Draft indicator - only shown when project has draft */}
{hasDraft && (
<div className="flex-shrink-0 pointer-events-none">
<Pencil className="h-3 w-3 text-sidebar-foreground opacity-60" />
</div>
)}
</div>
{/* 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<string[]>('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)}
/>
))}
</div>
+66 -19
View File
@@ -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<string, string>();
// 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<Map<string, string>>(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
};
}