forked from halo-dev/halo
/
PluginList.vue
323 lines (298 loc) · 9.75 KB
/
PluginList.vue
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<script lang="ts" setup>
import {
Dialog,
IconAddCircle,
IconPlug,
IconRefreshLine,
VButton,
VCard,
VDropdown,
VDropdownItem,
VEmpty,
VLoading,
VPageHeader,
VSpace,
} from "@halo-dev/components";
import PluginListItem from "./components/PluginListItem.vue";
import PluginInstallationModal from "./components/PluginInstallationModal.vue";
import type { Ref } from "vue";
import { computed, onMounted, provide, ref, watch } from "vue";
import { apiClient } from "@/utils/api-client";
import { usePermission } from "@/utils/permission";
import { useQuery } from "@tanstack/vue-query";
import { PluginStatusPhaseEnum, type Plugin } from "@halo-dev/api-client";
import { useI18n } from "vue-i18n";
import { useRouteQuery } from "@vueuse/router";
import { usePluginBatchOperations } from "./composables/use-plugin";
const { t } = useI18n();
const { currentUserHasPermission } = usePermission();
const pluginInstallationModalVisible = ref(false);
const keyword = ref("");
const selectedEnabledValue = ref();
const selectedSortValue = ref();
const hasFilters = computed(() => {
return selectedEnabledValue.value !== undefined || selectedSortValue.value;
});
function handleClearFilters() {
selectedSortValue.value = undefined;
selectedEnabledValue.value = undefined;
}
const total = ref(0);
const { data, isLoading, isFetching, refetch } = useQuery<Plugin[]>({
queryKey: ["plugins", keyword, selectedEnabledValue, selectedSortValue],
queryFn: async () => {
const { data } = await apiClient.plugin.listPlugins({
page: 0,
size: 0,
keyword: keyword.value,
enabled: selectedEnabledValue.value,
sort: [selectedSortValue.value].filter(Boolean) as string[],
});
total.value = data.total;
return data.items;
},
keepPreviousData: true,
refetchInterval: (data) => {
const hasDeletingData = data?.some(
(plugin) => !!plugin.metadata.deletionTimestamp
);
if (hasDeletingData) {
return 1000;
}
const hasStartingData = data?.some(
(plugin) =>
plugin.spec.enabled &&
plugin.status?.phase !==
(PluginStatusPhaseEnum.Started || PluginStatusPhaseEnum.Failed)
);
if (hasStartingData) {
return 3000;
}
return false;
},
});
// selection
const selectedNames = ref<string[]>([]);
provide<Ref<string[]>>("selectedNames", selectedNames);
const checkedAll = ref(false);
watch(
() => selectedNames.value,
(value) => {
checkedAll.value = value.length === data.value?.length;
}
);
const handleCheckAllChange = (e: Event) => {
const { checked } = e.target as HTMLInputElement;
if (checked) {
selectedNames.value =
data.value?.map((plugin) => {
return plugin.metadata.name;
}) || [];
} else {
selectedNames.value.length = 0;
}
};
const { handleChangeStatusInBatch, handleUninstallInBatch } =
usePluginBatchOperations(selectedNames);
// handle remote download url from route
const routeRemoteDownloadUrl = useRouteQuery<string | null>(
"remote-download-url"
);
onMounted(() => {
if (routeRemoteDownloadUrl.value) {
Dialog.warning({
title: t("core.plugin.operations.remote_download.title"),
description: t("core.plugin.operations.remote_download.description", {
url: routeRemoteDownloadUrl.value,
}),
confirmText: t("core.common.buttons.download"),
cancelText: t("core.common.buttons.cancel"),
onConfirm() {
pluginInstallationModalVisible.value = true;
},
onCancel() {
routeRemoteDownloadUrl.value = null;
},
});
}
});
</script>
<template>
<PluginInstallationModal
v-if="
pluginInstallationModalVisible &&
currentUserHasPermission(['system:plugins:manage'])
"
@close="pluginInstallationModalVisible = false"
/>
<VPageHeader :title="$t('core.plugin.title')">
<template #icon>
<IconPlug class="mr-2 self-center" />
</template>
<template #actions>
<VButton
v-permission="['system:plugins:manage']"
type="secondary"
@click="pluginInstallationModalVisible = true"
>
<template #icon>
<IconAddCircle class="h-full w-full" />
</template>
{{ $t("core.common.buttons.install") }}
</VButton>
</template>
</VPageHeader>
<div class="m-0 md:m-4">
<VCard :body-class="['!p-0']">
<template #header>
<div class="block w-full bg-gray-50 px-4 py-3">
<div
class="relative flex flex-col flex-wrap items-start gap-4 sm:flex-row sm:items-center"
>
<div
v-permission="['system:posts:manage']"
class="hidden items-center sm:flex"
>
<input
v-model="checkedAll"
type="checkbox"
@change="handleCheckAllChange"
/>
</div>
<div class="flex w-full flex-1 items-center gap-2 sm:w-auto">
<SearchInput v-if="!selectedNames.length" v-model="keyword" />
<VSpace v-else>
<VButton @click="handleChangeStatusInBatch(true)">
{{ $t("core.common.buttons.activate") }}
</VButton>
<VButton @click="handleChangeStatusInBatch(false)">
{{ $t("core.common.buttons.inactivate") }}
</VButton>
<VDropdown>
<VButton type="danger">
{{ $t("core.common.buttons.uninstall") }}
</VButton>
<template #popper>
<VDropdownItem
type="danger"
@click="handleUninstallInBatch(false)"
>
{{ $t("core.common.buttons.uninstall") }}
</VDropdownItem>
<VDropdownItem
type="danger"
@click="handleUninstallInBatch(true)"
>
{{
$t(
"core.plugin.operations.uninstall_and_delete_config.button"
)
}}
</VDropdownItem>
</template>
</VDropdown>
</VSpace>
</div>
<VSpace spacing="lg" class="flex-wrap">
<FilterCleanButton
v-if="hasFilters"
@click="handleClearFilters"
/>
<FilterDropdown
v-model="selectedEnabledValue"
:label="$t('core.common.filters.labels.status')"
:items="[
{
label: t('core.common.filters.item_labels.all'),
},
{
label: t('core.plugin.filters.status.items.active'),
value: true,
},
{
label: t('core.plugin.filters.status.items.inactive'),
value: false,
},
]"
/>
<FilterDropdown
v-model="selectedSortValue"
:label="$t('core.common.filters.labels.sort')"
:items="[
{
label: t('core.common.filters.item_labels.default'),
},
{
label: t('core.plugin.filters.sort.items.create_time_desc'),
value: 'creationTimestamp,desc',
},
{
label: t('core.plugin.filters.sort.items.create_time_asc'),
value: 'creationTimestamp,asc',
},
]"
/>
<div class="flex flex-row gap-2">
<div
class="group cursor-pointer rounded p-1 hover:bg-gray-200"
@click="refetch()"
>
<IconRefreshLine
v-tooltip="$t('core.common.buttons.refresh')"
:class="{ 'animate-spin text-gray-900': isFetching }"
class="h-4 w-4 text-gray-600 group-hover:text-gray-900"
/>
</div>
</div>
</VSpace>
</div>
</div>
</template>
<VLoading v-if="isLoading" />
<Transition v-else-if="!data?.length" appear name="fade">
<VEmpty
:message="$t('core.plugin.empty.message')"
:title="$t('core.plugin.empty.title')"
>
<template #actions>
<VSpace>
<VButton :loading="isFetching" @click="refetch()">
{{ $t("core.common.buttons.refresh") }}
</VButton>
<VButton
v-permission="['system:plugins:manage']"
type="secondary"
@click="pluginInstallationModalVisible = true"
>
<template #icon>
<IconAddCircle class="h-full w-full" />
</template>
{{ $t("core.plugin.empty.actions.install") }}
</VButton>
</VSpace>
</template>
</VEmpty>
</Transition>
<Transition v-else appear name="fade">
<ul
class="box-border h-full w-full divide-y divide-gray-100"
role="list"
>
<li v-for="plugin in data" :key="plugin.metadata.name">
<PluginListItem
:plugin="plugin"
:is-selected="selectedNames.includes(plugin.metadata.name)"
/>
</li>
</ul>
</Transition>
<template #footer>
<div class="flex h-8 items-center">
<span class="text-sm text-gray-500">
{{ $t("core.components.pagination.total_label", { total: total }) }}
</span>
</div>
</template>
</VCard>
</div>
</template>