(props: {
readonly value: string | null;
readonly valueOwner?: Owner;
readonly onSelect: (id: string, owner: Owner) => void;
readonly secrets: readonly SecretPickerSecret[];
readonly placeholder?: string;
/** When provided, renders a "+ New" row at the top of the dropdown. */
readonly onCreateNew?: () => void;
})
| 49 | }; |
| 50 | |
| 51 | export function SecretPicker(props: { |
| 52 | readonly value: string | null; |
| 53 | readonly valueOwner?: Owner; |
| 54 | readonly onSelect: (id: string, owner: Owner) => void; |
| 55 | readonly secrets: readonly SecretPickerSecret[]; |
| 56 | readonly placeholder?: string; |
| 57 | /** When provided, renders a "+ New" row at the top of the dropdown. */ |
| 58 | readonly onCreateNew?: () => void; |
| 59 | }) { |
| 60 | const { |
| 61 | value, |
| 62 | valueOwner, |
| 63 | onSelect, |
| 64 | secrets, |
| 65 | placeholder = "Search credentials…", |
| 66 | onCreateNew, |
| 67 | } = props; |
| 68 | const [open, setOpen] = useState(false); |
| 69 | const [query, setQuery] = useState(""); |
| 70 | const ownerDisplay = useOwnerDisplay(); |
| 71 | |
| 72 | const selected = |
| 73 | secrets.find( |
| 74 | (secret) => secret.id === value && (valueOwner === undefined || secret.owner === valueOwner), |
| 75 | ) ?? |
| 76 | secrets.find((secret) => secret.id === value) ?? |
| 77 | null; |
| 78 | |
| 79 | const grouped = new Map<string, SecretPickerSecret[]>(); |
| 80 | for (const secret of secrets) { |
| 81 | const key = providerLabel(secret.provider); |
| 82 | const group = grouped.get(key); |
| 83 | if (group) { |
| 84 | group.push(secret); |
| 85 | } else { |
| 86 | grouped.set(key, [secret]); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | const groups: [string, SecretPickerSecret[]][] = [...grouped.entries()] |
| 91 | .map(([label, items]): [string, SecretPickerSecret[]] => [ |
| 92 | label, |
| 93 | [...items].sort((a, b) => a.name.localeCompare(b.name)), |
| 94 | ]) |
| 95 | .sort(([a], [b]) => a.localeCompare(b)); |
| 96 | const showGroupHeadings = groups.length > 1; |
| 97 | |
| 98 | return ( |
| 99 | <div className="relative w-full"> |
| 100 | <Popover open={open} onOpenChange={setOpen} modal={false}> |
| 101 | <PopoverAnchor asChild> |
| 102 | <Input |
| 103 | value={open ? query : selected ? selected.name : (value ?? "")} |
| 104 | onChange={(event: ChangeEvent<HTMLInputElement>) => { |
| 105 | setQuery(event.target.value); |
| 106 | if (!open) setOpen(true); |
| 107 | }} |
| 108 | onFocus={() => setOpen(true)} |
nothing calls this directly
no test coverage detected