2022-12-06 20:48:35 +01:00
|
|
|
import { Alert, Button, createStyles, Group, Stack, Tabs, Text } from '@mantine/core';
|
2022-12-04 21:19:40 +01:00
|
|
|
import { useForm } from '@mantine/form';
|
2022-12-05 21:43:47 +01:00
|
|
|
import { ContextModalProps } from '@mantine/modals';
|
|
|
|
|
import { hideNotification, showNotification } from '@mantine/notifications';
|
2022-12-06 20:48:35 +01:00
|
|
|
import { IconAccessPoint, IconAdjustments, IconBrush, IconClick, IconPlug } from '@tabler/icons';
|
2022-12-04 21:19:40 +01:00
|
|
|
import { useTranslation } from 'next-i18next';
|
2022-12-05 21:43:47 +01:00
|
|
|
import Image from 'next/image';
|
2022-12-06 20:48:35 +01:00
|
|
|
import { useState } from 'react';
|
|
|
|
|
import { useConfigContext } from '../../../../config/provider';
|
|
|
|
|
import { useConfigStore } from '../../../../config/store';
|
2022-12-04 21:19:40 +01:00
|
|
|
import { ServiceType } from '../../../../types/service';
|
|
|
|
|
import { AppearanceTab } from './Tabs/AppereanceTab/AppereanceTab';
|
|
|
|
|
import { BehaviourTab } from './Tabs/BehaviourTab/BehaviourTab';
|
|
|
|
|
import { GeneralTab } from './Tabs/GeneralTab/GeneralTab';
|
|
|
|
|
import { IntegrationTab } from './Tabs/IntegrationTab/IntegrationTab';
|
|
|
|
|
import { NetworkTab } from './Tabs/NetworkTab/NetworkTab';
|
2022-12-06 20:48:35 +01:00
|
|
|
import { EditServiceModalTab } from './Tabs/type';
|
|
|
|
|
|
|
|
|
|
const serviceUrlRegex =
|
|
|
|
|
'(https?://(?:www.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\\s]{2,}|www.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9].[^\\s]{2,}|https?://(?:www.|(?!www))[a-zA-Z0-9]+.[^\\s]{2,}|www.[a-zA-Z0-9]+.[^\\s]{2,})';
|
2022-12-04 21:19:40 +01:00
|
|
|
|
|
|
|
|
export const EditServiceModal = ({
|
|
|
|
|
context,
|
|
|
|
|
id,
|
|
|
|
|
innerProps,
|
|
|
|
|
}: ContextModalProps<{ service: ServiceType }>) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { classes } = useStyles();
|
2022-12-06 20:48:35 +01:00
|
|
|
const { name: configName, config } = useConfigContext();
|
|
|
|
|
const updateConfig = useConfigStore((store) => store.updateConfig);
|
2022-12-04 21:19:40 +01:00
|
|
|
|
|
|
|
|
const form = useForm<ServiceType>({
|
|
|
|
|
initialValues: innerProps.service,
|
2022-12-06 20:48:35 +01:00
|
|
|
validate: {
|
|
|
|
|
name: (name) => (!name ? 'Name is required' : null),
|
|
|
|
|
url: (url) => {
|
|
|
|
|
if (!url) {
|
|
|
|
|
return 'Url is required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!url.match(serviceUrlRegex)) {
|
|
|
|
|
return 'Value is not a valid url';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
appearance: (appearance) => (!appearance.iconUrl ? 'Icon is required' : null),
|
|
|
|
|
behaviour: (behaviour) => {
|
|
|
|
|
if (behaviour.onClickUrl === undefined || behaviour.onClickUrl.length < 1) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!behaviour.onClickUrl?.match(serviceUrlRegex)) {
|
|
|
|
|
return 'Uri override is not a valid uri';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
validateInputOnChange: true,
|
2022-12-04 21:19:40 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const onSubmit = (values: ServiceType) => {
|
2022-12-06 20:48:35 +01:00
|
|
|
if (!configName) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateConfig(configName, (previousConfig) => ({
|
|
|
|
|
...previousConfig,
|
|
|
|
|
services: [...previousConfig.services.filter((x) => x.id !== form.values.id), form.values],
|
|
|
|
|
}));
|
2022-12-07 10:48:20 +01:00
|
|
|
|
|
|
|
|
// also close the parent modal
|
|
|
|
|
context.closeAll();
|
2022-12-04 21:19:40 +01:00
|
|
|
};
|
|
|
|
|
|
2022-12-06 20:48:35 +01:00
|
|
|
const [activeTab, setActiveTab] = useState<EditServiceModalTab>('general');
|
|
|
|
|
|
2022-12-04 21:19:40 +01:00
|
|
|
const tryCloseModal = () => {
|
|
|
|
|
if (form.isDirty()) {
|
|
|
|
|
showNotification({
|
|
|
|
|
id: 'unsaved-edit-service-modal-changes',
|
|
|
|
|
title: 'You have unsaved changes',
|
|
|
|
|
message: (
|
|
|
|
|
<Stack>
|
|
|
|
|
<Text color="dimmed">If you close, your changes will be discarded and not saved.</Text>
|
|
|
|
|
<Button
|
|
|
|
|
onClick={() => {
|
|
|
|
|
context.closeModal(id);
|
|
|
|
|
hideNotification('unsaved-edit-service-modal-changes');
|
|
|
|
|
}}
|
|
|
|
|
variant="light"
|
|
|
|
|
>
|
|
|
|
|
Close anyway
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
|
|
|
|
),
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
context.closeModal(id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
2022-12-06 20:48:35 +01:00
|
|
|
{configName === undefined ||
|
|
|
|
|
(config === undefined && (
|
|
|
|
|
<Alert color="red">
|
|
|
|
|
There was an unexpected problem loading the configuration. Functionality might be
|
|
|
|
|
restricted. Please report this incident.
|
|
|
|
|
</Alert>
|
|
|
|
|
))}
|
2022-12-04 21:19:40 +01:00
|
|
|
<Stack spacing={0} align="center" my="lg">
|
|
|
|
|
{form.values.appearance.iconUrl ? (
|
|
|
|
|
// disabled because image target is too dynamic for next image cache
|
|
|
|
|
// eslint-disable-next-line @next/next/no-img-element
|
|
|
|
|
<img
|
|
|
|
|
className={classes.serviceImage}
|
|
|
|
|
src={form.values.appearance.iconUrl}
|
|
|
|
|
width={120}
|
|
|
|
|
height={120}
|
|
|
|
|
alt="service icon"
|
|
|
|
|
/>
|
|
|
|
|
) : (
|
|
|
|
|
<Image src="/favicon-squared.png" width={120} height={120} />
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<Text align="center" weight="bold" size="lg" mt="md">
|
|
|
|
|
{form.values.name ?? 'New Service'}
|
|
|
|
|
</Text>
|
|
|
|
|
</Stack>
|
|
|
|
|
<form onSubmit={form.onSubmit(onSubmit)}>
|
2022-12-06 20:48:35 +01:00
|
|
|
<Tabs
|
|
|
|
|
value={activeTab}
|
|
|
|
|
onTabChange={(tab) => setActiveTab(tab as EditServiceModalTab)}
|
|
|
|
|
defaultValue="general"
|
|
|
|
|
>
|
2022-12-04 21:19:40 +01:00
|
|
|
<Tabs.List grow>
|
|
|
|
|
<Tabs.Tab value="general" icon={<IconAdjustments size={14} />}>
|
|
|
|
|
General
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab value="behaviour" icon={<IconClick size={14} />}>
|
|
|
|
|
Behaviour
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab value="network" icon={<IconAccessPoint size={14} />}>
|
|
|
|
|
Network
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab value="appearance" icon={<IconBrush size={14} />}>
|
|
|
|
|
Appearance
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
<Tabs.Tab value="integration" icon={<IconPlug size={14} />}>
|
|
|
|
|
Integration
|
|
|
|
|
</Tabs.Tab>
|
|
|
|
|
</Tabs.List>
|
|
|
|
|
|
2022-12-06 20:48:35 +01:00
|
|
|
<GeneralTab form={form} openTab={(targetTab) => setActiveTab(targetTab)} />
|
2022-12-04 21:19:40 +01:00
|
|
|
<BehaviourTab form={form} />
|
|
|
|
|
<NetworkTab form={form} />
|
|
|
|
|
<AppearanceTab form={form} />
|
|
|
|
|
<IntegrationTab form={form} />
|
|
|
|
|
</Tabs>
|
|
|
|
|
|
|
|
|
|
<Group position="right" mt={100}>
|
2022-12-06 20:48:35 +01:00
|
|
|
<Button px={50} variant="light" color="gray" onClick={tryCloseModal}>
|
2022-12-04 21:19:40 +01:00
|
|
|
Cancel
|
|
|
|
|
</Button>
|
2022-12-06 20:48:35 +01:00
|
|
|
<Button type="submit" px={50}>
|
2022-12-04 21:19:40 +01:00
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
</Group>
|
|
|
|
|
</form>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const useStyles = createStyles(() => ({
|
|
|
|
|
serviceImage: {
|
|
|
|
|
objectFit: 'contain',
|
|
|
|
|
},
|
|
|
|
|
}));
|