|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed, ref, watchEffect } from 'vue' |
| 3 | +import { useAsyncData } from '#app' |
| 4 | +import { toast } from 'vue-sonner' |
| 5 | +import { Button } from '~/components/ui/button' |
| 6 | +import { |
| 7 | + Card, |
| 8 | + CardContent, |
| 9 | + CardDescription, |
| 10 | + CardFooter, |
| 11 | + CardHeader, |
| 12 | + CardTitle |
| 13 | +} from '~/components/ui/card' |
| 14 | +import { Input } from '~/components/ui/input' |
| 15 | +import { SETTINGS } from '~~/shared/constants/settings' |
| 16 | +import type { SettingsSchema } from '~~/shared/schemas/settings' |
| 17 | +
|
| 18 | +const DELETE_OLD_TTL = SETTINGS.delete_old_ttl_minutes |
| 19 | +const TTL_MINUTES_MIN = DELETE_OLD_TTL.min |
| 20 | +const TTL_MINUTES_MAX = DELETE_OLD_TTL.max |
| 21 | +
|
| 22 | +const saving = ref(false) |
| 23 | +const { data, pending, refresh } = useAsyncData<SettingsSchema>( |
| 24 | + 'settings', |
| 25 | + () => $fetch('/api/settings') |
| 26 | +) |
| 27 | +
|
| 28 | +const ttlInput = computed({ |
| 29 | + get: () => data.value?.delete_old_ttl_minutes ?? '', |
| 30 | + set: (raw: string) => { |
| 31 | + const parsed = parseInt(raw, 10) |
| 32 | + if (isNaN(parsed)) { |
| 33 | + data.value!.delete_old_ttl_minutes = null |
| 34 | + } else { |
| 35 | + data.value!.delete_old_ttl_minutes = parsed |
| 36 | + } |
| 37 | + } |
| 38 | +}) |
| 39 | +
|
| 40 | +const hasValidationError = ref(false) |
| 41 | +
|
| 42 | +async function handleSubmit() { |
| 43 | + try { |
| 44 | + saving.value = true |
| 45 | + const response = await $fetch('/api/settings', { |
| 46 | + method: 'PUT', |
| 47 | + body: data.value |
| 48 | + }) |
| 49 | + saving.value = false |
| 50 | + } catch (error) { |
| 51 | + saving.value = false |
| 52 | + toast.error('Failed to save settings. Please try again.') |
| 53 | + return |
| 54 | + } |
| 55 | +
|
| 56 | + toast.success('Settings saved successfully!') |
| 57 | + await refresh() |
| 58 | +} |
| 59 | +</script> |
| 60 | + |
| 61 | +<template> |
| 62 | + <div class="container py-6"> |
| 63 | + <Card class="max-w-xl"> |
| 64 | + <form @submit.prevent="handleSubmit"> |
| 65 | + <CardHeader> |
| 66 | + <CardTitle>Settings</CardTitle> |
| 67 | + <CardDescription> |
| 68 | + Control how long task metadata should be retained before the |
| 69 | + automated cleanup runs the delete-old job. |
| 70 | + </CardDescription> |
| 71 | + </CardHeader> |
| 72 | + <CardContent |
| 73 | + v-if="data" |
| 74 | + class="flex flex-col gap-4" |
| 75 | + > |
| 76 | + <label class="flex flex-col gap-2"> |
| 77 | + <span class="text-sm font-medium text-foreground" |
| 78 | + >TTL (minutes)</span |
| 79 | + > |
| 80 | + <Input |
| 81 | + type="number" |
| 82 | + v-model="ttlInput" |
| 83 | + :disabled="pending" |
| 84 | + placeholder="Enter number of minutes" |
| 85 | + /> |
| 86 | + <span class="text-xs text-muted-foreground"> |
| 87 | + Minimum {{ TTL_MINUTES_MIN }} minute. Maximum |
| 88 | + {{ TTL_MINUTES_MAX }} minutes (~1 year). Leave this field empty to |
| 89 | + disable automatic cleanup. |
| 90 | + </span> |
| 91 | + </label> |
| 92 | + <p |
| 93 | + v-if="hasValidationError" |
| 94 | + class="text-xs text-red-500" |
| 95 | + > |
| 96 | + Value must be between {{ TTL_MINUTES_MIN }} and |
| 97 | + {{ TTL_MINUTES_MAX }}. |
| 98 | + </p> |
| 99 | + </CardContent> |
| 100 | + <CardFooter class="flex justify-end"> |
| 101 | + <Button |
| 102 | + type="submit" |
| 103 | + :disabled="saving" |
| 104 | + class="cursor-pointer" |
| 105 | + > |
| 106 | + Save |
| 107 | + </Button> |
| 108 | + </CardFooter> |
| 109 | + </form> |
| 110 | + </Card> |
| 111 | + </div> |
| 112 | +</template> |
0 commit comments