This commit is contained in:
MuslemRahimi 2024-10-30 22:00:33 +01:00
parent e4b17138c0
commit 04a5377b4f
3 changed files with 545 additions and 461 deletions

View File

@ -1,114 +1,107 @@
<script lang="ts"> <script lang="ts">
import {
import { searchBarData, stockTicker, etfTicker, cryptoTicker, screenWidth} from '$lib/store'; searchBarData,
import { goto } from '$app/navigation'; stockTicker,
import { onMount } from 'svelte'; etfTicker,
cryptoTicker,
screenWidth,
} from "$lib/store";
import { goto } from "$app/navigation";
import { onMount } from "svelte";
import Search from "lucide-svelte/icons/search"; import Search from "lucide-svelte/icons/search";
let dataLoaded = false; // Flag to track data loading let dataLoaded = false; // Flag to track data loading
let assetType = ''; let assetType = "";
let showSuggestions = false; let showSuggestions = false;
let notFoundTicker = false; let notFoundTicker = false;
let searchQuery = ''; let searchQuery = "";
let searchOpen = false; let searchOpen = false;
let searchBarModalChecked = false; // Initialize it to false let searchBarModalChecked = false; // Initialize it to false
let inputElement; let inputElement;
async function loadSearchData() { async function loadSearchData() {
if ($searchBarData.length !== 0 || dataLoaded) return; if ($searchBarData.length !== 0 || dataLoaded) return;
else { else {
dataLoaded = true; dataLoaded = true;
// make the GET request to the endpoint // make the GET request to the endpoint
const response = await fetch('/api/searchbar-data', { const response = await fetch("/api/searchbar-data", {
method: 'GET', method: "GET",
headers: { headers: {
"Content-Type": "application/json" "Content-Type": "application/json",
}, },
}); });
$searchBarData = await response.json(); $searchBarData = await response.json();
} }
} }
function popularTicker(state, assetType) { function popularTicker(state, assetType) {
searchOpen = false; searchOpen = false;
if (assetType === 'ETF') if (assetType === "ETF") {
{ etfTicker.update((value) => state?.toUpperCase());
etfTicker.update( value => state?.toUpperCase() ); } else if (assetType === "Stock") {
stockTicker.update((value) => state?.toUpperCase());
} else if (assetType === "Crypto") {
cryptoTicker.update((value) => state?.toUpperCase());
} }
else if (assetType === 'Stock') {
stockTicker.update( value => state?.toUpperCase() );
}
else if (assetType === 'Crypto') {
cryptoTicker.update( value => state?.toUpperCase() );
}
const closePopup = document.getElementById("searchBarModal"); const closePopup = document.getElementById("searchBarModal");
closePopup?.dispatchEvent(new MouseEvent('click')) closePopup?.dispatchEvent(new MouseEvent("click"));
} }
function searchBarTicker(state, assetType) function searchBarTicker(state, assetType) {
{
showSuggestions = false; showSuggestions = false;
if(state !== '' && $searchBarData?.find(item => item?.symbol === state?.toUpperCase())) if (
{ state !== "" &&
$searchBarData?.find((item) => item?.symbol === state?.toUpperCase())
) {
notFoundTicker = false; notFoundTicker = false;
if (assetType === 'ETF') if (assetType === "ETF") {
{ etfTicker.update((value) => state?.toUpperCase());
etfTicker.update( value => state?.toUpperCase() ); goto(`/etf/${state?.toUpperCase()}`);
goto(`/etf/${state?.toUpperCase()}`) } else if (assetType === "Stock") {
} stockTicker.update((value) => state?.toUpperCase());
else if (assetType === 'Stock') { goto(`/stocks/${state?.toUpperCase()}`);
stockTicker.update( value => state?.toUpperCase() ); } else if (assetType === "Crypto") {
goto(`/stocks/${state?.toUpperCase()}`) cryptoTicker.update((value) => state?.toUpperCase());
} goto(`/crypto/${state?.toUpperCase()}`);
else if (assetType === 'Crypto') {
cryptoTicker.update( value => state?.toUpperCase() );
goto(`/crypto/${state?.toUpperCase()}`)
} }
searchOpen = false searchOpen = false;
//searchQuery = state.toUpperCase(); //searchQuery = state.toUpperCase();
const closePopup = document.getElementById("searchBarModal"); const closePopup = document.getElementById("searchBarModal");
closePopup?.dispatchEvent(new MouseEvent('click')) closePopup?.dispatchEvent(new MouseEvent("click"));
} } else {
else{
notFoundTicker = false; notFoundTicker = false;
} }
searchQuery = "";
searchQuery ="";
} }
let searchResults = [];
async function search() {
let searchResults = [];
async function search() {
const normalizedSearchQuery = searchQuery?.toLowerCase(); const normalizedSearchQuery = searchQuery?.toLowerCase();
// Define names for which symbols without dots should be prioritized // Define names for which symbols without dots should be prioritized
const prioritizeWithoutDotsForNames = ["apple", /* Add more names as needed */]; const prioritizeWithoutDotsForNames = [
"apple" /* Add more names as needed */,
];
const filteredList = $searchBarData?.map(item => ({ const filteredList = $searchBarData
?.map((item) => ({
...item, ...item,
nameLower: item?.name?.toLowerCase(), nameLower: item?.name?.toLowerCase(),
symbolLower: item?.symbol?.toLowerCase(), symbolLower: item?.symbol?.toLowerCase(),
}))?.filter(({ nameLower, symbolLower }) => }))
?.filter(
({ nameLower, symbolLower }) =>
nameLower?.includes(normalizedSearchQuery) || nameLower?.includes(normalizedSearchQuery) ||
symbolLower?.includes(normalizedSearchQuery) symbolLower?.includes(normalizedSearchQuery),
); );
filteredList?.sort((a, b) => { filteredList?.sort((a, b) => {
@ -134,7 +127,8 @@ async function search() {
const bNameIndex = bNameLower?.indexOf(normalizedSearchQuery); const bNameIndex = bNameLower?.indexOf(normalizedSearchQuery);
// If no exact symbol match, prioritize based on the combined position in name and symbol // If no exact symbol match, prioritize based on the combined position in name and symbol
const positionComparison = aSymbolIndex + aNameIndex - (bSymbolIndex + bNameIndex); const positionComparison =
aSymbolIndex + aNameIndex - (bSymbolIndex + bNameIndex);
// Additional condition for prioritizing symbols without dots for specific names // Additional condition for prioritizing symbols without dots for specific names
if (prioritizeWithoutDotsForNames.includes(normalizedSearchQuery)) { if (prioritizeWithoutDotsForNames.includes(normalizedSearchQuery)) {
@ -150,35 +144,27 @@ async function search() {
searchResults = filteredList?.slice(0, 5); searchResults = filteredList?.slice(0, 5);
showSuggestions = normalizedSearchQuery !== ""; showSuggestions = normalizedSearchQuery !== "";
} }
const onKeyPress = (e) => {
const onKeyPress = e => {
if (e?.charCode === 13) { if (e?.charCode === 13) {
focusedSuggestion = ''; focusedSuggestion = "";
if (!notFoundTicker) { if (!notFoundTicker) {
searchBarTicker(searchQuery, assetType); searchBarTicker(searchQuery, assetType);
} }
} }
}; };
let focusedSuggestion = null;
function handleKeyDown(event) {
let focusedSuggestion = null;
function handleKeyDown(event) {
if (event.key === "ArrowDown" && showSuggestions) { if (event.key === "ArrowDown" && showSuggestions) {
// Move down in the suggestions // Move down in the suggestions
event.preventDefault(); // Prevent scrolling event.preventDefault(); // Prevent scrolling
const currentIndex = searchResults?.findIndex(item => item?.symbol === searchQuery); const currentIndex = searchResults?.findIndex(
(item) => item?.symbol === searchQuery,
);
if (currentIndex < searchResults?.length - 1) { if (currentIndex < searchResults?.length - 1) {
searchQuery = searchResults[currentIndex + 1]?.symbol; searchQuery = searchResults[currentIndex + 1]?.symbol;
assetType = searchResults[currentIndex + 1]?.type; assetType = searchResults[currentIndex + 1]?.type;
@ -187,83 +173,92 @@ function handleKeyDown(event) {
} else if (event.key === "ArrowUp" && showSuggestions) { } else if (event.key === "ArrowUp" && showSuggestions) {
// Move up in the suggestions // Move up in the suggestions
event.preventDefault(); // Prevent scrolling event.preventDefault(); // Prevent scrolling
const currentIndex = searchResults?.findIndex(item => item?.symbol === searchQuery); const currentIndex = searchResults?.findIndex(
(item) => item?.symbol === searchQuery,
);
if (currentIndex > 0) { if (currentIndex > 0) {
searchQuery = searchResults[currentIndex - 1]?.symbol; searchQuery = searchResults[currentIndex - 1]?.symbol;
assetType = searchResults[currentIndex - 1]?.type; assetType = searchResults[currentIndex - 1]?.type;
focusedSuggestion = searchQuery; // Update the focused suggestion focusedSuggestion = searchQuery; // Update the focused suggestion
} }
} } else if (event.key === "ArrowDown" && !showSuggestions) {
else if (event.key === "ArrowDown" && !showSuggestions) {
// Move down in the suggestions // Move down in the suggestions
event.preventDefault(); // Prevent scrolling event.preventDefault(); // Prevent scrolling
const currentIndex = popularList?.findIndex(item => item?.symbol === searchQuery); const currentIndex = popularList?.findIndex(
(item) => item?.symbol === searchQuery,
);
if (currentIndex < popularList?.length - 1) { if (currentIndex < popularList?.length - 1) {
searchQuery = popularList[currentIndex + 1]?.symbol; searchQuery = popularList[currentIndex + 1]?.symbol;
assetType = popularList[currentIndex + 1]?.type; assetType = popularList[currentIndex + 1]?.type;
focusedSuggestion = searchQuery; // Update the focused suggestion focusedSuggestion = searchQuery; // Update the focused suggestion
} }
} } else if (event.key === "ArrowUp" && !showSuggestions) {
else if (event.key === "ArrowUp" && !showSuggestions) {
// Move up in the suggestions // Move up in the suggestions
event.preventDefault(); // Prevent scrolling event.preventDefault(); // Prevent scrolling
const currentIndex = popularList?.findIndex(item => item?.symbol === searchQuery); const currentIndex = popularList?.findIndex(
(item) => item?.symbol === searchQuery,
);
if (currentIndex > 0) { if (currentIndex > 0) {
searchQuery = popularList[currentIndex - 1]?.symbol; searchQuery = popularList[currentIndex - 1]?.symbol;
assetType = popularList[currentIndex - 1]?.type; assetType = popularList[currentIndex - 1]?.type;
focusedSuggestion = searchQuery; // Update the focused suggestion focusedSuggestion = searchQuery; // Update the focused suggestion
} }
} }
} }
const handleControlF = async (event) => {
if (event.ctrlKey && event.key === "k") {
const handleControlF = async (event) => {
if (event.ctrlKey && event.key === 'k') {
// Ctrl+F is pressed, open the modal // Ctrl+F is pressed, open the modal
const keyboardSearch = document.getElementById('searchBarModal'); const keyboardSearch = document.getElementById("searchBarModal");
keyboardSearch?.dispatchEvent(new MouseEvent('click')) keyboardSearch?.dispatchEvent(new MouseEvent("click"));
event.preventDefault() event.preventDefault();
await loadSearchData(); await loadSearchData();
} }
}; };
const handleEscape = (event) => { const handleEscape = (event) => {
if (event.key === 'Escape') { if (event.key === "Escape") {
// Escape key is pressed, close the modal // Escape key is pressed, close the modal
searchBarModalChecked = false; searchBarModalChecked = false;
} }
}; };
onMount(() => { onMount(() => {
window.addEventListener('keydown', handleControlF); window.addEventListener("keydown", handleControlF);
window.addEventListener('keydown', handleEscape); window.addEventListener("keydown", handleEscape);
return () => { return () => {
window.removeEventListener('keydown', handleControlF); window.removeEventListener("keydown", handleControlF);
window.removeEventListener('keydown', handleEscape); window.removeEventListener("keydown", handleEscape);
}; };
}); });
let charNumber = 20;
let popularList = [];
const popularSymbols = [
"BTCUSD",
"ETHUSD",
"SOLUSD",
"SPY",
"ADBE",
"DBX",
"HOOD",
"AMZN",
"TSLA",
"AMD",
"MCD",
"NVDA",
"PYPL",
"AAPL",
"BYND",
"KO",
];
let charNumber = 20; $: {
let popularList = [];
const popularSymbols = ['BTCUSD','ETHUSD','SOLUSD','SPY','ADBE','DBX','HOOD','AMZN', 'TSLA', 'AMD','MCD','NVDA','PYPL','AAPL','BYND','KO'];
$: {
if ($searchBarData && popularList?.length === 0) { if ($searchBarData && popularList?.length === 0) {
popularList = $searchBarData?.filter(({ symbol }) => popularList = $searchBarData?.filter(({ symbol }) =>
popularSymbols?.includes(symbol) popularSymbols?.includes(symbol),
); );
// Fisher-Yates (Knuth) Shuffle Algorithm // Fisher-Yates (Knuth) Shuffle Algorithm
@ -274,46 +269,41 @@ $: {
popularList = popularList?.slice(0, 5); popularList = popularList?.slice(0, 5);
} }
} }
$: {
if (searchBarModalChecked === true && typeof window !== "undefined") {
$: { if ($screenWidth > 640) {
if( searchBarModalChecked === true && typeof window !== 'undefined')
{
if($screenWidth > 640)
{
inputElement.focus(); inputElement.focus();
} }
//Page is not scrollable now //Page is not scrollable now
document.body.classList.add("overflow-hidden"); document.body.classList.add("overflow-hidden");
} }
} }
$: { $: {
if( searchBarModalChecked === false && typeof window !== 'undefined') if (searchBarModalChecked === false && typeof window !== "undefined") {
{
showSuggestions = searchQuery = ""; showSuggestions = searchQuery = "";
document.body.classList?.remove("overflow-hidden"); document.body.classList?.remove("overflow-hidden");
} }
} }
$: { $: {
if(searchQuery?.length !== 0) if (searchQuery?.length !== 0) {
{
notFoundTicker = false; notFoundTicker = false;
} }
} }
</script> </script>
<label
on:mouseover={loadSearchData}
<label on:mouseover={loadSearchData} on:click={loadSearchData} for="searchBarModal" class="cursor-pointer p-2 sm:hover:bg-[#27272A] text-gray-300 sm:hover:text-white flex-shrink-0 flex items-center justify-center border border-gray-800 rounded-lg"> on:click={loadSearchData}
for="searchBarModal"
class="cursor-pointer p-2 sm:hover:bg-[#27272A] text-gray-300 sm:hover:text-white flex-shrink-0 flex items-center justify-center border border-gray-600 rounded-lg"
>
<Search class="h-[20px] w-[20px]" /> <Search class="h-[20px] w-[20px]" />
</label> </label>
<!-- <!--
<div class="ml-5 w-96 grow"> <div class="ml-5 w-96 grow">
<div class="relative flex items-center"> <div class="relative flex items-center">
@ -355,7 +345,7 @@ $: {
{#if !showSuggestions } {#if !showSuggestions }
{#each popularList as item} {#each popularList as item}
<li> <li>
<a href={`/${item?.type === 'ETF' ? 'etf' : item?.type === 'Crypto' ? 'crypto' : 'stocks'}/${item?.symbol}`} on:click={() => popularTicker(item?.symbol, item?.assetType) } class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#404040] bg-opacity-[0.25] rounded group' : 'shake-ticker cursor-pointer border-b border-gray-600 flex justify-start items-center p-2 text-white group'} w-full"> <a href={`/${item?.type === 'ETF' ? 'etf' : item?.type === 'Crypto' ? 'crypto' : 'stocks'}/${item?.symbol}`} on:click={() => popularTicker(item?.symbol, item?.assetType) } class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#27272A] rounded group' : 'shake-ticker cursor-pointer border-b border-gray-600 flex justify-start items-center p-2 text-white group'} w-full">
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<div class="flex flex-col ml-2"> <div class="flex flex-col ml-2">
<span class="text-blue-400">{item?.symbol}</span> <span class="text-blue-400">{item?.symbol}</span>
@ -381,31 +371,30 @@ $: {
</div> </div>
--> -->
<!--Start Desktop Searchbar--> <!--Start Desktop Searchbar-->
<!--Don't remove if since input.focus does not work anymore--> <!--Don't remove if since input.focus does not work anymore-->
{#if $screenWidth >= 640} {#if $screenWidth >= 640}
<input type="checkbox" id="searchBarModal" class="modal-toggle" bind:checked={searchBarModalChecked} /> <input
type="checkbox"
<dialog id="searchBarModal" class="modal modal-top z-[999]"> id="searchBarModal"
class="modal-toggle"
bind:checked={searchBarModalChecked}
/>
<dialog id="searchBarModal" class="modal modal-top z-[999]">
<label for="searchBarModal" class="cursor-pointer modal-backdrop"></label> <label for="searchBarModal" class="cursor-pointer modal-backdrop"></label>
<div
class="z-[999] modal-box overflow-hidden rounded-xl bg-[#09090B] border border-gray-600 sm:my-8 sm:m-auto sm:h-auto w-full sm:w-1/2 md:w-3/4 lg:w-1/2 2xl:w-1/3"
<div class="z-[999] modal-box overflow-hidden rounded-xl bg-[#09090B] border border-gray-800 sm:my-8 sm:m-auto sm:h-auto w-full sm:w-1/2 md:w-3/4 lg:w-1/2 2xl:w-1/3 " > >
<!-- Search layout --> <!-- Search layout -->
<div class="mt-5 sm:mt-0"> <div class="mt-5 sm:mt-0">
<div class="relative"> <div class="relative">
<label for="modal-search" class="sr-only">Search</label> <label for="modal-search" class="sr-only">Search</label>
<input <input
id="modal-search" id="modal-search"
class="rounded-lg w-full text-white bg-[#09090B] border border-gray-800 focus:ring-transparent placeholder-gray-200 py-3 pl-10 pr-4" class="rounded-lg w-full text-white bg-[#09090B] border border-gray-600 focus:ring-transparent placeholder-gray-200 py-3 pl-10 pr-4"
type="search" type="search"
placeholder="Search Anything…" placeholder="Search Anything…"
bind:value={searchQuery} bind:value={searchQuery}
@ -415,89 +404,128 @@ $: {
on:keypress={onKeyPress} on:keypress={onKeyPress}
autocomplete="off" autocomplete="off"
/> />
<button on:click={() => (searchBarTicker(searchQuery, assetType))} class="absolute inset-0 right-auto group" type="submit" aria-label="Search"> <button
<svg class="w-4 h-4 shrink-0 fill-current text-white ml-4 mr-2 text-slate-400" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"><path d="M7 14c-3.86 0-7-3.14-7-7s3.14-7 7-7 7 3.14 7 7-3.14 7-7 7zM7 2C4.243 2 2 4.243 2 7s2.243 5 5 5 5-2.243 5-5-2.243-5-5-5z" /> on:click={() => searchBarTicker(searchQuery, assetType)}
<path d="M15.707 14.293L13.314 11.9a8.019 8.019 0 01-1.414 1.414l2.393 2.393a.997.997 0 001.414 0 .999.999 0 000-1.414z" /> class="absolute inset-0 right-auto group"
type="submit"
aria-label="Search"
>
<svg
class="w-4 h-4 shrink-0 fill-current text-white ml-4 mr-2 text-slate-400"
viewBox="0 0 16 16"
xmlns="http://www.w3.org/2000/svg"
><path
d="M7 14c-3.86 0-7-3.14-7-7s3.14-7 7-7 7 3.14 7 7-3.14 7-7 7zM7 2C4.243 2 2 4.243 2 7s2.243 5 5 5 5-2.243 5-5-2.243-5-5-5z"
/>
<path
d="M15.707 14.293L13.314 11.9a8.019 8.019 0 01-1.414 1.414l2.393 2.393a.997.997 0 001.414 0 .999.999 0 000-1.414z"
/>
</svg> </svg>
</button> </button>
</div> </div>
</div> </div>
{#if $searchBarData?.length !== 0} {#if $searchBarData?.length !== 0}
<div class="py-4"> <div class="py-4">
<!-- Popular searches --> <!-- Popular searches -->
<div class="mb-3 last:mb-0 mt-3"> <div class="mb-3 last:mb-0 mt-3">
{#if notFoundTicker} {#if notFoundTicker}
<p class="text-xs font-semibold text-[#FB6A67] px-2 mb-4">Oh snapp, ticker does not exist in our database</p> <p class="text-xs font-semibold text-[#FB6A67] px-2 mb-4">
Oh snapp, ticker does not exist in our database
</p>
{/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 Popular
</div> </div>
{/if} {/if}
<ul class="text-sm" > <ul class="text-sm">
{#if !showSuggestions } {#if !showSuggestions}
{#each popularList as item} {#each popularList as item}
<li class="border-b border-gray-800"> <li class="border-b border-gray-600">
<a data-sveltekit-preload-data={false} href={`/${item?.type === 'ETF' ? 'etf' : item?.type === 'Crypto' ? 'crypto' : 'stocks'}/${item?.symbol}`} on:click={() => popularTicker(item?.symbol, item?.assetType) } class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#404040] bg-opacity-[0.25] rounded group' : 'shake-ticker cursor-pointer bg-[#09090B] sm:hover:bg-[#17171A] rounded-lg flex justify-start items-center p-2 text-white group'} w-full"> <a
data-sveltekit-preload-data={false}
href={`/${item?.type === "ETF" ? "etf" : item?.type === "Crypto" ? "crypto" : "stocks"}/${item?.symbol}`}
on:click={() =>
popularTicker(item?.symbol, item?.assetType)}
class="mb-2 {item?.symbol === focusedSuggestion
? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#27272A] rounded group'
: 'shake-ticker cursor-pointer bg-[#09090B] sm:hover:bg-[#27272A] rounded-lg flex justify-start items-center p-2 text-white group'} w-full"
>
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<div class="rounded-full w-10 h-10 relative bg-[#000] flex items-center justify-center"> <div
<img style="clip-path: circle(50%);" class="w-6 h-6" src={`https://financialmodelingprep.com/image-stock/${item?.symbol}.png`} loading="lazy" /> class="rounded-full w-10 h-10 relative bg-[#000] flex items-center justify-center"
>
<img
style="clip-path: circle(50%);"
class="w-6 h-6"
src={`https://financialmodelingprep.com/image-stock/${item?.symbol}.png`}
loading="lazy"
/>
</div> </div>
<div class="flex flex-col ml-2"> <div class="flex flex-col ml-2">
<span class="text-blue-400">{item?.symbol}</span> <span class="text-blue-400">{item?.symbol}</span>
<span class="text-white">{item?.name.length > 150 ? item?.name?.slice(0,150) + "..." : item?.name}</span> <span class="text-white"
>{item?.name.length > 150
? item?.name?.slice(0, 150) + "..."
: item?.name}</span
>
</div> </div>
<div class="text-white font-medium ml-auto"> <div class="text-white font-medium ml-auto">
{item?.type} {item?.type}
</div> </div>
</div> </div>
</a> </a>
</li> </li>
{/each} {/each}
{:else if showSuggestions && searchResults?.length > 0} {:else if showSuggestions && searchResults?.length > 0}
<div class="text-start text-sm font-semibold text-white mb-2"> <div class="text-start text-sm font-semibold text-white mb-2">
Suggestions Suggestions
</div> </div>
{#each searchResults as item} {#each searchResults as item}
<li class="border-b border-gray-800"> <li class="border-b border-gray-600">
<!-- svelte-ignore a11y-click-events-have-key-events --> <!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-label-has-associated-control --> <!-- svelte-ignore a11y-label-has-associated-control -->
<label data-sveltekit-preload-data={false} on:click={() => (searchBarTicker(item?.symbol, item?.type))} class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#404040] bg-opacity-[0.25] rounded group' : 'cursor-pointer mb-2 bg-[#09090B] sm:hover:bg-[#17171A] rounded-lg flex justify-start items-center p-2 text-white group'}"> <label
data-sveltekit-preload-data={false}
on:click={() => searchBarTicker(item?.symbol, item?.type)}
class="mb-2 {item?.symbol === focusedSuggestion
? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#27272A] rounded group'
: 'cursor-pointer mb-2 bg-[#09090B] sm:hover:bg-[#27272A] rounded-lg flex justify-start items-center p-2 text-white group'}"
>
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<div class="flex flex-col"> <div class="flex flex-col">
<span class="text-blue-400">{item?.symbol}</span> <span class="text-blue-400">{item?.symbol}</span>
<span class="text-white">{item?.name?.length > 150 ? item?.name?.slice(0,150) + "..." : item?.name}</span> <span class="text-white"
>{item?.name?.length > 150
? item?.name?.slice(0, 150) + "..."
: item?.name}</span
>
</div> </div>
<div class="text-white font-medium ml-auto"> <div class="text-white font-medium ml-auto">
{item?.type} {item?.type}
</div> </div>
</div> </div>
</label> </label>
</li> </li>
{/each} {/each}
{:else if showSuggestions && searchResults?.length === 0} {:else if showSuggestions && searchResults?.length === 0}
<li> <li>
<label class="flex items-center p-2 text-white hover:text-white hover:bg-[#404040] bg-opacity-[0.25] rounded group" > <label
<svg class="w-3 h-3 fill-slate-400 shrink-0 mr-3 dark:fill-slate-500" width="12" height="12" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg"> class="flex items-center p-2 text-white hover:text-white hover:bg-[#27272A] rounded group"
<path d="M11.953 4.29a.5.5 0 0 0-.454-.292H6.14L6.984.62A.5.5 0 0 0 6.12.173l-6 7a.5.5 0 0 0 .379.825h5.359l-.844 3.38a.5.5 0 0 0 .864.445l6-7a.5.5 0 0 0 .075-.534Z" /> >
<svg
class="w-3 h-3 fill-slate-400 shrink-0 mr-3 dark:fill-slate-500"
width="12"
height="12"
viewBox="0 0 12 12"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.953 4.29a.5.5 0 0 0-.454-.292H6.14L6.984.62A.5.5 0 0 0 6.12.173l-6 7a.5.5 0 0 0 .379.825h5.359l-.844 3.38a.5.5 0 0 0 .864.445l6-7a.5.5 0 0 0 .075-.534Z"
/>
</svg> </svg>
<span>No results found</span> <span>No results found</span>
</label> </label>
@ -509,41 +537,47 @@ $: {
{:else} {:else}
<div class="flex justify-center items-center m-auto mt-4 py-20"> <div class="flex justify-center items-center m-auto mt-4 py-20">
<div class="relative"> <div class="relative">
<label class="bg-[#09090B] rounded-xl h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2"> <label
<span class="loading loading-spinner loading-md text-gray-400"></span> class="bg-[#09090B] rounded-xl h-14 w-14 flex justify-center items-center absolute top-1/2 left-1/2 transform -translate-x-1/2 -translate-y-1/2"
>
<span class="loading loading-spinner loading-md text-gray-400"
></span>
</label> </label>
</div> </div>
</div> </div>
{/if} {/if}
<label for="searchBarModal" class="absolute left-6 top-4 sm:hidden"> <label for="searchBarModal" class="absolute left-6 top-4 sm:hidden">
<svg class="w-6 h-6 inline-block mb-0.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#fff" d="M9.125 21.1L.7 12.7q-.15-.15-.213-.325T.425 12q0-.2.063-.375T.7 11.3l8.425-8.425q.35-.35.875-.35t.9.375q.375.375.375.875t-.375.875L3.55 12l7.35 7.35q.35.35.35.863t-.375.887q-.375.375-.875.375t-.875-.375Z"/></svg> <svg
<span class="text-white text-md font-medium"> class="w-6 h-6 inline-block mb-0.5"
Return xmlns="http://www.w3.org/2000/svg"
</span> viewBox="0 0 24 24"
><path
fill="#fff"
d="M9.125 21.1L.7 12.7q-.15-.15-.213-.325T.425 12q0-.2.063-.375T.7 11.3l8.425-8.425q.35-.35.875-.35t.9.375q.375.375.375.875t-.375.875L3.55 12l7.35 7.35q.35.35.35.863t-.375.887q-.375.375-.875.375t-.875-.375Z"
/></svg
>
<span class="text-white text-md font-medium"> Return </span>
</label> </label>
</div> </div>
</dialog> </dialog>
{:else} {:else}
<!--End Desktop Searchbar-->
<!--End Desktop Searchbar--> <!--Start Drawer Sidewise for mobile-->
<div class="drawer drawer-end z-40 overflow-hidden">
<!--Start Drawer Sidewise for mobile--> <input
id="searchBarModal"
type="checkbox"
<div class="drawer drawer-end z-40 overflow-hidden"> class="drawer-toggle"
<input id="searchBarModal" type="checkbox" class="drawer-toggle" bind:checked={searchBarModalChecked} /> bind:checked={searchBarModalChecked}
/>
<div class="drawer-side overflow-hidden"> <div class="drawer-side overflow-hidden">
<div
class="p-3 bg-[#09090B] min-h-screen w-screen pb-20 overflow-hidden pt-10"
<div class="p-3 bg-[#09090B] min-h-screen w-screen pb-20 overflow-hidden pt-10"> >
<!-- Search layout --> <!-- Search layout -->
<div class="mt-5 lg:mt-0"> <div class="mt-5 lg:mt-0">
<div class="relative"> <div class="relative">
@ -560,84 +594,129 @@ $: {
on:keypress={onKeyPress} on:keypress={onKeyPress}
autocomplete="off" autocomplete="off"
/> />
<button on:click={() => (searchBarTicker(searchQuery, assetType))} class="absolute inset-0 right-auto group" type="submit" aria-label="Search"> <button
<svg class="w-4 h-4 shrink-0 fill-current text-white ml-4 mr-2 text-slate-400" viewBox="0 0 16 16" xmlns="http://www.w3.org/2000/svg"> <path d="M7 14c-3.86 0-7-3.14-7-7s3.14-7 7-7 7 3.14 7 7-3.14 7-7 7zM7 2C4.243 2 2 4.243 2 7s2.243 5 5 5 5-2.243 5-5-2.243-5-5-5z" /> on:click={() => searchBarTicker(searchQuery, assetType)}
<path d="M15.707 14.293L13.314 11.9a8.019 8.019 0 01-1.414 1.414l2.393 2.393a.997.997 0 001.414 0 .999.999 0 000-1.414z" /> class="absolute inset-0 right-auto group"
type="submit"
aria-label="Search"
>
<svg
class="w-4 h-4 shrink-0 fill-current text-white ml-4 mr-2 text-slate-400"
viewBox="0 0 16 16"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7 14c-3.86 0-7-3.14-7-7s3.14-7 7-7 7 3.14 7 7-3.14 7-7 7zM7 2C4.243 2 2 4.243 2 7s2.243 5 5 5 5-2.243 5-5-2.243-5-5-5z"
/>
<path
d="M15.707 14.293L13.314 11.9a8.019 8.019 0 01-1.414 1.414l2.393 2.393a.997.997 0 001.414 0 .999.999 0 000-1.414z"
/>
</svg> </svg>
</button> </button>
</div> </div>
</div> </div>
{#if $searchBarData?.length !== 0} {#if $searchBarData?.length !== 0}
<div class="py-4"> <div class="py-4">
<!-- Popular searches --> <!-- Popular searches -->
<div class="mb-3 last:mb-0 mt-3"> <div class="mb-3 last:mb-0 mt-3">
{#if notFoundTicker} {#if notFoundTicker}
<p class="text-xs font-semibold text-[#FB6A67] px-2 mb-4">Oh snapp, ticker does not exist in our database</p> <p class="text-xs font-semibold text-[#FB6A67] px-2 mb-4">
Oh snapp, ticker does not exist in our database
</p>
{/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 Popular
</div> </div>
{/if} {/if}
<ul class="text-sm" > <ul class="text-sm">
{#if !showSuggestions } {#if !showSuggestions}
{#each popularList as item} {#each popularList as item}
<li class="border-b border-gray-800"> <li class="border-b border-gray-600">
<a data-sveltekit-preload-data={false} href={`/${item?.type === 'ETF' ? 'etf' : item?.type === 'Crypto' ? 'crypto' : 'stocks'}/${item?.symbol}`} on:click={() => popularTicker(item?.symbol, item?.type) } class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#404040] bg-opacity-[0.25] rounded group' : 'cursor-pointer bg-[#09090B] bg-opacity-[0.4] rounded-lg flex justify-start items-center p-2 text-white group'} w-full"> <a
data-sveltekit-preload-data={false}
href={`/${item?.type === "ETF" ? "etf" : item?.type === "Crypto" ? "crypto" : "stocks"}/${item?.symbol}`}
on:click={() => popularTicker(item?.symbol, item?.type)}
class="mb-2 {item?.symbol === focusedSuggestion
? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#27272A] rounded group'
: 'cursor-pointer bg-[#09090B] bg-opacity-[0.4] rounded-lg flex justify-start items-center p-2 text-white group'} w-full"
>
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<div class="rounded-full w-10 h-10 relative bg-[#000] flex items-center justify-center"> <div
<img style="clip-path: circle(50%);" class="w-6 h-6" src={`https://financialmodelingprep.com/image-stock/${item?.symbol}.png`} loading="lazy" /> class="rounded-full w-10 h-10 relative bg-[#000] flex items-center justify-center"
>
<img
style="clip-path: circle(50%);"
class="w-6 h-6"
src={`https://financialmodelingprep.com/image-stock/${item?.symbol}.png`}
loading="lazy"
/>
</div> </div>
<div class="flex flex-col ml-2"> <div class="flex flex-col ml-2">
<span class="text-blue-400">{item?.symbol}</span> <span class="text-blue-400">{item?.symbol}</span>
<span class="text-white">{item?.name.length > charNumber ? item?.name.slice(0,charNumber) + "..." : item?.name}</span> <span class="text-white"
>{item?.name.length > charNumber
? item?.name.slice(0, charNumber) + "..."
: item?.name}</span
>
</div> </div>
<div class="text-white font-medium ml-auto mr-2"> <div class="text-white font-medium ml-auto mr-2">
{item?.type} {item?.type}
</div> </div>
</div> </div>
</a> </a>
</li> </li>
{/each} {/each}
{:else if showSuggestions && searchResults?.length > 0} {:else if showSuggestions && searchResults?.length > 0}
<div class="text-start text-sm font-semibold text-white mb-2"> <div class="text-start text-sm font-semibold text-white mb-2">
Suggestions Suggestions
</div> </div>
{#each searchResults as item} {#each searchResults as item}
<li class="border-b border-gray-800"> <li class="border-b border-gray-600">
<!-- svelte-ignore a11y-click-events-have-key-events --> <!-- svelte-ignore a11y-click-events-have-key-events -->
<!-- svelte-ignore a11y-label-has-associated-control --> <!-- svelte-ignore a11y-label-has-associated-control -->
<label data-sveltekit-preload-data={false} on:click={() => (searchBarTicker(item?.symbol, item?.type))} class="mb-2 {item?.symbol === focusedSuggestion ? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#404040] bg-opacity-[0.25] rounded group' : 'cursor-pointer mb-2 bg-[#09090B] bg-opacity-[0.4] rounded-lg flex justify-start items-center p-2 text-white group'}"> <label
data-sveltekit-preload-data={false}
on:click={() =>
searchBarTicker(item?.symbol, item?.type)}
class="mb-2 {item?.symbol === focusedSuggestion
? 'shake-ticker cursor-pointer flex justify-start items-center p-2 text-white bg-[#27272A] rounded group'
: 'cursor-pointer mb-2 bg-[#09090B] bg-opacity-[0.4] rounded-lg flex justify-start items-center p-2 text-white group'}"
>
<div class="flex flex-row items-center w-full"> <div class="flex flex-row items-center w-full">
<div class="flex flex-col ml-1"> <div class="flex flex-col ml-1">
<span class="text-blue-400">{item?.symbol}</span> <span class="text-blue-400">{item?.symbol}</span>
<span class="text-white">{item?.name.length > charNumber ? item?.name.slice(0,charNumber) + "..." : item?.name}</span> <span class="text-white"
>{item?.name.length > charNumber
? item?.name.slice(0, charNumber) + "..."
: item?.name}</span
>
</div> </div>
<div class="text-white font-medium ml-auto mr-2"> <div class="text-white font-medium ml-auto mr-2">
{item?.type} {item?.type}
</div> </div>
</div> </div>
</label> </label>
</li> </li>
{/each} {/each}
{:else if showSuggestions && searchResults?.length === 0} {:else if showSuggestions && searchResults?.length === 0}
<li> <li>
<label class="flex items-center p-2 text-white hover:text-white hover:bg-[#404040] bg-opacity-[0.25] rounded group" > <label
<svg class="w-3 h-3 fill-slate-400 shrink-0 mr-3 dark:fill-slate-500" width="12" height="12" viewBox="0 0 12 12" xmlns="http://www.w3.org/2000/svg"> class="flex items-center p-2 text-white hover:text-white hover:bg-[#27272A] rounded group"
<path d="M11.953 4.29a.5.5 0 0 0-.454-.292H6.14L6.984.62A.5.5 0 0 0 6.12.173l-6 7a.5.5 0 0 0 .379.825h5.359l-.844 3.38a.5.5 0 0 0 .864.445l6-7a.5.5 0 0 0 .075-.534Z" /> >
<svg
class="w-3 h-3 fill-slate-400 shrink-0 mr-3 dark:fill-slate-500"
width="12"
height="12"
viewBox="0 0 12 12"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M11.953 4.29a.5.5 0 0 0-.454-.292H6.14L6.984.62A.5.5 0 0 0 6.12.173l-6 7a.5.5 0 0 0 .379.825h5.359l-.844 3.38a.5.5 0 0 0 .864.445l6-7a.5.5 0 0 0 .075-.534Z"
/>
</svg> </svg>
<span>No results found</span> <span>No results found</span>
</label> </label>
@ -648,20 +727,25 @@ $: {
</div> </div>
{:else} {:else}
<div class="flex justify-center items-center m-auto mt-4 py-20"> <div class="flex justify-center items-center m-auto mt-4 py-20">
<span class="loading loading-lg loading-spinner text-success"></span> <span class="loading loading-lg loading-spinner text-success"
></span>
</div> </div>
{/if} {/if}
<label for="searchBarModal" class="absolute left-6 top-4 sm:hidden">
<label for="searchBarModal" class="absolute left-6 top-4 sm:hidden"> <svg
<svg class="w-6 h-6 inline-block mb-0.5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path fill="#fff" d="M9.125 21.1L.7 12.7q-.15-.15-.213-.325T.425 12q0-.2.063-.375T.7 11.3l8.425-8.425q.35-.35.875-.35t.9.375q.375.375.375.875t-.375.875L3.55 12l7.35 7.35q.35.35.35.863t-.375.887q-.375.375-.875.375t-.875-.375Z"/></svg> class="w-6 h-6 inline-block mb-0.5"
</label> xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 24 24"
</div> ><path
fill="#fff"
d="M9.125 21.1L.7 12.7q-.15-.15-.213-.325T.425 12q0-.2.063-.375T.7 11.3l8.425-8.425q.35-.35.875-.35t.9.375q.375.375.375.875t-.375.875L3.55 12l7.35 7.35q.35.35.35.863t-.375.887q-.375.375-.875.375t-.875-.375Z"
/></svg
>
</label>
</div>
</div>
</div> </div>
</div>
{/if} {/if}
<!--End Drawer Sidewise for mobile--> <!--End Drawer Sidewise for mobile-->

View File

@ -126,7 +126,7 @@
// Sort using the generic comparison function // Sort using the generic comparison function
stockList = [...originalData].sort(compareValues); stockList = [...originalData].sort(compareValues);
}; };
$: charNumber = $screenWidth < 640 ? 15 : 15; $: charNumber = $screenWidth < 640 ? 20 : 25;
</script> </script>
<svelte:head> <svelte:head>

View File

@ -150,7 +150,7 @@
> >
<div class="w-full flex justify-between items-center p-3 mt-3"> <div class="w-full flex justify-between items-center p-3 mt-3">
<h2 class="text-start text-xl font-semibold text-white ml-3"> <h2 class="text-start text-xl font-semibold text-white ml-3">
Pro Subscription 🔥 Pro Subscription
</h2> </h2>
<ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" /> <ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" />
</div> </div>
@ -170,7 +170,7 @@
> >
<div class="w-full flex justify-between items-center p-3 mt-3"> <div class="w-full flex justify-between items-center p-3 mt-3">
<h2 class="text-start text-xl font-semibold text-white ml-3"> <h2 class="text-start text-xl font-semibold text-white ml-3">
Price Alert Price Alert
</h2> </h2>
<ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" /> <ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" />
</div> </div>
@ -189,7 +189,7 @@
> >
<div class="w-full flex justify-between items-center p-3 mt-3"> <div class="w-full flex justify-between items-center p-3 mt-3">
<h2 class="text-start text-xl font-semibold text-white ml-3"> <h2 class="text-start text-xl font-semibold text-white ml-3">
Stock Screener 🔎 Stock Screener
</h2> </h2>
<ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" /> <ArrowLogo class="w-8 h-8 mr-3 flex-shrink-0" />
</div> </div>