import React from 'react'
import { Link } from 'react-router'
import { Bell, Lock, Plug, Send, Webhook } from 'lucide-react'
import { WEB_PUSH_CHANNEL_ID } from '@trek/shared'
import { useTranslation } from '../../i18n'
import ToggleSwitch from './ToggleSwitch'
import Section from './Section'
import WebPushCard from './WebPushCard'
import { EVENT_LABEL_KEYS, channelLabel, isLockedCell } from './notificationLabels'
import { useNotificationSettings } from './useNotificationSettings'
import { Tooltip } from '../shared/Tooltip'
import { fs } from '../shared/DialogShell'
import { EditorField, GRID_2, INPUT } from '../shared/dialogParts'
import { SettingsCard, SettingsHint, StatusPill, SETTINGS_BUTTON, SETTINGS_BUTTON_DANGER, SETTINGS_BUTTON_PRIMARY } from './settingsKit'
export default function NotificationsTab(): React.ReactElement {
const { t } = useTranslation()
const {
matrix, loadFailed, saving, visibleChannels, hasChannel, pluginChannels, toggle, testChannel, channelTesting,
webhookUrl, setWebhookUrl, webhookIsSet, webhookSaving, webhookTesting, saveWebhookUrl, testWebhookUrl,
ntfyTopic, setNtfyTopic, ntfyServer, setNtfyServer, ntfyToken, setNtfyToken, ntfyTokenIsSet,
ntfySaving, ntfyTesting, saveNtfySettings, clearNtfyToken, testNtfySettings,
} = useNotificationSettings()
const renderMatrix = () => {
if (!matrix) return loadFailed ? {t('common.error')} :
if (visibleChannels.length === 0) {
return {t('settings.notificationPreferences.noChannels')}
}
// The event column takes what is left; every channel gets the same narrow
// column, so the switches line up under their headers.
const columns = `minmax(0, 1fr) ${visibleChannels.map(() => '72px').join(' ')}`
return (
{/* Header row */}
{visibleChannels.map(ch => (
{channelLabel(ch, t)}
))}
{/* Event rows */}
{matrix.event_types.map(eventType => {
const implementedForEvent = matrix.implemented_combos[eventType] ?? []
const relevantChannels = visibleChannels.filter(ch => implementedForEvent.includes(ch.id))
if (relevantChannels.length === 0) return null
return (
{t(EVENT_LABEL_KEYS[eventType]) || eventType}
{visibleChannels.map(ch => {
if (!implementedForEvent.includes(ch.id)) {
return
—
}
const isOn = matrix.preferences[eventType]?.[ch.id] ?? true
// Switched off for everyone by the admin (#1536): the same footprint as
// the switch, so the grid keeps its columns, and a lock that says why.
if (isLockedCell(matrix.locked, eventType, ch.id)) {
return (
)
}
return (
toggle(eventType, ch.id)} />
)
})}
)
})}
)
}
const webhookTestDisabled = (!webhookUrl && !webhookIsSet) || webhookTesting
// The channel cards only make sense once there is a matrix to send through.
const ready = !!matrix && visibleChannels.length > 0
return (
)
}
/** A calm placeholder for the matrix while the preferences load. */
function MatrixSkeleton({ label }: { label: string }) {
return (
{label}
{[0, 1, 2].map(i => (
))}
)
}