Skip to content

Commit e3481c8

Browse files
committed
feat(web): Simplify network configuration in Docker Compose
- Remove individual external network switch for each network. - Allow all networks to be marked as external or to have a driver, streamlining the configuration
1 parent 52820d9 commit e3481c8

File tree

3 files changed

+34
-35
lines changed

3 files changed

+34
-35
lines changed

web/src/pages/docker-compose/components/network-fields.tsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const NetworkFields: FC = () => {
1515
name: 'networks.app_network',
1616
});
1717

18-
const customNetwork = watch('networks.custom');
18+
const customNetwork = watch('networks.external_network');
1919

2020
const handleRemoveNetwork = (index: number) => {
2121
remove(index);
@@ -25,7 +25,6 @@ const NetworkFields: FC = () => {
2525
const networkData = customNetwork
2626
? {
2727
network_name: '',
28-
external: false,
2928
name: '',
3029
}
3130
: {
@@ -52,11 +51,14 @@ const NetworkFields: FC = () => {
5251
Add <Plus className="size-3" />
5352
</button>
5453
</div>
55-
<FormCheckbox label="Custom" name="networks.custom" />
54+
<FormCheckbox
55+
label="External Network"
56+
name="networks.external_network"
57+
/>
5658
</div>
5759

5860
<div className="space-y-4">
59-
<div className="w-full rounded-md border border-gray-500 p-5">
61+
<div className="flex w-full flex-col gap-4 rounded-md border border-gray-500 p-5">
6062
{fields.map((field, index) => (
6163
<div key={field.id} className="mb-4">
6264
<div className="mb-4 flex items-center justify-between">
@@ -72,14 +74,6 @@ const NetworkFields: FC = () => {
7274
</div>
7375

7476
<div>
75-
{customNetwork && (
76-
<div className="mb-2 flex justify-end">
77-
<FormCheckbox
78-
name={`networks.app_network.${index}.external`}
79-
label="External Network"
80-
/>
81-
</div>
82-
)}
8377
<div className="flex items-center gap-3 [&>div]:flex-1">
8478
<FormInput
8579
name={`networks.app_network.${index}.network_name`}

web/src/pages/docker-compose/docker-compose.tsx

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ const DockerCompose: FC = () => {
8686
},
8787
],
8888
networks: {
89-
custom: false,
89+
external_network: false,
9090
app_network: [
9191
{
9292
network_name: '',
@@ -180,25 +180,31 @@ const DockerCompose: FC = () => {
180180
}),
181181
);
182182

183-
const refactoredNetwork = data.networks.app_network.reduce(
184-
(acc: INetworkConfig, network) => {
185-
if (!data.networks.custom) {
186-
if ('driver' in network) {
183+
let refactoredNetwork: any;
184+
185+
if (!data.networks.app_network.some((network) => network.network_name)) {
186+
refactoredNetwork = null;
187+
} else {
188+
refactoredNetwork = data.networks.app_network.reduce(
189+
(acc: INetworkConfig, network) => {
190+
if (!data.networks.external_network) {
191+
if ('driver' in network) {
192+
acc[network.network_name] = {
193+
driver: network.driver?.value,
194+
};
195+
}
196+
}
197+
if ('name' in network) {
187198
acc[network.network_name] = {
188-
driver: network.driver?.value,
199+
name: network.name,
200+
external_network: true,
189201
};
190202
}
191-
}
192-
if ('name' in network && 'external' in network) {
193-
acc[network.network_name] = {
194-
name: network.name,
195-
external: !!network.external,
196-
};
197-
}
198-
return acc;
199-
},
200-
{},
201-
);
203+
return acc;
204+
},
205+
{},
206+
);
207+
}
202208

203209
const services = refactoredService.map((item) => {
204210
const bodyService = {

web/src/pages/docker-compose/docker-compose.type.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export interface INetworkConfig {
3131
}
3232
| {
3333
name: string;
34-
external: boolean;
34+
external_network: boolean;
3535
};
3636
}
3737

@@ -117,20 +117,19 @@ const labelValueSchema = zod.object({
117117

118118
export const NetworkSchema = zod.union([
119119
zod.object({
120-
custom: zod.literal(false),
120+
external_network: zod.literal(false),
121121
app_network: zod.array(
122122
zod.object({
123-
network_name: zod.string().min(1, 'Network name is required.'),
123+
network_name: zod.string(),
124124
driver: labelValueSchema,
125125
}),
126126
),
127127
}),
128128
zod.object({
129-
custom: zod.literal(true),
129+
external_network: zod.literal(true),
130130
app_network: zod.array(
131131
zod.object({
132132
network_name: zod.string().min(1, 'Network name is required.'),
133-
external: zod.boolean().optional(),
134133
name: zod.string().min(1, 'Name is required.'),
135134
}),
136135
),
@@ -162,7 +161,7 @@ export type DockerCompose = {
162161
depends_on: string[] | null;
163162
};
164163
}[];
165-
networks: INetworkConfig;
164+
networks: INetworkConfig | null;
166165
};
167166

168167
type AppNetwork = {

0 commit comments

Comments
 (0)