2023-07-18 07:38:27 +02:00
|
|
|
import { Badge, Box, Button, Card, Group, Image, SimpleGrid, Stack, Text } from '@mantine/core';
|
2023-07-13 22:43:14 +02:00
|
|
|
import { useElementSize } from '@mantine/hooks';
|
2023-05-15 17:40:59 +09:00
|
|
|
import { IconDeviceGamepad, IconPlayerPlay, IconPlayerStop } from '@tabler/icons-react';
|
2023-07-18 07:38:27 +02:00
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
|
|
import { api } from '~/utils/api';
|
|
|
|
|
|
2023-05-06 19:51:53 +02:00
|
|
|
import { useConfigContext } from '../../config/provider';
|
2023-07-18 07:38:27 +02:00
|
|
|
import { queryClient } from '../../tools/server/configurations/tanstack/queryClient.tool';
|
2023-05-06 19:51:53 +02:00
|
|
|
import { defineWidget } from '../helper';
|
|
|
|
|
import { WidgetLoading } from '../loading';
|
|
|
|
|
import { IWidget } from '../widgets';
|
2023-06-10 14:04:32 +02:00
|
|
|
import { useDnsHoleSummeryQuery } from './DnsHoleSummary';
|
2023-07-18 07:38:27 +02:00
|
|
|
import { PiholeApiSummaryType } from './type';
|
2023-05-06 19:51:53 +02:00
|
|
|
|
|
|
|
|
const definition = defineWidget({
|
|
|
|
|
id: 'dns-hole-controls',
|
|
|
|
|
icon: IconDeviceGamepad,
|
|
|
|
|
options: {},
|
|
|
|
|
gridstack: {
|
2023-07-13 22:43:14 +02:00
|
|
|
minWidth: 2,
|
|
|
|
|
minHeight: 1,
|
2023-05-06 19:51:53 +02:00
|
|
|
maxWidth: 12,
|
|
|
|
|
maxHeight: 12,
|
|
|
|
|
},
|
|
|
|
|
component: DnsHoleControlsWidgetTile,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export type IDnsHoleControlsWidget = IWidget<(typeof definition)['id'], typeof definition>;
|
|
|
|
|
|
|
|
|
|
interface DnsHoleControlsWidgetProps {
|
|
|
|
|
widget: IDnsHoleControlsWidget;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function DnsHoleControlsWidgetTile({ widget }: DnsHoleControlsWidgetProps) {
|
2023-06-10 14:04:32 +02:00
|
|
|
const { isInitialLoading, data } = useDnsHoleSummeryQuery();
|
2023-05-06 19:51:53 +02:00
|
|
|
const { mutateAsync } = useDnsHoleControlMutation();
|
2023-07-13 22:43:14 +02:00
|
|
|
const { width, ref } = useElementSize();
|
2023-05-16 14:10:04 +09:00
|
|
|
const { t } = useTranslation('common');
|
2023-05-06 19:51:53 +02:00
|
|
|
|
2023-06-10 13:38:07 +02:00
|
|
|
const { name: configName, config } = useConfigContext();
|
2023-05-06 19:51:53 +02:00
|
|
|
|
2023-06-10 13:38:07 +02:00
|
|
|
if (isInitialLoading || !data || !configName) {
|
2023-05-06 19:51:53 +02:00
|
|
|
return <WidgetLoading />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2023-07-18 07:38:27 +02:00
|
|
|
<Stack justify="space-between" h={'100%'} spacing="0.25rem">
|
|
|
|
|
<SimpleGrid ref={ref} cols={width > 275 ? 2 : 1} verticalSpacing="0.25rem" spacing="0.25rem">
|
2023-05-06 19:51:53 +02:00
|
|
|
<Button
|
|
|
|
|
onClick={async () => {
|
2023-06-10 13:38:07 +02:00
|
|
|
await mutateAsync({
|
2023-07-18 07:38:27 +02:00
|
|
|
action: 'enable',
|
2023-06-10 13:38:07 +02:00
|
|
|
configName,
|
|
|
|
|
});
|
2023-05-06 19:51:53 +02:00
|
|
|
await queryClient.invalidateQueries({ queryKey: ['dns-hole-summary'] });
|
|
|
|
|
}}
|
|
|
|
|
leftIcon={<IconPlayerPlay size={20} />}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="green"
|
2023-07-17 01:21:39 +02:00
|
|
|
h="2rem"
|
2023-05-06 19:51:53 +02:00
|
|
|
>
|
2023-05-16 14:10:04 +09:00
|
|
|
{t('enableAll')}
|
2023-05-06 19:51:53 +02:00
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={async () => {
|
2023-06-10 13:38:07 +02:00
|
|
|
await mutateAsync({
|
2023-07-18 07:38:27 +02:00
|
|
|
action: 'disable',
|
2023-06-10 13:38:07 +02:00
|
|
|
configName,
|
|
|
|
|
});
|
2023-05-06 19:51:53 +02:00
|
|
|
await queryClient.invalidateQueries({ queryKey: ['dns-hole-summary'] });
|
|
|
|
|
}}
|
|
|
|
|
leftIcon={<IconPlayerStop size={20} />}
|
|
|
|
|
variant="light"
|
|
|
|
|
color="red"
|
2023-07-17 01:21:39 +02:00
|
|
|
h="2rem"
|
2023-05-06 19:51:53 +02:00
|
|
|
>
|
2023-05-16 14:10:04 +09:00
|
|
|
{t('disableAll')}
|
2023-05-06 19:51:53 +02:00
|
|
|
</Button>
|
2023-07-13 22:43:14 +02:00
|
|
|
</SimpleGrid>
|
2023-05-06 19:51:53 +02:00
|
|
|
|
2023-07-17 01:21:39 +02:00
|
|
|
<Stack spacing="0.25rem">
|
2023-07-13 22:43:14 +02:00
|
|
|
{data.status.map((status, index) => {
|
|
|
|
|
const app = config?.apps.find((x) => x.id === status.appId);
|
2023-05-06 19:51:53 +02:00
|
|
|
|
2023-07-13 22:43:14 +02:00
|
|
|
if (!app) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-05-06 19:51:53 +02:00
|
|
|
|
2023-07-13 22:43:14 +02:00
|
|
|
return (
|
|
|
|
|
<Card withBorder={true} key={index} p="xs">
|
2023-05-06 19:51:53 +02:00
|
|
|
<Group>
|
|
|
|
|
<Box
|
2023-07-18 07:38:27 +02:00
|
|
|
sx={(theme) => ({
|
|
|
|
|
backgroundColor:
|
|
|
|
|
theme.colorScheme === 'dark' ? theme.colors.dark[4] : theme.colors.gray[2],
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
padding: 5,
|
|
|
|
|
borderRadius: theme.radius.md,
|
|
|
|
|
})}
|
|
|
|
|
>
|
|
|
|
|
<Image src={app.appearance.iconUrl} width={40} height={40} fit="contain" />
|
2023-05-06 19:51:53 +02:00
|
|
|
</Box>
|
2023-07-17 01:21:39 +02:00
|
|
|
<Stack spacing="0rem">
|
2023-07-13 22:43:14 +02:00
|
|
|
<Text>{app.name}</Text>
|
|
|
|
|
<StatusBadge status={status.status} />
|
|
|
|
|
</Stack>
|
2023-05-06 19:51:53 +02:00
|
|
|
</Group>
|
2023-07-13 22:43:14 +02:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</Stack>
|
2023-05-06 19:51:53 +02:00
|
|
|
</Stack>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StatusBadge = ({ status }: { status: PiholeApiSummaryType['status'] }) => {
|
2023-05-16 14:10:04 +09:00
|
|
|
const { t } = useTranslation('common');
|
2023-05-06 19:51:53 +02:00
|
|
|
if (status === 'enabled') {
|
|
|
|
|
return (
|
|
|
|
|
<Badge variant="dot" color="green">
|
2023-05-16 14:10:04 +09:00
|
|
|
{t('enabled')}
|
2023-05-06 19:51:53 +02:00
|
|
|
</Badge>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Badge variant="dot" color="red">
|
2023-05-16 14:10:04 +09:00
|
|
|
{t('disabled')}
|
2023-05-06 19:51:53 +02:00
|
|
|
</Badge>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-10 13:38:07 +02:00
|
|
|
const useDnsHoleControlMutation = () => api.dnsHole.control.useMutation();
|
|
|
|
|
|
2023-05-06 19:51:53 +02:00
|
|
|
export default definition;
|