-
Notifications
You must be signed in to change notification settings - Fork 268
/
PodActions.svelte
138 lines (128 loc) · 4.16 KB
/
PodActions.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
<script lang="ts">
import { faFileCode, faPlay, faRocket, faStop, faArrowsRotate, faTrash } from '@fortawesome/free-solid-svg-icons';
import type { PodInfoUI } from './PodInfoUI';
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 pod: PodInfoUI;
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 startPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'STARTING');
try {
await window.startPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'RUNNING');
}
}
async function restartPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'RESTARTING');
try {
await window.restartPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
async function stopPod(podInfoUI: PodInfoUI) {
inProgressCallback(true, 'STOPPING');
try {
await window.stopPod(podInfoUI.engineId, podInfoUI.id);
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false, 'STOPPED');
}
}
async function deletePod(podInfoUI: PodInfoUI): Promise<void> {
inProgressCallback(true, 'DELETING');
try {
if (pod.kind === 'podman') {
await window.removePod(podInfoUI.engineId, podInfoUI.id);
} else {
await window.kubernetesDeletePod(podInfoUI.name);
}
} catch (error) {
errorCallback(String(error));
} finally {
inProgressCallback(false);
}
}
function openGenerateKube(): void {
router.goto(`/pods/${encodeURI(pod.kind)}/${encodeURI(pod.name)}/${encodeURI(pod.engineId)}/kube`);
}
function deployToKubernetes(): void {
router.goto(`/deploy-to-kube/${pod.id}/${pod.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>
{#if pod.kind === 'podman'}
<ListItemButtonIcon
title="Start Pod"
onClick="{() => startPod(pod)}"
hidden="{pod.status === 'RUNNING' || pod.status === 'STOPPING'}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'STARTING'}"
icon="{faPlay}"
iconOffset="pl-[0.15rem]" />
<ListItemButtonIcon
title="Stop Pod"
onClick="{() => stopPod(pod)}"
hidden="{!(pod.status === 'RUNNING' || pod.status === 'STOPPING')}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'STOPPING'}"
icon="{faStop}" />
{/if}
<ListItemButtonIcon
title="Delete Pod"
onClick="{() => deletePod(pod)}"
icon="{faTrash}"
detailed="{detailed}"
inProgress="{pod.actionInProgress && pod.status === 'DELETING'}" />
<!-- If dropdownMenu is true, use it, otherwise just show the regular buttons -->
<svelte:component this="{actionsStyle}">
{#if pod.kind === 'podman'}
{#if !detailed}
<ListItemButtonIcon
title="Generate Kube"
onClick="{() => openGenerateKube()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faFileCode}" />
{/if}
<ListItemButtonIcon
title="Deploy to Kubernetes"
onClick="{() => deployToKubernetes()}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faRocket}" />
<ListItemButtonIcon
title="Restart Pod"
onClick="{() => restartPod(pod)}"
menu="{dropdownMenu}"
detailed="{detailed}"
icon="{faArrowsRotate}" />
{/if}
<ContributionActions
args="{[pod]}"
contextPrefix="podListItem"
dropdownMenu="{dropdownMenu}"
contributions="{contributions}"
onError="{errorCallback}" />
</svelte:component>