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

@@ -1,3 +1,4 @@
export * from './date';
export * from './calendar';
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';