2019-07-10 05:00:29 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-11-09 02:09:22 +00:00
|
|
|
import getServerDatabases from '@/api/server/databases/getServerDatabases';
|
2019-07-10 05:00:29 +00:00
|
|
|
import { ServerContext } from '@/state/server';
|
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
|
|
|
import FlashMessageRender from '@/components/FlashMessageRender';
|
|
|
|
import DatabaseRow from '@/components/server/databases/DatabaseRow';
|
|
|
|
import Spinner from '@/components/elements/Spinner';
|
2019-07-10 05:41:09 +00:00
|
|
|
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
|
2020-03-30 05:20:27 +00:00
|
|
|
import Can from '@/components/elements/Can';
|
2020-04-10 17:56:25 +00:00
|
|
|
import useFlash from '@/plugins/useFlash';
|
2020-07-05 00:15:49 +00:00
|
|
|
import tw from 'twin.macro';
|
|
|
|
import Fade from '@/components/elements/Fade';
|
2020-08-26 05:09:54 +00:00
|
|
|
import ServerContentBlock from '@/components/elements/ServerContentBlock';
|
|
|
|
import { useDeepMemoize } from '@/plugins/useDeepMemoize';
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
export default () => {
|
2020-08-26 05:09:54 +00:00
|
|
|
const uuid = ServerContext.useStoreState(state => state.server.data!.uuid);
|
|
|
|
const databaseLimit = ServerContext.useStoreState(state => state.server.data!.featureLimits.databases);
|
|
|
|
|
2020-04-10 17:56:25 +00:00
|
|
|
const { addError, clearFlashes } = useFlash();
|
2019-07-10 05:00:29 +00:00
|
|
|
const [ loading, setLoading ] = useState(true);
|
2020-04-10 17:56:25 +00:00
|
|
|
|
2020-08-26 05:09:54 +00:00
|
|
|
const databases = useDeepMemoize(ServerContext.useStoreState(state => state.databases.data));
|
2020-04-10 17:56:25 +00:00
|
|
|
const setDatabases = ServerContext.useStoreActions(state => state.databases.setDatabases);
|
2019-07-10 05:00:29 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2019-07-27 22:17:50 +00:00
|
|
|
setLoading(!databases.length);
|
2019-07-10 05:00:29 +00:00
|
|
|
clearFlashes('databases');
|
2019-07-27 22:17:50 +00:00
|
|
|
|
2020-04-10 17:56:25 +00:00
|
|
|
getServerDatabases(uuid)
|
|
|
|
.then(databases => setDatabases(databases))
|
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
addError({ key: 'databases', message: httpErrorToHuman(error) });
|
2019-07-10 05:00:29 +00:00
|
|
|
})
|
2020-04-10 17:56:25 +00:00
|
|
|
.then(() => setLoading(false));
|
2019-07-10 05:00:29 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
return (
|
2020-08-26 05:09:54 +00:00
|
|
|
<ServerContentBlock title={'Databases'}>
|
2020-07-05 00:15:49 +00:00
|
|
|
<FlashMessageRender byKey={'databases'} css={tw`mb-4`}/>
|
2020-04-10 17:56:25 +00:00
|
|
|
{(!databases.length && loading) ?
|
2020-07-05 00:15:49 +00:00
|
|
|
<Spinner size={'large'} centered/>
|
2019-07-10 05:00:29 +00:00
|
|
|
:
|
2020-07-05 20:56:04 +00:00
|
|
|
<Fade timeout={150}>
|
2020-03-30 05:20:27 +00:00
|
|
|
<>
|
2019-07-10 05:00:29 +00:00
|
|
|
{databases.length > 0 ?
|
2019-07-17 05:15:14 +00:00
|
|
|
databases.map((database, index) => (
|
|
|
|
<DatabaseRow
|
|
|
|
key={database.id}
|
2020-04-10 17:56:25 +00:00
|
|
|
database={database}
|
2019-07-17 05:15:14 +00:00
|
|
|
className={index > 0 ? 'mt-1' : undefined}
|
|
|
|
/>
|
|
|
|
))
|
2019-07-10 05:00:29 +00:00
|
|
|
:
|
2020-07-05 00:15:49 +00:00
|
|
|
<p css={tw`text-center text-sm text-neutral-400`}>
|
2020-08-26 05:09:54 +00:00
|
|
|
{databaseLimit > 0 ?
|
2020-07-05 00:15:49 +00:00
|
|
|
'It looks like you have no databases.'
|
2020-01-19 21:54:39 +00:00
|
|
|
:
|
2020-07-05 00:15:49 +00:00
|
|
|
'Databases cannot be created for this server.'
|
2020-01-19 21:54:39 +00:00
|
|
|
}
|
2019-07-10 05:06:42 +00:00
|
|
|
</p>
|
2019-07-10 05:00:29 +00:00
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
<Can action={'database.create'}>
|
2020-11-09 02:09:22 +00:00
|
|
|
<div css={tw`mt-6 sm:flex items-center justify-end`}>
|
|
|
|
{(databaseLimit > 0 && databases.length > 0) &&
|
|
|
|
<p css={tw`text-sm text-neutral-300 mb-4 sm:mr-6 sm:mb-0`}>
|
|
|
|
{databases.length} of {databaseLimit} databases have been allocated to this
|
|
|
|
server.
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
{databaseLimit > 0 && databaseLimit !== databases.length &&
|
|
|
|
<CreateDatabaseButton css={tw`w-full sm:w-auto`}/>
|
|
|
|
}
|
2020-03-30 05:20:27 +00:00
|
|
|
</div>
|
|
|
|
</Can>
|
|
|
|
</>
|
2020-07-05 00:15:49 +00:00
|
|
|
</Fade>
|
2019-07-10 05:00:29 +00:00
|
|
|
}
|
2020-08-26 05:09:54 +00:00
|
|
|
</ServerContentBlock>
|
2019-07-10 05:00:29 +00:00
|
|
|
);
|
|
|
|
};
|