import { DeesElement, customElement, html, css, cssManager, state, type TemplateResult } from '../plugins.js';
import { deesCatalog } from '../plugins.js';
import { appState, type IAppState, type IProviderStatus } from '../state/appstate.js';
import { viewHostCss } from './shared/index.js';
import type { IStatsTile } from '@design.estate/dees-catalog';
// ---------------------------------------------------------------------------
// Default provider templates
// ---------------------------------------------------------------------------
const PROVIDER_TEMPLATES = {
sipgate: {
domain: 'sipgate.de',
outboundProxy: { address: 'sipgate.de', port: 5060 },
registerIntervalSec: 300,
codecs: [9, 0, 8, 101],
quirks: { earlyMediaSilence: false },
},
o2: {
domain: 'sip.alice-voip.de',
outboundProxy: { address: 'sip.alice-voip.de', port: 5060 },
registerIntervalSec: 300,
codecs: [9, 0, 8, 101],
quirks: { earlyMediaSilence: false },
},
};
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
function slugify(name: string): string {
return name
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-')
.replace(/^-|-$/g, '') || `provider-${Date.now()}`;
}
// ---------------------------------------------------------------------------
// View element
// ---------------------------------------------------------------------------
@customElement('sipproxy-view-providers')
export class SipproxyViewProviders extends DeesElement {
@state() accessor appData: IAppState = appState.getState();
public static styles = [
cssManager.defaultStyles,
viewHostCss,
css`
:host {
display: block;
padding: 16px;
}
.view-section {
margin-bottom: 24px;
}
`,
];
// ---- lifecycle -----------------------------------------------------------
connectedCallback() {
super.connectedCallback();
this.rxSubscriptions.push({
unsubscribe: appState.subscribe((s) => { this.appData = s; }),
} as any);
}
// ---- stats tiles ---------------------------------------------------------
private getStatsTiles(): IStatsTile[] {
const providers = this.appData.providers || [];
const total = providers.length;
const registered = providers.filter((p) => p.registered).length;
const unregistered = total - registered;
return [
{
id: 'total',
title: 'Total Providers',
value: total,
type: 'number',
icon: 'lucide:server',
description: 'Configured SIP trunks',
},
{
id: 'registered',
title: 'Registered',
value: registered,
type: 'number',
icon: 'lucide:CheckCircle',
color: 'hsl(142.1 76.2% 36.3%)',
description: 'Active registrations',
},
{
id: 'unregistered',
title: 'Unregistered',
value: unregistered,
type: 'number',
icon: 'lucide:AlertCircle',
color: unregistered > 0 ? 'hsl(0 84.2% 60.2%)' : undefined,
description: unregistered > 0 ? 'Needs attention' : 'All healthy',
},
];
}
// ---- table columns -------------------------------------------------------
private getColumns() {
return [
{
key: 'displayName',
header: 'Name',
sortable: true,
},
{
key: 'domain',
header: 'Domain',
value: (row: IProviderStatus) => (row as any).domain || '--',
renderer: (val: string) =>
html`${val}`,
},
{
key: 'registered',
header: 'Status',
value: (row: IProviderStatus) => (row.registered ? 'Registered' : 'Not Registered'),
renderer: (val: string, row: IProviderStatus) => {
const on = row.registered;
return html`
${val}
`;
},
},
{
key: 'publicIp',
header: 'Public IP',
renderer: (val: any) =>
html`${val || '--'}`,
},
];
}
// ---- table actions -------------------------------------------------------
private getDataActions() {
return [
{
name: 'Edit',
iconName: 'lucide:pencil',
type: ['inRow'] as any,
actionFunc: async (actionData: any) => {
await this.openEditModal(actionData.item.id);
},
},
{
name: 'Delete',
iconName: 'lucide:Trash2',
type: ['inRow'] as any,
actionFunc: async (actionData: any) => {
await this.confirmDelete(actionData.item);
},
},
{
name: 'Add Provider',
iconName: 'lucide:plus',
type: ['header'] as any,
actionFunc: async () => {
await this.openAddModal();
},
},
{
name: 'Add Sipgate',
iconName: 'lucide:phone',
type: ['header'] as any,
actionFunc: async () => {
await this.openAddModal(PROVIDER_TEMPLATES.sipgate, 'Sipgate');
},
},
{
name: 'Add O2/Alice',
iconName: 'lucide:phone',
type: ['header'] as any,
actionFunc: async () => {
await this.openAddModal(PROVIDER_TEMPLATES.o2, 'O2/Alice');
},
},
];
}
// ---- add provider modal --------------------------------------------------
private async openAddModal(
template?: typeof PROVIDER_TEMPLATES.sipgate,
templateName?: string,
) {
const { DeesModal } = await import('@design.estate/dees-catalog');
const formData = {
displayName: templateName || '',
domain: template?.domain || '',
outboundProxyAddress: template?.outboundProxy?.address || '',
outboundProxyPort: String(template?.outboundProxy?.port ?? 5060),
username: '',
password: '',
registerIntervalSec: String(template?.registerIntervalSec ?? 300),
codecs: template?.codecs ? template.codecs.join(', ') : '9, 0, 8, 101',
earlyMediaSilence: template?.quirks?.earlyMediaSilence ?? false,
};
const heading = template
? `Add ${templateName} Provider`
: 'Add Provider';
await DeesModal.createAndShow({
heading,
width: 'small',
showCloseButton: true,
content: html`
{ formData.displayName = (e.target as any).value; }}
>
{ formData.domain = (e.target as any).value; }}
>
{ formData.outboundProxyAddress = (e.target as any).value; }}
>
{ formData.outboundProxyPort = (e.target as any).value; }}
>
{ formData.username = (e.target as any).value; }}
>
{ formData.password = (e.target as any).value; }}
>
{ formData.registerIntervalSec = (e.target as any).value; }}
>
{ formData.codecs = (e.target as any).value; }}
>
{ formData.earlyMediaSilence = e.detail; }}
>
`,
menuOptions: [
{
name: 'Cancel',
iconName: 'lucide:x',
action: async (modalRef: any) => {
modalRef.destroy();
},
},
{
name: 'Create',
iconName: 'lucide:check',
action: async (modalRef: any) => {
if (!formData.displayName.trim() || !formData.domain.trim()) {
deesCatalog.DeesToast.error('Display name and domain are required');
return;
}
try {
const providerId = slugify(formData.displayName);
const codecs = formData.codecs
.split(',')
.map((s: string) => parseInt(s.trim(), 10))
.filter((n: number) => !isNaN(n));
const newProvider: any = {
id: providerId,
displayName: formData.displayName.trim(),
domain: formData.domain.trim(),
outboundProxy: {
address: formData.outboundProxyAddress.trim() || formData.domain.trim(),
port: parseInt(formData.outboundProxyPort, 10) || 5060,
},
username: formData.username.trim(),
password: formData.password,
registerIntervalSec: parseInt(formData.registerIntervalSec, 10) || 300,
codecs,
quirks: {
earlyMediaSilence: formData.earlyMediaSilence,
},
};
const result = await appState.apiSaveConfig({
addProvider: newProvider,
});
if (result.ok) {
modalRef.destroy();
deesCatalog.DeesToast.success(`Provider "${formData.displayName}" created`);
} else {
deesCatalog.DeesToast.error('Failed to save provider');
}
} catch (err: any) {
console.error('Failed to create provider:', err);
deesCatalog.DeesToast.error('Failed to create provider');
}
},
},
],
});
}
// ---- edit provider modal -------------------------------------------------
private async openEditModal(providerId: string) {
const { DeesModal } = await import('@design.estate/dees-catalog');
let cfg: any;
try {
cfg = await appState.apiGetConfig();
} catch {
deesCatalog.DeesToast.error('Failed to load configuration');
return;
}
const provider = cfg.providers?.find((p: any) => p.id === providerId);
if (!provider) {
deesCatalog.DeesToast.error('Provider not found in configuration');
return;
}
const allDevices: { id: string; displayName: string }[] = (cfg.devices || []).map((d: any) => ({
id: d.id,
displayName: d.displayName,
}));
const formData = {
displayName: provider.displayName || '',
domain: provider.domain || '',
outboundProxyAddress: provider.outboundProxy?.address || '',
outboundProxyPort: String(provider.outboundProxy?.port ?? 5060),
username: provider.username || '',
password: provider.password || '',
registerIntervalSec: String(provider.registerIntervalSec ?? 300),
codecs: (provider.codecs || []).join(', '),
earlyMediaSilence: provider.quirks?.earlyMediaSilence ?? false,
inboundDevices: (() => {
const route = (cfg.routing?.routes || []).find((r: any) => r.match?.direction === 'inbound' && r.match?.sourceProvider === providerId);
return route?.action?.targets ? [...route.action.targets] : [];
})() as string[],
ringBrowsers: (() => {
const route = (cfg.routing?.routes || []).find((r: any) => r.match?.direction === 'inbound' && r.match?.sourceProvider === providerId);
return route?.action?.ringBrowsers ?? false;
})(),
};
await DeesModal.createAndShow({
heading: `Edit Provider: ${formData.displayName}`,
width: 'small',
showCloseButton: true,
content: html`
{ formData.displayName = (e.target as any).value; }}
>
{ formData.domain = (e.target as any).value; }}
>
{ formData.outboundProxyAddress = (e.target as any).value; }}
>
{ formData.outboundProxyPort = (e.target as any).value; }}
>
{ formData.username = (e.target as any).value; }}
>
{ formData.password = (e.target as any).value; }}
>
{ formData.registerIntervalSec = (e.target as any).value; }}
>
{ formData.codecs = (e.target as any).value; }}
>
{ formData.earlyMediaSilence = e.detail; }}
>
Inbound Routing
Select which devices should ring when this provider receives an incoming call.
${allDevices.map((d) => html`
{
if (e.detail) {
if (!formData.inboundDevices.includes(d.id)) {
formData.inboundDevices = [...formData.inboundDevices, d.id];
}
} else {
formData.inboundDevices = formData.inboundDevices.filter((id) => id !== d.id);
}
}}
>
`)}
{ formData.ringBrowsers = e.detail; }}
>
`,
menuOptions: [
{
name: 'Cancel',
iconName: 'lucide:x',
action: async (modalRef: any) => {
modalRef.destroy();
},
},
{
name: 'Save',
iconName: 'lucide:check',
action: async (modalRef: any) => {
try {
const codecs = formData.codecs
.split(',')
.map((s: string) => parseInt(s.trim(), 10))
.filter((n: number) => !isNaN(n));
// Build updated routes: update/create the inbound route for this provider.
const currentRoutes = [...(cfg.routing?.routes || [])];
const existingIdx = currentRoutes.findIndex((r: any) =>
r.match?.direction === 'inbound' && r.match?.sourceProvider === providerId
);
const inboundRoute = {
id: `inbound-${providerId}`,
name: `Inbound from ${formData.displayName.trim() || providerId}`,
priority: 0,
enabled: true,
match: { direction: 'inbound' as const, sourceProvider: providerId },
action: {
targets: formData.inboundDevices.length ? formData.inboundDevices : undefined,
ringBrowsers: formData.ringBrowsers,
},
};
if (existingIdx >= 0) {
currentRoutes[existingIdx] = { ...currentRoutes[existingIdx], ...inboundRoute };
} else {
currentRoutes.push(inboundRoute);
}
const updates: any = {
providers: [{
id: providerId,
displayName: formData.displayName.trim(),
domain: formData.domain.trim(),
outboundProxy: {
address: formData.outboundProxyAddress.trim() || formData.domain.trim(),
port: parseInt(formData.outboundProxyPort, 10) || 5060,
},
username: formData.username.trim(),
registerIntervalSec: parseInt(formData.registerIntervalSec, 10) || 300,
codecs,
quirks: {
earlyMediaSilence: formData.earlyMediaSilence,
},
}] as any[],
routing: { routes: currentRoutes },
};
// Only send password if it was changed (not the masked placeholder).
if (formData.password && !formData.password.match(/^\*+$/)) {
updates.providers[0].password = formData.password;
}
const result = await appState.apiSaveConfig(updates);
if (result.ok) {
modalRef.destroy();
deesCatalog.DeesToast.success('Provider updated');
} else {
deesCatalog.DeesToast.error('Failed to save changes');
}
} catch (err: any) {
console.error('Save failed:', err);
deesCatalog.DeesToast.error('Failed to save changes');
}
},
},
],
});
}
// ---- delete confirmation -------------------------------------------------
private async confirmDelete(provider: IProviderStatus) {
const { DeesModal } = await import('@design.estate/dees-catalog');
await DeesModal.createAndShow({
heading: 'Delete Provider',
width: 'small',
showCloseButton: true,
content: html`
Are you sure you want to delete
${provider.displayName}?
This action cannot be undone.
`,
menuOptions: [
{
name: 'Cancel',
iconName: 'lucide:x',
action: async (modalRef: any) => {
modalRef.destroy();
},
},
{
name: 'Delete',
iconName: 'lucide:Trash2',
action: async (modalRef: any) => {
try {
const result = await appState.apiSaveConfig({
removeProvider: provider.id,
});
if (result.ok) {
modalRef.destroy();
deesCatalog.DeesToast.success(`Provider "${provider.displayName}" deleted`);
} else {
deesCatalog.DeesToast.error('Failed to delete provider');
}
} catch (err: any) {
console.error('Delete failed:', err);
deesCatalog.DeesToast.error('Failed to delete provider');
}
},
},
],
});
}
// ---- render --------------------------------------------------------------
public render(): TemplateResult {
const providers = this.appData.providers || [];
return html`
`;
}
}