mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-05 18:39:53 +08:00
271 lines
8.9 KiB
TypeScript
271 lines
8.9 KiB
TypeScript
'use client'
|
|
|
|
import { useEffect, useState } from 'react'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { Dialog } from '@/shared/ui/Dialog'
|
|
import { getPartnerTask, savePartnerTask, type PartnerTask } from '@/lib/partners-api'
|
|
|
|
const control = 'rounded-lg focus:outline-none focus-visible:bg-[var(--secondary)]'
|
|
const field = `w-full rounded-xl border border-border bg-transparent px-4 py-3 ${control}`
|
|
const row = 'flex flex-wrap items-center justify-between gap-3 px-4 py-3'
|
|
|
|
function localTime(ms?: number | null) {
|
|
if (!ms) return ''
|
|
const date = new Date(ms)
|
|
return new Date(ms - date.getTimezoneOffset() * 60000).toISOString().slice(0, 19)
|
|
}
|
|
|
|
export function PartnerTaskEditor({
|
|
partnerId,
|
|
partnerName,
|
|
sessionKey,
|
|
jobId,
|
|
messageTime,
|
|
description,
|
|
onClose,
|
|
onSaved,
|
|
}: {
|
|
partnerId: string
|
|
partnerName: string
|
|
sessionKey: string
|
|
jobId?: string
|
|
messageTime?: string
|
|
description: string
|
|
onClose: () => void
|
|
onSaved: (id: string) => void
|
|
}) {
|
|
const { t } = useTranslation()
|
|
const [original, setOriginal] = useState<PartnerTask | null>(null)
|
|
const [loading, setLoading] = useState(Boolean(jobId))
|
|
const [ready, setReady] = useState(!jobId)
|
|
const [saving, setSaving] = useState(false)
|
|
const [error, setError] = useState('')
|
|
const [name, setName] = useState(description.split('\n')[0].slice(0, 48))
|
|
const [message, setMessage] = useState(description)
|
|
const [kind, setKind] = useState<PartnerTask['schedule']['kind']>('at')
|
|
const [when, setWhen] = useState('')
|
|
const [amount, setAmount] = useState('1')
|
|
const [unit, setUnit] = useState(3600)
|
|
const [expr, setExpr] = useState('')
|
|
const [timezone, setTimezone] = useState('')
|
|
|
|
useEffect(() => {
|
|
if (!jobId) return
|
|
let active = true
|
|
getPartnerTask(partnerId, sessionKey, jobId)
|
|
.then(task => {
|
|
if (!active) return
|
|
if (task) {
|
|
setOriginal(task)
|
|
setName(task.name)
|
|
setMessage(task.message)
|
|
setKind(task.schedule.kind)
|
|
setWhen(localTime(task.schedule.at_ms))
|
|
const seconds = task.schedule.every_seconds ?? 3600
|
|
const scale = seconds % 3600 === 0 ? 3600 : seconds % 60 === 0 ? 60 : 1
|
|
setUnit(scale)
|
|
setAmount(String(seconds / scale))
|
|
setExpr(task.schedule.expr ?? '')
|
|
setTimezone(task.schedule.tz ?? '')
|
|
}
|
|
setReady(true)
|
|
})
|
|
.catch(e => {
|
|
if (active) setError(String(e.message ?? e))
|
|
})
|
|
.finally(() => {
|
|
if (active) setLoading(false)
|
|
})
|
|
return () => {
|
|
active = false
|
|
}
|
|
}, [jobId, partnerId, sessionKey])
|
|
|
|
const recorded = messageTime ? new Date(messageTime) : null
|
|
const seconds = Number(amount) * unit
|
|
const at =
|
|
original?.schedule.kind === 'at' && when === localTime(original.schedule.at_ms)
|
|
? original.schedule.at_ms
|
|
: new Date(when).getTime()
|
|
const schedule: PartnerTask['schedule'] =
|
|
kind === 'at'
|
|
? { kind, at_ms: at }
|
|
: kind === 'every'
|
|
? { kind, every_seconds: seconds }
|
|
: { kind, expr: expr.trim(), tz: timezone.trim() || null }
|
|
const unchanged =
|
|
original &&
|
|
original.name === name.trim() &&
|
|
original.message === message.trim() &&
|
|
Object.entries(schedule).every(
|
|
([key, value]) => original.schedule[key as keyof typeof schedule] === value
|
|
)
|
|
const valid =
|
|
name.trim() &&
|
|
message.trim() &&
|
|
(kind === 'at'
|
|
? at && at > Date.now()
|
|
: kind === 'every'
|
|
? Number.isSafeInteger(seconds) && seconds >= 30
|
|
: expr.trim())
|
|
|
|
async function save() {
|
|
if (!ready || saving || !valid || unchanged) return
|
|
setSaving(true)
|
|
setError('')
|
|
try {
|
|
const result = await savePartnerTask(
|
|
partnerId,
|
|
sessionKey,
|
|
{ name: name.trim(), message: message.trim(), schedule },
|
|
original ? jobId : undefined
|
|
)
|
|
onSaved(result.id)
|
|
} catch (e) {
|
|
setError(e instanceof Error ? e.message : String(e))
|
|
setSaving(false)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<Dialog
|
|
open
|
|
onClose={onClose}
|
|
title={t('Edit task')}
|
|
closeLabel={t('Close')}
|
|
size="lg"
|
|
busy={saving}
|
|
footer={
|
|
<>
|
|
<button
|
|
type="button"
|
|
disabled={saving}
|
|
className={`px-4 py-2 ${control}`}
|
|
onClick={onClose}
|
|
>
|
|
{t('Cancel')}
|
|
</button>
|
|
<button
|
|
type="button"
|
|
disabled={!ready || saving || !valid || Boolean(unchanged)}
|
|
onClick={save}
|
|
className="rounded-full bg-foreground px-5 py-2 text-background disabled:opacity-40 focus:outline-none focus-visible:underline"
|
|
>
|
|
{t(original || loading ? 'Save' : 'Schedule again')}
|
|
</button>
|
|
</>
|
|
}
|
|
>
|
|
{loading && <p>{t('Loading...')}</p>}
|
|
{ready && (
|
|
<fieldset disabled={saving} className="space-y-4 text-sm">
|
|
<label className="grid gap-1.5">
|
|
<span className="text-xs font-medium text-muted-foreground">{t('Task name')}</span>
|
|
<input className={field} value={name} onChange={e => setName(e.target.value)} />
|
|
</label>
|
|
<label className="grid gap-1.5">
|
|
<span className="text-xs font-medium text-muted-foreground">{t('Task description')}</span>
|
|
<textarea
|
|
className={`${field} min-h-32 resize-y leading-relaxed`}
|
|
value={message}
|
|
onChange={e => setMessage(e.target.value)}
|
|
/>
|
|
</label>
|
|
<div className="divide-y divide-border rounded-xl border border-border">
|
|
{recorded && Number.isFinite(recorded.getTime()) && (
|
|
<div className={row}>
|
|
<span>{t('Reminder message time')}</span>
|
|
<time dateTime={messageTime}>{recorded.toLocaleString()}</time>
|
|
</div>
|
|
)}
|
|
<label className={row}>
|
|
{t('Repeat')}
|
|
<select
|
|
className={control}
|
|
value={kind}
|
|
onChange={e => setKind(e.target.value as typeof kind)}
|
|
>
|
|
<option value="at">{t('Once')}</option>
|
|
<option value="every">{t('Fixed interval')}</option>
|
|
<option value="cron">Cron</option>
|
|
</select>
|
|
</label>
|
|
{kind === 'every' ? (
|
|
<div className={row}>
|
|
<label className="flex grow items-center justify-between">
|
|
{t('Every')}
|
|
<input
|
|
type="number"
|
|
min={unit === 1 ? 30 : 1}
|
|
className={`w-20 text-right ${control}`}
|
|
value={amount}
|
|
onChange={e => setAmount(e.target.value)}
|
|
/>
|
|
</label>
|
|
<select
|
|
aria-label={t('Interval unit')}
|
|
className={control}
|
|
value={unit}
|
|
onChange={e => setUnit(Number(e.target.value))}
|
|
>
|
|
<option value={1}>{t('Seconds')}</option>
|
|
<option value={60}>{t('Minutes')}</option>
|
|
<option value={3600}>{t('Hours')}</option>
|
|
</select>
|
|
</div>
|
|
) : kind === 'at' ? (
|
|
<label className={row}>
|
|
{t(original ? 'Run at' : 'New run time')}
|
|
<input
|
|
type="datetime-local"
|
|
step="1"
|
|
className={control}
|
|
value={when}
|
|
onChange={e => setWhen(e.target.value)}
|
|
/>
|
|
</label>
|
|
) : (
|
|
<label className={row}>
|
|
{t('Cron expression')}
|
|
<input
|
|
className={control}
|
|
value={expr}
|
|
placeholder="0 9 * * *"
|
|
onChange={e => setExpr(e.target.value)}
|
|
/>
|
|
</label>
|
|
)}
|
|
</div>
|
|
<details>
|
|
<summary className={`w-fit cursor-pointer text-muted-foreground ${control}`}>
|
|
{t('Advanced')}
|
|
</summary>
|
|
<div className="mt-2 divide-y divide-border rounded-xl border border-border">
|
|
<div className={row}>
|
|
<span>{t('Runs in')}</span>
|
|
<span>{partnerName}</span>
|
|
</div>
|
|
{kind === 'cron' && (
|
|
<label className={row}>
|
|
{t('Time zone')}
|
|
<input
|
|
className={control}
|
|
value={timezone}
|
|
placeholder={t('Server local time')}
|
|
onChange={e => setTimezone(e.target.value)}
|
|
/>
|
|
</label>
|
|
)}
|
|
</div>
|
|
</details>
|
|
</fieldset>
|
|
)}
|
|
{error && (
|
|
<p role="alert" className="mt-2 text-destructive">
|
|
{error}
|
|
</p>
|
|
)}
|
|
</Dialog>
|
|
)
|
|
}
|