Skip to content

Commit

Permalink
feat: database search (querymx#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
ebfood authored Aug 3, 2023
1 parent 148c8e6 commit a16af04
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
47 changes: 33 additions & 14 deletions src/renderer/components/DatabaseTable/DatabaseSelection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Modal from '../Modal';
import ListView from '../ListView';
import Button from '../Button';
import { useSqlExecute } from 'renderer/contexts/SqlExecuteProvider';
import TextField from '../TextField';

interface DatabaseSelectionModalProps {
open: boolean;
Expand All @@ -31,9 +32,16 @@ function DatabaseSelectionModal({
const [selectedDatabase, setSelectedDatabase] = useState<string | undefined>(
currentDatabase || undefined
);
const [searchTerm, setSearchTerm] = useState<string>('');

const { common } = useSqlExecute();

const filteredDatabaseList = useMemo(() => {
return databaseList.filter(database =>
database.toLowerCase().includes(searchTerm.toLowerCase())
);
}, [databaseList, searchTerm]);

const openDatabase = useCallback(
(databaseName: string) => {
if (databaseName !== currentDatabase && databaseName) {
Expand All @@ -59,23 +67,34 @@ function DatabaseSelectionModal({
return (
<Modal open={open} title="Database Selection" onClose={onClose}>
<Modal.Body>
<div className={styles.searchContainer}>
<TextField
placeholder="Search for a database..."
value={searchTerm}
onChange={(value) => setSearchTerm(value)}
/>
</div>
<div
style={{ maxHeight: '50vh', overflowY: 'auto' }}
style={{ minHeight: '15vh', maxHeight: '50vh', overflowY: 'auto' }}
className={'scroll'}
>
<ListView
selectedItem={selectedDatabase}
onSelectChange={(item) => setSelectedDatabase(item)}
items={databaseList}
onDoubleClick={(item) => {
openDatabase(item);
}}
extractMeta={(database) => ({
text: database,
key: database,
icon: <FontAwesomeIcon icon={faDatabase} />,
})}
/>
{filteredDatabaseList.length === 0 ? (
<p className={styles.noResultsText}>No matching databases found.</p>
) : (
<ListView
selectedItem={selectedDatabase}
onSelectChange={(item) => setSelectedDatabase(item)}
items={filteredDatabaseList}
onDoubleClick={(item) => {
openDatabase(item);
}}
extractMeta={(database) => ({
text: database,
key: database,
icon: <FontAwesomeIcon icon={faDatabase} />,
})}
/>
)}
</div>
</Modal.Body>
<Modal.Footer>
Expand Down
9 changes: 9 additions & 0 deletions src/renderer/components/DatabaseTable/styles.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,12 @@
border-radius: 50%;
animation: ping 1s ease-out infinite;
}

.searchContainer {
margin-bottom: 10px;
}

.noResultsText {
margin: 10px 10px 0;
opacity: 0.75;
}
2 changes: 1 addition & 1 deletion src/renderer/components/TextField/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ReactElement } from 'react';
import styles from './styles.module.scss';

interface TextFieldProps {
label: string;
label?: string;
value?: string;
autoFocus?: boolean;
placeholder?: string;
Expand Down

0 comments on commit a16af04

Please sign in to comment.