|
| 1 | +import { useState, useMemo, useCallback } from 'react'; |
| 2 | +import { faChevronRight, faDatabase } from '@fortawesome/free-solid-svg-icons'; |
| 3 | +import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; |
| 4 | +import styles from './styles.module.scss'; |
| 5 | +import { useSchmea } from 'renderer/contexts/SchemaProvider'; |
| 6 | +import Modal from '../Modal'; |
| 7 | +import ListView from '../ListView'; |
| 8 | +import Button from '../Button'; |
| 9 | +import { useSqlExecute } from 'renderer/contexts/SqlExecuteProvider'; |
| 10 | + |
| 11 | +interface DatabaseSelectionModalProps { |
| 12 | + open: boolean; |
| 13 | + onClose: () => void; |
| 14 | +} |
| 15 | + |
| 16 | +function DatabaseSelectionModal({ |
| 17 | + onClose, |
| 18 | + open, |
| 19 | +}: DatabaseSelectionModalProps) { |
| 20 | + const { currentDatabase, schema } = useSchmea(); |
| 21 | + |
| 22 | + const databaseList = useMemo(() => { |
| 23 | + if (schema) { |
| 24 | + return Object.keys(schema); |
| 25 | + } |
| 26 | + return []; |
| 27 | + }, [schema]); |
| 28 | + |
| 29 | + const [selectedDatabase, setSelectedDatabase] = useState<string | undefined>( |
| 30 | + currentDatabase || undefined |
| 31 | + ); |
| 32 | + |
| 33 | + const { common } = useSqlExecute(); |
| 34 | + |
| 35 | + const onOpenClicked = useCallback(() => { |
| 36 | + if (selectedDatabase !== currentDatabase && selectedDatabase) { |
| 37 | + common |
| 38 | + .switchDatabase(selectedDatabase) |
| 39 | + .then((result) => { |
| 40 | + if (result) { |
| 41 | + onClose(); |
| 42 | + } |
| 43 | + }) |
| 44 | + .catch(console.error); |
| 45 | + } |
| 46 | + }, [common, selectedDatabase, currentDatabase, onClose]); |
| 47 | + |
| 48 | + return ( |
| 49 | + <Modal open={open} title="Database Selection" onClose={onClose}> |
| 50 | + <Modal.Body> |
| 51 | + <div |
| 52 | + style={{ maxHeight: '50vh', overflowY: 'auto' }} |
| 53 | + className={'scroll'} |
| 54 | + > |
| 55 | + <ListView |
| 56 | + selectedItem={selectedDatabase} |
| 57 | + onSelectChange={(item) => setSelectedDatabase(item)} |
| 58 | + items={databaseList} |
| 59 | + extractMeta={(database) => ({ |
| 60 | + text: database, |
| 61 | + key: database, |
| 62 | + icon: <FontAwesomeIcon icon={faDatabase} />, |
| 63 | + })} |
| 64 | + /> |
| 65 | + </div> |
| 66 | + </Modal.Body> |
| 67 | + <Modal.Footer> |
| 68 | + <Button primary onClick={onOpenClicked} disabled={!selectedDatabase}> |
| 69 | + Open |
| 70 | + </Button> |
| 71 | + </Modal.Footer> |
| 72 | + </Modal> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +export default function DatabaseSelection() { |
| 77 | + const { currentDatabase } = useSchmea(); |
| 78 | + const [open, setOpen] = useState(false); |
| 79 | + |
| 80 | + const onClose = useCallback(() => { |
| 81 | + setOpen(false); |
| 82 | + }, [setOpen]); |
| 83 | + |
| 84 | + const onOpen = useCallback(() => { |
| 85 | + setOpen(true); |
| 86 | + }, [setOpen]); |
| 87 | + |
| 88 | + return ( |
| 89 | + <> |
| 90 | + <div className={styles.header} onClick={onOpen}> |
| 91 | + <FontAwesomeIcon icon={faDatabase} color="#27ae60" /> |
| 92 | + <span>{currentDatabase}</span> |
| 93 | + <FontAwesomeIcon icon={faChevronRight} /> |
| 94 | + </div> |
| 95 | + <DatabaseSelectionModal onClose={onClose} open={open} /> |
| 96 | + </> |
| 97 | + ); |
| 98 | +} |
0 commit comments