bugfixing

This commit is contained in:
MuslemRahimi 2025-03-07 20:16:34 +01:00
parent 98975c179a
commit 9d47ef0177

View File

@ -15,39 +15,24 @@
let showSuggestions = false; let showSuggestions = false;
let touchedInput = false; let touchedInput = false;
$: inputValue = ""; // FIX: Declare inputValue as a normal variable instead of using a reactive assignment.
let inputValue = "";
let nextPage = false; let nextPage = false;
let searchOpen = false; let searchOpen = false;
let searchBarModalChecked = false; // Initialize it to false let searchBarModalChecked = false; // Initialize it to false
let inputElement; let inputElement;
let isNavigating = false; let isNavigating = false;
// NEW: Flag to ensure we only auto-dispatch ArrowDown once per modal open.
let suggestionSelected = false;
const popularList = [ const popularList = [
{ { symbol: "KO", name: "Coca Cola Company", type: "Stock" },
symbol: "KO", { symbol: "TSLA", name: "Tesla Inc", type: "Stock" },
name: "Coca Cola Company", { symbol: "AMD", name: "Advanced Micro Devices", type: "Stock" },
type: "Stock", { symbol: "SPY", name: "SPDR S&P 500 ETF Trust", type: "ETF" },
}, { symbol: "NVDA", name: "Nvidia", type: "Stock" },
{
symbol: "TSLA",
name: "Tesla Inc",
type: "Stock",
},
{
symbol: "AMD",
name: "Advanced Micro Devices",
type: "Stock",
},
{
symbol: "SPY",
name: "SPDR S&P 500 ETF Trust",
type: "ETF",
},
{
symbol: "NVDA",
name: "Nvidia",
type: "Stock",
},
]; ];
async function handleSearch(symbol, assetType) { async function handleSearch(symbol, assetType) {
@ -111,7 +96,6 @@
const closePopup = document.getElementById("searchBarModal"); const closePopup = document.getElementById("searchBarModal");
closePopup?.dispatchEvent(new MouseEvent("click")); closePopup?.dispatchEvent(new MouseEvent("click"));
// Reset the flag after a short delay
setTimeout(() => { setTimeout(() => {
isNavigating = false; isNavigating = false;
}, 100); }, 100);
@ -156,11 +140,10 @@
async function search() { async function search() {
isLoading = true; isLoading = true;
clearTimeout(timeoutId); // Clear any existing timeout clearTimeout(timeoutId);
if (!inputValue.trim()) { if (!inputValue.trim()) {
// Skip if query is empty or just whitespace searchBarData = [];
searchBarData = []; // Clear previous results
isLoading = false; isLoading = false;
return; return;
} }
@ -171,7 +154,7 @@
); );
searchBarData = await response?.json(); searchBarData = await response?.json();
isLoading = false; isLoading = false;
}, 200); // delay }, 200);
} }
function handleKeyDown(symbol) { function handleKeyDown(symbol) {
@ -205,7 +188,6 @@
function saveRecentTicker() { function saveRecentTicker() {
try { try {
// Save the version along with the rules
localStorage?.setItem("search-history", JSON?.stringify(searchHistory)); localStorage?.setItem("search-history", JSON?.stringify(searchHistory));
} catch (e) { } catch (e) {
console.log("Failed saving indicator rules: ", e); console.log("Failed saving indicator rules: ", e);
@ -215,14 +197,12 @@
onMount(() => { onMount(() => {
try { try {
const savedRules = localStorage?.getItem("search-history"); const savedRules = localStorage?.getItem("search-history");
if (savedRules) { if (savedRules) {
searchHistory = JSON.parse(savedRules); searchHistory = JSON.parse(savedRules);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
window.addEventListener("keydown", handleControlK); window.addEventListener("keydown", handleControlK);
return () => { return () => {
window.removeEventListener("keydown", handleControlK); window.removeEventListener("keydown", handleControlK);
@ -235,14 +215,17 @@
if ($screenWidth > 640) { if ($screenWidth > 640) {
inputElement.focus(); inputElement.focus();
} }
//Page is not scrollable now // Prevent body scroll while modal is open
document.body.classList.add("overflow-hidden"); document.body.classList.add("overflow-hidden");
// Reset our flag when modal is (re)opened.
suggestionSelected = false;
} }
} }
$: { $: {
if (searchBarModalChecked === false && typeof window !== "undefined") { if (searchBarModalChecked === false && typeof window !== "undefined") {
showSuggestions = inputValue = ""; showSuggestions = inputValue = "";
suggestionSelected = false;
document.body.classList?.remove("overflow-hidden"); document.body.classList?.remove("overflow-hidden");
} }
} }
@ -253,10 +236,8 @@
updatedSearchHistory?.length > 0 updatedSearchHistory?.length > 0
) { ) {
(async () => { (async () => {
// Add 500 ms delay is important otherwise bug since #each has searchHistory and updates too quickly and redirects to wrong symbol // Delay is needed so that the #each block updates properly
await new Promise((resolve) => setTimeout(resolve, 500)); await new Promise((resolve) => setTimeout(resolve, 500));
// Update search history after delay
searchHistory = updatedSearchHistory; searchHistory = updatedSearchHistory;
updatedSearchHistory = []; updatedSearchHistory = [];
saveRecentTicker(); saveRecentTicker();
@ -267,11 +248,7 @@
$: { $: {
if (searchBarData) { if (searchBarData) {
if (searchBarData?.length > 0) { showSuggestions = searchBarData?.length > 0;
showSuggestions = true;
} else {
showSuggestions = false;
}
} }
} }
@ -281,13 +258,20 @@
} }
} }
$: if (showSuggestions && searchBarData?.length && touchedInput) { // FIX: Dispatch ArrowDown only once (when suggestions open) to autoselect the first suggestion.
$: if (
showSuggestions &&
searchBarData?.length &&
touchedInput &&
!suggestionSelected
) {
tick().then(() => { tick().then(() => {
const input = document.getElementById("combobox-input"); const input = document.getElementById("combobox-input");
if (input) { if (input) {
input.dispatchEvent( input.dispatchEvent(
new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }), new KeyboardEvent("keydown", { key: "ArrowDown", bubbles: true }),
); );
suggestionSelected = true;
} }
}); });
} }