|
| 1 | +<script lang="ts" setup> |
| 2 | +import { aiCoreApiClient } from '@/api' |
| 3 | +import type { AiModel } from '@/api/generated' |
| 4 | +import { QK_MODELS } from '@/composables/use-models-fetch' |
| 5 | +import { Toast, VSwitch } from '@halo-dev/components' |
| 6 | +import { useMutation, useQueryClient } from '@tanstack/vue-query' |
| 7 | +import { computed } from 'vue' |
| 8 | +
|
| 9 | +const props = defineProps<{ |
| 10 | + model: AiModel |
| 11 | +}>() |
| 12 | +
|
| 13 | +const queryClient = useQueryClient() |
| 14 | +
|
| 15 | +const enabled = computed(() => props.model.spec.enabled !== false) |
| 16 | +
|
| 17 | +const mutation = useMutation({ |
| 18 | + mutationFn: async (nextEnabled: boolean) => { |
| 19 | + return await aiCoreApiClient.model.patchAiModel({ |
| 20 | + name: props.model.metadata.name, |
| 21 | + jsonPatchInner: [ |
| 22 | + { |
| 23 | + op: 'add', |
| 24 | + path: '/spec/enabled', |
| 25 | + value: nextEnabled, |
| 26 | + }, |
| 27 | + ], |
| 28 | + }) |
| 29 | + }, |
| 30 | + onSuccess: (_data, nextEnabled) => { |
| 31 | + Toast.success(nextEnabled ? '模型已启用' : '模型已禁用') |
| 32 | + queryClient.invalidateQueries({ queryKey: [QK_MODELS] }) |
| 33 | + }, |
| 34 | + onError: (error) => { |
| 35 | + Toast.error('模型状态更新失败: ' + (error as Error).message) |
| 36 | + }, |
| 37 | +}) |
| 38 | +
|
| 39 | +const isUpdating = computed(() => mutation.isPending.value) |
| 40 | +const disabled = computed(() => !!props.model.metadata.deletionTimestamp || isUpdating.value) |
| 41 | +
|
| 42 | +function handleToggle() { |
| 43 | + if (disabled.value) { |
| 44 | + return |
| 45 | + } |
| 46 | + mutation.mutate(!enabled.value) |
| 47 | +} |
| 48 | +</script> |
| 49 | + |
| 50 | +<template> |
| 51 | + <VSwitch |
| 52 | + :model-value="enabled" |
| 53 | + :loading="isUpdating" |
| 54 | + :disabled="disabled" |
| 55 | + v-tooltip="enabled ? '禁用模型' : '启用模型'" |
| 56 | + @click.stop="handleToggle" |
| 57 | + /> |
| 58 | +</template> |
0 commit comments