generated from mantinedev/next-pages-template
-
-
Notifications
You must be signed in to change notification settings - Fork 253
/
docker-select-board.modal.tsx
143 lines (133 loc) · 4.22 KB
/
docker-select-board.modal.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
import { Button, Group, Select, Stack, Text, Title } from '@mantine/core';
import { useForm } from '@mantine/form';
import { ContextModalProps, modals } from '@mantine/modals';
import { showNotification } from '@mantine/notifications';
import { IconCheck, IconX } from '@tabler/icons-react';
import { ContainerInfo } from 'dockerode';
import { Trans, useTranslation } from 'next-i18next';
import { z } from 'zod';
import { useConfigContext } from '~/config/provider';
import { useConfigStore } from '~/config/store';
import { generateDefaultApp } from '~/tools/shared/app';
import { api } from '~/utils/api';
import { useI18nZodResolver } from '~/utils/i18n-zod-resolver';
const dockerSelectBoardSchema = z.object({
board: z.string().nonempty(),
});
type InnerProps = {
containers: (ContainerInfo & { icon?: string })[];
};
type FormType = z.infer<typeof dockerSelectBoardSchema>;
export const DockerSelectBoardModal = ({ id, innerProps }: ContextModalProps<InnerProps>) => {
const { t } = useTranslation('tools/docker');
const { mutateAsync, isLoading } = api.boards.addAppsForContainers.useMutation();
const { i18nZodResolver } = useI18nZodResolver();
const { name: configName } = useConfigContext();
const updateConfig = useConfigStore((store) => store.updateConfig);
const handleSubmit = async (values: FormType) => {
const newApps = innerProps.containers.map((container) => ({
name: (container.Names.at(0) ?? 'App').replace('/', ''),
port: container.Ports.at(0)?.PublicPort,
icon: container.icon,
}));
await mutateAsync(
{
apps: newApps,
boardName: values.board,
},
{
onSuccess: () => {
showNotification({
title: t('notifications.selectBoard.success.title'),
message: t('notifications.selectBoard.success.message'),
icon: <IconCheck />,
color: 'green',
});
updateConfig(configName!, (config) => {
const lowestWrapper = config?.wrappers.sort((a, b) => a.position - b.position)[0];
const defaultApp = generateDefaultApp(lowestWrapper.id);
return {
...config,
apps: [
...config.apps,
...newApps.map((app) => ({
...defaultApp,
...app,
wrapperId: lowestWrapper.id,
})),
],
};
});
modals.close(id);
},
onError: () => {
showNotification({
title: t('notifications.selectBoard.error.title'),
message: t('notifications.selectBoard.error.message'),
icon: <IconX />,
color: 'red',
});
},
}
);
};
const form = useForm<FormType>({
initialValues: {
board: '',
},
validate: i18nZodResolver(dockerSelectBoardSchema),
});
const { data: boards } = api.boards.all.useQuery();
return (
<form onSubmit={form.onSubmit(handleSubmit)}>
<Stack>
<Text>{t('modals.selectBoard.text')}</Text>
<Select
label={t('modals.selectBoard.form.board.label')}
withAsterisk
withinPortal
data={
boards?.map((board) => ({
value: board.name,
label: board.name,
})) ?? []
}
{...form.getInputProps('board')}
/>
<Group grow>
<Button
onClick={() => {
modals.close(id);
}}
variant="light"
color="gray"
type="button"
>
{t('common:cancel')}
</Button>
<Button
type="submit"
onClick={async () => {}}
disabled={isLoading}
variant="light"
color="green"
>
{t('modals.selectBoard.form.submit')}
</Button>
</Group>
</Stack>
</form>
);
};
export const openDockerSelectBoardModal = (innerProps: InnerProps) => {
modals.openContextModal({
modal: 'dockerSelectBoardModal',
title: (
<Title order={4}>
<Trans i18nKey="tools/docker:modals.selectBoard.title" />
</Title>
),
innerProps,
});
umami.track('Add to homarr modal');
};