Skip to content

Commit f3a6915

Browse files
committed
fix: display loading while calculating suggestions
1 parent d9c06df commit f3a6915

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

packages/diracx-web-components/src/components/shared/SearchBar/SearchBar.tsx

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ export function SearchBar<T extends string>({
102102
const [tokenEquations, setTokenEquations] = useState<
103103
SearchBarTokenEquation[]
104104
>([]);
105+
106+
const [isSuggestionsLoading, setIsSuggestionsLoading] =
107+
useState<boolean>(false);
108+
105109
/** A ref to store the current filters to avoid reloading the token equations */
106110
const currentFilters = useRef<string | null>(null);
107111
/** A ref to store a boolean indicating if the component is updating from search */
@@ -162,27 +166,52 @@ export function SearchBar<T extends string>({
162166
[createSuggestions],
163167
);
164168

169+
// Load suggestions (with proper loading tracking and cancellation)
165170
useEffect(() => {
166-
async function load() {
167-
const params: CreateSuggestionsParams = {
168-
previousToken,
169-
previousEquation,
170-
equationIndex: focusedTokenIndex?.equationIndex,
171-
};
171+
let cancelled = false;
172+
173+
const emptySuggestions: SearchBarSuggestions = {
174+
items: [],
175+
nature: [],
176+
type: [],
177+
};
172178

173-
if (usesCurrentInput && inputValue) {
174-
params.currentInput = inputValue;
179+
const run = async () => {
180+
setIsSuggestionsLoading(true);
181+
setSuggestions(emptySuggestions);
182+
183+
try {
184+
const params: CreateSuggestionsParams = {
185+
previousToken,
186+
previousEquation,
187+
equationIndex:
188+
focusedTokenIndex?.equationIndex ?? tokenEquations.length - 1,
189+
};
190+
if (usesCurrentInput && inputValue) {
191+
params.currentInput = inputValue;
192+
}
193+
194+
const result = await createSuggestions(params);
195+
if (!cancelled) {
196+
setSuggestions(result);
197+
}
198+
} finally {
199+
if (!cancelled) {
200+
setIsSuggestionsLoading(false);
201+
}
175202
}
203+
};
176204

177-
const result = await createSuggestions(params);
178-
setSuggestions(result);
179-
}
180-
load();
205+
run();
206+
return () => {
207+
cancelled = true;
208+
};
181209
}, [
182210
previousEquation,
183211
previousToken,
184212
createSuggestions,
185213
focusedTokenIndex,
214+
tokenEquations.length,
186215
// If the current input is not used, we don't want to trigger the suggestions for each letter typed
187216
...(usesCurrentInput ? [inputValue] : []),
188217
]);
@@ -288,6 +317,7 @@ export function SearchBar<T extends string>({
288317
setTokenEquations={setTokenEquations}
289318
tokenEquations={tokenEquations}
290319
suggestions={suggestions}
320+
suggestionsLoading={isSuggestionsLoading}
291321
focusedTokenIndex={focusedTokenIndex}
292322
setFocusedTokenIndex={setFocusedTokenIndex}
293323
allowKeyWordSearch={allowKeyWordSearch}

packages/diracx-web-components/src/components/shared/SearchBar/SearchField.tsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,34 @@ import "dayjs/locale/en-gb"; // Import the locale for dayjs
2424
import { MyDateTimePicker } from "./DatePicker";
2525

2626
interface SearchFieldProps {
27+
/** The current input value in the search field */
2728
inputValue: string;
29+
/** Function to update the input value */
2830
setInputValue: React.Dispatch<React.SetStateAction<string>>;
31+
32+
/** Reference to the input element */
2933
inputRef: React.RefObject<HTMLInputElement>;
34+
35+
/** The current token equations */
36+
tokenEquations: SearchBarTokenEquation[];
37+
/** Function to update the token equations */
3038
setTokenEquations: React.Dispatch<
3139
React.SetStateAction<SearchBarTokenEquation[]>
3240
>;
33-
tokenEquations: SearchBarTokenEquation[];
41+
42+
/** The current suggestions for the search field */
3443
suggestions: SearchBarSuggestions;
44+
/** Boolean indicating if suggestions are loading */
45+
suggestionsLoading: boolean;
46+
47+
/** The current focused token index */
3548
focusedTokenIndex: EquationAndTokenIndex | null;
49+
/** Function to update the focused token index */
3650
setFocusedTokenIndex: React.Dispatch<
3751
React.SetStateAction<EquationAndTokenIndex | null>
3852
>;
53+
54+
/** Boolean indicating if keyword search is allowed */
3955
allowKeyWordSearch?: boolean;
4056
}
4157

@@ -46,6 +62,7 @@ export default function SearchField({
4662
setTokenEquations,
4763
tokenEquations,
4864
suggestions,
65+
suggestionsLoading,
4966
focusedTokenIndex,
5067
setFocusedTokenIndex,
5168
allowKeyWordSearch = true,
@@ -289,6 +306,7 @@ export default function SearchField({
289306
onHighlightChange={(_e, option) => {
290307
optionSelectedRef.current = option !== null;
291308
}}
309+
loading={suggestionsLoading}
292310
renderInput={(params) => (
293311
<TextField
294312
{...params}

0 commit comments

Comments
 (0)