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';
|
2023-08-04 23:59:33 +03:00
|
|
|
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';
|
2021-12-30 12:02:31 +03:00
|
|
|
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 { TimeoutToggle } from '../utils/helpers/hooks';
|
|
|
|
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(
|
2022-08-07 14:26:26 +03:00
|
|
|
allServers.filter(({ name, url }) => `${name} ${url}`.toLowerCase().match(searchTerm.toLowerCase())),
|
2021-10-17 20:13:06 +03:00
|
|
|
);
|
|
|
|
const hasAutoConnect = serversList.some(({ autoConnect }) => !!autoConnect);
|
2022-05-29 13:18:21 +03:00
|
|
|
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">
|
2021-10-22 20:03:12 +03:00
|
|
|
<ImportServersBtn className="flex-fill" onImportError={setErrorImporting}>Import servers</ImportServersBtn>
|
|
|
|
{allServers.length > 0 && (
|
2022-03-05 15:26:28 +03:00
|
|
|
<Button outline className="ms-2 flex-fill" onClick={async () => serversExporter.exportServers()}>
|
2021-10-22 20:03:12 +03:00
|
|
|
<FontAwesomeIcon icon={exportIcon} fixedWidth /> Export servers
|
|
|
|
</Button>
|
|
|
|
)}
|
2021-10-22 19:53:00 +03:00
|
|
|
</div>
|
2022-03-05 18:08:30 +03:00
|
|
|
<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
|
|
|
|
2021-10-22 21:13:23 +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',
|
|
|
|
]);
|