add recent search results
This commit is contained in:
parent
04a5377b4f
commit
98a07486eb
@ -10,6 +10,9 @@
|
|||||||
import { onMount } from "svelte";
|
import { onMount } from "svelte";
|
||||||
import Search from "lucide-svelte/icons/search";
|
import Search from "lucide-svelte/icons/search";
|
||||||
|
|
||||||
|
let searchHistory = [];
|
||||||
|
let searchResults = [];
|
||||||
|
|
||||||
let dataLoaded = false; // Flag to track data loading
|
let dataLoaded = false; // Flag to track data loading
|
||||||
|
|
||||||
let assetType = "";
|
let assetType = "";
|
||||||
@ -38,52 +41,119 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function popularTicker(state, assetType) {
|
async function popularTicker(state, assetType) {
|
||||||
searchOpen = false;
|
searchOpen = false;
|
||||||
|
|
||||||
if (assetType === "ETF") {
|
if (!state) return;
|
||||||
etfTicker.update((value) => state?.toUpperCase());
|
|
||||||
} else if (assetType === "Stock") {
|
// Convert state to uppercase once
|
||||||
stockTicker.update((value) => state?.toUpperCase());
|
const upperState = state.toUpperCase();
|
||||||
} else if (assetType === "Crypto") {
|
|
||||||
cryptoTicker.update((value) => state?.toUpperCase());
|
// Find the matching ticker data
|
||||||
|
const newSearchItem = $searchBarData?.find(
|
||||||
|
({ symbol }) => symbol === upperState,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update search history:
|
||||||
|
// 1. Remove the item if it already exists (to avoid duplicates)
|
||||||
|
// 2. Add the new item at the beginning
|
||||||
|
// 3. Limit to 5 items
|
||||||
|
if (newSearchItem) {
|
||||||
|
searchHistory = [
|
||||||
|
newSearchItem,
|
||||||
|
...(searchHistory?.filter((item) => item.symbol !== upperState) || []),
|
||||||
|
].slice(0, 5);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Save recent ticker
|
||||||
|
await saveRecentTicker();
|
||||||
|
|
||||||
|
// Map of asset types to their corresponding actions
|
||||||
|
const assetActions = {
|
||||||
|
ETF: () => {
|
||||||
|
etfTicker.update(() => upperState);
|
||||||
|
},
|
||||||
|
Stock: () => {
|
||||||
|
stockTicker.update(() => upperState);
|
||||||
|
},
|
||||||
|
Crypto: () => {
|
||||||
|
cryptoTicker.update(() => upperState);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Execute the appropriate action for the asset type
|
||||||
|
if (assetActions[assetType]) {
|
||||||
|
assetActions[assetType]();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Close search modal
|
||||||
const closePopup = document.getElementById("searchBarModal");
|
const closePopup = document.getElementById("searchBarModal");
|
||||||
closePopup?.dispatchEvent(new MouseEvent("click"));
|
closePopup?.dispatchEvent(new MouseEvent("click"));
|
||||||
}
|
}
|
||||||
|
|
||||||
function searchBarTicker(state, assetType) {
|
async function searchBarTicker(state, assetType) {
|
||||||
showSuggestions = false;
|
showSuggestions = false;
|
||||||
|
|
||||||
|
// Early return if state is empty or ticker not found
|
||||||
if (
|
if (
|
||||||
state !== "" &&
|
!state ||
|
||||||
$searchBarData?.find((item) => item?.symbol === state?.toUpperCase())
|
!$searchBarData?.find((item) => item?.symbol === state?.toUpperCase())
|
||||||
) {
|
) {
|
||||||
notFoundTicker = false;
|
notFoundTicker = false;
|
||||||
if (assetType === "ETF") {
|
searchQuery = "";
|
||||||
etfTicker.update((value) => state?.toUpperCase());
|
return;
|
||||||
goto(`/etf/${state?.toUpperCase()}`);
|
|
||||||
} else if (assetType === "Stock") {
|
|
||||||
stockTicker.update((value) => state?.toUpperCase());
|
|
||||||
goto(`/stocks/${state?.toUpperCase()}`);
|
|
||||||
} else if (assetType === "Crypto") {
|
|
||||||
cryptoTicker.update((value) => state?.toUpperCase());
|
|
||||||
goto(`/crypto/${state?.toUpperCase()}`);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Convert state to uppercase once
|
||||||
|
const upperState = state.toUpperCase();
|
||||||
|
|
||||||
|
// Find the matching ticker data
|
||||||
|
const newSearchItem = $searchBarData?.find(
|
||||||
|
({ symbol }) => symbol === upperState,
|
||||||
|
);
|
||||||
|
|
||||||
|
// Update search history:
|
||||||
|
// 1. Remove the item if it already exists (to avoid duplicates)
|
||||||
|
// 2. Add the new item at the beginning
|
||||||
|
// 3. Limit to 5 items
|
||||||
|
if (newSearchItem) {
|
||||||
|
searchHistory = [
|
||||||
|
newSearchItem,
|
||||||
|
...(searchHistory?.filter((item) => item.symbol !== upperState) || []),
|
||||||
|
].slice(0, 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Map of asset types to their corresponding actions
|
||||||
|
await saveRecentTicker();
|
||||||
|
|
||||||
|
const assetActions = {
|
||||||
|
ETF: () => {
|
||||||
|
etfTicker.update(() => upperState);
|
||||||
|
goto(`/etf/${upperState}`);
|
||||||
|
},
|
||||||
|
Stock: () => {
|
||||||
|
stockTicker.update(() => upperState);
|
||||||
|
goto(`/stocks/${upperState}`);
|
||||||
|
},
|
||||||
|
Crypto: () => {
|
||||||
|
cryptoTicker.update(() => upperState);
|
||||||
|
goto(`/crypto/${upperState}`);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
// Execute the appropriate action for the asset type
|
||||||
|
if (assetActions[assetType]) {
|
||||||
|
assetActions[assetType]();
|
||||||
|
|
||||||
|
// Close search modal
|
||||||
searchOpen = false;
|
searchOpen = false;
|
||||||
//searchQuery = state.toUpperCase();
|
|
||||||
const closePopup = document.getElementById("searchBarModal");
|
const closePopup = document.getElementById("searchBarModal");
|
||||||
closePopup?.dispatchEvent(new MouseEvent("click"));
|
closePopup?.dispatchEvent(new MouseEvent("click"));
|
||||||
} else {
|
|
||||||
notFoundTicker = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
searchQuery = "";
|
searchQuery = "";
|
||||||
}
|
}
|
||||||
|
|
||||||
let searchResults = [];
|
|
||||||
|
|
||||||
async function search() {
|
async function search() {
|
||||||
const normalizedSearchQuery = searchQuery?.toLowerCase();
|
const normalizedSearchQuery = searchQuery?.toLowerCase();
|
||||||
|
|
||||||
@ -224,7 +294,26 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function saveRecentTicker() {
|
||||||
|
try {
|
||||||
|
// Save the version along with the rules
|
||||||
|
localStorage?.setItem("search-history", JSON?.stringify(searchHistory));
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Failed saving indicator rules: ", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
|
try {
|
||||||
|
const savedRules = localStorage?.getItem("search-history");
|
||||||
|
|
||||||
|
if (savedRules) {
|
||||||
|
searchHistory = JSON.parse(savedRules);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.log(e);
|
||||||
|
}
|
||||||
|
|
||||||
window.addEventListener("keydown", handleControlF);
|
window.addEventListener("keydown", handleControlF);
|
||||||
window.addEventListener("keydown", handleEscape);
|
window.addEventListener("keydown", handleEscape);
|
||||||
return () => {
|
return () => {
|
||||||
@ -436,12 +525,12 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{#if !showSuggestions}
|
{#if !showSuggestions}
|
||||||
<div class="text-start text-sm font-semibold text-white mb-2">
|
<div class="text-start text-sm font-semibold text-white mb-2">
|
||||||
Popular
|
{searchHistory?.length > 0 ? "Recent" : "Popular"}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<ul class="text-sm">
|
<ul class="text-sm">
|
||||||
{#if !showSuggestions}
|
{#if !showSuggestions}
|
||||||
{#each popularList as item}
|
{#each searchHistory?.length > 0 ? searchHistory : popularList as item}
|
||||||
<li class="border-b border-gray-600">
|
<li class="border-b border-gray-600">
|
||||||
<a
|
<a
|
||||||
data-sveltekit-preload-data={false}
|
data-sveltekit-preload-data={false}
|
||||||
@ -627,12 +716,12 @@
|
|||||||
{/if}
|
{/if}
|
||||||
{#if !showSuggestions}
|
{#if !showSuggestions}
|
||||||
<div class="text-start text-sm font-semibold text-white mb-2">
|
<div class="text-start text-sm font-semibold text-white mb-2">
|
||||||
Popular
|
{searchHistory?.length > 0 ? "Recent" : "Popular"}
|
||||||
</div>
|
</div>
|
||||||
{/if}
|
{/if}
|
||||||
<ul class="text-sm">
|
<ul class="text-sm">
|
||||||
{#if !showSuggestions}
|
{#if !showSuggestions}
|
||||||
{#each popularList as item}
|
{#each searchHistory?.length > 0 ? searchHistory : popularList as item}
|
||||||
<li class="border-b border-gray-600">
|
<li class="border-b border-gray-600">
|
||||||
<a
|
<a
|
||||||
data-sveltekit-preload-data={false}
|
data-sveltekit-preload-data={false}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user