Files
EvoBGP/apps/web/src/lib/modules/helpers.ts
T
Denozordec 821f342476
CI / changes (push) Successful in 7s
CI / commitlint (push) Skipped
CI / go (push) Skipped
CI / bird2 (push) Skipped
CI / openapi (push) Successful in 51s
CI / web (push) Successful in 1m26s
CI / release (push) Successful in 4m26s
feat(modules): enhance ModuleKpiCards and add update functionality for modules
Updated the ModuleKpiCards component to improve the display of KPI metrics by introducing a new entries count prop and simplifying the logic for displaying community and DoH information. Added a new mutation hook for updating modules, which includes success and error handling with toast notifications. Enhanced the ModuleDetailComponent to support editing modules with a new dialog and integrated the entries count into the KPI display.

Additionally, introduced a new helper function for generating short labels for DoH profiles, improving the overall user experience in module management.
2026-08-12 11:32:44 +07:00

55 lines
1.8 KiB
TypeScript

import type { BgpCommunity, DohProfile, ModuleRow } from '@/types/api'
export const NONE_OPTION = '__none__'
export function communityLabel(id: string | null | undefined, communities: BgpCommunity[]): string {
if (!id) return '—'
const c = communities.find((x) => x.id === id)
if (!c) return `${id.slice(0, 8)}…`
const t = c.title?.trim()
return t || c.community
}
export function communityOptionLabel(c: BgpCommunity): string {
const t = c.title?.trim()
return t || c.community
}
export function nullableSelectValue(value: string | null | undefined): string {
if (value === null || value === undefined || value === '') return NONE_OPTION
return value
}
export function fromNullableSelect(value: string): string | null {
if (value === NONE_OPTION || value === '') return null
return value
}
export function moduleDohProfileIds(modRow: ModuleRow | null): string[] {
if (!modRow) return []
if (modRow.doh_profile_ids?.length) return modRow.doh_profile_ids
return modRow.doh_profile_id ? [modRow.doh_profile_id] : []
}
export function dohProfileLabel(id: string, dohProfiles: DohProfile[]): string {
const p = dohProfiles.find((d) => d.id === id)
return p ? (p.name?.trim() ? `${p.name} (${p.url})` : p.url) : `${id.slice(0, 8)}…`
}
/** Short label for forms / chips — name preferred, else hostname from URL. */
export function dohProfileShortLabel(id: string, dohProfiles: DohProfile[]): string {
const p = dohProfiles.find((d) => d.id === id)
if (!p) return `${id.slice(0, 8)}…`
const name = p.name?.trim()
if (name) return name
try {
return new URL(p.url).hostname
} catch {
return p.url
}
}
export function normalizeCdnSourceKind(k: string): 'plaintext' | 'json' {
return k.trim().toLowerCase() === 'json' ? 'json' : 'plaintext'
}