import React, { useContext, useEffect, useState } from 'react'; import getDatabases, { Context as DatabasesContext, Filters } from '@/api/admin/databases/getDatabases'; import FlashMessageRender from '@/components/FlashMessageRender'; import useFlash from '@/plugins/useFlash'; import { AdminContext } from '@/state/admin'; import { NavLink, useRouteMatch } from 'react-router-dom'; import tw from 'twin.macro'; import AdminContentBlock from '@/components/admin/AdminContentBlock'; import AdminCheckbox from '@/components/admin/AdminCheckbox'; import AdminTable, { TableBody, TableHead, TableHeader, TableRow, Pagination, Loading, NoItems, ContentWrapper } from '@/components/admin/AdminTable'; import Button from '@/components/elements/Button'; import CopyOnClick from '@/components/elements/CopyOnClick'; const RowCheckbox = ({ id }: { id: number}) => { const isChecked = AdminContext.useStoreState(state => state.databases.selectedDatabases.indexOf(id) >= 0); const appendSelectedDatabase = AdminContext.useStoreActions(actions => actions.databases.appendSelectedDatabase); const removeSelectedDatabase = AdminContext.useStoreActions(actions => actions.databases.removeSelectedDatabase); return ( ) => { if (e.currentTarget.checked) { appendSelectedDatabase(id); } else { removeSelectedDatabase(id); } }} /> ); }; const DatabasesContainer = () => { const match = useRouteMatch(); const { page, setPage, setFilters, sort, setSort, sortDirection } = useContext(DatabasesContext); const { clearFlashes, clearAndAddHttpError } = useFlash(); const { data: databases, error, isValidating } = getDatabases(); useEffect(() => { if (!error) { clearFlashes('databases'); return; } clearAndAddHttpError({ key: 'databases', error }); }, [ error ]); const length = databases?.items?.length || 0; const setSelectedDatabases = AdminContext.useStoreActions(actions => actions.databases.setSelectedDatabases); const selectedDatabasesLength = AdminContext.useStoreState(state => state.databases.selectedDatabases.length); const onSelectAllClick = (e: React.ChangeEvent) => { setSelectedDatabases(e.currentTarget.checked ? (databases?.items?.map(database => database.id) || []) : []); }; const onSearch = (query: string): Promise => { return new Promise((resolve) => { if (query.length < 2) { setFilters(null); } else { setFilters({ name: query }); } return resolve(); }); }; useEffect(() => { setSelectedDatabases([]); }, [ page ]); return (

Database Hosts

Database hosts that servers can have databases created on.

setSort('id')}/> setSort('name')}/> setSort('username')}/> { databases !== undefined && !error && !isValidating && length > 0 && databases.items.map(database => ( )) }
{database.id} {database.name} {database.getAddress()} {database.username}
{ databases === undefined || (error && isValidating) ? : length < 1 ? : null }
); }; export default () => { const [ page, setPage ] = useState(1); const [ filters, setFilters ] = useState(null); const [ sort, setSortState ] = useState(null); const [ sortDirection, setSortDirection ] = useState(false); const setSort = (newSort: string | null) => { if (sort === newSort) { setSortDirection(!sortDirection); } else { setSortState(newSort); setSortDirection(false); } }; return ( ); };