Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure
TypeScript
'use client'
import { useEffect, useMemo, useState } from 'react'
import { useRouter } from 'next/navigation'
import { SingleValueEditDialog } from '@clerum/frontend-components'
import { AuthGate } from '@components/AuthGate'
import { Button } from '@components/Button'
import { useProfileAccess } from '@components/ProfileAccessContext'
import { ProfileBodySkeleton } from '@components/ProfileBodySkeleton'
import { ProfileShell } from '@components/ProfileShell'
import { SelectControl } from '@components/SelectControl'
import { useToast } from '@components/Toast'
import { IconRefresh } from '@components/icons'
import { PROFILE_ROUTES } from '@constants/routes'
import {
getMe,
getNotificationPreferences,
isSilentApiError,
updateNotificationPreferences,
} from '@lib/api'
import {
activeApprovalAccounts,
listWorkflowApprovalMediums,
preferredAccountOptionLabel,
} from '@lib/approvalChannels'
import type { WorkflowApprovalMediumAccount } from '@/app/types/approvalChannels'
import type { Me, NotificationPreferences } from '@/app/types/profile'
type LoadState = 'loading' | 'ready' | 'error'
function settingsPathForMedium(medium: string): string {
if (medium === 'slack') return PROFILE_ROUTES.settings.social('slack')
if (medium === 'teams') return PROFILE_ROUTES.settings.social('teams')
return PROFILE_ROUTES.settings.social('telegram')
}
function ApprovalChannelsContent() {
const router = useRouter()
const { showToast } = useToast()
const { approvalTargets: targets, refreshApprovalTargets } = useProfileAccess()
const [me, setMe] = useState<Me | null>(null)
const [accounts, setAccounts] = useState<WorkflowApprovalMediumAccount[]>([])
const [preferences, setPreferences] = useState<NotificationPreferences | null>(null)
const [state, setState] = useState<LoadState>('loading')
const [busy, setBusy] = useState(false)
const [error, setError] = useState('')
const [editingPreferred, setEditingPreferred] = useState(false)
const [preferredEditError, setPreferredEditError] = useState('')
const activeAccounts = useMemo(() => {
const authorizedTargetIds = new Set(targets.map(target => target.id))
return activeApprovalAccounts(accounts).filter(account =>
(account.targets || []).some(target => authorizedTargetIds.has(target.id))
)
}, [accounts, targets])
const selectedPreferredAccountId = preferences?.preferredAccountId ?? ''
const selectedAccount = activeAccounts.find(account => account.id === selectedPreferredAccountId)
const selectedPreferredAccountLabel = selectedAccount
? preferredAccountOptionLabel(selectedAccount)
: 'Automatic (most recent channel)'
const manageMedium = selectedAccount?.medium || activeAccounts[0]?.medium || 'telegram'
async function loadAll(forceAccess = false) {
setState('loading')
setError('')
try {
const [current, , nextAccounts, nextPreferences] = await Promise.all([
getMe() as Promise<Me>,
refreshApprovalTargets({ force: forceAccess }),
listWorkflowApprovalMediums(),
getNotificationPreferences(),
])
setMe(current)
setAccounts(nextAccounts)
setPreferences(nextPreferences)
setState('ready')
} catch (err) {
if (isSilentApiError(err)) return
setState('error')
setError(err instanceof Error ? err.message : 'Failed to load approval channels')
}
}
useEffect(() => {
void loadAll()
}, [])
async function changePreferredAccount(nextAccountId: string) {
if (!preferences) return
const preferredAccountId = nextAccountId || null
const preferredMedium = preferredAccountId
? (activeAccounts.find(account => account.id === preferredAccountId)?.medium ?? null)
: null
setBusy(true)
setPreferredEditError('')
try {
const next = await updateNotificationPreferences({
preferredMedium:
preferredMedium === 'telegram' ||
preferredMedium === 'slack' ||
preferredMedium === 'teams'
? preferredMedium
: null,
preferredAccountId,
channelFallbackEnabled: preferences.channelFallbackEnabled,
})
setPreferences(next)
setEditingPreferred(false)
showToast(
preferredAccountId ? 'Preferred channel updated.' : 'Preferred channel set to automatic.',
{ tone: 'success' }
)
} catch (err) {
const message = err instanceof Error ? err.message : ''
setPreferredEditError(
message.includes('preferred_account_not_found')
? 'That channel is no longer available. Pick another or leave Automatic.'
: 'Could not update the preferred channel.'
)
} finally {
setBusy(false)
}
}
return (
<ProfileShell currentRoute="approvalChannels">
<div className="profile-page">
<header className="header-row">
<div>
<h1 className="page-title">Approval Channels</h1>
<div className="small-plus muted">
{me ? `Signed in as ${me.email}` : 'Loading user session'}
</div>
</div>
<div className="toolbar">
<Button
variant="secondary"
className="cu-btn--icon cu-btn--toolbar"
onClick={() => void loadAll(true)}
disabled={busy || state === 'loading'}
aria-label={
state === 'loading' ? 'Refreshing approval channels' : 'Refresh approval channels'
}
>
<IconRefresh className={state === 'loading' ? 'cu-spin' : undefined} />
</Button>
</div>
</header>
{state === 'loading' ? (
<ProfileBodySkeleton
label="Loading approval channels"
sections={[{ title: 'Preferred approval channel', actions: ['Manage'], rows: 3 }]}
/>
) : null}
{error ? <div className="message message--error">{error}</div> : null}
{state !== 'loading' && preferences && targets.length > 0 ? (
<section className="section">
<div className="settings-section-head">
<div>
<h2 className="section-title">Preferred approval channel</h2>
<p className="body-copy">
Choose which connected conversation receives approval notifications by default.
</p>
</div>
<Button
variant="secondary"
onClick={() => router.push(settingsPathForMedium(manageMedium))}
>
Manage
</Button>
</div>
{activeAccounts.length === 0 ? (
<div className="message message--plain">
No external channels are connected. Use Manage to connect one.
</div>
) : (
<div className="stack-tight">
<span className="small muted">Preferred channel</span>
<div className="settings-section-head">
<span>{selectedPreferredAccountLabel}</span>
<Button
variant="secondary"
aria-label="Edit preferred channel"
onClick={() => {
setPreferredEditError('')
setEditingPreferred(true)
}}
>
Edit
</Button>
</div>
</div>
)}
</section>
) : null}
{preferences && activeAccounts.length > 0 ? (
<SingleValueEditDialog
open={editingPreferred}
initialValue={selectedPreferredAccountId}
title="Edit preferred approval channel"
description="Choose which connected conversation receives approval notifications by default."
pending={busy}
error={preferredEditError || undefined}
discardLabel="Cancel"
onDismiss={() => {
setPreferredEditError('')
setEditingPreferred(false)
}}
onSave={value => void changePreferredAccount(value)}
renderEditor={({ value, onChange, disabled }) => (
<label className="stack-tight" htmlFor="preferred-notification-account">
<span className="small muted">Preferred channel</span>
<SelectControl
id="preferred-notification-account"
value={value}
onChange={event => onChange(event.target.value)}
disabled={disabled}
>
<option value="">Automatic (most recent channel)</option>
{activeAccounts.map(account => (
<option key={account.id} value={account.id}>
{preferredAccountOptionLabel(account)}
</option>
))}
</SelectControl>
</label>
)}
/>
) : null}
</div>
</ProfileShell>
)
}
export default function ApprovalChannelsPage() {
return (
<AuthGate>
<ApprovalChannelsContent />
</AuthGate>
)
}