import type { RefObject } from "react"; import { Box, Text, TextAttributes, useUiHost, type InputRenderable } from "../../../ui"; import { colors } from "../../../theme/colors"; import { t, tf } from "../../../i18n"; import type { BrokerConfigField } from "../../../types/broker"; import { TextField, type ListViewItem } from "../../ui"; import { SelectField } from "../../ui/select-field"; import { formatBrokerFieldValue } from "./utils"; export function BrokerFieldsPanel({ selectedBrokerId, brokerFields, brokerFieldIdx, brokerSelectIdx, onBrokerSelect, brokerValues, onBrokerFieldChange, onSubmitBrokerField, editing, inputRef, }: { choices: ListViewItem[]; selectedBrokerId: string; brokerFields: BrokerConfigField[]; brokerFieldIdx: number; brokerSelectIdx: number; onBrokerSelect?: (index: number) => void; brokerValues: Record>; onBrokerFieldChange: (brokerId: string, key: string, value: string) => void; onSubmitBrokerField?: () => void; editing: boolean; inputRef: RefObject; }) { const values = brokerValues[selectedBrokerId] ?? {}; const desktop = useUiHost().kind === "desktop-web"; return ( {brokerFields.map((field, index) => { if (index > brokerFieldIdx) return null; const value = values[field.key] ?? ""; const isActive = index === brokerFieldIdx; if (!isActive && value) { return ( {desktop ? "+ " : "\u2713 "} {`${field.label}: ${formatBrokerFieldValue(field, value)}`} ); } if (isActive) { const activeSelectValue = field.options?.[brokerSelectIdx]?.value ?? values[field.key] ?? ""; const activeSelectOption = field.options?.[brokerSelectIdx]; const effectiveValue = value || field.defaultValue || ""; return ( {index > 0 && } {!desktop ? ( {tf("Step {step}: ", { step: index + 1 })} ) : null} {t(field.label)} {field.type !== "select" ? ( {editing ? ( onBrokerFieldChange(selectedBrokerId, field.key, nextValue)} onSubmit={() => onSubmitBrokerField?.()} /> ) : ( {effectiveValue ? `\u2713 ${t(field.label)}: ${formatBrokerFieldValue(field, effectiveValue)}` : t("Press enter to type...")} )} ) : null} {field.type !== "select" && !value && field.defaultValue && ( {tf("Press enter to use {value}", { value: field.defaultValue })} )} {field.type === "select" && desktop ? ( ({ value: option.value, label: t(option.label), description: option.description ? t(option.description) : undefined, }))} onChange={(nextValue) => { const nextIndex = Math.max(0, field.options?.findIndex((option) => option.value === nextValue) ?? 0); onBrokerSelect?.(nextIndex); onBrokerFieldChange(selectedBrokerId, field.key, nextValue); }} /> ) : field.type === "select" ? ( {(field.options ?? []).map((option, optionIdx) => { const selected = optionIdx === brokerSelectIdx; return ( onBrokerSelect?.(optionIdx)} style={{ cursor: "pointer" }} > {selected ? "\u25b8 " : " "} {t(option.label)} ); })} {activeSelectOption?.description ? ( {t(activeSelectOption.description)} ) : null} ) : null} {field.type === "select" && !desktop && ( {t("Use \u2191\u2193 to choose")} )} ); } return null; })} {t("Credentials are saved locally.")} {!desktop ? ( <> {tf("Field {current} of {total}", { current: brokerFieldIdx + 1, total: brokerFields.length })} ) : null} ); }