2023-06-20 22:02:00 +02:00
|
|
|
import { Box, Indicator, Tooltip } from '@mantine/core';
|
|
|
|
|
import { IconCheck, IconCheckbox, IconDownload, IconLoader, IconX } from '@tabler/icons-react';
|
2023-04-21 10:21:34 +02:00
|
|
|
import Consola from 'consola';
|
2023-06-20 22:02:00 +02:00
|
|
|
import { TargetAndTransition, Transition, motion } from 'framer-motion';
|
2022-12-11 13:58:28 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2023-06-14 17:31:52 +09:00
|
|
|
import { api } from '~/utils/api';
|
|
|
|
|
|
2022-12-11 13:58:28 +01:00
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
2022-12-18 22:27:01 +01:00
|
|
|
import { AppType } from '../../../../types/app';
|
2022-12-11 13:58:28 +01:00
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
interface AppPingProps {
|
|
|
|
|
app: AppType;
|
2022-12-11 13:58:28 +01:00
|
|
|
}
|
|
|
|
|
|
2022-12-18 22:27:01 +01:00
|
|
|
export const AppPing = ({ app }: AppPingProps) => {
|
2022-12-11 13:58:28 +01:00
|
|
|
const { t } = useTranslation('modules/ping');
|
|
|
|
|
const { config } = useConfigContext();
|
|
|
|
|
const active =
|
2022-12-18 22:27:01 +01:00
|
|
|
(config?.settings.customization.layout.enabledPing && app.network.enabledStatusChecker) ??
|
2022-12-11 13:58:28 +01:00
|
|
|
false;
|
2023-06-20 22:02:00 +02:00
|
|
|
|
2023-06-14 17:31:52 +09:00
|
|
|
const { data, isLoading, isFetching, isSuccess } = api.app.ping.useQuery(app.id, {
|
|
|
|
|
retry: false,
|
|
|
|
|
enabled: active,
|
|
|
|
|
select: (data) => {
|
|
|
|
|
const isOk = getIsOk(app, data.status);
|
|
|
|
|
Consola.info(`Ping ${app.name} (${app.url}) ${data.status} ${isOk}`);
|
|
|
|
|
return {
|
|
|
|
|
status: data.status,
|
|
|
|
|
state: isOk ? ('online' as const) : ('down' as const),
|
|
|
|
|
statusText: data.statusText,
|
|
|
|
|
};
|
|
|
|
|
},
|
|
|
|
|
});
|
2022-12-11 13:58:28 +01:00
|
|
|
|
|
|
|
|
if (!active) return null;
|
|
|
|
|
|
2023-06-20 22:02:00 +02:00
|
|
|
const isOnline = data?.state === 'online';
|
|
|
|
|
|
|
|
|
|
const disablePulse = config?.settings.customization.accessibility?.disablePingPulse ?? false;
|
|
|
|
|
const replaceDotWithIcon =
|
|
|
|
|
config?.settings.customization.accessibility?.replacePingDotsWithIcons ?? false;
|
|
|
|
|
|
|
|
|
|
const scaleAnimation = isOnline ? [1, 0.7, 1] : 1;
|
|
|
|
|
const animate: TargetAndTransition | undefined = disablePulse
|
|
|
|
|
? undefined
|
|
|
|
|
: {
|
|
|
|
|
scale: scaleAnimation,
|
|
|
|
|
};
|
|
|
|
|
const transition: Transition | undefined = disablePulse
|
|
|
|
|
? undefined
|
|
|
|
|
: {
|
|
|
|
|
repeat: Infinity,
|
|
|
|
|
duration: 2.5,
|
|
|
|
|
ease: 'easeInOut',
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-11 13:58:28 +01:00
|
|
|
return (
|
2023-06-20 22:02:00 +02:00
|
|
|
<motion.div
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
bottom: replaceDotWithIcon ? 5 : 20,
|
|
|
|
|
right: replaceDotWithIcon ? 8 : 20,
|
|
|
|
|
zIndex: 2,
|
|
|
|
|
}}
|
|
|
|
|
animate={animate}
|
|
|
|
|
transition={transition}
|
|
|
|
|
>
|
2022-12-11 13:58:28 +01:00
|
|
|
<Tooltip
|
|
|
|
|
withinPortal
|
|
|
|
|
radius="lg"
|
|
|
|
|
label={
|
|
|
|
|
isLoading
|
|
|
|
|
? t('states.loading')
|
2023-06-14 17:31:52 +09:00
|
|
|
: data?.state === 'online'
|
|
|
|
|
? t('states.online', { response: data?.status ?? 'N/A' })
|
|
|
|
|
: `${data?.statusText} ${data?.status}`
|
2022-12-11 13:58:28 +01:00
|
|
|
}
|
|
|
|
|
>
|
2023-06-20 22:02:00 +02:00
|
|
|
{config?.settings.customization.accessibility?.replacePingDotsWithIcons ? (
|
|
|
|
|
<Box>
|
|
|
|
|
<AccessibleIndicatorPing isLoading={isLoading} isOnline={isOnline} />
|
|
|
|
|
</Box>
|
|
|
|
|
) : (
|
|
|
|
|
<Indicator
|
|
|
|
|
size={15}
|
|
|
|
|
color={isLoading ? 'yellow' : isOnline ? 'green' : 'red'}
|
|
|
|
|
children={null}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2022-12-11 13:58:28 +01:00
|
|
|
</Tooltip>
|
2023-06-20 22:02:00 +02:00
|
|
|
</motion.div>
|
2022-12-11 13:58:28 +01:00
|
|
|
);
|
|
|
|
|
};
|
2023-04-21 10:21:34 +02:00
|
|
|
|
2023-06-20 22:02:00 +02:00
|
|
|
const AccessibleIndicatorPing = ({
|
|
|
|
|
isLoading,
|
|
|
|
|
isOnline,
|
|
|
|
|
}: {
|
|
|
|
|
isOnline: boolean;
|
|
|
|
|
isLoading: boolean;
|
|
|
|
|
}) => {
|
|
|
|
|
if (isOnline) {
|
|
|
|
|
return <IconCheck color="green" />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isLoading) {
|
|
|
|
|
return <IconLoader />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <IconX color="red" />;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-14 17:31:52 +09:00
|
|
|
export const getIsOk = (app: AppType, status: number) => {
|
2023-06-10 10:23:54 +02:00
|
|
|
if (app.network.okStatus === undefined || app.network.statusCodes.length >= 1) {
|
|
|
|
|
Consola.log('Using new status codes');
|
|
|
|
|
return app.network.statusCodes.includes(status.toString());
|
|
|
|
|
}
|
|
|
|
|
Consola.warn('Using deprecated okStatus');
|
|
|
|
|
return app.network.okStatus.includes(status);
|
2023-04-21 10:21:34 +02:00
|
|
|
};
|