fix: add type-safe Array checks to autocomplete suggestions to prevent TypeError rendering crashes
Browse files
frontend/src/components/WatchlistSidebar.tsx
CHANGED
|
@@ -89,7 +89,13 @@ export default function WatchlistSidebar({
|
|
| 89 |
const response = await fetch(`${API_URL}/api/v1/stocks/search?q=${encodeURIComponent(query)}`);
|
| 90 |
if (response.ok) {
|
| 91 |
const data = await response.json();
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
}
|
| 94 |
} catch (err) {
|
| 95 |
console.error('Error fetching stock suggestions:', err);
|
|
@@ -102,7 +108,7 @@ export default function WatchlistSidebar({
|
|
| 102 |
}, [newTicker]);
|
| 103 |
|
| 104 |
// Determine what suggestions to display
|
| 105 |
-
const displaySuggestions = newTicker.trim().length >= 2 ? suggestions : DEFAULT_SUGGESTIONS;
|
| 106 |
|
| 107 |
const handleAdd = async (ticker: string) => {
|
| 108 |
let cleanTicker = ticker.trim().toUpperCase();
|
|
|
|
| 89 |
const response = await fetch(`${API_URL}/api/v1/stocks/search?q=${encodeURIComponent(query)}`);
|
| 90 |
if (response.ok) {
|
| 91 |
const data = await response.json();
|
| 92 |
+
if (Array.isArray(data)) {
|
| 93 |
+
setSuggestions(data);
|
| 94 |
+
} else {
|
| 95 |
+
setSuggestions([]);
|
| 96 |
+
}
|
| 97 |
+
} else {
|
| 98 |
+
setSuggestions([]);
|
| 99 |
}
|
| 100 |
} catch (err) {
|
| 101 |
console.error('Error fetching stock suggestions:', err);
|
|
|
|
| 108 |
}, [newTicker]);
|
| 109 |
|
| 110 |
// Determine what suggestions to display
|
| 111 |
+
const displaySuggestions = (newTicker.trim().length >= 2 && Array.isArray(suggestions)) ? suggestions : DEFAULT_SUGGESTIONS;
|
| 112 |
|
| 113 |
const handleAdd = async (ticker: string) => {
|
| 114 |
let cleanTicker = ticker.trim().toUpperCase();
|