diff --git a/webui/src/pages/Cron.tsx b/webui/src/pages/Cron.tsx index 7f5fbe6..2f5d0d1 100644 --- a/webui/src/pages/Cron.tsx +++ b/webui/src/pages/Cron.tsx @@ -15,13 +15,48 @@ const initialCronForm = { enabled: true, }; +const isNonGroupRecipient = (channel: string, id: string) => { + const ch = String(channel || '').toLowerCase(); + const v = String(id || '').trim(); + if (!v) return false; + if (ch === 'telegram') { + if (v.startsWith('-')) return false; + if (v.startsWith('telegram:')) { + const raw = v.slice('telegram:'.length); + if (raw.startsWith('-')) return false; + } + } + if (ch === 'discord') { + if (v.startsWith('#') || v.startsWith('discord:channel:')) return false; + } + return true; +}; + const Cron: React.FC = () => { const { t } = useTranslation(); - const { cron, refreshCron, q } = useAppContext(); + const { cron, refreshCron, q, cfg } = useAppContext(); const [isCronModalOpen, setIsCronModalOpen] = useState(false); const [editingCron, setEditingCron] = useState(null); const [cronForm, setCronForm] = useState(initialCronForm); + const enabledChannels = React.useMemo(() => { + const channels = (cfg as any)?.channels || {}; + return Object.keys(channels).filter((k) => { + const v = channels[k]; + return v && typeof v === 'object' && v.enabled === true; + }); + }, [cfg]); + + const channelRecipients = React.useMemo(() => { + const channels = (cfg as any)?.channels || {}; + const out: Record = {}; + enabledChannels.forEach((ch) => { + const arr = Array.isArray(channels?.[ch]?.allow_from) ? channels[ch].allow_from : []; + out[ch] = arr.map((x: any) => String(x || '').trim()).filter((id: string) => isNonGroupRecipient(ch, id)); + }); + return out; + }, [cfg, enabledChannels]); + async function cronAction(action: 'delete' | 'enable' | 'disable', id: string) { try { await fetch(`/webui/api/cron${q}`, { @@ -57,7 +92,9 @@ const Cron: React.FC = () => { } } else { setEditingCron(null); - setCronForm(initialCronForm); + const defaultChannel = enabledChannels[0] || initialCronForm.channel; + const defaultTo = (channelRecipients[defaultChannel] && channelRecipients[defaultChannel][0]) || ''; + setCronForm({ ...initialCronForm, channel: defaultChannel, to: defaultTo }); } setIsCronModalOpen(true); } @@ -223,21 +260,42 @@ const Cron: React.FC = () => {