/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import { z } from 'zod'
import { useForm, type Resolver } from 'react-hook-form'
import { zodResolver } from '@hookform/resolvers/zod'
import { useTranslation } from 'react-i18next'
import { toast } from 'sonner'
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from '@/components/ui/form'
import { Input } from '@/components/ui/input'
import { Switch } from '@/components/ui/switch'
import {
SettingsForm,
SettingsSwitchContent,
SettingsSwitchItem,
} from '../components/settings-form-layout'
import { SettingsPageFormActions } from '../components/settings-page-context'
import { SettingsSection } from '../components/settings-section'
import { useUpdateOption } from '../hooks/use-update-option'
const schema = z.object({
enabled: z.boolean(),
minQuota: z.coerce.number().int().min(0),
maxQuota: z.coerce.number().int().min(0),
})
type Values = z.infer
export function CheckinSettingsSection({
defaultValues,
}: {
defaultValues: {
enabled: boolean
minQuota: number
maxQuota: number
}
}) {
const { t } = useTranslation()
const updateOption = useUpdateOption()
const form = useForm({
resolver: zodResolver(schema) as unknown as Resolver,
defaultValues: {
enabled: defaultValues.enabled,
minQuota: defaultValues.minQuota,
maxQuota: defaultValues.maxQuota,
},
})
const { isDirty, isSubmitting } = form.formState
const enabled = form.watch('enabled')
async function onSubmit(values: Values) {
const updates: Array<{ key: string; value: string }> = []
if (values.enabled !== defaultValues.enabled) {
updates.push({
key: 'checkin_setting.enabled',
value: String(values.enabled),
})
}
if (values.minQuota !== defaultValues.minQuota) {
updates.push({
key: 'checkin_setting.min_quota',
value: String(values.minQuota),
})
}
if (values.maxQuota !== defaultValues.maxQuota) {
updates.push({
key: 'checkin_setting.max_quota',
value: String(values.maxQuota),
})
}
if (updates.length === 0) {
toast.info(t('No changes to save'))
return
}
for (const update of updates) {
await updateOption.mutateAsync(update)
}
form.reset(values)
}
return (
)
}