From 149f34bfd2e2dfdf45470ba111feccdad5c507f2 Mon Sep 17 00:00:00 2001 From: Timothy Jaeryang Baek Date: Mon, 6 Jul 2026 16:47:19 -0500 Subject: [PATCH] refac --- cptr/frontend/src/lib/apis/notifications.ts | 5 ++ .../components/Settings/Notifications.svelte | 67 +++++++++++++------ cptr/frontend/src/lib/i18n/locales/de.json | 1 + cptr/frontend/src/lib/i18n/locales/en.json | 1 + cptr/frontend/src/lib/i18n/locales/es.json | 1 + cptr/frontend/src/lib/i18n/locales/fr.json | 1 + cptr/frontend/src/lib/i18n/locales/ja.json | 1 + cptr/frontend/src/lib/i18n/locales/ko.json | 1 + cptr/frontend/src/lib/i18n/locales/pt-BR.json | 1 + cptr/frontend/src/lib/i18n/locales/ru.json | 1 + cptr/frontend/src/lib/i18n/locales/zh-CN.json | 1 + cptr/frontend/src/lib/i18n/locales/zh-TW.json | 1 + cptr/routers/notifications.py | 9 +++ cptr/routers/state.py | 3 +- cptr/utils/notifications.py | 46 +++++++++++-- cptr/utils/tools.py | 8 +-- 16 files changed, 118 insertions(+), 30 deletions(-) diff --git a/cptr/frontend/src/lib/apis/notifications.ts b/cptr/frontend/src/lib/apis/notifications.ts index e288091..3442e06 100644 --- a/cptr/frontend/src/lib/apis/notifications.ts +++ b/cptr/frontend/src/lib/apis/notifications.ts @@ -20,6 +20,7 @@ export interface NotificationTarget { id: string; type: NotificationTargetType; enabled: boolean; + is_default?: boolean; events: ChatNotificationEvent[]; delivery: NotificationDelivery; config: { @@ -77,6 +78,10 @@ export function testNotificationTarget(id: string) { return fetchJSON<{ ok: boolean }>(`/api/notifications/targets/${id}/test`, { method: 'POST' }); } +export function setDefaultNotificationTarget(id: string) { + return fetchJSON(`/api/notifications/targets/${id}/default`, { method: 'PUT' }); +} + export async function listNotificationBotOptions(): Promise { const data = await fetchJSON<{ bots: BotOption[] }>('/api/notifications/bot-options'); return data.bots; diff --git a/cptr/frontend/src/lib/components/Settings/Notifications.svelte b/cptr/frontend/src/lib/components/Settings/Notifications.svelte index 8c3e1b6..5f9d803 100644 --- a/cptr/frontend/src/lib/components/Settings/Notifications.svelte +++ b/cptr/frontend/src/lib/components/Settings/Notifications.svelte @@ -10,6 +10,7 @@ listNotificationBotOptions, listNotificationEvents, listNotificationTargets, + setDefaultNotificationTarget, testNotificationTarget, updateNotificationTarget, type BotOption, @@ -63,20 +64,22 @@ async function loadNotificationTargets() { loadingTargets = true; - try { - const [nextTargets, bots, events] = await Promise.all([ - listNotificationTargets(), - listNotificationBotOptions(), - listNotificationEvents() - ]); - targets = nextTargets; - botOptions = bots; - eventOptions = events.length ? events : fallbackEventOptions; - } catch { + const [targetResult, botResult, eventResult] = await Promise.allSettled([ + listNotificationTargets(), + listNotificationBotOptions(), + listNotificationEvents() + ]); + if (targetResult.status === 'fulfilled') { + targets = targetResult.value; + } else { toast.error($t('general.notificationTargetsLoadFailed')); - } finally { - loadingTargets = false; } + botOptions = botResult.status === 'fulfilled' ? botResult.value : []; + eventOptions = + eventResult.status === 'fulfilled' && eventResult.value.length + ? eventResult.value + : fallbackEventOptions; + loadingTargets = false; } async function toggleNotifications() { @@ -150,12 +153,12 @@ destination_chat_id: form.destination_chat_id.trim() }; } - const saved = originalId - ? await updateNotificationTarget(originalId, payload) - : await createNotificationTarget(payload); - targets = originalId - ? targets.map((target) => (target.id === originalId ? saved : target)) - : [...targets, saved]; + if (originalId) { + await updateNotificationTarget(originalId, payload); + } else { + await createNotificationTarget(payload); + } + await loadNotificationTargets(); formOpen = false; toast.success($t('settings.saved')); } catch (error) { @@ -167,8 +170,8 @@ async function patchTarget(target: NotificationTarget, patch: Partial) { try { - const saved = await updateNotificationTarget(target.id, patch as any); - targets = targets.map((item) => (item.id === saved.id ? saved : item)); + await updateNotificationTarget(target.id, patch as any); + await loadNotificationTargets(); } catch (error) { toast.error(error instanceof Error ? error.message : $t('general.notificationTargetSaveFailed')); } @@ -177,7 +180,16 @@ async function removeTarget(target: NotificationTarget) { try { await deleteNotificationTarget(target.id); - targets = targets.filter((item) => item.id !== target.id); + await loadNotificationTargets(); + } catch (error) { + toast.error(error instanceof Error ? error.message : $t('general.notificationTargetSaveFailed')); + } + } + + async function makeDefault(target: NotificationTarget) { + try { + await setDefaultNotificationTarget(target.id); + await loadNotificationTargets(); } catch (error) { toast.error(error instanceof Error ? error.message : $t('general.notificationTargetSaveFailed')); } @@ -255,6 +267,11 @@ {target.type === 'webhook' ? $t('general.webhook') : $t('general.bot')} + {#if target.is_default} + + {$t('general.default')} + + {/if}
{targetDestination} @@ -274,6 +291,14 @@ > {$t('general.sendTest')} + {#if !target.is_default} + + {/if}