BatchProjectModal.tsx 9.1 KB

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