Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | import { useState, useEffect } from 'react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { X, Tag, Plus, Loader2 } from 'lucide-react'; import { api } from '../api/client'; import { Card, CardContent } from './Card'; import { Button } from './Button'; import { useToast } from '../contexts/ToastContext'; interface BatchTagModalProps { selectedIds: number[]; existingTags: string[]; onClose: () => void; } export function BatchTagModal({ selectedIds, existingTags, onClose }: BatchTagModalProps) { const queryClient = useQueryClient(); const { showToast } = useToast(); const [newTag, setNewTag] = useState(''); const [selectedTags, setSelectedTags] = useState<Set<string>>(new Set()); const [mode, setMode] = useState<'add' | 'remove'>('add'); // Close on Escape key useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [onClose]); const batchTagMutation = useMutation({ mutationFn: async () => { const tagsArray = Array.from(selectedTags); let successCount = 0; // Process sequentially to avoid SQLite database locks for (const id of selectedIds) { try { const archive = await api.getArchive(id); const currentTags = archive.tags ? archive.tags.split(',').map(t => t.trim()).filter(Boolean) : []; let newTags: string[]; if (mode === 'add') { // Add tags that aren't already present newTags = [...new Set([...currentTags, ...tagsArray])]; } else { // Remove selected tags newTags = currentTags.filter(t => !selectedTags.has(t)); } await api.updateArchive(id, { tags: newTags.join(', ') }); successCount++; } catch (err) { console.error(`Failed to update archive ${id}:`, err); throw new Error(`Failed on archive ${id}: ${err instanceof Error ? err.message : 'Unknown error'}`); } } return { count: successCount, mode, tags: tagsArray }; }, onSuccess: ({ count, mode, tags }) => { queryClient.invalidateQueries({ queryKey: ['archives'] }); showToast(`${mode === 'add' ? 'Added' : 'Removed'} ${tags.length} tag${tags.length !== 1 ? 's' : ''} ${mode === 'add' ? 'to' : 'from'} ${count} archive${count !== 1 ? 's' : ''}`); onClose(); }, onError: (error: Error) => { showToast(error.message || 'Failed to update tags', 'error'); }, }); const toggleTag = (tag: string) => { setSelectedTags((prev) => { const next = new Set(prev); if (next.has(tag)) { next.delete(tag); } else { next.add(tag); } return next; }); }; const addNewTag = () => { if (newTag.trim() && !selectedTags.has(newTag.trim())) { setSelectedTags((prev) => new Set([...prev, newTag.trim()])); setNewTag(''); } }; const handleKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { e.preventDefault(); addNewTag(); } }; return ( <div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50 p-4"> <Card className="w-full max-w-md"> <CardContent className="p-0"> {/* Header */} <div className="flex items-center justify-between p-4 border-b border-bambu-dark-tertiary"> <div className="flex items-center gap-2"> <Tag className="w-5 h-5 text-bambu-green" /> <h2 className="text-xl font-semibold text-white"> {mode === 'add' ? 'Add Tags' : 'Remove Tags'} </h2> </div> <button onClick={onClose} className="text-bambu-gray hover:text-white transition-colors" > <X className="w-5 h-5" /> </button> </div> {/* Content */} <div className="p-4 space-y-4"> <p className="text-sm text-bambu-gray"> {mode === 'add' ? 'Add' : 'Remove'} tags for {selectedIds.length} selected archive{selectedIds.length !== 1 ? 's' : ''} </p> {/* Mode toggle */} <div className="flex gap-2"> <Button size="sm" variant={mode === 'add' ? 'primary' : 'secondary'} onClick={() => setMode('add')} > Add Tags </Button> <Button size="sm" variant={mode === 'remove' ? 'primary' : 'secondary'} onClick={() => setMode('remove')} > Remove Tags </Button> </div> {/* New tag input (only for add mode) */} {mode === 'add' && ( <div className="flex gap-2"> <input type="text" placeholder="Enter new tag..." className="flex-1 px-3 py-2 bg-bambu-dark border border-bambu-dark-tertiary rounded-lg text-white text-sm focus:border-bambu-green focus:outline-none" value={newTag} onChange={(e) => setNewTag(e.target.value)} onKeyDown={handleKeyDown} /> <Button size="sm" variant="secondary" onClick={addNewTag} disabled={!newTag.trim()}> <Plus className="w-4 h-4" /> </Button> </div> )} {/* Existing tags */} {existingTags.length > 0 && ( <div> <p className="text-xs text-bambu-gray mb-2">Existing tags:</p> <div className="flex flex-wrap gap-2"> {existingTags.map((tag) => ( <button key={tag} onClick={() => toggleTag(tag)} className={`px-2 py-1 rounded text-sm transition-colors ${ selectedTags.has(tag) ? 'bg-bambu-green text-white' : 'bg-bambu-dark-tertiary text-bambu-gray-light hover:bg-bambu-dark' }`} > {tag} </button> ))} </div> </div> )} {/* Selected tags preview */} {selectedTags.size > 0 && ( <div> <p className="text-xs text-bambu-gray mb-2"> Tags to {mode === 'add' ? 'add' : 'remove'}: </p> <div className="flex flex-wrap gap-2"> {Array.from(selectedTags).map((tag) => ( <span key={tag} className={`px-2 py-1 rounded text-sm flex items-center gap-1 ${ mode === 'add' ? 'bg-green-500/20 text-green-400' : 'bg-red-500/20 text-red-400' }`} > {tag} <button onClick={() => toggleTag(tag)} className="hover:opacity-70"> <X className="w-3 h-3" /> </button> </span> ))} </div> </div> )} </div> {/* Footer */} <div className="flex gap-3 p-4 border-t border-bambu-dark-tertiary"> <Button variant="secondary" onClick={onClose} className="flex-1"> Cancel </Button> <Button onClick={() => batchTagMutation.mutate()} disabled={selectedTags.size === 0 || batchTagMutation.isPending} className="flex-1" > {batchTagMutation.isPending ? ( <> <Loader2 className="w-4 h-4 animate-spin" /> Processing... </> ) : ( <> <Tag className="w-4 h-4" /> {mode === 'add' ? 'Add Tags' : 'Remove Tags'} </> )} </Button> </div> </CardContent> </Card> </div> ); } |