-
Notifications
You must be signed in to change notification settings - Fork 268
/
ContainerActions.svelte
185 lines (169 loc) · 5.33 KB
/
ContainerActions.svelte
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
<script lang="ts">
import {
faAlignLeft,
faFileCode,
faPlay,
faRocket,
faTerminal,
faStop,
faArrowsRotate,
faTrash,
faExternalLinkSquareAlt,
} from '@fortawesome/free-solid-svg-icons';
import { ContainerGroupInfoTypeUI, type ContainerInfoUI } from './ContainerInfoUI';
import { router } from 'tinro';
import ListItemButtonIcon from '../ui/ListItemButtonIcon.svelte';
import DropdownMenu from '../ui/DropdownMenu.svelte';
import FlatMenu from '../ui/FlatMenu.svelte';
import type { Menu } from '../../../../main/src/plugin/menu-registry';
import ContributionActions from '/@/lib/actions/ContributionActions.svelte';
export let container: ContainerInfoUI;
export let dropdownMenu = false;
export let detailed = false;
export let contributions: Menu[] = [];
export let inProgressCallback: (inProgress: boolean, state?: string) => void = () => {};
export let errorCallback: (erroMessage: string) => void = () => {};
async function startContainer() {
inProgressCallback(true, 'STARTING');
try {
await window.startContainer(container.engineId, container.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'RUNNING');
}
}
async function restartContainer() {
inProgressCallback(true, 'RESTARTING');
try {
await window.restartContainer(container.engineId, container.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
async function stopContainer() {
inProgressCallback(true, 'STOPPING');
try {
await window.stopContainer(container.engineId, container.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'STOPPED');
}
}
function openBrowser(): void {
if (!container.openingUrl) {
return;
}
window.openExternal(container.openingUrl);
}
function openLogs(): void {
router.goto(`/containers/${container.id}/logs`);
}
async function deleteContainer(): Promise<void> {
inProgressCallback(true, 'DELETING');
try {
await window.deleteContainer(container.engineId, container.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
function openTerminalContainer(): void {
router.goto(`/containers/${container.id}/terminal`);
}
function openGenerateKube(): void {
router.goto(`/containers/${container.id}/kube`);
}
function deployToKubernetes(): void {
router.goto(`/deploy-to-kube/${container.id}/${container.engineId}`);
}
// If dropdownMenu = true, we'll change style to the imported dropdownMenu style
// otherwise, leave blank.
let actionsStyle: typeof DropdownMenu | typeof FlatMenu;
if (dropdownMenu) {
actionsStyle = DropdownMenu;
} else {
actionsStyle = FlatMenu;
}
</script>
<ListItemButtonIcon
title="Start Container"
onClick="{() => startContainer()}"
hidden="{container.state === 'RUNNING' || container.state === 'STOPPING'}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'STARTING'}"
icon="{faPlay}"
iconOffset="pl-[0.15rem]" />
<ListItemButtonIcon
title="Stop Container"
onClick="{() => stopContainer()}"
hidden="{!(container.state === 'RUNNING' || container.state === 'STOPPING')}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'STOPPING'}"
icon="{faStop}" />
<ListItemButtonIcon
title="Delete Container"
onClick="{() => deleteContainer()}"
icon="{faTrash}"
detailed="{detailed}"
inProgress="{container.actionInProgress && container.state === 'DELETING'}" />
<!-- If dropdownMenu is true, use it, otherwise just show the regular buttons -->
<svelte:component this="{actionsStyle}">
{#if !detailed}
<ListItemButtonIcon
title="Open Logs"
onClick="{() => openLogs()}"
menu="{dropdownMenu}"
detailed="{false}"
icon="{faAlignLeft}" />
<ListItemButtonIcon
title="Generate Kube"
onClick="{() => openGenerateKube()}"
menu="{dropdownMenu}"
hidden="{!(
container.engineType === 'podman' && container.groupInfo.type === ContainerGroupInfoTypeUI.STANDALONE
)}"
detailed="{detailed}"
icon="{faFileCode}" />
{/if}
<ListItemButtonIcon
title="Deploy to Kubernetes"
onClick="{() => deployToKubernetes()}"
menu="{dropdownMenu}"
hidden="{!(container.engineType === 'podman' && container.groupInfo.type === ContainerGroupInfoTypeUI.STANDALONE)}"
detailed="{detailed}"
icon="{faRocket}" />
<ListItemButtonIcon
title="Open Browser"
onClick="{() => openBrowser()}"
menu="{dropdownMenu}"
enabled="{container.state === 'RUNNING' && container.hasPublicPort}"
hidden="{dropdownMenu && container.state !== 'RUNNING'}"
detailed="{detailed}"
icon="{faExternalLinkSquareAlt}" />
{#if !detailed}
<ListItemButtonIcon
title="Open Terminal"
onClick="{() => openTerminalContainer()}"
menu="{dropdownMenu}"
hidden="{!(container.state === 'RUNNING')}"
detailed="{false}"
icon="{faTerminal}" />
{/if}
<ListItemButtonIcon
title="Restart Container"
onClick="{() => restartContainer()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faArrowsRotate}" />
<ContributionActions
args="{[container]}"
contextPrefix="containerListItem"
dropdownMenu="{dropdownMenu}"
contributions="{contributions}"
onError="{errorCallback}" />
</svelte:component>