shlink-web-client/src/servers/ManageServers.tsx

103 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-10-17 20:13:06 +03:00
import { faFileDownload as exportIcon, faPlus as plusIcon } from '@fortawesome/free-solid-svg-icons';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import type { TimeoutToggle } from '@shlinkio/shlink-frontend-kit';
import { Result, SearchField, SimpleCard } from '@shlinkio/shlink-frontend-kit';
2023-02-18 13:11:01 +03:00
import type { FC } from 'react';
import { useEffect, useState } from 'react';
2021-10-17 20:13:06 +03:00
import { Link } from 'react-router-dom';
2023-02-18 13:11:01 +03:00
import { Button, Row } from 'reactstrap';
import { NoMenuLayout } from '../common/NoMenuLayout';
2023-09-05 10:08:42 +03:00
import type { FCWithDeps } from '../container/utils';
import { componentFactory, useDependencies } from '../container/utils';
2023-02-18 12:40:37 +03:00
import type { ServersMap } from './data';
2023-02-18 13:11:01 +03:00
import type { ImportServersBtnProps } from './helpers/ImportServersBtn';
2023-02-18 12:40:37 +03:00
import type { ManageServersRowProps } from './ManageServersRow';
2023-02-18 13:37:49 +03:00
import type { ServersExporter } from './services/ServersExporter';
2021-10-17 20:13:06 +03:00
2023-09-05 10:08:42 +03:00
type ManageServersProps = {
2021-10-17 20:13:06 +03:00
servers: ServersMap;
2023-09-05 10:08:42 +03:00
};
type ManageServersDeps = {
ServersExporter: ServersExporter;
ImportServersBtn: FC<ImportServersBtnProps>;
useTimeoutToggle: TimeoutToggle;
ManageServersRow: FC<ManageServersRowProps>;
};
2021-10-17 20:13:06 +03:00
const SHOW_IMPORT_MSG_TIME = 4000;
2023-09-05 10:08:42 +03:00
const ManageServers: FCWithDeps<ManageServersProps, ManageServersDeps> = ({ servers }) => {
const {
ServersExporter: serversExporter,
ImportServersBtn,
useTimeoutToggle,
ManageServersRow,
} = useDependencies(ManageServers);
2021-10-17 20:13:06 +03:00
const allServers = Object.values(servers);
2022-03-26 14:17:42 +03:00
const [serversList, setServersList] = useState(allServers);
2021-10-17 20:13:06 +03:00
const filterServers = (searchTerm: string) => setServersList(
allServers.filter(({ name, url }) => `${name} ${url}`.toLowerCase().match(searchTerm.toLowerCase())),
2021-10-17 20:13:06 +03:00
);
const hasAutoConnect = serversList.some(({ autoConnect }) => !!autoConnect);
const [errorImporting, setErrorImporting] = useTimeoutToggle(false, SHOW_IMPORT_MSG_TIME);
2021-10-17 20:13:06 +03:00
useEffect(() => {
setServersList(Object.values(servers));
2022-03-26 14:17:42 +03:00
}, [servers]);
2021-10-17 20:13:06 +03:00
return (
<NoMenuLayout>
<SearchField className="mb-3" onChange={filterServers} />
2021-10-22 19:53:00 +03:00
<Row className="mb-3">
<div className="col-md-6 d-flex d-md-block mb-2 mb-md-0">
<ImportServersBtn className="flex-fill" onImportError={setErrorImporting}>Import servers</ImportServersBtn>
{allServers.length > 0 && (
<Button outline className="ms-2 flex-fill" onClick={async () => serversExporter.exportServers()}>
<FontAwesomeIcon icon={exportIcon} fixedWidth /> Export servers
</Button>
)}
2021-10-22 19:53:00 +03:00
</div>
<div className="col-md-6 text-md-end d-flex d-md-block">
2021-10-22 19:53:00 +03:00
<Button outline color="primary" className="flex-fill" tag={Link} to="/server/create">
<FontAwesomeIcon icon={plusIcon} fixedWidth /> Add a server
</Button>
</div>
</Row>
2021-10-17 20:13:06 +03:00
<SimpleCard>
2022-03-07 11:09:30 +03:00
<table className="table table-hover responsive-table mb-0">
2021-10-17 20:13:06 +03:00
<thead className="responsive-table__header">
<tr>
2022-03-26 14:17:42 +03:00
{hasAutoConnect && <th aria-label="Auto-connect" style={{ width: '50px' }} />}
2021-10-17 20:13:06 +03:00
<th>Name</th>
<th>Base URL</th>
2022-03-26 14:17:42 +03:00
<th aria-label="Options" />
2021-10-17 20:13:06 +03:00
</tr>
</thead>
<tbody>
{!serversList.length && <tr className="text-center"><td colSpan={4}>No servers found.</td></tr>}
2022-03-26 14:17:42 +03:00
{serversList.map((server) => (
<ManageServersRow key={server.id} server={server} hasAutoConnect={hasAutoConnect} />
))}
2021-10-17 20:13:06 +03:00
</tbody>
</table>
</SimpleCard>
{errorImporting && (
<div className="mt-3">
<Result type="error">The servers could not be imported. Make sure the format is correct.</Result>
</div>
)}
</NoMenuLayout>
);
};
2023-09-05 10:08:42 +03:00
export const ManageServersFactory = componentFactory(ManageServers, [
'ServersExporter',
'ImportServersBtn',
'useTimeoutToggle',
'ManageServersRow',
]);