import { useState } from 'react' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import StyledModal from '../StyledModal' import StyledButton from '~/components/StyledButton' import { useNotifications } from '~/context/NotificationContext' import api from '~/lib/api' interface CollectionsManagerProps { onClose: () => void } export default function CollectionsManager({ onClose }: CollectionsManagerProps) { const { addNotification } = useNotifications() const queryClient = useQueryClient() const [editingName, setEditingName] = useState(null) const [editValue, setEditValue] = useState('') const [confirmDelete, setConfirmDelete] = useState(null) const { data: collections = [], isLoading } = useQuery({ queryKey: ['kbCollections'], queryFn: () => api.getKnowledgeCollections(), select: (data) => data?.collections ?? [], }) const invalidateAll = () => { queryClient.invalidateQueries({ queryKey: ['kbCollections'] }) queryClient.invalidateQueries({ queryKey: ['storedFiles'] }) } const renameMutation = useMutation({ mutationFn: ({ oldName, newName }: { oldName: string; newName: string }) => api.renameCollection(oldName, newName), onSuccess: (data) => { addNotification({ type: 'success', message: data?.message || 'Collection renamed.' }) setEditingName(null) invalidateAll() }, onError: (error: any) => { addNotification({ type: 'error', message: error?.message || 'Failed to rename collection.' }) }, }) const deleteMutation = useMutation({ mutationFn: (name: string) => api.deleteCollection(name), onSuccess: (data) => { addNotification({ type: 'success', message: data?.message || 'Collection removed.' }) setConfirmDelete(null) invalidateAll() }, onError: (error: any) => { addNotification({ type: 'error', message: error?.message || 'Failed to remove collection.' }) }, }) return (

Rename or remove collections. Removing a collection doesn't delete any files — they're simply moved back to Uncategorized so you can re-sort them.

{isLoading &&

Loading…

} {!isLoading && collections.length === 0 && (

No collections yet. Assign a file to a collection from the Knowledge Base table to create one.

)}
    {collections.map((name) => (
  • {editingName === name ? ( <> setEditValue(e.target.value)} className="flex-1 rounded border border-border-subtle bg-surface-primary px-2 py-1 text-sm text-text-primary" /> renameMutation.mutate({ oldName: name, newName: editValue.trim() }) } > Save setEditingName(null)}> Cancel ) : confirmDelete === name ? ( <> Remove "{name}"? Files move to Uncategorized. deleteMutation.mutate(name)} > Confirm setConfirmDelete(null)}> Cancel ) : ( <> {name} { setEditingName(name) setEditValue(name) }} > Rename setConfirmDelete(name)} > Remove )}
  • ))}
) }