|
3 | 3 | import { Input, Spinner } from '@trycompai/design-system'; |
4 | 4 | import { Close } from '@trycompai/design-system/icons'; |
5 | 5 | import MultipleSelector from '@trycompai/ui/multiple-selector'; |
6 | | -import { useEffect, useRef } from 'react'; |
| 6 | +import { useEffect, useMemo, useRef, useState } from 'react'; |
7 | 7 | import type { ConnectionVariable, VariableValue } from './ConnectionVariablesForm'; |
8 | 8 |
|
9 | 9 | interface ConnectionVariableMultiSelectProps { |
@@ -31,6 +31,30 @@ export function ConnectionVariableMultiSelect({ |
31 | 31 | const isGitHubRepos = variable.id === 'target_repos'; |
32 | 32 | const parsedConfigs = isGitHubRepos ? normalizedSelectedValues.map(parseRepoBranch) : []; |
33 | 33 |
|
| 34 | + const reposForSelector = isGitHubRepos |
| 35 | + ? parsedConfigs.map((config) => config.repo) |
| 36 | + : normalizedSelectedValues; |
| 37 | + |
| 38 | + // Remembers every value that has appeared in the multi-select during the |
| 39 | + // lifetime of this component so that values the user removes (clicking the |
| 40 | + // X on a tag) remain available as re-selectable options in the dropdown. |
| 41 | + // Without this, free-form multi-selects like Google Workspace's |
| 42 | + // `sync_excluded_emails` have an empty dropdown (no fetchOptions, no static |
| 43 | + // options), so a removed tag can only be brought back by retyping it. |
| 44 | + const [seenValues, setSeenValues] = useState<Set<string>>(() => new Set(reposForSelector)); |
| 45 | + useEffect(() => { |
| 46 | + setSeenValues((prev) => { |
| 47 | + let next: Set<string> | null = null; |
| 48 | + for (const v of reposForSelector) { |
| 49 | + if (v && !prev.has(v)) { |
| 50 | + if (!next) next = new Set(prev); |
| 51 | + next.add(v); |
| 52 | + } |
| 53 | + } |
| 54 | + return next ?? prev; |
| 55 | + }); |
| 56 | + }, [reposForSelector]); |
| 57 | + |
34 | 58 | useEffect(() => { |
35 | 59 | if ( |
36 | 60 | variable.hasDynamicOptions && |
@@ -77,21 +101,31 @@ export function ConnectionVariableMultiSelect({ |
77 | 101 | onChange(newValues); |
78 | 102 | }; |
79 | 103 |
|
80 | | - const reposForSelector = isGitHubRepos |
81 | | - ? parsedConfigs.map((config) => config.repo) |
82 | | - : normalizedSelectedValues; |
83 | 104 | const isCreatable = isGitHubRepos || options.length === 0; |
84 | 105 |
|
| 106 | + const combinedOptions = useMemo(() => { |
| 107 | + const map = new Map<string, { value: string; label: string }>(); |
| 108 | + for (const option of options) { |
| 109 | + map.set(option.value, { value: option.value, label: option.label }); |
| 110 | + } |
| 111 | + for (const v of seenValues) { |
| 112 | + if (!map.has(v)) { |
| 113 | + map.set(v, { value: v, label: v }); |
| 114 | + } |
| 115 | + } |
| 116 | + return Array.from(map.values()); |
| 117 | + }, [options, seenValues]); |
| 118 | + |
85 | 119 | return ( |
86 | 120 | <div className="space-y-3"> |
87 | 121 | <MultipleSelector |
88 | 122 | value={reposForSelector.map((value) => ({ |
89 | 123 | value, |
90 | | - label: options.find((option) => option.value === value)?.label || value, |
| 124 | + label: combinedOptions.find((option) => option.value === value)?.label || value, |
91 | 125 | }))} |
92 | 126 | onChange={(selected) => handleRepoSelectionChange(selected.map((item) => item.value))} |
93 | | - defaultOptions={options.map((option) => ({ value: option.value, label: option.label }))} |
94 | | - options={options.map((option) => ({ value: option.value, label: option.label }))} |
| 127 | + defaultOptions={combinedOptions} |
| 128 | + options={combinedOptions} |
95 | 129 | placeholder={variable.placeholder || `Select ${variable.label.toLowerCase()}...`} |
96 | 130 | creatable={isCreatable} |
97 | 131 | emptyIndicator={ |
|
0 commit comments