Add ping service module

Resolves #78
This commit is contained in:
ajnart
2022-05-17 04:02:14 +02:00
parent b8fe799ac6
commit c9c6f2b0c9
7 changed files with 107 additions and 0 deletions

View File

@@ -4,6 +4,7 @@ import { Text, AspectRatio, Card, Image, Center, Grid, createStyles } from '@man
import { useConfig } from '../../tools/state'; import { useConfig } from '../../tools/state';
import { serviceItem } from '../../tools/types'; import { serviceItem } from '../../tools/types';
import AppShelfMenu from './AppShelfMenu'; import AppShelfMenu from './AppShelfMenu';
import PingComponent from '../modules/ping/PingModule';
const useStyles = createStyles((theme) => ({ const useStyles = createStyles((theme) => ({
item: { item: {
@@ -89,6 +90,7 @@ export function AppShelfItem(props: any) {
/> />
</motion.i> </motion.i>
</AspectRatio> </AspectRatio>
<PingComponent url={service.url} />
</Card.Section> </Card.Section>
</Center> </Center>
</Card> </Card>

View File

@@ -1,3 +1,4 @@
export * from './date'; export * from './date';
export * from './calendar'; export * from './calendar';
export * from './search'; export * from './search';
export * from './ping';

View File

@@ -0,0 +1,15 @@
import { serviceItem } from '../../../tools/types';
import PingComponent from './PingModule';
export default {
title: 'Modules/Search bar',
};
const service: serviceItem = {
type: 'Other',
name: 'YouTube',
icon: 'https://cdn.jsdelivr.net/gh/walkxhub/dashboard-icons/png/youtube.png',
url: 'https://youtube.com/',
};
export const Default = (args: any) => <PingComponent service={service} />;

View File

@@ -0,0 +1,51 @@
import { Indicator, Tooltip } from '@mantine/core';
import axios from 'axios';
import { useEffect, useState } from 'react';
import { Plug } from 'tabler-icons-react';
import { useConfig } from '../../../tools/state';
import { IModule } from '../modules';
export const PingModule: IModule = {
title: 'Ping Services',
description: 'Pings your services and shows their status as an indicator',
icon: Plug,
component: PingComponent,
};
export default function PingComponent(props: any) {
type State = 'loading' | 'down' | 'online';
const { config } = useConfig();
const { url }: { url: string } = props;
const [isOnline, setOnline] = useState<State>('loading');
useEffect(() => {
if (!config.settings.enabledModules.includes('Ping Services')) {
return;
}
axios
.get('/api/modules/ping', { params: { url } })
.then(() => {
setOnline('online');
})
.catch(() => {
setOnline('down');
});
}, []);
if (!config.settings.enabledModules.includes('Ping Services')) {
return null;
}
return (
<Tooltip
radius="lg"
style={{ position: 'absolute', bottom: 20, right: 20 }}
label={isOnline === 'loading' ? 'Loading...' : isOnline === 'online' ? 'Online' : 'Offline'}
>
<Indicator
size={13}
color={isOnline === 'online' ? 'green' : isOnline === 'down' ? 'red' : 'yellow'}
>
{null}
</Indicator>
</Tooltip>
);
}

View File

@@ -0,0 +1 @@
export { PingModule } from './PingModule';

8
src/pages/[slug].tsx Normal file
View File

@@ -0,0 +1,8 @@
import { Title } from '@mantine/core';
import { useRouter } from 'next/router';
export default function SlugPage(props: any) {
const router = useRouter();
const { slug } = router.query;
return <Title>ok</Title>;
}

View File

@@ -0,0 +1,29 @@
import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
async function Get(req: NextApiRequest, res: NextApiResponse) {
// Parse req.body as a ServiceItem
const { url } = req.query;
await axios
.get(url as string)
.then((response) => {
res.status(200).json(response.data);
})
.catch((error) => {
res.status(500).json(error);
});
// // Make a request to the URL
// const response = await axios.get(url);
// // Return the response
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
// Filter out if the reuqest is a POST or a GET
if (req.method === 'GET') {
return Get(req, res);
}
return res.status(405).json({
statusCode: 405,
message: 'Method not allowed',
});
};