generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
EditAppModal.tsx
246 lines (220 loc) · 7.8 KB
/
EditAppModal.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { Alert, Button, Group, Popover, Stack, Tabs, Text, ThemeIcon } from '@mantine/core';
import { useForm } from '@mantine/form';
import { useDisclosure } from '@mantine/hooks';
import { ContextModalProps } from '@mantine/modals';
import {
IconAccessPoint,
IconAdjustments,
IconAlertTriangle,
IconBrush,
IconClick,
IconPlug,
} from '@tabler/icons-react';
import { useTranslation } from 'next-i18next';
import { removeTrailingSlash } from 'next/dist/shared/lib/router/utils/remove-trailing-slash';
import { useState } from 'react';
import { useConfigContext } from '~/config/provider';
import { useConfigStore } from '~/config/store';
import { AppType } from '~/types/app';
import { DebouncedImage } from '../../../IconSelector/DebouncedImage';
import { useEditModeStore } from '../../Views/useEditModeStore';
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';
import { EditAppModalTab } from './Tabs/type';
const appUrlRegex =
'(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,})';
const appUrlWithAnyProtocolRegex =
'([A-z]+://(?: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,}|[A-z]+://(?:www.|(?!www))\\[?[a-zA-Z0-9]+\\]?.[^\\s]{2,}|www.[a-zA-Z0-9]+.[^\\s]{2,})';
export const EditAppModal = ({
context,
id,
innerProps,
}: ContextModalProps<{ app: AppType; allowAppNamePropagation: boolean }>) => {
const { t } = useTranslation(['layout/modals/add-app', 'common']);
const { name: configName, config } = useConfigContext();
const updateConfig = useConfigStore((store) => store.updateConfig);
const { enabled: isEditMode } = useEditModeStore();
const [allowAppNamePropagation, setAllowAppNamePropagation] = useState<boolean>(
innerProps.allowAppNamePropagation
);
const form = useForm<AppType>({
initialValues: innerProps.app,
validate: {
name: (name) => (!name ? t('validation.name') : null),
url: (url) => {
if (!url) {
return t('validation.noUrl');
}
if (!url.match(appUrlRegex)) {
return t('validation.invalidUrl');
}
return null;
},
appearance: {
iconUrl: (url: string) => {
if (url.length < 1) {
return t('validation.noIconUrl');
}
return null;
},
},
behaviour: {
externalUrl: (url: string) => {
if (url === undefined || url.length < 1) {
return t('validation.noExternalUri');
}
if (!url.match(appUrlWithAnyProtocolRegex)) {
return t('validation.invalidExternalUri');
}
return null;
},
},
},
validateInputOnChange: true,
});
const onSubmit = (values: AppType) => {
if (!configName) {
return;
}
values.url = removeTrailingSlash(values.url);
void updateConfig(
configName,
(previousConfig) => ({
...previousConfig,
apps: [
...previousConfig.apps.filter((x) => x.id !== values.id),
{
...values,
},
],
}),
true,
!isEditMode
);
// also close the parent modal
context.closeAll();
umami.track('Add app', { name: values.name });
};
const [activeTab, setActiveTab] = useState<EditAppModalTab>('general');
const closeModal = () => {
context.closeModal(id);
};
const validationErrors = Object.keys(form.errors);
const ValidationErrorIndicator = ({ keys }: { keys: string[] }) => {
const relevantErrors = validationErrors.filter((x) => keys.includes(x));
return (
<ThemeIcon
opacity={relevantErrors.length === 0 ? 0 : 1}
color="red"
size={18}
variant="light"
>
<IconAlertTriangle size={15} />
</ThemeIcon>
);
};
return (
<>
{configName === undefined ||
(config === undefined && (
<Alert color="red">
There was an unexpected problem loading the configuration. Functionality might be
restricted. Please report this incident.
</Alert>
))}
<Stack spacing={0} align="center" my="lg">
<DebouncedImage src={form.values.appearance.iconUrl} width={120} height={120} />
<Text align="center" weight="bold" size="lg" mt="md">
{form.values.name ?? 'New App'}
</Text>
</Stack>
<form onSubmit={form.onSubmit(onSubmit)}>
<Stack
justify="space-between"
style={{
minHeight: 300,
}}
>
<Tabs
value={activeTab}
onTabChange={(tab) => setActiveTab(tab as EditAppModalTab)}
defaultValue="general"
radius="md"
>
<Tabs.List grow>
<Tabs.Tab
rightSection={<ValidationErrorIndicator keys={['name', 'url']} />}
icon={<IconAdjustments size={14} />}
value="general"
>
{t('tabs.general')}
</Tabs.Tab>
<Tabs.Tab
rightSection={<ValidationErrorIndicator keys={['behaviour.externalUrl']} />}
icon={<IconClick size={14} />}
value="behaviour"
>
{t('tabs.behaviour')}
</Tabs.Tab>
<Tabs.Tab
rightSection={<ValidationErrorIndicator keys={[]} />}
icon={<IconAccessPoint size={14} />}
value="network"
>
{t('tabs.network')}
</Tabs.Tab>
<Tabs.Tab
rightSection={<ValidationErrorIndicator keys={['appearance.iconUrl']} />}
icon={<IconBrush size={14} />}
value="appearance"
>
{t('tabs.appearance')}
</Tabs.Tab>
<Tabs.Tab
rightSection={<ValidationErrorIndicator keys={[]} />}
icon={<IconPlug size={14} />}
value="integration"
>
{t('tabs.integration')}
</Tabs.Tab>
</Tabs.List>
<GeneralTab form={form} openTab={(targetTab) => setActiveTab(targetTab)} />
<BehaviourTab form={form} />
<NetworkTab form={form} />
<AppearanceTab
form={form}
disallowAppNamePropagation={() => setAllowAppNamePropagation(false)}
allowAppNamePropagation={allowAppNamePropagation}
/>
<IntegrationTab form={form} />
</Tabs>
<Group noWrap position="right" mt="md">
<Button onClick={closeModal} px={50} variant="light" color="gray">
{t('common:cancel')}
</Button>
<SaveButton formIsValid={form.isValid()} />
</Group>
</Stack>
</form>
</>
);
};
const SaveButton = ({ formIsValid }: { formIsValid: boolean }) => {
const [opened, { close, open }] = useDisclosure(false);
const { t } = useTranslation(['layout/modals/add-app', 'common']);
return (
<Popover opened={opened && !formIsValid} width={300} withArrow withinPortal>
<Popover.Target>
<div onMouseEnter={open} onMouseLeave={close}>
<Button disabled={!formIsValid} px={50} type="submit">
{t('common:save')}
</Button>
</div>
</Popover.Target>
<Popover.Dropdown sx={{ pointerEvents: 'none' }}>{t('validation.popover')}</Popover.Dropdown>
</Popover>
);
};