import { useTranslation } from "react-i18next"; import type { ChannelConfigField, ChannelFieldSection, ChannelSetupRequirement, } from "@/channel-plugins/catalog"; import { CredentialForm, channelFieldInputId, type CredentialFormProps, } from "@/components/settings/channels/CredentialForm"; const CHANNEL_FIELD_SECTION_ORDER: ChannelFieldSection[] = [ "account", "credentials", "connection", "access", "behavior", "security", ]; type ChannelFieldGroupsProps = Omit & { fields: ChannelConfigField[]; requirements: ChannelSetupRequirement[]; sectionLabels?: Record; }; export function ChannelFieldGroups({ fields, requirements, sectionLabels, ...formProps }: ChannelFieldGroupsProps) { const { t } = useTranslation(); const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback }); const labels = new Map(fields.map((field) => [field.key, field.label])); const compositeRequirements = requirements.filter( (requirement) => requirement.alternatives.length > 1, ); const groups = new Map(); for (const field of fields) { const section = field.section ?? "credentials"; const current = groups.get(section) ?? []; current.push(field); groups.set(section, current); } const orderedSections = [ ...CHANNEL_FIELD_SECTION_ORDER, ...[...groups.keys()].filter( (section) => !CHANNEL_FIELD_SECTION_ORDER.includes(section as ChannelFieldSection), ), ]; return (
{compositeRequirements.map((requirement, index) => (
{tx("settings.channels.chooseCredentialMethod", "Choose one credential method")}
{requirement.alternatives.map((alternative, alternativeIndex) => ( {alternativeIndex ? {tx("settings.channels.or", "or")} : null} {alternative.map((key) => labels.get(key) ?? key.split(".").at(-1)).join(" + ")} ))}
))} {orderedSections.map((section) => { const sectionFields = groups.get(section); if (!sectionFields?.length) return null; return (
{channelFieldSectionLabel(section, tx, sectionLabels)}
); })}
); } function channelFieldSectionLabel( section: string, tx: (key: string, fallback: string) => string, sectionLabels?: Record, ): string { const customLabel = sectionLabels?.[section]; if (customLabel) return customLabel; const fallbacks: Record = { account: "Account", credentials: "Credentials", connection: "Connection", access: "Access", behavior: "Behavior", security: "Security", advanced: "Advanced", }; const fallback = fallbacks[section as ChannelFieldSection]; if (fallback) return tx(`settings.channels.sections.${section}`, fallback); return section .replace(/([a-z0-9])([A-Z])/g, "$1 $2") .replace(/[_-]+/g, " ") .replace(/\b\w/g, (letter) => letter.toUpperCase()); } export function channelRequirementErrors( fields: ChannelConfigField[], requirements: ChannelSetupRequirement[], values: Record, configuredFields: Set, clearedSecrets: Set, message: string, ): Record { const fieldByKey = new Map(fields.map((field) => [field.key, field])); const present = (key: string) => { const field = fieldByKey.get(key); if (!field || clearedSecrets.has(key)) return false; const value = (values[key] ?? "").trim(); if (field.kind === "bool") return value === "true"; if (value) return true; return Boolean(field.secret && configuredFields.has(key)); }; const errors: Record = {}; for (const requirement of requirements) { if (requirement.alternatives.some((alternative) => alternative.every(present))) continue; const closest = [...requirement.alternatives].sort( (left, right) => left.filter((key) => !present(key)).length - right.filter((key) => !present(key)).length, )[0] ?? []; for (const key of closest) { if (!present(key) && fieldByKey.has(key)) errors[key] = message; } } return errors; } export function channelServerValidationErrors( fields: ChannelConfigField[], missingFields: string[], message: string, ): Record { const missing = new Set(missingFields); return Object.fromEntries( fields .filter((field) => missing.has(field.key) || missing.has(field.key.split(".").at(-1) ?? "")) .map((field) => [field.key, message]), ); } export function focusFirstChannelFieldError(errors: Record) { const key = Object.keys(errors)[0]; if (!key) return; window.requestAnimationFrame(() => { document.getElementById(channelFieldInputId(key))?.focus(); }); }