Your lightweight open-source personal agent.
Python agent-frameworkai-agentai-agentschatbotchatopsdiscord-botllm-agentsllmslocal-llmmcpmodel-context-protocolmulti-agentopenai-compatibleopenclawpersonal-ai-assistantpythonself-hostedtelegram-bot-ai-assistantwebuiworkflow-automation
import { Eye, EyeOff } from "lucide-react";
import { useTranslation } from "react-i18next";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import type { ChannelConfigField } from "@/channel-plugins/catalog";
import { ToggleButton } from "@/components/ui/toggle-button";
import { cn } from "@/lib/utils";
function channelFieldValue(field: ChannelConfigField, values: Record<string, string>): string {
return values[field.key] ?? field.defaultValue ?? field.options?.[0]?.value ?? "";
}
export function channelFieldInputId(key: string): string {
return `channel-field-${key.replace(/[^a-zA-Z0-9_-]/g, "-")}`;
}
export function defaultChannelFieldValues(
fields: ChannelConfigField[],
configValues: Record<string, string> | undefined = undefined,
): Record<string, string> {
return Object.fromEntries(
fields.map((field) => [
field.key,
configValues?.[field.key] ?? field.defaultValue ?? field.options?.[0]?.value ?? "",
]),
);
}
export function channelValuesForSave(
fields: ChannelConfigField[],
values: Record<string, string>,
): Record<string, string> {
const payload: Record<string, string> = {};
for (const field of fields) {
const value = channelFieldValue(field, values);
if (field.secret && !value.trim()) continue;
payload[field.key] = value;
}
return payload;
}
export function channelValuesForSubmit(
fields: ChannelConfigField[],
values: Record<string, string>,
touchedFields: Set<string>,
clearedSecrets: Set<string> = new Set(),
): Record<string, string | null> {
const payload: Record<string, string | null> = {};
for (const field of fields) {
if (field.secret && clearedSecrets.has(field.key)) {
payload[field.key] = null;
continue;
}
const touched = touchedFields.has(field.key);
const value = channelFieldValue(field, values);
if (field.secret && !value.trim()) continue;
if (!touched && !value.trim()) continue;
if (!touched && field.options?.length) continue;
payload[field.key] = value;
}
return payload;
}
export type CredentialFormProps = {
fields: ChannelConfigField[];
values: Record<string, string>;
configuredFields?: Set<string>;
visibleSecrets: Record<string, boolean>;
onChange: (key: string, value: string) => void;
onFieldBlur?: (key: string) => void;
onToggleSecret: (key: string) => void;
errors?: Record<string, string>;
clearedSecrets?: Set<string>;
onClearSecret?: (key: string, clear: boolean) => void;
compact?: boolean;
showSecretActions?: boolean;
disabled?: boolean;
};
export function CredentialForm({
fields,
values,
configuredFields,
visibleSecrets,
onChange,
onFieldBlur,
onToggleSecret,
errors = {},
clearedSecrets = new Set(),
onClearSecret,
compact = false,
showSecretActions = false,
disabled = false,
}: CredentialFormProps) {
const { t } = useTranslation();
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
return (
<div className={cn("grid", compact ? "gap-y-4" : "mt-3 gap-y-2.5")}>
{fields.map((field) => {
const inputId = channelFieldInputId(field.key);
const error = errors[field.key];
const errorId = error ? `${inputId}-error` : undefined;
const describedBy = errorId;
const visible = Boolean(visibleSecrets[field.key]);
const value = values[field.key] ?? "";
const clearSecret = clearedSecrets.has(field.key);
const savedSecret = Boolean(
field.secret && configuredFields?.has(field.key) && !value.trim() && !clearSecret,
);
const showSecretToggle = Boolean(field.secret && value.trim());
const inputType = field.secret && !visible ? "password" : field.inputType ?? "text";
const selectedOption = channelFieldValue(field, values);
const header = (
<span className="flex items-center justify-between gap-2 text-[11px] font-medium text-foreground/85">
<span>{field.label}</span>
{clearSecret ? (
<span className="font-normal text-destructive">
{tx("settings.channels.secretWillBeRemoved", "Will be removed")}
</span>
) : field.optional && !compact ? (
<span className="font-normal text-muted-foreground">
{tx("settings.channels.optional", "Optional")}
</span>
) : null}
</span>
);
const errorMessage = error ? (
<span id={errorId} className="mt-1 block text-[11px] leading-4 text-destructive">
{error}
</span>
) : null;
if (field.control === "switch" && field.kind === "bool") {
return (
<div key={field.key} className="flex items-center justify-between gap-4 py-2">
<label htmlFor={inputId} className="min-w-0">{header}</label>
<div className="flex shrink-0 items-center gap-2">
{errorMessage}
<ToggleButton
id={inputId}
checked={selectedOption === "true"}
disabled={disabled}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
label={field.label}
onChange={(checked) => onChange(field.key, String(checked))}
/>
</div>
</div>
);
}
if (field.options?.length) {
return (
<fieldset
key={field.key}
id={`${inputId}-group`}
aria-labelledby={`${inputId}-label`}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
className="block"
>
<div className="grid grid-cols-[minmax(0,0.8fr)_minmax(0,1.2fr)] items-start gap-x-4">
<span id={`${inputId}-label`} className="flex min-h-12 items-center sm:min-h-10">{header}</span>
<div className="min-w-0">
<span
className="grid rounded-control bg-muted p-0.5 text-[12px] font-medium text-muted-foreground"
style={{ gridTemplateColumns: `repeat(${field.options.length}, minmax(0, 1fr))` }}
>
{field.options.map((option, index) => (
<label key={option.value} className="relative block">
<input
id={index === 0 ? inputId : `${inputId}-${index}`}
type="radio"
name={inputId}
value={option.value}
checked={selectedOption === option.value}
disabled={disabled}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
onChange={() => onChange(field.key, option.value)}
onBlur={() => onFieldBlur?.(field.key)}
className="peer sr-only"
/>
<span className="grid min-h-11 cursor-pointer place-items-center rounded-compact px-2 py-1.5 transition-colors hover:text-foreground peer-checked:bg-background peer-checked:text-foreground peer-checked:ring-1 peer-checked:ring-inset peer-checked:ring-border/45 peer-focus-visible:ring-2 peer-focus-visible:ring-ring sm:min-h-9">
{option.label}
</span>
</label>
))}
</span>
{errorMessage}
</div>
</div>
</fieldset>
);
}
if (field.kind === "json") {
return (
<div key={field.key} className="grid grid-cols-[minmax(0,0.8fr)_minmax(0,1.2fr)] items-start gap-x-4">
<label htmlFor={inputId} className="flex min-h-10 min-w-0 items-center self-start sm:min-h-9">
{header}
</label>
<div className="min-w-0">
<Textarea
id={inputId}
aria-label={field.label}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
placeholder={field.placeholder}
value={value}
disabled={disabled}
onChange={(event) => onChange(field.key, event.target.value)}
onBlur={() => onFieldBlur?.(field.key)}
rows={4}
spellCheck={false}
className={cn(
"resize-y border-border/40 bg-background font-mono text-[12px]",
error && "border-destructive focus-visible:ring-destructive/30",
)}
/>
{errorMessage}
</div>
</div>
);
}
return (
<div key={field.key} className="grid grid-cols-[minmax(0,0.8fr)_minmax(0,1.2fr)] items-start gap-x-4">
<label htmlFor={inputId} className="flex min-h-10 min-w-0 items-center self-start sm:min-h-9">{header}</label>
<div className="min-w-0">
<span className="relative block">
<Input
id={inputId}
aria-label={field.label}
aria-invalid={Boolean(error)}
aria-describedby={describedBy}
type={inputType}
autoComplete={field.secret ? "off" : undefined}
inputMode={field.inputType === "number" ? "numeric" : undefined}
placeholder={
savedSecret
? tx("settings.channels.savedSecretPlaceholder", "Saved secret")
: field.placeholder
}
value={values[field.key] ?? ""}
disabled={disabled}
onChange={(event) => onChange(field.key, event.target.value)}
onBlur={() => onFieldBlur?.(field.key)}
className={cn(
"h-10 rounded-full border-border/40 bg-background text-base sm:h-9 sm:text-[13px]",
error && "border-destructive focus-visible:ring-destructive/30",
showSecretToggle && "pr-9",
)}
/>
{showSecretToggle ? (
<button
type="button"
aria-label={
visible
? tx("settings.channels.hideSecret", "Hide secret")
: tx("settings.channels.showSecret", "Show secret")
}
onClick={() => onToggleSecret(field.key)}
disabled={disabled}
className="absolute right-0 top-1/2 grid h-10 w-10 -translate-y-1/2 place-items-center rounded-full text-muted-foreground hover:text-foreground sm:right-1 sm:h-8 sm:w-8"
>
{visible ? (
<EyeOff className="h-3.5 w-3.5" aria-hidden />
) : (
<Eye className="h-3.5 w-3.5" aria-hidden />
)}
</button>
) : null}
</span>
{errorMessage}
{showSecretActions && field.secret && configuredFields?.has(field.key) && !value.trim() && onClearSecret ? (
<button
type="button"
className="mt-1 min-h-8 text-[11px] font-medium text-muted-foreground underline-offset-4 hover:text-foreground hover:underline"
onClick={() => onClearSecret(field.key, !clearSecret)}
disabled={disabled}
>
{clearSecret
? tx("settings.channels.keepSavedSecret", "Keep saved credential")
: tx("settings.channels.removeSavedSecret", "Remove saved credential")}
</button>
) : null}
</div>
</div>
);
})}
</div>
);
}