|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +import { useState, useMemo } from 'react'; |
| 19 | +import { Modal, Input, List, Typography, Space, message } from 'antd'; |
| 20 | +import { SearchOutlined, FolderOutlined } from '@ant-design/icons'; |
| 21 | +import { useQuery, useQueryClient } from '@tanstack/react-query'; |
| 22 | +import { getProjects, addSavedQuery, addVisualization, addDashboard } from '../../api/projects'; |
| 23 | + |
| 24 | +const { Text } = Typography; |
| 25 | + |
| 26 | +interface AddToProjectModalProps { |
| 27 | + open: boolean; |
| 28 | + onClose: () => void; |
| 29 | + itemId: string; |
| 30 | + itemType: 'savedQuery' | 'visualization' | 'dashboard'; |
| 31 | +} |
| 32 | + |
| 33 | +const typeLabels = { savedQuery: 'query', visualization: 'visualization', dashboard: 'dashboard' }; |
| 34 | + |
| 35 | +export default function AddToProjectModal({ open, onClose, itemId, itemType }: AddToProjectModalProps) { |
| 36 | + const [search, setSearch] = useState(''); |
| 37 | + const [loading, setLoading] = useState<string | null>(null); |
| 38 | + const queryClient = useQueryClient(); |
| 39 | + |
| 40 | + const { data: projects } = useQuery({ |
| 41 | + queryKey: ['projects'], |
| 42 | + queryFn: getProjects, |
| 43 | + enabled: open, |
| 44 | + }); |
| 45 | + |
| 46 | + const filtered = useMemo(() => { |
| 47 | + if (!projects) { |
| 48 | + return []; |
| 49 | + } |
| 50 | + if (!search) { |
| 51 | + return projects; |
| 52 | + } |
| 53 | + const lower = search.toLowerCase(); |
| 54 | + return projects.filter(p => p.name.toLowerCase().includes(lower)); |
| 55 | + }, [projects, search]); |
| 56 | + |
| 57 | + const handleAdd = async (projectId: string) => { |
| 58 | + setLoading(projectId); |
| 59 | + try { |
| 60 | + if (itemType === 'savedQuery') { |
| 61 | + await addSavedQuery(projectId, itemId); |
| 62 | + } else if (itemType === 'visualization') { |
| 63 | + await addVisualization(projectId, itemId); |
| 64 | + } else { |
| 65 | + await addDashboard(projectId, itemId); |
| 66 | + } |
| 67 | + message.success(`Added ${typeLabels[itemType]} to project`); |
| 68 | + queryClient.invalidateQueries({ queryKey: ['project', projectId] }); |
| 69 | + onClose(); |
| 70 | + } catch (err) { |
| 71 | + message.error(`Failed: ${(err as Error).message}`); |
| 72 | + } finally { |
| 73 | + setLoading(null); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <Modal title="Add to Project" open={open} onCancel={onClose} footer={null} width={480}> |
| 79 | + <Input |
| 80 | + placeholder="Search projects..." |
| 81 | + prefix={<SearchOutlined />} |
| 82 | + value={search} |
| 83 | + onChange={e => setSearch(e.target.value)} |
| 84 | + allowClear |
| 85 | + style={{ marginBottom: 12 }} |
| 86 | + /> |
| 87 | + <List |
| 88 | + dataSource={filtered} |
| 89 | + style={{ maxHeight: 320, overflow: 'auto' }} |
| 90 | + loading={!projects} |
| 91 | + locale={{ emptyText: 'No projects found' }} |
| 92 | + renderItem={project => ( |
| 93 | + <List.Item |
| 94 | + style={{ cursor: 'pointer', padding: '8px 12px' }} |
| 95 | + onClick={() => !loading && handleAdd(project.id)} |
| 96 | + > |
| 97 | + <Space> |
| 98 | + <FolderOutlined /> |
| 99 | + <div> |
| 100 | + <Text strong>{project.name}</Text> |
| 101 | + {project.description && ( |
| 102 | + <Text type="secondary" style={{ display: 'block', fontSize: 12 }} ellipsis>{project.description}</Text> |
| 103 | + )} |
| 104 | + </div> |
| 105 | + </Space> |
| 106 | + {loading === project.id && <Text type="secondary">Adding...</Text>} |
| 107 | + </List.Item> |
| 108 | + )} |
| 109 | + /> |
| 110 | + </Modal> |
| 111 | + ); |
| 112 | +} |
0 commit comments