Skip to content

Commit 14bfc31

Browse files
authored
Merge pull request #2962 from trycompai/chas/account-settings-multiselect
CS-415 [Bug] - GWS integration - when a user is removed from the exclude list, and resync is conducted, that user is not being pulled in with from the sync
2 parents f618429 + 3a60c08 commit 14bfc31

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

‎apps/app/src/components/integrations/ConnectionVariableMultiSelect.tsx‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { Input, Spinner } from '@trycompai/design-system';
44
import { Close } from '@trycompai/design-system/icons';
55
import MultipleSelector from '@trycompai/ui/multiple-selector';
6-
import { useEffect, useRef } from 'react';
6+
import { useEffect, useMemo, useRef, useState } from 'react';
77
import type { ConnectionVariable, VariableValue } from './ConnectionVariablesForm';
88

99
interface ConnectionVariableMultiSelectProps {
@@ -31,6 +31,30 @@ export function ConnectionVariableMultiSelect({
3131
const isGitHubRepos = variable.id === 'target_repos';
3232
const parsedConfigs = isGitHubRepos ? normalizedSelectedValues.map(parseRepoBranch) : [];
3333

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+
3458
useEffect(() => {
3559
if (
3660
variable.hasDynamicOptions &&
@@ -77,21 +101,31 @@ export function ConnectionVariableMultiSelect({
77101
onChange(newValues);
78102
};
79103

80-
const reposForSelector = isGitHubRepos
81-
? parsedConfigs.map((config) => config.repo)
82-
: normalizedSelectedValues;
83104
const isCreatable = isGitHubRepos || options.length === 0;
84105

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+
85119
return (
86120
<div className="space-y-3">
87121
<MultipleSelector
88122
value={reposForSelector.map((value) => ({
89123
value,
90-
label: options.find((option) => option.value === value)?.label || value,
124+
label: combinedOptions.find((option) => option.value === value)?.label || value,
91125
}))}
92126
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}
95129
placeholder={variable.placeholder || `Select ${variable.label.toLowerCase()}...`}
96130
creatable={isCreatable}
97131
emptyIndicator={

0 commit comments

Comments
 (0)