misc_pterodactyl-panel/resources/scripts/components/server/databases/DatabasesContainer.tsx

74 lines
3.1 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import getServerDatabases from '@/api/server/getServerDatabases';
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';
import { CSSTransition } from 'react-transition-group';
2019-07-10 05:41:09 +00:00
import CreateDatabaseButton from '@/components/server/databases/CreateDatabaseButton';
import Can from '@/components/elements/Can';
2020-04-10 17:56:25 +00:00
import useFlash from '@/plugins/useFlash';
import useServer from '@/plugins/useServer';
import PageContentBlock from '@/components/elements/PageContentBlock';
export default () => {
2020-04-10 17:56:25 +00:00
const { uuid, featureLimits } = useServer();
const { addError, clearFlashes } = useFlash();
const [ loading, setLoading ] = useState(true);
2020-04-10 17:56:25 +00:00
const databases = ServerContext.useStoreState(state => state.databases.data);
const setDatabases = ServerContext.useStoreActions(state => state.databases.setDatabases);
useEffect(() => {
2019-07-27 22:17:50 +00:00
setLoading(!databases.length);
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) });
})
2020-04-10 17:56:25 +00:00
.then(() => setLoading(false));
}, []);
return (
<PageContentBlock>
2020-04-10 17:56:25 +00:00
<FlashMessageRender byKey={'databases'} className={'mb-4'}/>
{(!databases.length && loading) ?
<Spinner size={'large'} centered={true}/>
:
<CSSTransition classNames={'fade'} timeout={250}>
<>
{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-12-22 00:40:28 +00:00
<p className={'text-center text-sm text-neutral-400'}>
2020-04-10 17:56:25 +00:00
{featureLimits.databases > 0 ?
`It looks like you have no databases.`
:
`Databases cannot be created for this server.`
}
</p>
}
<Can action={'database.create'}>
2020-04-10 17:56:25 +00:00
{featureLimits.databases > 0 &&
<div className={'mt-6 flex justify-end'}>
2020-04-10 17:56:25 +00:00
<CreateDatabaseButton/>
</div>
}
</Can>
</>
</CSSTransition>
}
</PageContentBlock>
);
};