BatchProjectModal.tsx 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { useEffect, useMemo, useState } from 'react';
  2. import { useTranslation } from 'react-i18next';
  3. import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
  4. import { X, FolderKanban, Loader2, XCircle, Search } from 'lucide-react';
  5. import { api } from '../api/client';
  6. import { Card, CardContent } from './Card';
  7. import { Button } from './Button';
  8. import { useToast } from '../contexts/ToastContext';
  9. import { invalidateArchiveAndProjectViews } from '../utils/projectQueries';
  10. interface BatchProjectModalProps {
  11. selectedIds: number[];
  12. onClose: () => void;
  13. }
  14. export function BatchProjectModal({ selectedIds, onClose }: BatchProjectModalProps) {
  15. const { t } = useTranslation();
  16. const queryClient = useQueryClient();
  17. const { showToast } = useToast();
  18. const [query, setQuery] = useState('');
  19. const { data: projects, isLoading } = useQuery({
  20. queryKey: ['projects'],
  21. queryFn: () => api.getProjects(),
  22. });
  23. const sortedProjects = useMemo(
  24. () => (projects ? [...projects].sort((a, b) => a.name.localeCompare(b.name)) : undefined),
  25. [projects],
  26. );
  27. const trimmed = query.trim().toLowerCase();
  28. const visibleProjects = trimmed
  29. ? sortedProjects?.filter((p) => p.name.toLowerCase().includes(trimmed))
  30. : sortedProjects;
  31. const showSearch = (sortedProjects?.length ?? 0) > 5;
  32. // Close on Escape key
  33. useEffect(() => {
  34. const handleKeyDown = (e: KeyboardEvent) => {
  35. if (e.key === 'Escape') onClose();
  36. };
  37. window.addEventListener('keydown', handleKeyDown);
  38. return () => window.removeEventListener('keydown', handleKeyDown);
  39. }, [onClose]);
  40. // Helper to invalidate all project-related queries. The shared version also
  41. // covers the timeline and file-progress views, which this list was missing.
  42. const invalidateProjectQueries = () => invalidateArchiveAndProjectViews(queryClient);
  43. // Assign to project mutation (uses bulk API)
  44. const assignMutation = useMutation({
  45. mutationFn: async (projectId: number) => {
  46. await api.addArchivesToProject(projectId, selectedIds);
  47. return projectId;
  48. },
  49. onSuccess: (projectId) => {
  50. const project = projects?.find(p => p.id === projectId);
  51. invalidateProjectQueries();
  52. showToast(`Added ${selectedIds.length} archive${selectedIds.length !== 1 ? 's' : ''} to "${project?.name}"`);
  53. onClose();
  54. },
  55. onError: () => {
  56. showToast('Failed to assign project', 'error');
  57. },
  58. });
  59. // Remove from project mutation (updates each archive individually)
  60. const removeMutation = useMutation({
  61. mutationFn: async () => {
  62. for (const id of selectedIds) {
  63. await api.updateArchive(id, { project_id: null });
  64. }
  65. return selectedIds.length;
  66. },
  67. onSuccess: (count) => {
  68. invalidateProjectQueries();
  69. showToast(`Removed ${count} archive${count !== 1 ? 's' : ''} from project`);
  70. onClose();
  71. },
  72. onError: () => {
  73. showToast('Failed to remove from project', 'error');
  74. },
  75. });
  76. const isPending = assignMutation.isPending || removeMutation.isPending;
  77. return (
  78. <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4">
  79. <Card className="w-full max-w-md max-h-[80vh] flex flex-col">
  80. <CardContent className="p-0 flex flex-col min-h-0">
  81. {/* Header */}
  82. <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary shrink-0">
  83. <div className="flex items-center gap-2">
  84. <FolderKanban className="w-5 h-5 text-bambu-green" />
  85. <h2 className="text-xl font-semibold text-white">
  86. Assign to Project
  87. </h2>
  88. </div>
  89. <button
  90. onClick={onClose}
  91. className="text-bambu-gray hover:text-white transition-colors"
  92. disabled={isPending}
  93. >
  94. <X className="w-5 h-5" />
  95. </button>
  96. </div>
  97. {/* Content */}
  98. <div className="p-4 space-y-3 overflow-y-auto min-h-0">
  99. <p className="text-sm text-bambu-gray">
  100. Assign {selectedIds.length} selected archive{selectedIds.length !== 1 ? 's' : ''} to a project
  101. </p>
  102. {isLoading ? (
  103. <div className="flex items-center justify-center py-8">
  104. <Loader2 className="w-6 h-6 animate-spin text-bambu-gray" />
  105. </div>
  106. ) : (
  107. <div className="space-y-2">
  108. {/* Remove from project option */}
  109. <button
  110. onClick={() => removeMutation.mutate()}
  111. disabled={isPending}
  112. className="w-full flex items-center gap-3 p-3 rounded-lg bg-bambu-dark hover:bg-bambu-dark-tertiary border border-bambu-dark-tertiary transition-colors text-left disabled:opacity-50"
  113. >
  114. <div className="w-8 h-8 rounded-full bg-red-500/20 flex items-center justify-center shrink-0">
  115. <XCircle className="w-4 h-4 text-red-600 dark:text-red-400" />
  116. </div>
  117. <div className="min-w-0 flex-1">
  118. <p className="text-white font-medium">Remove from project</p>
  119. <p className="text-sm text-bambu-gray truncate">Clear project assignment</p>
  120. </div>
  121. {removeMutation.isPending && (
  122. <Loader2 className="w-4 h-4 animate-spin text-bambu-gray shrink-0" />
  123. )}
  124. </button>
  125. {/* Divider */}
  126. {sortedProjects && sortedProjects.length > 0 && (
  127. <div className="flex items-center gap-2 py-2">
  128. <div className="flex-1 h-px bg-bambu-dark-tertiary" />
  129. <span className="text-xs text-bambu-gray">or assign to</span>
  130. <div className="flex-1 h-px bg-bambu-dark-tertiary" />
  131. </div>
  132. )}
  133. {/* Search input */}
  134. {showSearch && (
  135. <div className="relative">
  136. <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-bambu-gray pointer-events-none" />
  137. <input
  138. type="text"
  139. value={query}
  140. onChange={(e) => setQuery(e.target.value)}
  141. placeholder={t('archives.menu.searchProjects')}
  142. className="w-full pl-9 pr-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white placeholder-bambu-gray text-sm focus:border-bambu-green focus:outline-none"
  143. />
  144. </div>
  145. )}
  146. {/* Project list */}
  147. {visibleProjects?.map((project) => (
  148. <button
  149. key={project.id}
  150. onClick={() => assignMutation.mutate(project.id)}
  151. disabled={isPending}
  152. className="w-full flex items-center gap-3 p-3 rounded-lg bg-bambu-dark hover:bg-bambu-dark-tertiary border border-bambu-dark-tertiary transition-colors text-left disabled:opacity-50"
  153. >
  154. <div
  155. className="w-8 h-8 rounded-full flex items-center justify-center shrink-0"
  156. style={{ backgroundColor: project.color ? `${project.color}20` : 'rgb(var(--bambu-green) / 0.2)' }}
  157. >
  158. <FolderKanban
  159. className="w-4 h-4"
  160. style={{ color: project.color || 'rgb(var(--bambu-green))' }}
  161. />
  162. </div>
  163. <div className="min-w-0 flex-1">
  164. <p className="text-white font-medium truncate">{project.name}</p>
  165. <p className="text-sm text-bambu-gray truncate">
  166. {project.archive_count} archive{project.archive_count !== 1 ? 's' : ''}
  167. {project.status && ` • ${project.status}`}
  168. </p>
  169. </div>
  170. {assignMutation.isPending && assignMutation.variables === project.id && (
  171. <Loader2 className="w-4 h-4 animate-spin text-bambu-gray shrink-0" />
  172. )}
  173. </button>
  174. ))}
  175. {(!sortedProjects || sortedProjects.length === 0) && (
  176. <p className="text-center text-bambu-gray py-4">
  177. No projects yet. Create one from the Projects page.
  178. </p>
  179. )}
  180. {sortedProjects && sortedProjects.length > 0 && visibleProjects?.length === 0 && (
  181. <p className="text-center text-bambu-gray text-sm py-4">—</p>
  182. )}
  183. </div>
  184. )}
  185. </div>
  186. {/* Footer */}
  187. <div className="flex gap-3 p-4 border-t border-bambu-dark-tertiary shrink-0">
  188. <Button variant="secondary" onClick={onClose} className="flex-1" disabled={isPending}>
  189. Cancel
  190. </Button>
  191. </div>
  192. </CardContent>
  193. </Card>
  194. </div>
  195. );
  196. }