import { useState, type FormEvent } from 'react' import { useTranslation } from 'react-i18next' import type { GithubSyncSchedulePolicy, GithubSyncSchedulePolicyInput } from '@/api/client' import { useUpdateGithubSyncSchedulePolicy } from './use-github-sync-admin' import { toast } from '@/shared/lib/toast' import { Card } from '@/shared/ui/card' import { Input } from '@/shared/ui/input' import { Button } from '@/shared/ui/button' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle } from '@/shared/ui/dialog' const fields = ['repositoryHours', 'curatedOwnerHours', 'searchRuleHours'] as const type Field = typeof fields[number] export function GithubScheduleConfig({ policy }: { policy: GithubSyncSchedulePolicy }) { const { t } = useTranslation() const mutation = useUpdateGithubSyncSchedulePolicy() const [open, setOpen] = useState(false) const [hours, setHours] = useState>({ repositoryHours: policy.repositoryHours?.toString() ?? '', curatedOwnerHours: policy.curatedOwnerHours?.toString() ?? '24', searchRuleHours: policy.searchRuleHours?.toString() ?? '72', }) const openEditor = () => { setHours({ repositoryHours: policy.repositoryHours?.toString() ?? '', curatedOwnerHours: policy.curatedOwnerHours?.toString() ?? '24', searchRuleHours: policy.searchRuleHours?.toString() ?? '72', }) setOpen(true) } const save = async (event: FormEvent) => { event.preventDefault() const body = { repositoryHours: Number(hours.repositoryHours), curatedOwnerHours: Number(hours.curatedOwnerHours), searchRuleHours: Number(hours.searchRuleHours), } satisfies GithubSyncSchedulePolicyInput if (Object.values(body).some((value) => !Number.isInteger(value) || value < 1 || value > 720)) { toast.error(t('githubSync.scheduleInvalid')) return } try { await mutation.mutateAsync(body) setOpen(false) toast.success(t('githubSync.saved')) } catch (error) { toast.error(t('githubSync.saveFailed'), error instanceof Error ? error.message : '') } } return

{t('githubSync.scheduleTitle')}

{t('githubSync.scheduleHelp')}

{fields.map((field) =>
{t(`githubSync.${field}`)}

{policy[field] == null ? '—' : `${policy[field]} ${t('githubSync.hoursUnit')}`}

)}
{policy.repositoryHours == null ?

{t('githubSync.scheduleLegacy', { interval: policy.effectiveRepositoryInterval })}

: null} {t('githubSync.scheduleTitle')}{t('githubSync.scheduleHelp')}
{fields.map((field) => )}
}